| 1 | # Copyright (C) 2004 Identity Commons. All Rights Reserved. | ||||||||||||||||||||||||||||
| 2 | # See LICENSE for licensing details | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | # Author: Fen Labalme <fen@idcommons.net>, <fen@comedia.com> | ||||||||||||||||||||||||||||
| 5 | # with a tip-of-the-cap to parse.py written by Gabe Wachob | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | # TODO: | ||||||||||||||||||||||||||||
| 8 | # fix FIXME sections | ||||||||||||||||||||||||||||
| 9 | # add UNICODE support | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | package XRI::Parse; | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 6 | our $VERSION = 0.1; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 6 | ▶ | use Text::Balanced qw( extract_bracketed ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 6 | ▶ | use URI::Escape; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 6 | our @SEPARATORS = qw( / * : ); | |||||||||||||||||||||||||||
| 19 | 6 | our @GCS_CHARS = qw( @ = + $ * ); | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 21 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 23 | 21 | my $self = shift; | |||||||||||||||||||||||||||
| 24 | 21 | my $xri = shift; | |||||||||||||||||||||||||||
| 25 | 21 | $xri =~ s/^xri://i; | |||||||||||||||||||||||||||
| 26 | # $xri = stripComments( $xri ); | ||||||||||||||||||||||||||||
| 27 | 21 | my $this = { token=>undef, | |||||||||||||||||||||||||||
| 28 | remainder=>undef, | ||||||||||||||||||||||||||||
| 29 | authority=>undef, | ||||||||||||||||||||||||||||
| 30 | xri=>$xri }; | ||||||||||||||||||||||||||||
| 31 | 21 | bless $this, $self; | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | # escapes an XRI (including relative XRIs) for inclusion in an HTTP request | ||||||||||||||||||||||||||||
| 36 | # FIXME: currently handles xrefs identically to sub-segments | ||||||||||||||||||||||||||||
| 37 | # | ||||||||||||||||||||||||||||
| 38 | 6 | ▶ | sub escapeURI { | ||||||||||||||||||||||||||
|
Subroutine
escapeURI: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 39 | 6 | my $this = shift; | |||||||||||||||||||||||||||
| 40 | 6 | my $result; | |||||||||||||||||||||||||||
| 41 | 6 | ▶ | while (my $seg = $this->nextSegment) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 8 | $result = shift @$seg; # always one of qw( @ // /. /: ) | |||||||||||||||||||||||||||
| 43 | 8 | foreach my $subseg ( @$seg ) { | |||||||||||||||||||||||||||
| 44 | 15 | ▶ | if ($subseg =~ m|^\(|) { # xref | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 1 | $result .= uri_escape($subseg, "^A-Za-z0-9\\\-\_\.\!\~\*\'"); | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | else { # sub-segment | ||||||||||||||||||||||||||||
| 48 | 14 | $result .= uri_escape($subseg, "^A-Za-z0-9\\\-\_\.\!\~\*\'"); | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | 6 | return $result; | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | # if an absolute-xri, emit the array ref [ [ firstSegment ], local-path ] | ||||||||||||||||||||||||||||
| 56 | # if a relative-xri, emit the local-path or relative-path as a string | ||||||||||||||||||||||||||||
| 57 | # | ||||||||||||||||||||||||||||
| 58 | 1 | ▶ | sub splitAuthLocal { | ||||||||||||||||||||||||||
|
Subroutine
splitAuthLocal: called CC=3 SCAR=11.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 59 | 1 | my $this = shift; | |||||||||||||||||||||||||||
| 60 | 1 | my $firstRef = $this->nextSegment; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 1 | ▶ | if ( defined $this->{'authority'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 1 | my @auth = (); | |||||||||||||||||||||||||||
| 64 | # | ||||||||||||||||||||||||||||
| 65 | # lowercase the authority segments | ||||||||||||||||||||||||||||
| 66 | # | ||||||||||||||||||||||||||||
| 67 | 1 | foreach my $seg (@$firstRef) { | |||||||||||||||||||||||||||
| 68 | 3 | push @auth, lc $seg; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | 1 | return [ \@auth, $this->{remainder} ]; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | else { | ||||||||||||||||||||||||||||
| 73 | 0 | return $this->{xri}; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | # Emits a series of segments, each of which is a | ||||||||||||||||||||||||||||
| 78 | # list of (separator, part, separator...) tuples | ||||||||||||||||||||||||||||
| 79 | # Segments are separated by forward slash '/' | ||||||||||||||||||||||||||||
| 80 | # Emits (gcs-char, part, separator, part...) | ||||||||||||||||||||||||||||
| 81 | # for the first segment if using a gcs-char | ||||||||||||||||||||||||||||
| 82 | # Separator is one of "/.", "/:", ".", or ":" | ||||||||||||||||||||||||||||
| 83 | # | ||||||||||||||||||||||||||||
| 84 | 29 | ▶ | sub nextSegment { | ||||||||||||||||||||||||||
|
Subroutine
nextSegment: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 85 | 29 | my $this = shift; | |||||||||||||||||||||||||||
| 86 | 29 | my ( $token, @segment ); | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 29 | ▶ | if (defined $this->{token}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 8 | @segment = ( $this->{token} ); | |||||||||||||||||||||||||||
| 90 | 8 | undef $this->{token}; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | else { | ||||||||||||||||||||||||||||
| 93 | 21 | ▶ | if ( $token = $this->nextToken ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 11 | @segment = ( $token ); | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | else { | ||||||||||||||||||||||||||||
| 97 | 10 | return undef; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | 19 | ▶ | while (( $token = $this->nextToken ) && $token !~ m|^\/| ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$token = $this->nextToken and not $token =~ m[^\/]
MC/DC100%2/2
$token = $this->nextToken and not $token =~ m[^\/]
$token = $this->nextToken
not $token =~ m[^\/]
Truth table100%3/3
A and not(B)
A= $token = $this->nextTokenB= not $token =~ m[^\/]
| |||||||||||||||||||||||||||||
| 101 | 43 | push @segment, $token; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | 19 | ▶ | $this->{token} = $token if $token; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 19 | return \@segment; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 162 | ▶ | sub getCrossReference { | ||||||||||||||||||||||||||
|
Subroutine
getCrossReference: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 108 | 162 | my $this = shift; | |||||||||||||||||||||||||||
| 109 | 162 | my $xri = shift; | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | # FIXME: what to do if: 'xri:(!comment1).(!comment2)' -- (is this legal?) | ||||||||||||||||||||||||||||
| 112 | # FIXME: raise error if unbalanced parens | ||||||||||||||||||||||||||||
| 113 | 162 | ▶ | while (($this->{remainder} = $xri) =~ m|^\(|) { # cross-reference | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | 8 | my $xref; | |||||||||||||||||||||||||||
| 115 | 8 | ($xref, $xri) = extract_bracketed($xri, '()'); | |||||||||||||||||||||||||||
| 116 | 8 | ▶ | next if $xref =~ m|^\(\!|; # skip leading comments | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 6 | $this->{remainder} = $xri; | |||||||||||||||||||||||||||
| 118 | 6 | return $xref; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | 156 | return undef; | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | # return initial qw( @ = * // ) or undef | ||||||||||||||||||||||||||||
| 124 | # created to better strip leading comments | ||||||||||||||||||||||||||||
| 125 | # perhaps comment stripping should occur on object instantiation? | ||||||||||||||||||||||||||||
| 126 | # | ||||||||||||||||||||||||||||
| 127 | 21 | ▶ | sub getAuthority { | ||||||||||||||||||||||||||
|
Subroutine
getAuthority: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 128 | 21 | my $this = shift; | |||||||||||||||||||||||||||
| 129 | 21 | my $xri = $this->{xri}; | |||||||||||||||||||||||||||
| 130 | 21 | my $xref; | |||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 21 | ▶ | if ( $xref = $this->getCrossReference( $xri )) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | 2 | $this->{'authority'} = $xref; | |||||||||||||||||||||||||||
| 134 | 2 | return $xref; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | 19 | ▶ | if ($this->{remainder} =~ m|^\/\/(.*)$|) { # initial '//' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | 8 | $this->{'authority'} = '//'; | |||||||||||||||||||||||||||
| 138 | 8 | $this->{remainder} = $1; | |||||||||||||||||||||||||||
| 139 | 8 | return '//'; | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | 11 | ▶ | if ($this->{remainder} =~ m|^([\@\=\*])(.*)$|) { # gcs-char | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | 4 | my ($gcs, $rem) = ($1, $2); | |||||||||||||||||||||||||||
| 143 | 4 | ▶ | $this->{remainder} = (($rem =~ m|^[\/\*\:]|)?'':'*') . $rem; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | 4 | $this->{'authority'} = $gcs; | |||||||||||||||||||||||||||
| 145 | 4 | return $gcs; | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | 7 | $this->{remainder} = $xri; | |||||||||||||||||||||||||||
| 148 | 7 | return; | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | # Generates a list of (separator, string) pairs | ||||||||||||||||||||||||||||
| 153 | # Ignores the leading xri: | ||||||||||||||||||||||||||||
| 154 | # If the first two characters (ignoring the xri:) are //, returns this *once* as the | ||||||||||||||||||||||||||||
| 155 | # first token, as the // is only legal at the very beginning | ||||||||||||||||||||||||||||
| 156 | # Everything within () is treated as a single token | ||||||||||||||||||||||||||||
| 157 | # Yields a series of strings, one of the characters in SEPARATORS, or | ||||||||||||||||||||||||||||
| 158 | # a string of characters (a sub-segment) | ||||||||||||||||||||||||||||
| 159 | # FIXME: fix handling of '*' | ||||||||||||||||||||||||||||
| 160 | # FIXME: add handling of '&' | ||||||||||||||||||||||||||||
| 161 | # FIXME: strip comments: including multiple, before or after GCS | ||||||||||||||||||||||||||||
| 162 | # | ||||||||||||||||||||||||||||
| 163 | 155 | ▶ | sub nextToken { | ||||||||||||||||||||||||||
|
Subroutine
nextToken: called CC=8 SCAR=20.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 164 | 155 | my $this = shift; | |||||||||||||||||||||||||||
| 165 | 155 | my $auth; | |||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 155 | ▶ | if (!defined $this->{remainder} && ($auth = $this->getAuthority)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $this->{"remainder"} and $auth = $this->getAuthority
MC/DC100%2/2
not defined $this->{"remainder"} and $auth = $this->getAuthority
not defined $this->{"remainder"}
$auth = $this->getAuthority
Truth table100%3/3
not(A) and B
A= not defined $this->{"remainder"}B= $auth = $this->getAuthority
| |||||||||||||||||||||||||||||
| 168 | 14 | return $auth; | |||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | 141 | ▶ | return $xref if $xref = $this->getCrossReference( $this->{remainder} ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 137 | ▶ | if ($this->{remainder} =~ m|^([\/\*\:])(.*)$|) { # initial separators | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 173 | 54 | my ($sep, $rem) = ($1, $2); | |||||||||||||||||||||||||||
| 174 | 54 | ▶ | if ($sep eq '/') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 175 | 27 | ▶ | if ($rem =~ m|^([\*\:])(.*)$|) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 176 | 1 | $sep .= $1; # '/.' or '/:' | |||||||||||||||||||||||||||
| 177 | 1 | $rem = $2; | |||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | else { | ||||||||||||||||||||||||||||
| 180 | 26 | $sep = '/*'; | |||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | 54 | $this->{remainder} = $rem; | |||||||||||||||||||||||||||
| 184 | 54 | return $sep; | |||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | 83 | ▶ | if ($this->{remainder} =~ m|^([^\/\*\:]+)(.*)$|) { # sub-segment | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 187 | 53 | $this->{remainder} = $2; | |||||||||||||||||||||||||||
| 188 | 53 | return $1; | |||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | 30 | return undef; | |||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | 6 | 1; | |||||||||||||||||||||||||||
| 194 | __END__ | ||||||||||||||||||||||||||||