| 1 | #----------------------------------------------------------------- | ||
| 2 | # MOSES::MOBY::Parser | ||
| 3 | # Author: Edward Kawas <edward.kawas@gmail.com>, | ||
| 4 | # Martin Senger <martin.senger@gmail.com> | ||
| 5 | # | ||
| 6 | # For copyright and disclaimer see below. | ||
| 7 | # | ||
| 8 | # $Id: Parser.pm,v 1.4 2008/04/29 19:45:01 kawas Exp $ | ||
| 9 | #----------------------------------------------------------------- | ||
| 10 | |||
| 11 | package MOSES::MOBY::Parser; | ||
| 12 | |||
| 13 | use MOSES::MOBY::Base; | ||
| 14 | use base qw( MOSES::MOBY::Base XML::SAX::Base ); | ||
| 15 | use XML::SAX::ParserFactory; | ||
| 16 | |||
| 17 | use MOSES::MOBY::Data::ProvisionInformation; | ||
| 18 | use MOSES::MOBY::Data::Xref; | ||
| 19 | use MOSES::MOBY::Data::Object; | ||
| 20 | use MOSES::MOBY::Tags; | ||
| 21 | use MOSES::MOBY::Package; | ||
| 22 | use MOSES::MOBY::ServiceException; | ||
| 23 | use MOSES::MOBY::Generators::GenTypes; | ||
| 24 | use strict; | ||
| 25 | |||
| 26 | # add versioning to this module | ||
| 27 | use vars qw /$VERSION/; | ||
| 28 | $VERSION = sprintf "%d.%02d", q$Revision: 1.4 $ =~ /: (\d+)\.(\d+)/; | ||
| 29 | |||
| 30 | #----------------------------------------------------------------- | ||
| 31 | # A list of allowed attribute names. See MOSES::MOBY::Base for details. | ||
| 32 | #----------------------------------------------------------------- | ||
| 33 | { | ||
| 34 | my %_allowed = | ||
| 35 | ( | ||
| 36 | cachedir => undef, | ||
| 37 | registry => undef, | ||
| 38 | lowestKnownDataTypes => { type => 'HASH' }, | ||
| 39 | generator => { type => 'MOSES::MOBY::Generators::GenTypes' } | ||
| 40 | ); | ||
| 41 | |||
| 42 | sub _accessible { | ||
| 43 | my ($self, $attr) = @_; | ||
| 44 | exists $_allowed{$attr} or $self->SUPER::_accessible ($attr); | ||
| 45 | } | ||
| 46 | sub _attr_prop { | ||
| 47 | my ($self, $attr_name, $prop_name) = @_; | ||
| 48 | my $attr = $_allowed {$attr_name}; | ||
| 49 | return ref ($attr) ? $attr->{$prop_name} : $attr if $attr; | ||
| 50 | return $self->SUPER::_attr_prop ($attr_name, $prop_name); | ||
| 51 | } | ||
| 52 | } | ||
| 53 | |||
| 54 | #----------------------------------------------------------------- | ||
| 55 | # init | ||
| 56 | #----------------------------------------------------------------- | ||
| 57 | sub init { | ||
| 58 | my ($self) = shift; | ||
| 59 | $self->SUPER::init(); | ||
| 60 | $self->lowestKnownDataTypes ({}); | ||
| 61 | } | ||
| 62 | |||
| 63 | my %pcdataNames ; | ||
| 64 | my %pcdataNamesForPrimitives; | ||
| 65 | my @pcdataNamesArray; | ||
| 66 | my @pcdataNamesArrayForPrimitives; | ||
| 67 | |||
| 68 | # special logger just for the parser | ||
| 69 | my $PLOG; | ||
| 70 | |||
| 71 | BEGIN { | ||
| 72 | @pcdataNamesArray = | ||
| 73 | ( NOTES, SERVICECOMMENT, VALUE, XREF, EXCEPTIONCODE, EXCEPTIONMESSAGE ); | ||
| 74 | @pcdataNamesArrayForPrimitives = | ||
| 75 | ( MOBYSTRING, MOBYINTEGER, MOBYFLOAT, MOBYBOOLEAN, MOBYDATETIME ); | ||
| 76 | |||
| 77 | foreach (@pcdataNamesArray) { | ||
| 78 | $pcdataNames{$_} = 1; | ||
| 79 | } | ||
| 80 | foreach (@pcdataNamesArrayForPrimitives) { | ||
| 81 | $pcdataNamesForPrimitives{$_} = 1; | ||
| 82 | } | ||
| 83 | |||
| 84 | # special logger just for the parser because debugging level from | ||
| 85 | # the parser clutters other logging too much | ||
| 86 | use Log::Log4perl qw(get_logger :levels :no_extra_logdie_message); | ||
| 87 | $PLOG = get_logger ('parser'); | ||
| 88 | } | ||
| 89 | |||
| 90 | ############################################################ | ||
| 91 | # GLOBAL VARIABLES | ||
| 92 | ############################################################ | ||
| 93 | my %generated; # keep track of whether a type was generated or not | ||
| 94 | my @objectStack; # type MobyObject | ||
| 95 | my @pcdataStack; # strings | ||
| 96 | my $readingMobyObject = 0; # true if inside Simple | ||
| 97 | my $readingCollection = 0; # true if inside collection | ||
| 98 | my $readingXrefs = 0; # true if inside Crossreference | ||
| 99 | my $readingProvision = 0; # true if inside provision information | ||
| 100 | my $insubstitution = 0; # when just using 'lowestKnownDataTypes' it contains a name of substituted element | ||
| 101 | my $inServiceNotes = 0; # true if inside serviceNotes | ||
| 102 | my $inMobyException = 0; | ||
| 103 | my $ignoring = 0; # count depth of ignored (unknown) data objects | ||
| 104 | my $result; # type MobyPackage - the whole result | ||
| 105 | my @articleNames; | ||
| 106 | my $locator; | ||
| 107 | ######################END OF GLOBALS######################## | ||
| 108 | |||
| 109 | #----------------------------------------------------------------- | ||
| 110 | # parse | ||
| 111 | # args: method => 'string', data => direct XML | ||
| 112 | # OR | ||
| 113 | # method => 'file', data => filename | ||
| 114 | #----------------------------------------------------------------- | ||
| 115 | sub parse { | ||
| 116 | my ($self, %args) = @_; | ||
| 117 | $self->throw ("parse() needs arguments 'method' and 'data'.") | ||
| 118 | unless $args{method} and $args{data}; | ||
| 119 | |||
| 120 | # I could not assign this default value in init(), because it was | ||
| 121 | # before 'cachedir' etc. were set | ||
| 122 | my @generator_args = (); | ||
| 123 | push (@generator_args, (cachedir => $self->cachedir)) if $self->cachedir; | ||
| 124 | push (@generator_args, (registry => $self->registry)) if $self->registry; | ||
| 125 | $self->generator ( new MOSES::MOBY::Generators::GenTypes (@generator_args) ) | ||
| 126 | unless $self->generator; | ||
| 127 | |||
| 128 | $self->select_parser; | ||
| 129 | my $parser = XML::SAX::ParserFactory->parser(Handler => $self); | ||
| 130 | |||
| 131 | if ($args{method} eq 'string') { | ||
| 132 | $parser->parse_string ($args{data}); | ||
| 133 | } elsif ($args{method} eq 'file') { | ||
| 134 | $parser->parse_file ($args{data}); | ||
| 135 | } else { | ||
| 136 | $self->throw ("in parse(): 'method' is neither 'string' nor 'file'."); | ||
| 137 | } | ||
| 138 | return $result if $result; | ||
| 139 | $PLOG->error("There was a problem parsing\n$args{data}.\nIf this is a file, please make sure that the file exists, otherwise please ensure that the XML is 'valid'."); | ||
| 140 | $self->throw ("There was a problem parsing\n$args{data}.\nIf this is a file, please make sure that the file exists, otherwise please ensure that the XML is 'valid'."); | ||
| 141 | } | ||
| 142 | |||
| 143 | #----------------------------------------------------------------- | ||
| 144 | # select_parser | ||
| 145 | # | ||
| 146 | # If there is a configuration option defining what XML parser to use, | ||
| 147 | # this method selects it. Otherwise, it leaves it to the parser | ||
| 148 | # factory to find it out. | ||
| 149 | # | ||
| 150 | #----------------------------------------------------------------- | ||
| 151 | sub select_parser { | ||
| 152 | my $self = shift; | ||
| 153 | if (defined $MOBYCFG::XML_PARSER) { | ||
| 154 | $XML::SAX::ParserPackage = $MOBYCFG::XML_PARSER; | ||
| 155 | } | ||
| 156 | } | ||
| 157 | |||
| 158 | #********************************************************************* | ||
| 159 | # | ||
| 160 | # XML-SAX 2.0 handler routines. | ||
| 161 | # | ||
| 162 | #******************************************************************** | ||
| 163 | |||
| 164 | sub start_element { | ||
| 165 | my ( $self, $element ) = @_; | ||
| 166 | |||
| 167 | # element is a hash reference with these properties: | ||
| 168 | # Name The element type name (including prefix). | ||
| 169 | # Attributes The attributes attached to the element, if any. | ||
| 170 | # NamespaceURI The namespace of this element. | ||
| 171 | # Prefix The namespace prefix used on this element. | ||
| 172 | # LocalName The local name of this element. | ||
| 173 | # | ||
| 174 | # Attributes is a reference to hash keyed by JClark namespace notation. | ||
| 175 | # That is, the keys are of the form "{NamespaceURI}LocalName". If the attribute | ||
| 176 | # has no NamespaceURI, then it is simply "{}LocalName". Each attribute is a hash reference with these properties: | ||
| 177 | # Name The attribute name (including prefix). | ||
| 178 | # Value The normalized value of the attribute. | ||
| 179 | # NamespaceURI The namespace of this attribute. | ||
| 180 | # Prefix The namespace prefix used on this attribute. | ||
| 181 | # LocalName The local name of this attribute. | ||
| 182 | $PLOG->debug ("Starting element $element->{Name} with local name $element->{LocalName} \n"); | ||
| 183 | |||
| 184 | if ( $ignoring > 0 ) { | ||
| 185 | $ignoring++; | ||
| 186 | return; | ||
| 187 | } | ||
| 188 | if ( exists $pcdataNames{ $element->{LocalName} } ) { | ||
| 189 | my $st = ""; | ||
| 190 | push @pcdataStack, \$st; | ||
| 191 | } | ||
| 192 | if ($readingMobyObject) { | ||
| 193 | if ( exists $pcdataNamesForPrimitives{ $element->{LocalName} } ) { | ||
| 194 | my $st = ""; | ||
| 195 | push @pcdataStack, \$st; | ||
| 196 | } | ||
| 197 | } | ||
| 198 | if ($readingMobyObject) { | ||
| 199 | if ( $element->{LocalName} eq PROVISIONINFORMATION ) { | ||
| 200 | push @objectStack, \MOSES::MOBY::Data::ProvisionInformation->new(); | ||
| 201 | $readingProvision = 1; | ||
| 202 | } | ||
| 203 | elsif ( $element->{LocalName} eq CROSSREFERENCE ) { | ||
| 204 | $readingXrefs = 1; | ||
| 205 | } | ||
| 206 | elsif ($readingXrefs) { | ||
| 207 | if ( $element->{LocalName} eq XREF ) { | ||
| 208 | my $xref = MOSES::MOBY::Data::Xref->new(); | ||
| 209 | $xref->id( $self->getValue( attributes=>$element->{Attributes}, name=>OBJ_ID ) ); | ||
| 210 | $xref->namespace( | ||
| 211 | $self->getValue( attributes=>$element->{Attributes}, name=>OBJ_NAMESPACE ) ); | ||
| 212 | eval { | ||
| 213 | $xref->evidenceCode($self->getValue( attributes=>$element->{Attributes}, name=>EVIDENCECODE ) | ||
| 214 | ); | ||
| 215 | }; | ||
| 216 | if ($@) { | ||
| 217 | $self->error ($@); | ||
| 218 | } | ||
| 219 | my $serviceName = | ||
| 220 | $self->getValue( attributes=>$element->{Attributes}, name=>SERVICENAME ); | ||
| 221 | my $serviceAuthority = | ||
| 222 | $self->getValue( attributes=>$element->{Attributes}, name=>AUTHURI ); | ||
| 223 | if ($serviceName and $serviceAuthority) { | ||
| 224 | $xref->service ($serviceName); | ||
| 225 | $xref->authority ($serviceAuthority); | ||
| 226 | } | ||
| 227 | push @objectStack, \$xref; | ||
| 228 | } | ||
| 229 | elsif ( $element->{LocalName} eq MOBYOBJECT ) { | ||
| 230 | my $xref = MOSES::MOBY::Data::Xref->new(); | ||
| 231 | $xref->id( $self->getValue( attributes=>$element->{Attributes}, name=>OBJ_ID ) ); | ||
| 232 | $xref->namespace( | ||
| 233 | $self->getValue( attributes=>$element->{Attributes}, name=>OBJ_NAMESPACE ) ); | ||
| 234 | #$xref->isSimpleXref(1); | ||
| 235 | push @objectStack, \$xref; | ||
| 236 | } | ||
| 237 | } | ||
| 238 | elsif ($readingProvision) { | ||
| 239 | if ( $element->{LocalName} eq SERVICESOFTWARE ) { | ||
| 240 | my $info = ${ $self->vPeek('MOSES::MOBY::Data::ProvisionInformation') }; | ||
| 241 | $info->softwareName( | ||
| 242 | $self->getValue( attributes=>$element->{Attributes}, name=>SOFTWARENAME ) ); | ||
| 243 | |||
| 244 | my $version = | ||
| 245 | $self->getValue( attributes=>$element->{Attributes}, name=>SOFTWAREVERSION ); | ||
| 246 | $version = | ||
| 247 | $self->getValue( attributes=>$element->{Attributes}, name=>PLAINVERSION ) | ||
| 248 | unless $version; | ||
| 249 | $info->softwareVersion($version); | ||
| 250 | |||
| 251 | my $comment = | ||
| 252 | $self->getValue( attributes=>$element->{Attributes}, name=>SOFTWARECOMMENT ); | ||
| 253 | $comment = $self->getValue( attributes=>$element->{Attributes}, name=>COMMENT ) | ||
| 254 | unless $comment; | ||
| 255 | $info->softwareComment($comment); | ||
| 256 | |||
| 257 | } | ||
| 258 | elsif ( $element->{LocalName} eq SERVICEDATABASE ) { | ||
| 259 | my $info = ${ $self->vPeek('MOSES::MOBY::Data::ProvisionInformation') }; | ||
| 260 | $info->dbName( | ||
| 261 | $self->getValue( attributes=>$element->{Attributes}, name=>DATABASENAME ) ); | ||
| 262 | |||
| 263 | my $version = | ||
| 264 | $self->getValue( attributes=>$element->{Attributes}, name=>DATABASEVERSION ); | ||
| 265 | $version = | ||
| 266 | $self->getValue( attributes=>$element->{Attributes}, name=>PLAINVERSION ) | ||
| 267 | unless $version; | ||
| 268 | $info->dbVersion($version); | ||
| 269 | |||
| 270 | my $comment = | ||
| 271 | $self->getValue( attributes=>$element->{Attributes}, name=>DATABASECOMMENT ); | ||
| 272 | $comment = $self->getValue( attributes=>$element->{Attributes}, name=>COMMENT ) | ||
| 273 | unless $comment; | ||
| 274 | $info->dbComment($comment); | ||
| 275 | } | ||
| 276 | } | ||
| 277 | else { | ||
| 278 | |||
| 279 | # deal with real data objects | ||
| 280 | my $theClass; | ||
| 281 | eval { | ||
| 282 | $self->generator->load (datatype_names => [$element->{LocalName}]); | ||
| 283 | $theClass = $self->datatype2module ($element->{LocalName}); | ||
| 284 | $generated{$element->{LocalName}} = 1 if $theClass; | ||
| 285 | }; | ||
| 286 | if ( not $theClass and not $insubstitution ) { | ||
| 287 | my $obj = ${ $self->peek() }; | ||
| 288 | if ( $obj->isa('MOSES::MOBY::Simple') ) { | ||
| 289 | |||
| 290 | # try to find a substitute | ||
| 291 | my $lowestKnownDataType; | ||
| 292 | if ($obj->name and exists $self->lowestKnownDataTypes->{$obj->name}) { | ||
| 293 | $lowestKnownDataType = $self->lowestKnownDataTypes->{$obj->name}; | ||
| 294 | $self->generator->load ($lowestKnownDataType); | ||
| 295 | $theClass = $self->datatype2module ($lowestKnownDataType); | ||
| 296 | $generated{$lowestKnownDataType} = 1 if $theClass; | ||
| 297 | } | ||
| 298 | if ($theClass) { | ||
| 299 | $insubstitution = $obj->name || 1; | ||
| 300 | $LOG->warn ("'" . $element->{LocalName} | ||
| 301 | . "' substituted by '" | ||
| 302 | . $lowestKnownDataType | ||
| 303 | . "'" ); | ||
| 304 | } | ||
| 305 | } | ||
| 306 | } | ||
| 307 | if ($theClass) { | ||
| 308 | my $mobyObj = $theClass->new ( mobyname => $element->{LocalName} ); | ||
| 309 | $mobyObj->namespace( | ||
| 310 | $self->getValue( attributes=>$element->{Attributes}, name=>OBJ_NAMESPACE ) ); | ||
| 311 | $mobyObj->id( | ||
| 312 | $self->getValue( attributes=>$element->{Attributes}, name=>OBJ_ID ) ); | ||
| 313 | push @articleNames, $self->getValue( attributes=>$element->{Attributes}, name=>ARTICLENAME ) ; | ||
| 314 | push @objectStack, \$mobyObj; | ||
| 315 | } | ||
| 316 | else { | ||
| 317 | $ignoring++; | ||
| 318 | if ( not $insubstitution ) { | ||
| 319 | $LOG->warn ("Ignoring unknown element '" | ||
| 320 | . ( $element->{LocalName} || "" ) | ||
| 321 | . "'" ); | ||
| 322 | } | ||
| 323 | } | ||
| 324 | } | ||
| 325 | } | ||
| 326 | elsif ( $element->{LocalName} eq MOBY ) { | ||
| 327 | push @objectStack, \MOSES::MOBY::Package->new; | ||
| 328 | } | ||
| 329 | elsif ( $element->{LocalName} eq MOBYCONTENT ) { | ||
| 330 | my $str = $self->getValue( attributes=>$element->{Attributes}, name=>AUTHORITY ); | ||
| 331 | ${ $self->vPeek('MOSES::MOBY::Package') } | ||
| 332 | ->authority( $str ) | ||
| 333 | if $str; | ||
| 334 | } | ||
| 335 | elsif ( $element->{LocalName} eq MOBYDATA ) { | ||
| 336 | my $job = MOSES::MOBY::Job->new; | ||
| 337 | $job->jid( $self->getValue( attributes=>$element->{Attributes}, name=>QUERYID ) ); | ||
| 338 | push @objectStack, \$job; | ||
| 339 | } | ||
| 340 | elsif ( $element->{LocalName} eq SERVICENOTES ) { | ||
| 341 | $inServiceNotes = 1; | ||
| 342 | } | ||
| 343 | elsif ( $element->{LocalName} eq MOBYEXCEPTION ) { | ||
| 344 | if ($inServiceNotes) { | ||
| 345 | my $se = MOSES::MOBY::ServiceException->new; | ||
| 346 | $se->severity( | ||
| 347 | $self->getValue( attributes=>$element->{Attributes}, name=>SEVERITY ) ); | ||
| 348 | $se->jobId( $self->getValue( attributes=>$element->{Attributes}, name=>REFQUERYID ) ); | ||
| 349 | $se->dataName( | ||
| 350 | $self->getValue( attributes=>$element->{Attributes}, name=>REFELEMENT ) ); | ||
| 351 | push @objectStack, \$se; | ||
| 352 | $inMobyException = 1; | ||
| 353 | } | ||
| 354 | } | ||
| 355 | elsif ( $element->{LocalName} eq SIMPLE ) { | ||
| 356 | @articleNames = (); | ||
| 357 | my $simple = MOSES::MOBY::Simple->new; | ||
| 358 | $PLOG->error("A 'Simple' element cannot have an empty articlename unless it is a part of a collection.") if $self->getValue( attributes=>$element->{Attributes}, name=>ARTICLENAME ) eq "" and not $readingCollection; | ||
| 359 | $self->throw("A 'Simple' element cannot have an empty articlename unless it is a part of a collection.") if $self->getValue( attributes=>$element->{Attributes}, name=>ARTICLENAME ) eq "" and not $readingCollection; | ||
| 360 | $simple->name( $self->getValue( attributes=>$element->{Attributes}, name=>ARTICLENAME ) ) unless $readingCollection; | ||
| 361 | push @objectStack, \$simple; | ||
| 362 | $readingMobyObject = 1; | ||
| 363 | } | ||
| 364 | elsif ( $element->{LocalName} eq COLLECTION ) { | ||
| 365 | my $collection = MOSES::MOBY::Collection->new; | ||
| 366 | $PLOG->error("A 'Collection' element cannot have an empty articlename.") if $self->getValue( attributes=>$element->{Attributes}, name=>ARTICLENAME ) eq "" ; | ||
| 367 | $self->throw("A 'Collection' element cannot have an empty articlename.") if $self->getValue( attributes=>$element->{Attributes}, name=>ARTICLENAME ) eq "" ; | ||
| 368 | $collection->name( | ||
| 369 | $self->getValue( attributes=>$element->{Attributes}, name=>ARTICLENAME ) ); | ||
| 370 | push @objectStack, \$collection; | ||
| 371 | $readingCollection = 1; | ||
| 372 | } | ||
| 373 | elsif ( $element->{LocalName} eq PARAMETER ) { | ||
| 374 | my $parameter = MOSES::MOBY::Parameter->new; | ||
| 375 | $parameter->name( | ||
| 376 | $self->getValue( attributes=>$element->{Attributes}, name=>ARTICLENAME ) ); | ||
| 377 | push @objectStack, \$parameter; | ||
| 378 | } | ||
| 379 | $PLOG->debug ($self->printInfo); | ||
| 380 | } | ||
| 381 | |||
| 382 | sub end_element { | ||
| 383 | my ( $self, $element ) = @_; | ||
| 384 | $PLOG->debug ("Ending element $element->{Name}\n"); | ||
| 385 | my ( $obj, $obj2 ); | ||
| 386 | if ( $ignoring > 0 ) { | ||
| 387 | $ignoring--; | ||
| 388 | return; | ||
| 389 | } | ||
| 390 | if ($readingMobyObject) { | ||
| 391 | if ( $element->{LocalName} eq SIMPLE ) { | ||
| 392 | $obj = pop @objectStack; | ||
| 393 | $obj = ${$obj}; | ||
| 394 | $obj2 = ${ $self->peek() }; | ||
| 395 | |||
| 396 | if ( $obj2->isa('MOSES::MOBY::Job') ) { | ||
| 397 | $obj2->add_dataElements($obj); | ||
| 398 | } | ||
| 399 | elsif ( $obj2->isa('MOSES::MOBY::Collection') ) { | ||
| 400 | $obj2->add_data($obj); | ||
| 401 | } | ||
| 402 | else { | ||
| 403 | $self->error ("A simple element should not be in '" | ||
| 404 | . ref($obj2) . "'." | ||
| 405 | ); | ||
| 406 | } | ||
| 407 | $readingMobyObject = 0; | ||
| 408 | } | ||
| 409 | elsif ( $element->{LocalName} eq PROVISIONINFORMATION ) { | ||
| 410 | $obj = pop @objectStack; | ||
| 411 | $obj = ${$obj}; | ||
| 412 | ${ $self->vPeek("MOSES::MOBY::Data::Object") }->provision($obj); | ||
| 413 | $readingProvision = 0; | ||
| 414 | } | ||
| 415 | elsif ( $element->{LocalName} eq CROSSREFERENCE ) { | ||
| 416 | $readingXrefs = 0; | ||
| 417 | } | ||
| 418 | elsif ($readingXrefs) { | ||
| 419 | if ( $element->{LocalName} eq XREF ) { | ||
| 420 | $obj = pop @pcdataStack; | ||
| 421 | $obj = ${$obj}; | ||
| 422 | ${ $self->vPeek("MOSES::MOBY::Data::Xref") } | ||
| 423 | ->description($obj); | ||
| 424 | $obj = pop @objectStack; | ||
| 425 | $obj = ${$obj}; | ||
| 426 | ${ $self->vPeek("MOSES::MOBY::Data::Object") }->add_xrefs($obj); | ||
| 427 | } | ||
| 428 | elsif ( $element->{LocalName} eq MOBYOBJECT ) { | ||
| 429 | $obj = pop @objectStack; | ||
| 430 | $obj = ${$obj}; | ||
| 431 | ${ $self->vPeek("MOSES::MOBY::Data::Object") }->add_xrefs($obj); | ||
| 432 | } | ||
| 433 | } | ||
| 434 | elsif ($readingProvision) { | ||
| 435 | if ( $element->{LocalName} eq SERVICECOMMENT ) { | ||
| 436 | $obj = pop @pcdataStack; | ||
| 437 | $obj = ${$obj}; | ||
| 438 | ${ $self->vPeek("MOSES::MOBY::Data::ProvisionInformation") } | ||
| 439 | ->serviceComment($obj); | ||
| 440 | } | ||
| 441 | } | ||
| 442 | else { | ||
| 443 | if ($insubstitution or $generated{$element->{LocalName}}) { | ||
| 444 | my $mobyObj = pop @objectStack; | ||
| 445 | $mobyObj = ${$mobyObj}; | ||
| 446 | if ( $insubstitution | ||
| 447 | and $element->{LocalName} eq $insubstitution ) { | ||
| 448 | $insubstitution = 0; | ||
| 449 | } | ||
| 450 | if ( exists $pcdataNamesForPrimitives{ $element->{LocalName} } ) | ||
| 451 | { | ||
| 452 | my $value = pop @pcdataStack; | ||
| 453 | $value = ${$value}; | ||
| 454 | $mobyObj->value($value); | ||
| 455 | } | ||
| 456 | $obj2 = ${ $self->peek() }; | ||
| 457 | if ( $obj2->isa("MOSES::MOBY::Simple") ) { | ||
| 458 | $obj2->data($mobyObj); | ||
| 459 | } | ||
| 460 | else { | ||
| 461 | |||
| 462 | # save original article name in data object itself | ||
| 463 | # (will be needed for creating back an XML of this object) | ||
| 464 | my $methodName = pop @articleNames; | ||
| 465 | $mobyObj->original_memberName ($methodName); | ||
| 466 | |||
| 467 | #is the article name for children empty? throw error | ||
| 468 | $PLOG->error("Invalid article name given for children of '".$obj2->mobyname."'. Please make sure that these fields are not empty.") if $methodName eq ""; | ||
| 469 | $self->throw("Invalid article name given for children of '".$obj2->mobyname."'. Please make sure that these fields are not empty.") if $methodName eq ""; | ||
| 470 | $methodName = $self->escape_name ($methodName); | ||
| 471 | $self->callMethod( | ||
| 472 | actor => $obj2, | ||
| 473 | method => $methodName, | ||
| 474 | parameter => $mobyObj, | ||
| 475 | ); | ||
| 476 | } | ||
| 477 | } | ||
| 478 | } | ||
| 479 | } | ||
| 480 | elsif ( $element->{LocalName} eq MOBY ) { | ||
| 481 | if ( scalar @objectStack == 0 ) { | ||
| 482 | $self->error( "Nothing came out from the parsed XML data."); | ||
| 483 | } | ||
| 484 | $obj = pop @objectStack; | ||
| 485 | $obj = ${$obj}; | ||
| 486 | if ( not $obj->isa("MOSES::MOBY::Package") ) { | ||
| 487 | $self->error("The input XML does not start with a MOBY tag"); | ||
| 488 | } | ||
| 489 | $result = $obj; | ||
| 490 | } | ||
| 491 | elsif ( $element->{LocalName} eq MOBYCONTENT ) { | ||
| 492 | |||
| 493 | # do nothing | ||
| 494 | } | ||
| 495 | elsif ( $element->{LocalName} eq SERVICENOTES ) { | ||
| 496 | $inServiceNotes = 0; | ||
| 497 | } | ||
| 498 | elsif ( $element->{LocalName} eq MOBYEXCEPTION ) { | ||
| 499 | if ($inServiceNotes) { | ||
| 500 | $obj = pop @objectStack; | ||
| 501 | $obj = ${$obj}; | ||
| 502 | ${ $self->vPeek("MOSES::MOBY::Package") }->add_exceptions($obj); | ||
| 503 | $inMobyException = 0; | ||
| 504 | } | ||
| 505 | } | ||
| 506 | elsif ( $element->{LocalName} eq MOBYDATA ) { | ||
| 507 | $obj = pop @objectStack; | ||
| 508 | $obj = ${$obj}; | ||
| 509 | ${ $self->vPeek("MOSES::MOBY::Package") }->add_jobs($obj); | ||
| 510 | } | ||
| 511 | elsif ( $element->{LocalName} eq COLLECTION ) { | ||
| 512 | $obj = pop @objectStack; | ||
| 513 | $obj = ${$obj}; | ||
| 514 | ${ $self->vPeek("MOSES::MOBY::Job") }->add_dataElements($obj); | ||
| 515 | $readingCollection = 0; | ||
| 516 | } | ||
| 517 | elsif ( $element->{LocalName} eq PARAMETER ) { | ||
| 518 | $obj = pop @objectStack; | ||
| 519 | $obj = ${$obj}; | ||
| 520 | ${ $self->vPeek("MOSES::MOBY::Job") }->add_dataElements($obj); | ||
| 521 | } | ||
| 522 | elsif ( $element->{LocalName} eq NOTES ) { | ||
| 523 | $obj = pop @pcdataStack; | ||
| 524 | $obj = ${$obj}; | ||
| 525 | ${ $self->vPeek("MOSES::MOBY::Package") }->serviceNotes($obj); | ||
| 526 | } | ||
| 527 | elsif ( $element->{LocalName} eq EXCEPTIONCODE ) { | ||
| 528 | $obj = pop @pcdataStack; | ||
| 529 | $obj = ${$obj}; | ||
| 530 | if ($inMobyException) { | ||
| 531 | ${ $self->vPeek("MOSES::MOBY::ServiceException") }->code($obj); | ||
| 532 | } | ||
| 533 | } | ||
| 534 | elsif ( $element->{LocalName} eq EXCEPTIONMESSAGE ) { | ||
| 535 | $obj = pop @pcdataStack; | ||
| 536 | $obj = ${$obj}; | ||
| 537 | if ($inMobyException ){ | ||
| 538 | ${ $self->vPeek("MOSES::MOBY::ServiceException") }->message($obj); | ||
| 539 | } | ||
| 540 | } | ||
| 541 | elsif ( $element->{LocalName} eq VALUE ) { | ||
| 542 | $obj = pop @pcdataStack; | ||
| 543 | $obj = ${$obj}; | ||
| 544 | ${ $self->vPeek("MOSES::MOBY::Parameter") }->value($obj); | ||
| 545 | } | ||
| 546 | $PLOG->debug ($self->printInfo) if $PLOG->is_debug; | ||
| 547 | } | ||
| 548 | |||
| 549 | sub characters { | ||
| 550 | my ( $self, $characters ) = @_; | ||
| 551 | $PLOG->debug ("characters: $characters->{Data}\n") if $PLOG->is_debug; | ||
| 552 | |||
| 553 | # characters is a hash reference with this property: | ||
| 554 | # Data The characters from the XML document. | ||
| 555 | if ( @pcdataStack . "" == 0 ) { | ||
| 556 | return; | ||
| 557 | } | ||
| 558 | my $text = $characters->{Data}; | ||
| 559 | $text =~ s/^\s+//; | ||
| 560 | $text =~ s/\s+$//; | ||
| 561 | ${ $self->pcdataPeek() } = ${ $self->pcdataPeek() } . $text; | ||
| 562 | |||
| 563 | } | ||
| 564 | |||
| 565 | sub start_document { | ||
| 566 | my ( $self, $document ) = @_; | ||
| 567 | @objectStack = (); | ||
| 568 | @articleNames = (); | ||
| 569 | @pcdataStack = (); | ||
| 570 | $ignoring = 0; | ||
| 571 | } | ||
| 572 | |||
| 573 | sub end_document { | ||
| 574 | my ( $self, $document ) = @_; | ||
| 575 | @objectStack = undef; | ||
| 576 | @pcdataStack = undef; | ||
| 577 | $ignoring = 0; | ||
| 578 | } | ||
| 579 | |||
| 580 | sub ignorable_whitespace { | ||
| 581 | my ( $self, $characters ) = @_; | ||
| 582 | } | ||
| 583 | |||
| 584 | # returns type MobyObject or dies if shouldBeThere->isa(ref(objectStack.top)) | ||
| 585 | sub vPeek { | ||
| 586 | my ( $self, $shouldBeThere ) = @_; | ||
| 587 | |||
| 588 | # my version of peek | ||
| 589 | my $obj = pop @objectStack; | ||
| 590 | push @objectStack, $obj; | ||
| 591 | if ( $$obj->isa($shouldBeThere) ) { | ||
| 592 | return $obj; | ||
| 593 | } | ||
| 594 | $PLOG->error("Wrong XML: Expected '$shouldBeThere' - but found " .ref $$obj); | ||
| 595 | $self->error("Wrong XML: Expected '$shouldBeThere' - but found " .ref $$obj); | ||
| 596 | } | ||
| 597 | |||
| 598 | # returns type MobyObject | ||
| 599 | sub peek { | ||
| 600 | my ($self) = @_; | ||
| 601 | |||
| 602 | # my version of peek | ||
| 603 | my $obj = pop @objectStack; | ||
| 604 | push @objectStack, $obj; | ||
| 605 | return $obj; | ||
| 606 | } | ||
| 607 | |||
| 608 | # returns string references | ||
| 609 | sub pcdataPeek { | ||
| 610 | my ($self) = @_; | ||
| 611 | |||
| 612 | # my version of peek | ||
| 613 | my $obj = pop @pcdataStack; | ||
| 614 | push @pcdataStack, $obj; | ||
| 615 | return $obj; | ||
| 616 | } | ||
| 617 | |||
| 618 | # returns the value of an attribute either using a namespace or not | ||
| 619 | # attributes=>, name=> | ||
| 620 | sub getValue { | ||
| 621 | my ( $self, %hash) = @_; | ||
| 622 | my (%attributes, $name); | ||
| 623 | |||
| 624 | %attributes = %{$hash{attributes}} if exists $hash{attributes}; | ||
| 625 | $name = $hash{name} if exists $hash{name}; | ||
| 626 | # die ("You need to provide attributes and an element name $name.") unless ($name and %attributes); | ||
| 627 | |||
| 628 | my $attr = | ||
| 629 | $attributes{ "{" . MOBY_XML_NS . "}$name" }; | ||
| 630 | my $string = $attr->{Value} || ""; | ||
| 631 | if ( $string eq "" ) { | ||
| 632 | $attr = $attributes{"{}$name"}; | ||
| 633 | $string = $attr->{Value} || ""; | ||
| 634 | } | ||
| 635 | return $string; | ||
| 636 | |||
| 637 | } | ||
| 638 | |||
| 639 | # call a method, methodName, on object actor using parameter | ||
| 640 | # actor=>, method=> parameter=> | ||
| 641 | sub callMethod { | ||
| 642 | my ( $self, %hash ) = @_; | ||
| 643 | my ( $actor, $methodName, $parameter ); | ||
| 644 | |||
| 645 | $actor = $hash{actor} ||''; | ||
| 646 | $methodName = $hash{method} ||''; | ||
| 647 | $parameter = $hash{parameter}; | ||
| 648 | eval { | ||
| 649 | if (ref($actor->$methodName) eq 'ARRAY' ) { | ||
| 650 | my $method = "add_$methodName"; | ||
| 651 | $actor->$method($parameter); | ||
| 652 | } else { | ||
| 653 | $actor->$methodName($parameter); | ||
| 654 | } | ||
| 655 | }; | ||
| 656 | if ($@ and not $insubstitution) { | ||
| 657 | $PLOG->error("Method '$methodName' was not found in the object ". $actor->mobyname); | ||
| 658 | $self->error( "Method '$methodName' was not found in the object " . $actor->mobyname); | ||
| 659 | } | ||
| 660 | |||
| 661 | } | ||
| 662 | |||
| 663 | #********************************************************************* | ||
| 664 | # | ||
| 665 | # XML-SAX 2.0 event to set document locator | ||
| 666 | # unfortunately, this event is passed along as it should be | ||
| 667 | # and as such, reporting exact errors specific to MOBY XML is hard | ||
| 668 | # | ||
| 669 | #******************************************************************** | ||
| 670 | sub set_document_locator { | ||
| 671 | my ($self) = shift; | ||
| 672 | $PLOG->debug ("setting the document locator"); | ||
| 673 | my( $loc) = @_; | ||
| 674 | $locator = $loc; | ||
| 675 | } | ||
| 676 | |||
| 677 | #********************************************************************* | ||
| 678 | # | ||
| 679 | # XML-SAX 2.0 error events | ||
| 680 | # | ||
| 681 | #******************************************************************** | ||
| 682 | |||
| 683 | sub fatal_error { | ||
| 684 | my ($self) = shift; | ||
| 685 | my $msg = $self->_format_msg (@_); | ||
| 686 | $self->throw ("Parsing XML fatally failed: $msg"); | ||
| 687 | } | ||
| 688 | |||
| 689 | sub error { | ||
| 690 | my ($self) = shift; | ||
| 691 | my $msg = $self->_format_msg (@_); | ||
| 692 | $self->throw ("Parsing XML failed: $msg"); | ||
| 693 | } | ||
| 694 | |||
| 695 | sub warning { | ||
| 696 | my ($self) = shift; | ||
| 697 | my $msg = $self->_format_msg (@_); | ||
| 698 | $LOG->warning ($msg); | ||
| 699 | } | ||
| 700 | |||
| 701 | sub _format_msg { | ||
| 702 | my ($self, $message) = @_; | ||
| 703 | return $message unless ref ($message) eq 'XML::SAX::Exception::Parse'; | ||
| 704 | |||
| 705 | my $pubId = $message->{PublicId} || ''; | ||
| 706 | my $sysId = $message->{SystemId} || ''; | ||
| 707 | my $linNo = $message->{LineNumber} || '?'; | ||
| 708 | my $colNo = $message->{ColumnNumber} || '?'; | ||
| 709 | my $msg = $message->{Message} || ''; | ||
| 710 | |||
| 711 | return "$msg [line $linNo, column $colNo] $sysId $pubId"; | ||
| 712 | } | ||
| 713 | |||
| 714 | sub printInfo { | ||
| 715 | my $self = shift; | ||
| 716 | return unless $PLOG->is_debug; | ||
| 717 | use Data::Dumper; | ||
| 718 | my $buf = | ||
| 719 | "##########################################################\n" | ||
| 720 | . "# INFO #\n" | ||
| 721 | . "##########################################################\n" | ||
| 722 | . "Object stack currently holds:\n\tBOTTOM: "; | ||
| 723 | foreach (@objectStack) { | ||
| 724 | $buf .= Dumper($$_) . "\n"; | ||
| 725 | } | ||
| 726 | $buf .= "TOP\n"; | ||
| 727 | $buf .= "pcdataStack currently holds:\n\tBOTTOM: "; | ||
| 728 | foreach (@pcdataStack) { | ||
| 729 | $buf .= Dumper($$_) . "\n"; | ||
| 730 | } | ||
| 731 | $buf .= "TOP\n"; | ||
| 732 | $buf .= | ||
| 733 | "readingMobyObject: $readingMobyObject\n" | ||
| 734 | . "readingCollection: $readingCollection\n" | ||
| 735 | . "readingXrefs : $readingXrefs\n" | ||
| 736 | . "readingProvision : $readingProvision\n" | ||
| 737 | . "insubstition : $insubstitution\n" | ||
| 738 | . "inServiceNotes : $inServiceNotes\n" | ||
| 739 | . "ignoring : $ignoring\n"; | ||
| 740 | $buf .= | ||
| 741 | "##########################################################\n" | ||
| 742 | . "# END INFO #\n" | ||
| 743 | . "##########################################################\n"; | ||
| 744 | return $buf; | ||
| 745 | } | ||
| 746 | |||
| 747 | 1; | ||
| 748 | __END__ |