| 1 | package XAS::Lib::XML; | ||
| 2 | |||
| 3 | our $VERSION = '0.01'; | ||
| 4 | |||
| 5 | use Try::Tiny; | ||
| 6 | use XML::LibXML; | ||
| 7 | use XML::LibXML::XPathContext; | ||
| 8 | |||
| 9 | use XAS::Class | ||
| 10 | debug => 0, | ||
| 11 | version => $VERSION, | ||
| 12 | base => 'XAS::Base', | ||
| 13 | import => 'class', | ||
| 14 | accessors => 'parser schema doc xpc', | ||
| 15 | utils => 'dotid compress', | ||
| 16 | constants => 'TRUE FALSE', | ||
| 17 | vars => { | ||
| 18 | PARAMS => { | ||
| 19 | -xsd => { optional => 1, default => undef }, | ||
| 20 | -default_namespace => { optional => 1, default => 'def' }, | ||
| 21 | }, | ||
| 22 | XMLERR => '', | ||
| 23 | } | ||
| 24 | ; | ||
| 25 | |||
| 26 | # ---------------------------------------------------------------------- | ||
| 27 | # Public Methods | ||
| 28 | # ---------------------------------------------------------------------- | ||
| 29 | |||
| 30 | sub get_items { | ||
| 31 | my $self = shift; | ||
| 32 | my ($xpath) = $self->validate_params(\@_, [1]); | ||
| 33 | |||
| 34 | my @nodes; | ||
| 35 | my $xpc = $self->xpc; | ||
| 36 | my $node = $self->doc->documentElement(); | ||
| 37 | |||
| 38 | if (my ($key) = $xpc->findnodes($xpath, $node)) { | ||
| 39 | |||
| 40 | if ($key->hasChildNodes()) { | ||
| 41 | |||
| 42 | @nodes = $key->childNodes(); | ||
| 43 | |||
| 44 | } | ||
| 45 | |||
| 46 | } | ||
| 47 | |||
| 48 | return wantarray ? @nodes : \@nodes; | ||
| 49 | |||
| 50 | } | ||
| 51 | |||
| 52 | sub get_item { | ||
| 53 | my $self = shift; | ||
| 54 | my ($xpath) = $self->validate_params(\@_, [1]); | ||
| 55 | |||
| 56 | my $value = ''; | ||
| 57 | my $xpc = $self->xpc; | ||
| 58 | my $node = $self->doc->documentElement(); | ||
| 59 | |||
| 60 | if (my ($key) = $xpc->findnodes($xpath, $node)) { | ||
| 61 | |||
| 62 | $value = $key->textContent(); | ||
| 63 | |||
| 64 | } | ||
| 65 | |||
| 66 | return $value; | ||
| 67 | |||
| 68 | } | ||
| 69 | |||
| 70 | sub is_valid { | ||
| 71 | my $self = shift; | ||
| 72 | |||
| 73 | my $doc = $self->doc; | ||
| 74 | |||
| 75 | return TRUE unless (defined($self->{schema})); | ||
| 76 | |||
| 77 | try { | ||
| 78 | |||
| 79 | $self->schema->validate($doc); # validate the document | ||
| 80 | |||
| 81 | } catch { | ||
| 82 | |||
| 83 | my $ex = $_; | ||
| 84 | |||
| 85 | $self->class->var('XMLERR', $ex); | ||
| 86 | |||
| 87 | $self->throw_msg( | ||
| 88 | dotid($self->class) . '.is_valid', | ||
| 89 | 'xml_validate', | ||
| 90 | ); | ||
| 91 | |||
| 92 | }; | ||
| 93 | |||
| 94 | return TRUE; | ||
| 95 | |||
| 96 | } | ||
| 97 | |||
| 98 | sub load { | ||
| 99 | my $self = shift; | ||
| 100 | my ($xml) = $self->validate_params(\@_, [1]); | ||
| 101 | |||
| 102 | try { | ||
| 103 | |||
| 104 | # load and parse the document | ||
| 105 | |||
| 106 | $self->{doc} = $self->parser->load_xml(string => $xml); | ||
| 107 | |||
| 108 | # find and register all namespaces | ||
| 109 | |||
| 110 | $self->_load_namespace(); | ||
| 111 | |||
| 112 | } catch { | ||
| 113 | |||
| 114 | my $ex = $_; | ||
| 115 | |||
| 116 | $self->throw_msg( | ||
| 117 | dotid($self->class). '.parser', | ||
| 118 | 'xml_parser', | ||
| 119 | ); | ||
| 120 | |||
| 121 | }; | ||
| 122 | |||
| 123 | } | ||
| 124 | |||
| 125 | sub xmlerr { | ||
| 126 | my $class = shift; | ||
| 127 | my ($value) = $class->validate_params(\@_, [ | ||
| 128 | { optional => 1, default => undef } | ||
| 129 | ]); | ||
| 130 | |||
| 131 | if (defined($value)) { | ||
| 132 | |||
| 133 | class->var('XMLERR', $value); | ||
| 134 | |||
| 135 | } | ||
| 136 | |||
| 137 | return class->var('XMLERR'); | ||
| 138 | |||
| 139 | } | ||
| 140 | |||
| 141 | # ---------------------------------------------------------------------- | ||
| 142 | # Private Methods | ||
| 143 | # ---------------------------------------------------------------------- | ||
| 144 | |||
| 145 | sub init { | ||
| 146 | my $class = shift; | ||
| 147 | |||
| 148 | my $self = $class->SUPER::init(@_); | ||
| 149 | |||
| 150 | $self->{parser} = XML::LibXML->new(); | ||
| 151 | $self->{xpc} = XML::LibXML::XPathContext->new(); | ||
| 152 | |||
| 153 | if (defined($self->{xsd})) { | ||
| 154 | |||
| 155 | $self->{schema} = XML::LibXML::Schema->new(location => $self->xsd); | ||
| 156 | |||
| 157 | } | ||
| 158 | |||
| 159 | return $self; | ||
| 160 | |||
| 161 | } | ||
| 162 | |||
| 163 | sub _load_namespace { | ||
| 164 | my $self = shift; | ||
| 165 | |||
| 166 | my $hash; | ||
| 167 | my $doc = $self->doc; | ||
| 168 | my $def = $self->default_namespace; | ||
| 169 | |||
| 170 | foreach my $node ($doc->findnodes('//*/namespace::*')) { | ||
| 171 | |||
| 172 | my $ns = $node->getLocalName() || $def; | ||
| 173 | my $uri = $node->getValue(); | ||
| 174 | |||
| 175 | $hash->{$ns} = $uri; # filter multiple namespaces | ||
| 176 | |||
| 177 | } | ||
| 178 | |||
| 179 | while (my ($key, $value) = each(%$hash)) { | ||
| 180 | |||
| 181 | $self->xpc->registerNs($key, $value); | ||
| 182 | |||
| 183 | } | ||
| 184 | |||
| 185 | } | ||
| 186 | |||
| 187 | 1; | ||
| 188 | |||
| 189 | __END__ | ||
| 190 | |||
| 191 | =head1 NAME | ||
| 192 | |||
| 193 | XAS::Lib::XML - A class to manipulate XML documents | ||
| 194 | |||
| 195 | =head1 SYNOPSIS | ||
| 196 | |||
| 197 | use XAS::Lib::XML; | ||
| 198 | |||
| 199 | my $xpath; | ||
| 200 | my $buffer = <STDIN>; | ||
| 201 | my $filename = 'schemna.xsd', | ||
| 202 | |||
| 203 | my $xml = XAS::Lib::XML->new( | ||
| 204 | -xsd => $filename, | ||
| 205 | ); | ||
| 206 | |||
| 207 | $xml->load($buffer); | ||
| 208 | if ($xml->is_valid) { | ||
| 209 | |||
| 210 | $xpath = '//sif:SIF_Header/sif:SIF_SourceId'; | ||
| 211 | printf("value: %s\n", $xml->get_item($xpath)); | ||
| 212 | |||
| 213 | } | ||
| 214 | |||
| 215 | =head1 DESCRIPTION | ||
| 216 | |||
| 217 | This module is able to load, parse and validate a xml document. | ||
| 218 | |||
| 219 | =head1 METHODS | ||
| 220 | |||
| 221 | =head2 new | ||
| 222 | |||
| 223 | This method initialize the module and takes these parameters: | ||
| 224 | |||
| 225 | =over 4 | ||
| 226 | |||
| 227 | =item B<-xsd> | ||
| 228 | |||
| 229 | The XML Schema to validate against. | ||
| 230 | |||
| 231 | =item B<-default_namespace> | ||
| 232 | |||
| 233 | The default namespace to use, defaults to 'def'. | ||
| 234 | |||
| 235 | =back | ||
| 236 | |||
| 237 | =head2 load($xml) | ||
| 238 | |||
| 239 | This method loads and parses a XML document. | ||
| 240 | |||
| 241 | =over 4 | ||
| 242 | |||
| 243 | =item B<$xml> | ||
| 244 | |||
| 245 | The XML to load. | ||
| 246 | |||
| 247 | =back | ||
| 248 | |||
| 249 | =head2 is_valid | ||
| 250 | |||
| 251 | This method validates the document to the XSD. If valid returns TRUE, | ||
| 252 | otherwise throws an exception. | ||
| 253 | |||
| 254 | =head2 get_item($xpath) | ||
| 255 | |||
| 256 | This method will return the string valuse of the Xpath. | ||
| 257 | |||
| 258 | =over 4 | ||
| 259 | |||
| 260 | =item B<$xpath> | ||
| 261 | |||
| 262 | The Xpath string. | ||
| 263 | |||
| 264 | =back | ||
| 265 | |||
| 266 | =head2 get_items($xpath) | ||
| 267 | |||
| 268 | This method will return an array of nodes for the given Xpath. | ||
| 269 | |||
| 270 | =over 4 | ||
| 271 | |||
| 272 | =item B<$xpath> | ||
| 273 | |||
| 274 | The Xpath string. | ||
| 275 | |||
| 276 | =back | ||
| 277 | |||
| 278 | =head2 xmlerr | ||
| 279 | |||
| 280 | A class method to return the full XML error string. | ||
| 281 | |||
| 282 | =head1 SEE ALSO | ||
| 283 | |||
| 284 | =over 4 | ||
| 285 | |||
| 286 | =item L<XAS|XAS> | ||
| 287 | |||
| 288 | =item L<XML::LibXML|XML::LibXML> | ||
| 289 | |||
| 290 | =back | ||
| 291 | |||
| 292 | =head1 AUTHOR | ||
| 293 | |||
| 294 | Kevin L. Esteb, E<lt>kevin@kesteb.usE<gt> | ||
| 295 | |||
| 296 | =head1 COPYRIGHT AND LICENSE | ||
| 297 | |||
| 298 | Copyright (C) 2014 Kevin L. Esteb | ||
| 299 | |||
| 300 | This is free software; you can redistribute it and/or modify it under | ||
| 301 | the terms of the Artistic License 2.0. For details, see the full text | ||
| 302 | of the license at http://www.perlfoundation.org/artistic_license_2_0. | ||
| 303 | |||
| 304 | =cut |