4 # The contents of this file are subject to the terms of the
5 # Common Development and Distribution License (the "License").
6 # You may not use this file except in compliance with the License.
8 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 # or http://www.opensolaris.org/os/licensing.
10 # See the License for the specific language governing permissions
11 # and limitations under the License.
13 # When distributing Covered Code, include this CDDL HEADER in each
14 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 # If applicable, add the following below this CDDL HEADER, with the
16 # fields enclosed by brackets "[]" replaced with your own identifying
17 # information: Portions Copyright [yyyy] [name of copyright owner]
22 # Copyright 2007 Sun Microsystems, Inc. All rights reserved.
23 # Use is subject to license terms.
25 # ident "%Z%%M% %I% %E% SMI"
28 # <t> xmlHandlers -- package for generating a tree from an XML doc
46 # pkg reference, object name (tag), optional fileName.
51 my $parent = shift; # ref to parent object
52 my $class = shift; # for debug use
54 my @kids = (); # list of child objects
56 push (@objStack, $parent);
57 $currentObj = bless {'class' => $class,
59 # 'parent' => $parent,
61 'content' => ''}, $pkg;
63 if (@_) { # if fileName passed, go!
64 die "parent for document creation must be null"
71 # we'll call you when your object is started
74 sub registerStartCallback
{
75 my $objName = shift; # call me when you get <objName>
76 my $callback = shift; # \&foo($objRef, $source);
78 if ($startCallback{$objName}) {
79 print STDERR
"duplicate callback for $objName\n";
82 $startCallback{$objName} = $callback;
86 # we'll call you when your object is completed
89 sub registerEndCallback
{
90 my $objName = shift; # call me when you get </objName>
91 my $callback = shift; # \&foo($objRef);
93 if ($endCallback{$objName}) {
94 print STDERR
"duplicate callback for $objName\n";
97 $endCallback{$objName} = $callback;
106 my ($obj, $class, $string) = @_;
115 push (@
{$parent->{'kids'}}, $kid);
116 # $kid->{'parent'} = $parent;
119 # <s> internal functions
123 # ErrorContext - 0 don't report errors
124 # - other = number of lines to display
125 # ParseparamEnt - 1 allow parsing of dtd
126 my $parser = XML
::Parser
->new(ErrorContext
=> 1,
129 $parser->setHandlers (Char
=> \
&charHandler
,
130 Start
=> \
&startHandler
,
131 Default
=> \
&defaultHandler
,
133 Proc
=> \
&procHandler
,
134 Comment
=> \
&commentHandler
,
135 ExternEnt
=> \
&externalHandler
);
137 $parser->parsefile ($file);
141 my ($xmlObj, $string) = @_;
146 unless ($string =~ /^\s*$/) {
147 # print "charHandler: $currentObj->{'class'} $string\n" if $main::debug;
148 $currentObj->{'content'} .= ' ' if ($currentObj->{'content'});
149 $currentObj->{'content'} .= $string;
153 # create new object and attach to tree
160 my $parent = $currentObj;
162 $obj = new xmlHandlers
($currentObj, $tag);
166 $obj->processAttributes ($tag, @_);
169 if ($functionRef = $startCallback{$tag}) {
170 &$functionRef($obj, 'start');
172 elsif ($main::debug
) {
173 # print "no start callback for $tag\n";
181 # print "end tag $element\n" if $main::debug;
184 if ($functionRef = $endCallback{$element}) {
185 &$functionRef($currentObj, 'end');
187 elsif ($main::debug
) {
188 # print "no end callback for $element\n";
190 # $currentObj = $currentObj->{'parent'};
191 $currentObj = pop (@objStack);
195 my ($obj, $string) = @_;
197 unless (!$main::debug
|| ($string =~ /^\s*$/)) {
198 if ($string =~ /<\?xml/) {
199 $string =~ s/<\?\S+\s+(.*)/$1/;
201 parseProcInstruction
($string);
202 print STDERR
"Got call to default, guessed what to do: $string\n";
205 print STDERR
"Got call to default, didn't know what to do: $string\n";
210 sub externalHandler
{
211 my ($obj, $base, $sysid, $pubid) = @_;
213 $base = '' if !$base;
214 $pubid = '' if !$pubid;
215 print "external: base $base\nexternal: sysid $sysid\nexternal: pubid $pubid\n";
219 my ($obj, $element) = @_;
221 return unless $main::debug
;
223 unless ($element =~ /^\s*$/) {
224 print "comment: $element\n";
234 parseProcInstruction
($data);
236 $currentObj->processAttributes ($target, $data, @_);
240 sub parseProcInstruction
{
243 my (@outputArray) = ();
245 while ($args =~ s/([^ =]+)=\"([^"]+)\"(.*)/$3/) { # "
246 push (@outputArray, $1);
247 push (@outputArray, $2);
249 return (@outputArray);
252 sub processAttributes
{
254 my ($element, %content) = @_;
256 # print "processAttributes: element = $element\n" if $main::debug;
259 foreach $attributeName (keys %content) {
260 if ($attributeName =~ /^\s*$/) {
261 delete $content{$attributeName}; # remove null entries
265 # print "attribute: $attributeName = $content{$attributeName}\n"
268 if ($hashCount && $pkg->{'attributes'}) {
269 print STDERR
"need to write attribute merge logic\n";
272 $pkg->{'attributes'} = \
%content;
278 my $whichKid = shift;
280 my @kids = $pkg->getKids();
282 foreach $kid (@kids) {
283 my $class = $kid->getClass();
284 return $kid if $class eq $whichKid;
292 return @
{$pkg->{'kids'}};
298 my $ref = $pkg->{'attributes'};
307 my $ref = $pkg->{'attributes'};
315 return $pkg->{'class'};
321 my $content = $pkg->{'content'};
322 return $content ?
$content : undef;