| 1 | package XML::DOM::Lite::Parser; | ||
| 2 | |||
| 3 | use XML::DOM::Lite::Document; | ||
| 4 | use XML::DOM::Lite::Node; | ||
| 5 | use XML::DOM::Lite::Constants qw(:all); | ||
| 6 | |||
| 7 | #======================================================================== | ||
| 8 | # These regular expressions have been gratefully borrowed from: | ||
| 9 | # | ||
| 10 | # REX/Perl 1.0 | ||
| 11 | # Robert D. Cameron "REX: XML Shallow Parsing with Regular Expressions", | ||
| 12 | # Technical Report TR 1998-17, School of Computing Science, Simon Fraser | ||
| 13 | # University, November, 1998. | ||
| 14 | # Copyright (c) 1998, Robert D. Cameron. | ||
| 15 | # The following code may be freely used and distributed provided that | ||
| 16 | # this copyright and citation notice remains intact and that modifications | ||
| 17 | # or additions are clearly identified. | ||
| 18 | |||
| 19 | our $TextSE = "[^<]+"; | ||
| 20 | our $UntilHyphen = "[^-]*-"; | ||
| 21 | our $Until2Hyphens = "$UntilHyphen(?:[^-]$UntilHyphen)*-"; | ||
| 22 | our $CommentCE = "$Until2Hyphens>?"; | ||
| 23 | our $UntilRSBs = "[^\\]]*](?:[^\\]]+])*]+"; | ||
| 24 | our $CDATA_CE = "$UntilRSBs(?:[^\\]>]$UntilRSBs)*>"; | ||
| 25 | our $S = "[ \\n\\t\\r]+"; | ||
| 26 | our $NameStrt = "[A-Za-z_:]|[^\\x00-\\x7F]"; | ||
| 27 | our $NameChar = "[A-Za-z0-9_:.-]|[^\\x00-\\x7F]"; | ||
| 28 | our $Name = "(?:$NameStrt)(?:$NameChar)*"; | ||
| 29 | our $QuoteSE = "\"[^\"]*\"|'[^']*'"; | ||
| 30 | our $DT_IdentSE = "$S$Name(?:$S(?:$Name|$QuoteSE))*"; | ||
| 31 | our $MarkupDeclCE = "(?:[^\\]\"'><]+|$QuoteSE)*>"; | ||
| 32 | our $S1 = "[\\n\\r\\t ]"; | ||
| 33 | our $UntilQMs = "[^?]*\\?+"; | ||
| 34 | our $PI_Tail = "\\?>|$S1$UntilQMs(?:[^>?]$UntilQMs)*>"; | ||
| 35 | our $DT_ItemSE = "<(?:!(?:--$Until2Hyphens>|[^-]$MarkupDeclCE)|\\?$Name(?:$PI_Tail))|%$Name;|$S"; | ||
| 36 | our $DocTypeCE = "$DT_IdentSE(?:$S)?(?:\\[(?:$DT_ItemSE)*](?:$S)?)?>?"; | ||
| 37 | our $DeclCE = "--(?:$CommentCE)?|\\[CDATA\\[(?:$CDATA_CE)?|DOCTYPE(?:$DocTypeCE)?"; | ||
| 38 | our $PI_CE = "$Name(?:$PI_Tail)?"; | ||
| 39 | our $EndTagCE = "$Name(?:$S)?>?"; | ||
| 40 | our $AttValSE = "\"[^<\"]*\"|'[^<']*'"; | ||
| 41 | our $ElemTagCE = "$Name(?:$S$Name(?:$S)?=(?:$S)?(?:$AttValSE))*(?:$S)?/?>?"; | ||
| 42 | our $ElementCE = "/(?:$EndTagCE)?|(?:$ElemTagCE)?"; | ||
| 43 | our $MarkupSPE = "<(?:!(?:$DeclCE)?|\\?(?:$PI_CE)?|(?:$ElementCE)?)"; | ||
| 44 | our $XML_SPE = "$TextSE|$MarkupSPE"; | ||
| 45 | |||
| 46 | #======================================================================== | ||
| 47 | |||
| 48 | # these have captures for parsing the attributes | ||
| 49 | our $AttValSE2 = "\"([^<\"]*)\"|'([^<']*)'"; | ||
| 50 | our $ElemTagCE2 = "(?:($Name)(?:$S)?=(?:$S)?($AttValSE2))+(?:$S)?/?>?"; | ||
| 51 | |||
| 52 | sub new { | ||
| 53 | my ($class, %options) = @_; | ||
| 54 | my $self = bless { | ||
| 55 | stack => [ ], | ||
| 56 | options => \%options, | ||
| 57 | }, $class; | ||
| 58 | return $self; | ||
| 59 | } | ||
| 60 | |||
| 61 | sub parse { | ||
| 62 | my ($self, $XML) = (shift, shift); | ||
| 63 | unless (ref($self)) { | ||
| 64 | $self = __PACKAGE__->new(@_); | ||
| 65 | } | ||
| 66 | my @nodes = $self->_shallow_parse($XML); | ||
| 67 | |||
| 68 | $self->{document} = XML::DOM::Lite::Document->new(); | ||
| 69 | push @{$self->{stack}}, $self->{document}; | ||
| 70 | |||
| 71 | STEP : foreach my $n ( @nodes ) { | ||
| 72 | substr($n, 0, 1) eq '<' && do { | ||
| 73 | substr($n, 1, 1) eq '!' && do { | ||
| 74 | $self->_handle_decl_node($n); | ||
| 75 | next STEP; | ||
| 76 | }; | ||
| 77 | substr($n, 1, 1) eq '?' && do { | ||
| 78 | $self->_handle_pi_node($n); | ||
| 79 | next STEP; | ||
| 80 | }; | ||
| 81 | $self->_handle_element_node($n); | ||
| 82 | next STEP; | ||
| 83 | }; | ||
| 84 | $self->_handle_text_node($n); | ||
| 85 | } | ||
| 86 | |||
| 87 | return $self->{document}; | ||
| 88 | } | ||
| 89 | |||
| 90 | sub parseFile { | ||
| 91 | my ($self, $filename) = @_; | ||
| 92 | unless (ref $self) { | ||
| 93 | $self = __PACKAGE__->new; | ||
| 94 | } | ||
| 95 | my $stream; | ||
| 96 | { | ||
| 97 | open FH, $filename or | ||
| 98 | die "can't open file $filename for reading ".$!; | ||
| 99 | local $/ = undef; | ||
| 100 | $stream = <FH>; | ||
| 101 | close FH; | ||
| 102 | } | ||
| 103 | return $self->parse($stream); | ||
| 104 | } | ||
| 105 | |||
| 106 | sub _shallow_parse { | ||
| 107 | my ($self, $XML) = @_; | ||
| 108 | |||
| 109 | # Check the options. | ||
| 110 | my %options = %{$self->{options}}; | ||
| 111 | if (defined($options{'whitespace'})) { | ||
| 112 | my $mode = $options{'whitespace'}; | ||
| 113 | if (index($mode, 'strip') >= 0) { | ||
| 114 | $XML =~ s/>$S/>/sg; | ||
| 115 | $XML =~ s/$S</</sg; | ||
| 116 | } | ||
| 117 | if (index($mode, 'normalize') >= 0) { | ||
| 118 | $XML =~ s/$S/ /sg | ||
| 119 | } | ||
| 120 | } | ||
| 121 | |||
| 122 | return $XML =~ /$XML_SPE/go; | ||
| 123 | } | ||
| 124 | |||
| 125 | sub _handle_decl_node { | ||
| 126 | my ($self, $decl) = @_; | ||
| 127 | my $kind; | ||
| 128 | my $length = length($decl); | ||
| 129 | my $start = 1; | ||
| 130 | $parent = $self->{stack}->[$#{$self->{stack}}]; | ||
| 131 | substr($decl, 0, 4) eq '<!--' && do { | ||
| 132 | $start = 4; | ||
| 133 | $length = $length - $start - 3; | ||
| 134 | $kind = COMMENT_NODE; | ||
| 135 | }; | ||
| 136 | substr($decl, 0, 9) eq '<![CDATA[' && do { | ||
| 137 | $start = 9; | ||
| 138 | $length = $length - $start - 3; | ||
| 139 | $kind = CDATA_SECTION_NODE; | ||
| 140 | }; | ||
| 141 | substr($decl, 0, 9) eq '<!DOCTYPE' && do { # I'm cheating here, should be a separate node! | ||
| 142 | $start = 9; | ||
| 143 | $length = $length - $start - 1; | ||
| 144 | $kind = DOCUMENT_TYPE_NODE; | ||
| 145 | }; | ||
| 146 | return $self->_mk_gen_node(substr($decl, $start, $length), $parent, $kind); | ||
| 147 | } | ||
| 148 | |||
| 149 | sub _handle_pi_node { | ||
| 150 | my ($self, $pi) = @_; | ||
| 151 | $pi =~ s/^<\?\S+//o; | ||
| 152 | $pi =~ s/\?>$//so; | ||
| 153 | $parent = $self->{stack}->[$#{$self->{stack}}]; | ||
| 154 | return $self->_mk_gen_node($pi, $parent, PROCESSING_INSTRUCTION_NODE); | ||
| 155 | } | ||
| 156 | |||
| 157 | sub _handle_text_node { | ||
| 158 | my ($self, $text) = @_; | ||
| 159 | $parent = $self->{stack}->[$#{$self->{stack}}]; | ||
| 160 | $text =~ s/^\n//so; return unless defined $text; | ||
| 161 | return $self->_mk_gen_node($text, $parent, TEXT_NODE); | ||
| 162 | } | ||
| 163 | |||
| 164 | sub _handle_element_node { | ||
| 165 | my ($self, $elmnt) = @_; | ||
| 166 | if ($elmnt =~ /^<\/($EndTagCE)/o) { | ||
| 167 | $self->_handle_element_node_end($1); | ||
| 168 | } | ||
| 169 | elsif ($elmnt =~ /($ElemTagCE)>$/o) { | ||
| 170 | $self->_handle_element_node_start($1); | ||
| 171 | } | ||
| 172 | } | ||
| 173 | |||
| 174 | sub _handle_element_node_start { | ||
| 175 | my ($self, $elmnt) = @_; | ||
| 176 | # this node is a child of the last opened node (top of stack) | ||
| 177 | my $parent = $self->{stack}->[$#{$self->{stack}}]; | ||
| 178 | my $node = $self->_mk_element_node($elmnt, $parent); | ||
| 179 | |||
| 180 | # last opened node to the top of the stack | ||
| 181 | push @{$self->{stack}}, $node; | ||
| 182 | |||
| 183 | # deal with XML style empty tags | ||
| 184 | if ($elmnt =~ /\/$/) { | ||
| 185 | $node = $self->_handle_element_node_end($elmnt); | ||
| 186 | } | ||
| 187 | if (defined $node->getAttribute('id')) { | ||
| 188 | $self->{document}->setElementById($node->getAttribute("id"), $node); | ||
| 189 | } | ||
| 190 | |||
| 191 | return $node; | ||
| 192 | } | ||
| 193 | |||
| 194 | sub _handle_element_node_end { | ||
| 195 | my ($self, $elmnt) = @_; | ||
| 196 | |||
| 197 | # node is now closed, pop it off the stack | ||
| 198 | pop @{$self->{stack}}; | ||
| 199 | |||
| 200 | # parentNode is now at the top of the stack | ||
| 201 | return $self->{stack}->[$#{$self->{stack}}]; | ||
| 202 | } | ||
| 203 | |||
| 204 | sub _mk_gen_node { | ||
| 205 | my ($self, $str, $parent, $type) = @_; | ||
| 206 | $parent = $self->{stack}->[$#{$self->{stack}}] unless $parent; | ||
| 207 | my $node = XML::DOM::Lite::Node->new({ | ||
| 208 | nodeType => $type, | ||
| 209 | nodeValue => $str, | ||
| 210 | }); | ||
| 211 | |||
| 212 | $parent->appendChild($node); | ||
| 213 | $node->ownerDocument($self->{document}); | ||
| 214 | |||
| 215 | if ($type == DOCUMENT_TYPE_NODE) { | ||
| 216 | $node->{nodeName} = '#document-type'; | ||
| 217 | } elsif ($type == PROCESSING_INSTRUCTION_NODE) { | ||
| 218 | $node->{nodeName} = '#processing-instruction'; | ||
| 219 | } elsif ($type == TEXT_NODE) { | ||
| 220 | $node->{nodeName} = '#text'; | ||
| 221 | } elsif ($type == CDATA_SECTION_NODE) { | ||
| 222 | $node->{nodeName} = '#cdata'; | ||
| 223 | } elsif ($type == COMMENT_NODE) { | ||
| 224 | $node->{nodeName} = '#comment'; | ||
| 225 | } | ||
| 226 | return $node; | ||
| 227 | } | ||
| 228 | |||
| 229 | sub _mk_text_node { | ||
| 230 | my ($self, $str, $parent) = @_; | ||
| 231 | $parent = $self->{stack}->[$#{$self->{stack}}] unless $parent; | ||
| 232 | |||
| 233 | my $node = XML::DOM::Lite::Node->new({ | ||
| 234 | nodeName => '#text', | ||
| 235 | nodeType => TEXT_NODE, | ||
| 236 | nodeValue => $str, | ||
| 237 | }); | ||
| 238 | |||
| 239 | $parent->appendChild($node); | ||
| 240 | $node->ownerDocument($self->{document}); | ||
| 241 | |||
| 242 | return $node; | ||
| 243 | } | ||
| 244 | |||
| 245 | sub _mk_element_node { | ||
| 246 | my ($self, $elmnt, $parent) = @_; | ||
| 247 | |||
| 248 | ($tagName, $elmnt) = split(/\s+/, $elmnt, 2); | ||
| 249 | $tagName =~ s/\/$//; | ||
| 250 | my $attrs = $self->_parse_attributes($elmnt); | ||
| 251 | my $node = XML::DOM::Lite::Node->new({ | ||
| 252 | nodeType => ELEMENT_NODE, | ||
| 253 | attributes => $attrs, | ||
| 254 | nodeName => $tagName, | ||
| 255 | tagName => $tagName, | ||
| 256 | }); | ||
| 257 | $parent->appendChild($node); | ||
| 258 | $node->ownerDocument($self->{document}); | ||
| 259 | |||
| 260 | return $node; | ||
| 261 | } | ||
| 262 | |||
| 263 | sub _parse_attributes { | ||
| 264 | my ($self, $elmnt) = @_; | ||
| 265 | |||
| 266 | my $attrs = XML::DOM::Lite::NodeList->new([ ]); | ||
| 267 | return $attrs unless $elmnt; | ||
| 268 | |||
| 269 | while ($elmnt =~ s/$ElemTagCE2//o) { | ||
| 270 | push @$attrs, XML::DOM::Lite::Node->new({ | ||
| 271 | nodeType => ATTRIBUTE_NODE, | ||
| 272 | nodeName => $1, | ||
| 273 | nodeValue => defined($3) ? $3 : $4, | ||
| 274 | ownerDocument => $self->{document} | ||
| 275 | }); | ||
| 276 | } | ||
| 277 | |||
| 278 | return $attrs; | ||
| 279 | } | ||
| 280 | |||
| 281 | 1; | ||
| 282 | |||
| 283 | |||
| 284 | __END__ |