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