| 1 | # ===================================================================== | ||
| 2 | # Copyright (c) 2002,2003 IBM Corporation | ||
| 3 | # All rights reserved. This program and the accompanying materials | ||
| 4 | # are made available under the terms of the Common Public License v1.0 | ||
| 5 | # which accompanies this distribution, and is available at | ||
| 6 | # http://www.opensource.org/licenses/cpl.php | ||
| 7 | # | ||
| 8 | # ===================================================================== | ||
| 9 | package LS::ID; | ||
| 10 | |||
| 11 | use strict; | ||
| 12 | use warnings; | ||
| 13 | |||
| 14 | use base 'URI::urn'; | ||
| 15 | |||
| 16 | |||
| 17 | # | ||
| 18 | # new - Creates a new LS::ID object from a string | ||
| 19 | # | ||
| 20 | # Parameters: A URI containing the LSID | ||
| 21 | # | ||
| 22 | # Returns: An LS::ID object if successful, | ||
| 23 | # undef if the URI is not in the correct form | ||
| 24 | # | ||
| 25 | sub new { | ||
| 26 | my ($class, $uri) = @_; | ||
| 27 | |||
| 28 | return undef unless _is_valid($uri); | ||
| 29 | |||
| 30 | return bless \$uri, $class; | ||
| 31 | } | ||
| 32 | |||
| 33 | |||
| 34 | # | ||
| 35 | # _is_valid - Determines whether or not the LSID is a valid LSID | ||
| 36 | # | ||
| 37 | # Returns: undef if the LSID is not valid, | ||
| 38 | # true if the LSID is valid | ||
| 39 | # | ||
| 40 | sub _is_valid { | ||
| 41 | my ($string) = @_; | ||
| 42 | |||
| 43 | return $string =~ /^[uU][rR][nN]:[lL][sS][iI][dD]:[A-Za-z0-9][\w\(\)\+\,\-\.\=\@\;\$\"\!\*\']*:[A-Za-z0-9][\w\(\)\+\,\-\.\=\@\;\$\"\!\*\']*:[A-Za-z0-9][\w\(\)\+\,\-\.\=\@\;\$\"\!\*\']*(:[A-Za-z0-9][\w\(\)\+\,\-\.\=\@\;\$\"\!\*\']*)?$/; | ||
| 44 | } | ||
| 45 | |||
| 46 | |||
| 47 | # | ||
| 48 | # _component - | ||
| 49 | # | ||
| 50 | sub _component { | ||
| 51 | my $self = shift; | ||
| 52 | my $index = shift; | ||
| 53 | |||
| 54 | my @components = split(/:/, $self->nss()); | ||
| 55 | my $value = $components[$index]; | ||
| 56 | |||
| 57 | if (@_) { | ||
| 58 | if (($index == 3 && $_[0] eq '') || ($_[0] =~ /^[A-Za-z0-9][\w\(\)\+\,\-\.\=\@\;\$\"\_\!\*\']*$/)) { | ||
| 59 | $components[$index] = $_[0]; | ||
| 60 | $self->nss(join(':', @components)); | ||
| 61 | |||
| 62 | return 1; | ||
| 63 | } | ||
| 64 | else { | ||
| 65 | return undef; | ||
| 66 | } | ||
| 67 | } | ||
| 68 | |||
| 69 | return $value; | ||
| 70 | } | ||
| 71 | |||
| 72 | |||
| 73 | # | ||
| 74 | # _authority - Access to the raw authority component of the LSID | ||
| 75 | # | ||
| 76 | # Returns: The raw string of the authority component | ||
| 77 | # | ||
| 78 | sub _authority { | ||
| 79 | my $self = shift; | ||
| 80 | return $self->_component(0, @_); | ||
| 81 | } | ||
| 82 | |||
| 83 | |||
| 84 | # | ||
| 85 | # authority - Access to the authority component of the LSID | ||
| 86 | # | ||
| 87 | # Returns: The authority component | ||
| 88 | # | ||
| 89 | sub authority { | ||
| 90 | my $self = shift; | ||
| 91 | return lc $self->_authority(@_); | ||
| 92 | } | ||
| 93 | |||
| 94 | |||
| 95 | # | ||
| 96 | # _namespace - | ||
| 97 | # | ||
| 98 | sub _namespace { | ||
| 99 | my $self = shift; | ||
| 100 | return $self->_component(1, @_); | ||
| 101 | } | ||
| 102 | |||
| 103 | |||
| 104 | # | ||
| 105 | # namespace - Access to the namespace component of the LSID | ||
| 106 | # | ||
| 107 | # Returns: The namespace component | ||
| 108 | # | ||
| 109 | sub namespace { | ||
| 110 | my $self = shift; | ||
| 111 | return $self->_namespace(@_); | ||
| 112 | } | ||
| 113 | |||
| 114 | |||
| 115 | # | ||
| 116 | # _object - | ||
| 117 | # | ||
| 118 | sub _object { | ||
| 119 | my $self = shift; | ||
| 120 | return $self->_component(2, @_); | ||
| 121 | } | ||
| 122 | |||
| 123 | |||
| 124 | # | ||
| 125 | # object - Access to the object component of the LSID | ||
| 126 | # | ||
| 127 | # Returns: The object component | ||
| 128 | # | ||
| 129 | sub object { | ||
| 130 | my $self = shift; | ||
| 131 | return $self->_object(@_); | ||
| 132 | } | ||
| 133 | |||
| 134 | |||
| 135 | # | ||
| 136 | # _revision - | ||
| 137 | # | ||
| 138 | sub _revision { | ||
| 139 | my $self = shift; | ||
| 140 | return $self->_component(3, @_); | ||
| 141 | } | ||
| 142 | |||
| 143 | |||
| 144 | # | ||
| 145 | # revision - Access to the revision component of the LSID | ||
| 146 | # | ||
| 147 | # Returns: The revision component | ||
| 148 | # | ||
| 149 | sub revision { | ||
| 150 | my $self = shift; | ||
| 151 | return $self->_revision(@_); | ||
| 152 | } | ||
| 153 | |||
| 154 | |||
| 155 | # | ||
| 156 | # canonical - Retrieves the canonicalized form of the LSID | ||
| 157 | # | ||
| 158 | # Parameters: | ||
| 159 | # | ||
| 160 | # Returns: The canonicalized LSID if successful, | ||
| 161 | # undef if unsuccessful | ||
| 162 | # | ||
| 163 | sub canonical { | ||
| 164 | |||
| 165 | my $self = shift; | ||
| 166 | my $nss = $self->nss; | ||
| 167 | |||
| 168 | my $new = $self->SUPER::canonical(); | ||
| 169 | |||
| 170 | # If the scheme portion of the URN is not lowercase, e.g. "URN", | ||
| 171 | # URI::canonical will reset the scheme to lc scheme, which in turn will | ||
| 172 | # rebless the object as a URI::urn. In case this happens, we need to | ||
| 173 | # rebless it as an LS::ID here. | ||
| 174 | |||
| 175 | bless ($new, __PACKAGE__) if (ref $new ne __PACKAGE__); | ||
| 176 | |||
| 177 | return $new if $nss !~ /[A-Z]/ && $nss !~ /:$/; | ||
| 178 | |||
| 179 | $nss =~ s/:$//; | ||
| 180 | |||
| 181 | $new = $new->clone() if $new == $self; | ||
| 182 | $new->authority(lc($self->authority())); | ||
| 183 | |||
| 184 | return $new; | ||
| 185 | } | ||
| 186 | |||
| 187 | |||
| 188 | 1; | ||
| 189 | |||
| 190 | __END__ |