| 1 | package XML::XPathToXML; | ||
| 2 | ### | ||
| 3 | # | ||
| 4 | # Name: XML::XPathToXML | ||
| 5 | # Version: 0.01 | ||
| 6 | # Description: Parses a list of XPath/value pairs and returns an XML::LibXML note-tree. | ||
| 7 | # Author: Kurt George Gjerde | ||
| 8 | # Copyright: InterMedia, University of Bergen (2002) | ||
| 9 | # Licence: Same as Perl | ||
| 10 | # | ||
| 11 | ### | ||
| 12 | |||
| 13 | ### POD at bottom. | ||
| 14 | |||
| 15 | |||
| 16 | ### | ||
| 17 | |||
| 18 | use XML::LibXML; | ||
| 19 | |||
| 20 | my $PACKAGE = 'XML::XPathToXML'; | ||
| 21 | |||
| 22 | |||
| 23 | ### NEW | ||
| 24 | # | ||
| 25 | sub new { | ||
| 26 | my ($class, %args) = @_; | ||
| 27 | my $self = \%args; | ||
| 28 | bless $self, $class; | ||
| 29 | |||
| 30 | $self->{version} ||= '1.0'; | ||
| 31 | $self->{debug} ||= 0; | ||
| 32 | $self->{maxAutoSiblings} = 256 if not defined $self->{maxAutoSiblings}; | ||
| 33 | |||
| 34 | $self->init(); | ||
| 35 | |||
| 36 | return $self; | ||
| 37 | } | ||
| 38 | |||
| 39 | |||
| 40 | ### CLOSE | ||
| 41 | # | ||
| 42 | sub close { | ||
| 43 | my $self = shift; | ||
| 44 | undef $self->{tree}; | ||
| 45 | undef $self->{doc}; | ||
| 46 | } | ||
| 47 | |||
| 48 | |||
| 49 | ### INIT | ||
| 50 | # | ||
| 51 | sub init { | ||
| 52 | my $self = shift; | ||
| 53 | $self->{doc} = XML::LibXML::Document->createDocument( $self->{version}, $self->{encoding} ); | ||
| 54 | $self->{doc}->setDocumentElement( $self->{doc}->createElement('root') ); | ||
| 55 | $self->{tree} = {}; | ||
| 56 | } | ||
| 57 | |||
| 58 | |||
| 59 | ### RESET | ||
| 60 | # | ||
| 61 | sub reset { | ||
| 62 | my $self = shift; | ||
| 63 | $self->close; | ||
| 64 | $self->init; | ||
| 65 | } | ||
| 66 | |||
| 67 | |||
| 68 | ### GET IT | ||
| 69 | # | ||
| 70 | sub document { shift->{doc} } | ||
| 71 | sub documentElement { shift->{doc}->documentElement } | ||
| 72 | |||
| 73 | |||
| 74 | ### PARSE | ||
| 75 | # | ||
| 76 | sub parse { | ||
| 77 | my ($self, $data, $value) = @_; | ||
| 78 | |||
| 79 | if (ref($data) eq 'HASH') { | ||
| 80 | foreach my $xpath (sort keys %{$data}) { | ||
| 81 | print "$xpath\n" if $self->{debug}; | ||
| 82 | $self->_createNode( $xpath, $data->{$xpath} ); | ||
| 83 | } | ||
| 84 | } | ||
| 85 | elsif (ref($data) eq 'ARRAY') { | ||
| 86 | die "[$PACKAGE] Array not implemented.\n"; | ||
| 87 | } | ||
| 88 | else { | ||
| 89 | $self->_createNode( $data, $value ); | ||
| 90 | } | ||
| 91 | |||
| 92 | return $self->{doc}->documentElement(); | ||
| 93 | } | ||
| 94 | |||
| 95 | |||
| 96 | |||
| 97 | |||
| 98 | ### _CREATE NODE | ||
| 99 | # | ||
| 100 | # Creates nodes, text nodes and attributes from an xpath and a value. | ||
| 101 | # | ||
| 102 | sub _createNode { | ||
| 103 | my ($self, $xpath, $value) = @_; | ||
| 104 | |||
| 105 | my $name = $xpath; | ||
| 106 | $name =~ s{^.*\/(.*)$}{$1}; | ||
| 107 | $name =~ s{\[.*\]$}{}; | ||
| 108 | |||
| 109 | my $parent = $xpath; | ||
| 110 | $parent =~ s{^(.*)\/.*$}{$1}; | ||
| 111 | |||
| 112 | my $xpathDisplay; | ||
| 113 | if ($self->{debug}) { | ||
| 114 | $xpathDisplay = $xpath; | ||
| 115 | $xpathDisplay =~ s{\[.*\]$}{}; | ||
| 116 | } | ||
| 117 | |||
| 118 | if ($parent && !$self->{doc}->find($parent)->size) { | ||
| 119 | $self->_createNode($parent,undef); | ||
| 120 | } | ||
| 121 | |||
| 122 | my $parentNode; | ||
| 123 | if ($parent) { | ||
| 124 | $parentNode = $self->{doc}->find($parent)->get_node(1); | ||
| 125 | die "[$PACKAGE] Parent node '$parent' to '$xpath' not found!\n" if !$parentNode; | ||
| 126 | } | ||
| 127 | |||
| 128 | if ($name =~ /^@(.*)$/) { | ||
| 129 | print " addAttribute: $xpathDisplay\n" if $self->{debug}; | ||
| 130 | $parentNode->setAttribute($1,$value); | ||
| 131 | } | ||
| 132 | else { | ||
| 133 | |||
| 134 | my $newNode = $self->{doc}->find($xpath)->get_node(1); | ||
| 135 | |||
| 136 | my $cnt; | ||
| 137 | |||
| 138 | while (!$newNode) { | ||
| 139 | print " createElement: $xpathDisplay\n" if $self->{debug}; | ||
| 140 | |||
| 141 | $cnt++; | ||
| 142 | if ($self->{maxAutoSiblings} && $cnt>$self->{maxAutoSiblings}) { | ||
| 143 | die "[$PACKAGE] Max automatic creation of siblings overflow ($self->{maxAutoSiblings}) for '$xpath'!\n"; | ||
| 144 | } | ||
| 145 | |||
| 146 | $newNode = $self->{doc}->createElement($name); | ||
| 147 | |||
| 148 | if ($parent) { | ||
| 149 | $parentNode->appendChild( $newNode ); | ||
| 150 | } else { | ||
| 151 | $self->{doc}->setDocumentElement( $newNode ); | ||
| 152 | } | ||
| 153 | |||
| 154 | $newNode = $self->{doc}->find($xpath)->get_node(1); | ||
| 155 | |||
| 156 | } | ||
| 157 | |||
| 158 | $newNode->appendTextNode($value) if $value; | ||
| 159 | |||
| 160 | } | ||
| 161 | |||
| 162 | return undef; | ||
| 163 | } | ||
| 164 | |||
| 165 | |||
| 166 | |||
| 167 | ### | ||
| 168 | # | ||
| 169 | # The following method use a hash to store all the xpaths and their nodes. | ||
| 170 | # Locating a node is purely based on looking up its xpath the way it's been | ||
| 171 | # written. Thus 'somenode[1]' and 'somenode[position()=1]' will be treated as | ||
| 172 | # two different nodes! | ||
| 173 | # | ||
| 174 | # Using proper xpath doc->find to retrieve nodes instead of the hash will | ||
| 175 | # eliminate this problem. | ||
| 176 | # | ||
| 177 | # Anyway: | ||
| 178 | # - if you have a 'node[1]' and a 'node[3]' a 2nd node will not be | ||
| 179 | # created inbetween. | ||
| 180 | # - if you have a 'node[position()=1]' and a 'node[2]' the nodes will | ||
| 181 | # be reversed in the doc (all xpaths are created in alphabetical order). | ||
| 182 | # | ||
| 183 | # ALL OF THESE PROBLEMS HAVE BEEN FIXED IN THE _createNode METHOD ABOVE! | ||
| 184 | # | ||
| 185 | # | ||
| 186 | sub _createNode_simple { | ||
| 187 | my ($self, $xpath, $value) = @_; | ||
| 188 | |||
| 189 | my $name = $xpath; | ||
| 190 | $name =~ s{^.*\/(.*)$}{$1}; | ||
| 191 | $name =~ s{\[.*\]$}{}; | ||
| 192 | |||
| 193 | my $parent = $xpath; | ||
| 194 | $parent =~ s{^(.*)\/.*$}{$1}; | ||
| 195 | |||
| 196 | if ($parent && !defined $self->{tree}->{$parent}) { | ||
| 197 | $self->_createNode_simple($parent, undef); | ||
| 198 | } | ||
| 199 | |||
| 200 | # Attribute | ||
| 201 | if ($name =~ /^@/) { | ||
| 202 | print " addAttribute: $xpath\n" if $self->{debug}; | ||
| 203 | $self->{tree}->{$parent}->setAttribute($name,$value); | ||
| 204 | } | ||
| 205 | # Element | ||
| 206 | else { | ||
| 207 | print " createElement: $xpath\n" if $self->{debug}; | ||
| 208 | |||
| 209 | $self->{tree}->{$xpath} = $self->{doc}->createElement($name); | ||
| 210 | |||
| 211 | if ($parent) { | ||
| 212 | $self->{tree}->{$parent}->appendChild( $self->{tree}->{$xpath} ); | ||
| 213 | } else { | ||
| 214 | $self->{doc}->setDocumentElement( $self->{tree}->{$xpath} ); | ||
| 215 | } | ||
| 216 | |||
| 217 | $self->{tree}->{$xpath}->appendTextNode($value) if $value; | ||
| 218 | } | ||
| 219 | |||
| 220 | return undef; | ||
| 221 | } | ||
| 222 | |||
| 223 | |||
| 224 | ########### | ||
| 225 | 451; | ||
| 226 | |||
| 227 | |||
| 228 | __END__ |