blib/lib/XML/Toolset/Xerces.pm untested

filter: Statementstmt 0.0% 0 / 116 Branchbran 0.0% 0 / 56 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 197 CC 29 SCAR 29.6
CC
29
Cov
0%
new_dom
42.0
parse
30.0
new_parser
20.0
CRAP 19.3
1 package XML::Toolset::Xerces;
2  
3 use strict;
4 use XML::Toolset::DOM;
5 use XML::Xerces;
6  
7 use vars qw($VERSION $CATCH_ERROR @ISA);
8 $VERSION = sprintf"%d.%03d", q$Revision: 1.21 $ =~ /: (\d+)\.(\d+)/;
9 @ISA = qw(XML::Toolset::DOM);
10  
11 # This should happen in the XML::Xerces INIT block, but we expect this module to
12 # be dynamically loaded, so the INIT block probably won't happen.
13 XML::Xerces::XMLPlatformUtils::Initialize();
14  
15 sub version {
16 &App::sub_entry if ($App::trace);
17 my $version = XML::Xerces->VERSION;
18 &App::sub_exit($version) if ($App::trace);
19 return($version);
20 }
21  
22 sub parse {
23 &App::sub_entry if ($App::trace);
24 my ($self, $xml, $options) = @_;
25  
26 $options = {} if (!$options);
27 my $parser = $self->parser();
28 my $results = $self->{results};
29 delete $results->{error};
30 delete $results->{error_line};
31 delete $results->{error_column};
32  
33 # Use Memory buffer input source to read the XML string
34 my $input = XML::Xerces::MemBufInputSource->new($xml,$self->{schema_location});
35 $parser->parse($input);
36 my $dom = $parser->getDocument();
37
38 if ($results->{error}) {
39 $options->{error} = $results->{error};
40 $options->{error_line} = $results->{error_line};
41 $options->{error_column} = $results->{error_column};
42 $dom = undef;
43 }
44 else {
45 $options->{dom} = $dom;
46 }
47  
48 &App::sub_exit($dom) if ($App::trace);
49 return($dom);
50 }
51  
52 sub new_parser {
53 &App::sub_entry if ($App::trace);
54 my ($self, $options) = @_;
55  
56 my $results = {};
57 $self->{results} = $results;
58 my $parser = new XML::Xerces::XercesDOMParser;
59 # set various validation arguments based on argument
60 my $validation = $self->{validation};
61 if ($validation) {
62 #print "SETTING STRICT VALIDATION [results=$results]\n";
63 $parser->setValidationScheme("$XML::Xerces::AbstractDOMParser::Val_Auto");
64 $parser->setIncludeIgnorableWhitespace(0);
65 $parser->setDoSchema(1);
66 $parser->setDoNamespaces(1);
67 $parser->setValidationSchemaFullChecking(1);
68 $parser->setLoadExternalDTD(1);
69 $parser->setExitOnFirstFatalError(1);
70 $parser->setValidationConstraintFatal(1);
71 }
72 else {
73 #print "SETTING MINIMUM VALIDATION [results=$results]\n";
74 $parser->setValidationScheme("$XML::Xerces::AbstractDOMParser::Val_Never");
75 $parser->setDoSchema(0);
76 $parser->setDoNamespaces(0);
77 $parser->setValidationSchemaFullChecking(0);
78 $parser->setLoadExternalDTD(0);
79 }
80 # error handler
81 my $ErrorHandler = XML::Toolset::Xerces::ErrorHandler->new($self, results => $results);
82 $parser->setErrorHandler($ErrorHandler);
83  
84 &App::sub_exit($parser) if ($App::trace);
85 return($parser);
86 }
87  
88 sub new_dom {
89 &App::sub_entry if ($App::trace);
90 my ($self, $root_tag, $xmlns) = @_;
91  
92 $xmlns = $self->{xmlns} if (!$xmlns);
93 $xmlns = "" if (!$xmlns);
94  
95 #print "impl=$impl createDocument($xmlns, $root_tag, undef)\n";
96 my $impl = XML::Xerces::DOMImplementationRegistry::getDOMImplementation('LS');
97 my $dom = eval{$impl->createDocument($xmlns, $root_tag, undef)};
98 XML::Xerces::error($@) if $@;
99  
100 &App::sub_exit($dom) if ($App::trace);
101 return($dom);
102 }
103  
104 sub to_string {
105 &App::sub_entry if ($App::trace);
106 my ($self, $doc) = @_;
107  
108 my $dom = $doc->dom();
109 my $impl = XML::Xerces::DOMImplementationRegistry::getDOMImplementation('LS');
110 my $writer = $impl->createDOMWriter();
111 if ($writer->canSetFeature('format-pretty-print',1)) {
112 $writer->setFeature('format-pretty-print',1);
113 }
114 my $target = XML::Xerces::MemBufFormatTarget->new();
115 $writer->writeNode($target,$dom);
116 my $xml = $target->getRawBuffer();
117  
118 &App::sub_exit($xml) if ($App::trace);
119 return($xml);
120 }
121  
122 1;
123  
124 # Override XML::Xerces errors into warnings we can catch
125 package XML::Toolset::Xerces::ErrorHandler;
126  
127 use vars '@ISA';
128 @ISA = qw(XML::Xerces::PerlErrorHandler);
129  
130 sub new {
131 &App::sub_entry if ($App::trace);
132 my ($class, $xml_toolset, @args) = @_;
133 my $self = {
134 xml_toolset => $xml_toolset, @args
135 };
136 die "results hash not provided" if (!$self->{results});
137 bless $self, $class;
138 &App::sub_exit($self) if ($App::trace);
139 return($self);
140 }
141  
142 sub warning {
143 &App::sub_entry if ($App::trace);
144 my ($self, $exception) = @_;
145 $self->error_result($exception,"Warning");
146 &App::sub_exit() if ($App::trace);
147 }
148  
149 sub error {
150 &App::sub_entry if ($App::trace);
151 my ($self, $exception) = @_;
152 $self->error_result($exception,"Invalid XML");
153 &App::sub_exit() if ($App::trace);
154 }
155  
156 sub fatal_error {
157 &App::sub_entry if ($App::trace);
158 my ($self, $exception) = @_;
159 $self->error_result($exception,"XML error");
160 &App::sub_exit() if ($App::trace);
161 }
162  
163 sub error_result {
164 &App::sub_entry if ($App::trace);
165 my ($self, $exception, $message_prefix) = @_;
166 my $results = $self->{results};
167 $results->{error} = "$message_prefix: " . $exception->getMessage;
168 $results->{error_line} = $exception->getLineNumber;
169 $results->{error_column} = $exception->getColumnNumber;
170 #print "ERROR: $results->{error} [Line: $results->{error_line}][Col: $results->{error_column}] [results=$results]\n";
171 &App::sub_exit() if ($App::trace);
172 }
173  
174 1;
175  
176 __END__