XML/Axk/SAX/BuildDOM2.pm untested

filter: Statementstmt 0.0% 0 / 124 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 15 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 18 totaltotal 0.0% 0 / 204 CC 14 SCAR 26.8
CC
14
Cov
0%
start_document
42.0
start_element
30.0
characters
12.0
CRAP 14.5
1 package XML::Axk::SAX::BuildDOM2;
2 use strict;
3 use XML::Axk::DOM;
4 use Data::Dumper;
5  
6 # TODO support namespaces
7  
8 #use parent XML::SAX::Base; # TODO later
9  
10 our $VERSION;
11 $VERSION = "1.50";
12  
13 our $DEBUG = 0;
14  
15 # TODO:
16 # - add support for parameter entity references
17 # - expand API: insert Elements in the tree or stuff into DocType etc.
18  
19 sub new
20 {
21 my ($class, %args) = @_;
22 bless \%args, $class;
23 } #new()
24  
25 #-------- PerlSAX Handler methods ------------------------------
26  
27 sub start_document # was Init
28 {
29 my $self = shift;
30  
31 # Define Document if it's not set & not obtainable from Element or DocType
32 $self->{Document} ||=
33 (defined $self->{Element} ? $self->{Element}->getOwnerDocument : undef)
34 || (defined $self->{DocType} ? $self->{DocType}->getOwnerDocument : undef)
35 || new XML::DOM::Document();
36  
37 $self->{Element} ||= $self->{Document};
38  
39 unless (defined $self->{DocType})
40 {
41 $self->{DocType} = $self->{Document}->getDoctype
42 if defined $self->{Document};
43  
44 unless (defined $self->{Doctype})
45 {
46 #?? should be $doc->createDocType for extensibility!
47 $self->{DocType} = new XML::DOM::DocumentType ($self->{Document});
48 $self->{Document}->setDoctype ($self->{DocType});
49 }
50 }
51  
52 # Prepare for document prolog
53 $self->{InProlog} = 1;
54  
55 # We haven't passed the root element yet
56 $self->{EndDoc} = 0;
57  
58 undef $self->{LastText};
59 } #start_document()
60  
61 sub end_document # was Final
62 {
63 my $self = shift;
64 unless ($self->{SawDocType})
65 {
66 my $doctype = $self->{Document}->removeDoctype;
67 $doctype->dispose;
68 #?? do we always want to destroy the Doctype?
69 }
70 $self->{Document};
71 } #end_document()
72  
73 sub characters # was Char
74 {
75 my $self = $_[0];
76 my $str = $_[1]->{Data};
77  
78 if ($self->{InCDATA} && $self->{KeepCDATA})
79 {
80 undef $self->{LastText};
81 # Merge text with previous node if possible
82 $self->{Element}->addCDATA ($str);
83 }
84 else
85 {
86 # Merge text with previous node if possible
87 # Used to be: $expat->{DOM_Element}->addText ($str);
88 if ($self->{LastText})
89 {
90 $self->{LastText}->appendData ($str);
91 }
92 else
93 {
94 $self->{LastText} = $self->{Document}->createTextNode ($str);
95 $self->{Element}->appendChild ($self->{LastText});
96 }
97 }
98 } #characters()
99  
100 sub start_element # was Start
101 {
102 my ($self, $hash) = @_;
103 my $elem = $hash->{Name};
104 my $attr = $hash->{Attributes};
105 print "start_element:\n", Dumper($hash) if $DEBUG;
106  
107 my $parent = $self->{Element};
108 my $doc = $self->{Document};
109  
110 if ($parent == $doc)
111 {
112 # End of document prolog, i.e. start of first Element
113 $self->{InProlog} = 0;
114 }
115  
116 undef $self->{LastText};
117 my $node = $doc->createElement ($elem);
118 $self->{Element} = $node;
119 $parent->appendChild ($node);
120  
121 my $i = 0;
122 my $n = scalar keys %$attr;
123 return unless $n;
124  
125 if (exists $hash->{AttributeOrder})
126 {
127 my $defaulted = $hash->{Defaulted};
128 my @order = @{ $hash->{AttributeOrder} };
129  
130 # Specified attributes
131 for (my $i = 0; $i < $defaulted; $i++)
132 {
133 my $a = $order[$i];
134 my $att = $doc->createAttribute ($attr->{$a}->{LocalName},
135 $attr->{$a}->{Value}, 1);
136 $node->setAttributeNode ($att);
137 }
138  
139 # Defaulted attributes
140 for (my $i = $defaulted; $i < @order; $i++)
141 {
142 my $a = $order[$i];
143 die "Defaulted attributes not supported!";
144 # Because I don't know what to do with them.
145 my $att = $doc->createAttribute ($elem, $attr->{$a}->{LocalName}, 0);
146 $node->setAttributeNode ($att);
147 }
148 }
149 else
150 {
151 # We're assuming that all attributes were specified (1)
152 for my $a (keys %$attr)
153 {
154 my $att = $doc->createAttribute ($attr->{$a}->{LocalName},
155 $attr->{$a}->{Value}, 1);
156 $node->setAttributeNode ($att);
157 }
158 }
159 } #start_element()
160  
161 sub end_element
162 {
163 my $self = shift;
164 $self->{Element} = $self->{Element}->getParentNode;
165 undef $self->{LastText};
166  
167 # Check for end of root element
168 $self->{EndDoc} = 1 if ($self->{Element} == $self->{Document});
169 } #end_element
170  
171 sub entity_reference # was Default
172 {
173 my $self = $_[0];
174 my $name = $_[1]->{Name};
175  
176 $self->{Element}->appendChild (
177 $self->{Document}->createEntityReference ($name));
178 undef $self->{LastText};
179 } #entity_reference()
180  
181 sub start_cdata
182 {
183 my $self = shift;
184 $self->{InCDATA} = 1;
185 } #start_cdata()
186  
187 sub end_cdata
188 {
189 my $self = shift;
190 $self->{InCDATA} = 0;
191 } #end_cdata()
192  
193 sub comment
194 {
195 my $self = $_[0];
196  
197 local $XML::DOM::IgnoreReadOnly = 1;
198  
199 undef $self->{LastText};
200 my $comment = $self->{Document}->createComment ($_[1]->{Data});
201 $self->{Element}->appendChild ($comment);
202 } #comment()
203  
204 sub doctype_decl
205 {
206 my ($self, $hash) = @_;
207  
208 $self->{DocType}->setParams ($hash->{Name}, $hash->{SystemId},
209 $hash->{PublicId}, $hash->{Internal});
210 $self->{SawDocType} = 1;
211 } #doctype_decl()
212  
213 sub attlist_decl
214 {
215 my ($self, $hash) = @_;
216  
217 local $XML::DOM::IgnoreReadOnly = 1;
218  
219 $self->{DocType}->addAttDef ($hash->{ElementName},
220 $hash->{AttributeName},
221 $hash->{Type},
222 $hash->{Default},
223 $hash->{Fixed});
224 } #attlist_decl()
225  
226 sub xml_decl
227 {
228 my ($self, $hash) = @_;
229  
230 local $XML::DOM::IgnoreReadOnly = 1;
231  
232 undef $self->{LastText};
233 $self->{Document}->setXMLDecl (new XML::DOM::XMLDecl ($self->{Document},
234 $hash->{Version},
235 $hash->{Encoding},
236 $hash->{Standalone}));
237 } #xml_decl()
238  
239 sub entity_decl
240 {
241 my ($self, $hash) = @_;
242  
243 local $XML::DOM::IgnoreReadOnly = 1;
244  
245 # Parameter Entities names are passed starting with '%'
246 my $parameter = 0;
247  
248 #?? parameter entities currently not supported by PerlSAX!
249  
250 undef $self->{LastText};
251 $self->{DocType}->addEntity ($parameter, $hash->{Name}, $hash->{Value},
252 $hash->{SystemId}, $hash->{PublicId},
253 $hash->{Notation});
254 } #entity_decl()
255  
256 # Unparsed is called when it encounters e.g:
257 #
258 # <!ENTITY logo SYSTEM "http://server/logo.gif" NDATA gif>
259 #
260 sub unparsed_decl
261 {
262 my ($self, $hash) = @_;
263  
264 local $XML::DOM::IgnoreReadOnly = 1;
265  
266 # same as regular ENTITY, as far as DOM is concerned
267 $self->entity_decl ($hash);
268 } #unparsed_decl()
269  
270 sub element_decl
271 {
272 my ($self, $hash) = @_;
273  
274 local $XML::DOM::IgnoreReadOnly = 1;
275  
276 undef $self->{LastText};
277 $self->{DocType}->addElementDecl ($hash->{Name}, $hash->{Model});
278 } #element_decl()
279  
280 sub notation_decl
281 {
282 my ($self, $hash) = @_;
283  
284 local $XML::DOM::IgnoreReadOnly = 1;
285  
286 undef $self->{LastText};
287 $self->{DocType}->addNotation ($hash->{Name}, $hash->{Base},
288 $hash->{SystemId}, $hash->{PublicId});
289 } #notation_decl()
290  
291 sub processing_instruction
292 {
293 my ($self, $hash) = @_;
294  
295 local $XML::DOM::IgnoreReadOnly = 1;
296  
297 undef $self->{LastText};
298 $self->{Element}->appendChild (new XML::DOM::ProcessingInstruction
299 ($self->{Document}, $hash->{Target}, $hash->{Data}));
300 } #processing_instruction()
301  
302 return 1;
303  
304 __END__