1 | package ClassifyTreePath;
|
---|
2 |
|
---|
3 | require ClassifyTreePath;
|
---|
4 |
|
---|
5 | # /** Construct a new tree path object based on the given value path.
|
---|
6 | # *
|
---|
7 | # * @param $class The name of the class to bless as a string
|
---|
8 | # * @param $path The path as a pip delimited string
|
---|
9 | # * @return A reference to the ClassifyTreePath object
|
---|
10 | # *
|
---|
11 | # * @author John Thompson, DL Consulting Ltd.
|
---|
12 | # */
|
---|
13 | sub new()
|
---|
14 | {
|
---|
15 | my ($class, $path) = @_;
|
---|
16 | my $debug = 0;
|
---|
17 | print STDERR "ClassifyTreePath.new(\"$path\")\n" unless !$debug;
|
---|
18 | # Store the variables
|
---|
19 | my $self = {};
|
---|
20 | $self->{'debug'} = $debug;
|
---|
21 | $self->{'path'} = $path;
|
---|
22 | # Bless me father for I have sinned
|
---|
23 | bless $self, $class;
|
---|
24 | return $self;
|
---|
25 | }
|
---|
26 | # /** new() **/
|
---|
27 |
|
---|
28 | # /** Adds a new path component on to the end of the current path.
|
---|
29 | # *
|
---|
30 | # * @param $component The new component to add as a string
|
---|
31 | # *
|
---|
32 | # * @author John Thompson, DL Consulting Ltd.
|
---|
33 | # */
|
---|
34 | sub addPathComponent()
|
---|
35 | {
|
---|
36 | my ($self, $component) = @_;
|
---|
37 | print STDERR "ClassifyTreePath.addPathComponent(\"$component\")\n" unless !$self->{'debug'};
|
---|
38 | if($self->{'path'} =~ /\w+/)
|
---|
39 | {
|
---|
40 | $self->{'path'} .= "|" . $component;
|
---|
41 | }
|
---|
42 | else
|
---|
43 | {
|
---|
44 | $self->{'path'} = $component;
|
---|
45 | }
|
---|
46 | }
|
---|
47 | # /** addPathComponent() **/
|
---|
48 |
|
---|
49 | # /** Compare this path against another for equality.
|
---|
50 | # *
|
---|
51 | # * @param $other_path_obj The path object to compare to
|
---|
52 | # * @return 1 if the paths match, 0 otherwise
|
---|
53 | # *
|
---|
54 | # * @author John Thompson, DL Consulting Ltd.
|
---|
55 | # */
|
---|
56 | sub equals()
|
---|
57 | {
|
---|
58 | my ($self, $other_path_obj) = @_;
|
---|
59 | print STDERR "ClassifyTreePath.equals()\n" unless !$self->{'debug'};
|
---|
60 | return $self->{'path'} eq $other_path_obj->toString();
|
---|
61 | }
|
---|
62 | # /** equals() **/
|
---|
63 |
|
---|
64 | # /** Extracts the first path component from the path.
|
---|
65 | # *
|
---|
66 | # * @return The first path component as a string
|
---|
67 | # *
|
---|
68 | # * @author John Thompson, DL Consulting Ltd.
|
---|
69 | # */
|
---|
70 | sub getFirstPathComponent()
|
---|
71 | {
|
---|
72 | my ($self) = @_;
|
---|
73 | print STDERR "ClassifyTreePath.getFirstPathComponent()\n" unless !$self->{'debug'};
|
---|
74 | my @path = split(/\|/, $self->{'path'});
|
---|
75 | return $path[0];
|
---|
76 | }
|
---|
77 | # /** getFirstPathComponent() **/
|
---|
78 |
|
---|
79 | # /** Extracts the last path component from the path.
|
---|
80 | # *
|
---|
81 | # * @return The last path component as a string
|
---|
82 | # *
|
---|
83 | # * @author John Thompson, DL Consulting Ltd.
|
---|
84 | # */
|
---|
85 | sub getLastPathComponent()
|
---|
86 | {
|
---|
87 | my ($self) = @_;
|
---|
88 | print STDERR "ClassifyTreePath.getLastPathComponent()\n" unless !$self->{'debug'};
|
---|
89 | my @path = split(/\|/, $self->{'path'});
|
---|
90 | return @path[scalar(@path) - 1];
|
---|
91 | }
|
---|
92 | # /** getLastPathComponent() **/
|
---|
93 |
|
---|
94 | # /** Return a path object which is the parent path of this one.
|
---|
95 | # *
|
---|
96 | # * @return The parent path object
|
---|
97 | # *
|
---|
98 | # * @author John Thompson, DL Consulting Ltd.
|
---|
99 | # */
|
---|
100 | sub getParentPath()
|
---|
101 | {
|
---|
102 | my ($self) = @_;
|
---|
103 | print STDERR "ClassifyTreePath.getParentPath()\n" unless !$self->{'debug'};
|
---|
104 | my $result = 0;
|
---|
105 | my @path = split(/\|/, $self->{'path'});
|
---|
106 | if (scalar(@path) > 0)
|
---|
107 | {
|
---|
108 | pop(@path);
|
---|
109 | $result = new ClassifyTreePath(join("|", @path));
|
---|
110 | }
|
---|
111 | return $result;
|
---|
112 | }
|
---|
113 | # /** getParentPath() **/
|
---|
114 |
|
---|
115 | # /** Retrieves the path component located at the indicated index.
|
---|
116 | # *
|
---|
117 | # * @param $index The index of the component as an integer
|
---|
118 | # * @return The component as a string, or 0 if index out of range
|
---|
119 | # *
|
---|
120 | # * @author John Thompson, DL Consulting Ltd.
|
---|
121 | # */
|
---|
122 | sub getPathComponent()
|
---|
123 | {
|
---|
124 | my ($self, $index) = @_;
|
---|
125 | print STDERR "ClassifyTreePath.getPathComponent($index)\n" unless !$self->{'debug'};
|
---|
126 | my $result = 0;
|
---|
127 | my @path = split(/\|/, $self->{'path'});
|
---|
128 | # Check index is in range
|
---|
129 | if(0 <= $index && $index < scalar(@path))
|
---|
130 | {
|
---|
131 | $result = $path[$index];
|
---|
132 | }
|
---|
133 | return $result;
|
---|
134 | }
|
---|
135 | # /** getPathComponent() **/
|
---|
136 |
|
---|
137 | # /** Determine is this path is the root node one - which it must be if it has
|
---|
138 | # * one or fewer path components.
|
---|
139 | # *
|
---|
140 | # * @return true if this is the root path, false otherwise
|
---|
141 | # *
|
---|
142 | # * @author John Thompson, DL Consulting Ltd.
|
---|
143 | # */
|
---|
144 | sub isRootPath()
|
---|
145 | {
|
---|
146 | my ($self, $index) = @_;
|
---|
147 | print STDERR "ClassifyTreePath.isRootPath()\n" unless !$self->{'debug'};
|
---|
148 | my @path = split(/\|/, $self->{'path'});
|
---|
149 | return (scalar(@path) <= 1);
|
---|
150 | }
|
---|
151 | # /** isRootPath() **/
|
---|
152 |
|
---|
153 | # /** Represent this path as a string.
|
---|
154 | # *
|
---|
155 | # * @return The string representation of this path
|
---|
156 | # *
|
---|
157 | # * @author John Thompson, DL Consulting Ltd.
|
---|
158 | # */
|
---|
159 | sub toString()
|
---|
160 | {
|
---|
161 | my ($self) = @_;
|
---|
162 | print STDERR "ClassifyTreePath.toString()\n" unless !$self->{'debug'};
|
---|
163 | return $self->{'path'};
|
---|
164 | }
|
---|
165 | # /** toString() **/
|
---|
166 |
|
---|
167 | 1;
|
---|