1 | ###########################################################################
|
---|
2 | #
|
---|
3 | # ClassifyTreePath.pm --
|
---|
4 | #
|
---|
5 | # A component of the Greenstone digital library software
|
---|
6 | # from the New Zealand Digital Library Project at the
|
---|
7 | # University of Waikato, New Zealand.
|
---|
8 | #
|
---|
9 | # Copyright (C) 2006-2010 DL Consulting Ltd
|
---|
10 | #
|
---|
11 | # This program is free software; you can redistribute it and/or modify
|
---|
12 | # it under the terms of the GNU General Public License as published by
|
---|
13 | # the Free Software Foundation; either version 2 of the License, or
|
---|
14 | # (at your option) any later version.
|
---|
15 | #
|
---|
16 | # This program is distributed in the hope that it will be useful,
|
---|
17 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
18 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
19 | # GNU General Public License for more details.
|
---|
20 | #
|
---|
21 | # You should have received a copy of the GNU General Public License
|
---|
22 | # along with this program; if not, write to the Free Software
|
---|
23 | # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
24 | #
|
---|
25 | ###########################################################################
|
---|
26 |
|
---|
27 |
|
---|
28 | package ClassifyTreePath;
|
---|
29 |
|
---|
30 | use strict;
|
---|
31 |
|
---|
32 |
|
---|
33 | # /** Construct a new tree path object based on the given value path.
|
---|
34 | # *
|
---|
35 | # * @param $class The name of the class to bless as a string
|
---|
36 | # * @param $path The path as a pip delimited string
|
---|
37 | # * @return A reference to the ClassifyTreePath object
|
---|
38 | # *
|
---|
39 | # * @author John Thompson, DL Consulting Ltd.
|
---|
40 | # */
|
---|
41 | sub new()
|
---|
42 | {
|
---|
43 | my ($class, $path) = @_;
|
---|
44 | my $debug = 0;
|
---|
45 | print STDERR "ClassifyTreePath.new(\"$path\")\n" unless !$debug;
|
---|
46 | # Store the variables
|
---|
47 | my $self = {};
|
---|
48 | $self->{'debug'} = $debug;
|
---|
49 | $self->{'path'} = $path;
|
---|
50 | # Bless me father for I have sinned
|
---|
51 | bless $self, $class;
|
---|
52 | return $self;
|
---|
53 | }
|
---|
54 | # /** new() **/
|
---|
55 |
|
---|
56 | # /** Adds a new path component on to the end of the current path.
|
---|
57 | # *
|
---|
58 | # * @param $component The new component to add as a string
|
---|
59 | # *
|
---|
60 | # * @author John Thompson, DL Consulting Ltd.
|
---|
61 | # */
|
---|
62 | sub addPathComponent()
|
---|
63 | {
|
---|
64 | my ($self, $component) = @_;
|
---|
65 | print STDERR "ClassifyTreePath.addPathComponent(\"$component\")\n" unless !$self->{'debug'};
|
---|
66 | if($self->{'path'} =~ /\w+/)
|
---|
67 | {
|
---|
68 | $self->{'path'} .= "|" . $component;
|
---|
69 | }
|
---|
70 | else
|
---|
71 | {
|
---|
72 | $self->{'path'} = $component;
|
---|
73 | }
|
---|
74 | }
|
---|
75 | # /** addPathComponent() **/
|
---|
76 |
|
---|
77 | # /** Compare this path against another for equality.
|
---|
78 | # *
|
---|
79 | # * @param $other_path_obj The path object to compare to
|
---|
80 | # * @return 1 if the paths match, 0 otherwise
|
---|
81 | # *
|
---|
82 | # * @author John Thompson, DL Consulting Ltd.
|
---|
83 | # */
|
---|
84 | sub equals()
|
---|
85 | {
|
---|
86 | my ($self, $other_path_obj) = @_;
|
---|
87 | print STDERR "ClassifyTreePath.equals()\n" unless !$self->{'debug'};
|
---|
88 | return $self->{'path'} eq $other_path_obj->toString();
|
---|
89 | }
|
---|
90 | # /** equals() **/
|
---|
91 |
|
---|
92 | # /** Extracts the first path component from the path.
|
---|
93 | # *
|
---|
94 | # * @return The first path component as a string
|
---|
95 | # *
|
---|
96 | # * @author John Thompson, DL Consulting Ltd.
|
---|
97 | # */
|
---|
98 | sub getFirstPathComponent()
|
---|
99 | {
|
---|
100 | my ($self) = @_;
|
---|
101 | print STDERR "ClassifyTreePath.getFirstPathComponent()\n" unless !$self->{'debug'};
|
---|
102 | my @path = split(/\|/, $self->{'path'});
|
---|
103 | return $path[0];
|
---|
104 | }
|
---|
105 | # /** getFirstPathComponent() **/
|
---|
106 |
|
---|
107 | # /** Extracts the last path component from the path.
|
---|
108 | # *
|
---|
109 | # * @return The last path component as a string
|
---|
110 | # *
|
---|
111 | # * @author John Thompson, DL Consulting Ltd.
|
---|
112 | # */
|
---|
113 | sub getLastPathComponent()
|
---|
114 | {
|
---|
115 | my ($self) = @_;
|
---|
116 | print STDERR "ClassifyTreePath.getLastPathComponent()\n" unless !$self->{'debug'};
|
---|
117 | my @path = split(/\|/, $self->{'path'});
|
---|
118 | return @path[scalar(@path) - 1];
|
---|
119 | }
|
---|
120 | # /** getLastPathComponent() **/
|
---|
121 |
|
---|
122 | # /** Return a path object which is the parent path of this one.
|
---|
123 | # *
|
---|
124 | # * @return The parent path object
|
---|
125 | # *
|
---|
126 | # * @author John Thompson, DL Consulting Ltd.
|
---|
127 | # */
|
---|
128 | sub getParentPath()
|
---|
129 | {
|
---|
130 | my ($self) = @_;
|
---|
131 | print STDERR "ClassifyTreePath.getParentPath()\n" unless !$self->{'debug'};
|
---|
132 | my $result = 0;
|
---|
133 | my @path = split(/\|/, $self->{'path'});
|
---|
134 | if (scalar(@path) > 0)
|
---|
135 | {
|
---|
136 | pop(@path);
|
---|
137 | $result = new ClassifyTreePath(join("|", @path));
|
---|
138 | }
|
---|
139 | return $result;
|
---|
140 | }
|
---|
141 | # /** getParentPath() **/
|
---|
142 |
|
---|
143 | # /** Retrieves the path component located at the indicated index.
|
---|
144 | # *
|
---|
145 | # * @param $index The index of the component as an integer
|
---|
146 | # * @return The component as a string, or 0 if index out of range
|
---|
147 | # *
|
---|
148 | # * @author John Thompson, DL Consulting Ltd.
|
---|
149 | # */
|
---|
150 | sub getPathComponent()
|
---|
151 | {
|
---|
152 | my ($self, $index) = @_;
|
---|
153 | print STDERR "ClassifyTreePath.getPathComponent($index)\n" unless !$self->{'debug'};
|
---|
154 | my $result = 0;
|
---|
155 | my @path = split(/\|/, $self->{'path'});
|
---|
156 | # Check index is in range
|
---|
157 | if(0 <= $index && $index < scalar(@path))
|
---|
158 | {
|
---|
159 | $result = $path[$index];
|
---|
160 | }
|
---|
161 | return $result;
|
---|
162 | }
|
---|
163 | # /** getPathComponent() **/
|
---|
164 |
|
---|
165 | # /** Determine is this path is the root node one - which it must be if it has
|
---|
166 | # * one or fewer path components.
|
---|
167 | # *
|
---|
168 | # * @return true if this is the root path, false otherwise
|
---|
169 | # *
|
---|
170 | # * @author John Thompson, DL Consulting Ltd.
|
---|
171 | # */
|
---|
172 | sub isRootPath()
|
---|
173 | {
|
---|
174 | my ($self, $index) = @_;
|
---|
175 | print STDERR "ClassifyTreePath.isRootPath()\n" unless !$self->{'debug'};
|
---|
176 | my @path = split(/\|/, $self->{'path'});
|
---|
177 | return (scalar(@path) <= 1);
|
---|
178 | }
|
---|
179 | # /** isRootPath() **/
|
---|
180 |
|
---|
181 | # /** Represent this path as a string.
|
---|
182 | # *
|
---|
183 | # * @return The string representation of this path
|
---|
184 | # *
|
---|
185 | # * @author John Thompson, DL Consulting Ltd.
|
---|
186 | # */
|
---|
187 | sub toString()
|
---|
188 | {
|
---|
189 | my ($self) = @_;
|
---|
190 | print STDERR "ClassifyTreePath.toString()\n" unless !$self->{'debug'};
|
---|
191 | return $self->{'path'};
|
---|
192 | }
|
---|
193 | # /** toString() **/
|
---|
194 |
|
---|
195 | 1;
|
---|