blib/lib/Glade/PerlXML.pm untested

filter: Statementstmt 0.0% 0 / 76 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 124 CC 10 SCAR 37.9
CC
10
Cov
0%
Proto_from_XML_Parser_Tree
90.0
Proto_from_XML
6.0
BEGIN
2.0
CRAP 44.1
1 package Glade::PerlXML;
2 require 5.000; use strict 'vars', 'refs', 'subs';
3  
4 # Copyright (c) 1999 Dermot Musgrove <dermot.musgrove@virgin.net>
5 #
6 # This library is released under the same conditions as Perl, that
7 # is, either of the following:
8 #
9 # a) the GNU General Public License as published by the Free
10 # Software Foundation; either version 1, or (at your option) any
11 # later version.
12 #
13 # b) the Artistic License.
14 #
15 # If you use this library in a commercial enterprise, you are invited,
16 # but not required, to pay what you feel is a reasonable fee to perl.org
17 # to ensure that useful software is available now and in the future.
18 #
19 # (visit http://www.perl.org/ or email donors@perlmongers.org for details)
20  
21 BEGIN {
22 use XML::Parser qw( ); # for new, parse, parsefile
23 use Glade::PerlRun qw(:METHODS :VARS);
24 # Uncomment the line below if you are using european characters
25 # NB you will also have to uncomment line 183 and comment out line 181
26 # use Unicode::String qw(utf8 latin1); # To read ISO-8859-1 chars
27 use vars qw(
28 @ISA
29 @EXPORT @EXPORT_OK %EXPORT_TAGS
30 $PACKAGE $VERSION $AUTHOR $DATE
31 );
32 # Tell interpreter who we are inheriting from
33 @ISA = qw( Glade::PerlRun );
34 $PACKAGE = __PACKAGE__;
35 $VERSION = q(0.61);
36 $AUTHOR = q(Dermot Musgrove <dermot.musgrove@virgin.net>);
37 $DATE = q(Sun Nov 17 03:21:11 GMT 2002);
38 }
39  
40 sub DESTROY {
41 # This sub will be called on object destruction
42 } # End of sub DESTROY
43  
44 #===============================================================================
45 #=========== Utilities to read XML and build the Proto ==========
46 #===============================================================================
47 sub Proto_from_File {
48 my ($class, $filename, $repeated, $special, $encoding) = @_;
49 my $me = "$class->Proto_from_File";
50 my $xml = $class->string_from_File($filename);
51 #print "$filename - $xml\n";
52 return $class->Proto_from_XML($xml, $repeated, $special, $encoding );
53 }
54  
55 sub Proto_from_XML {
56 my ($class, $xml, $repeated, $special, $encoding) = @_;
57 my $me = "$class->Proto_from_XML";
58 my $xml_encoding;
59 if ($xml =~ s/\<\?xml.*\s*encoding\=["'](.*?)['"]\?\>\n*//) {
60 $xml_encoding = $1
61 } else {
62 $xml_encoding = $encoding;
63 }
64 my $tree = new XML::Parser(
65 Style =>'Tree',
66 ProtocolEncoding => $xml_encoding,
67 ErrorContext => 2)->parse($xml );
68 my $proto = $class->Proto_from_XML_Parser_Tree(
69 $tree->[1], 0, $repeated, $special, $xml_encoding );
70 return $xml_encoding, $proto;
71 }
72  
73 sub Proto_from_XML_Parser_Tree {
74 my ($class, $self, $depth, $repeated, $special, $encoding) = @_;
75 my $me = "$class->Proto_from_XML_Parser_Tree";
76 # Tree[0] contains fileelement name
77 # Tree[1] contains fileelement contents
78
79 # Tree[1][n] contains element name
80 # Tree[1][n+1] contains element contents
81 # Tree[1][n+1][0] contains ref to hash of element attributes
82 # Tree[1][n+1][1] contains '0' ie next element is text
83 # Tree[1][n+1][2] contains text before subelement
84  
85 # Tree[1][n+1][3] contains subelement name
86 # Tree[1][n+1][4] contains subelement contents
87 # Tree[1][n+1][5] contains '0' ie next element is text
88 # Tree[1][n+1][6] contains text before subelement
89 # recursed
90
91 # Tree[3] cannot exist since the fileelement must enclose everything
92 # FIXME make this general for all encodings
93 if ($encoding && ($encoding eq 'ISO-8859-1')) {
94 eval "use Unicode::String qw(utf8 latin1)";
95 undef $encoding if $@; # We can't use encodings correctly
96 } else {
97 undef $encoding; # We don't recognise the encodings name
98 }
99 my ($tk, $i, $ilimit );
100 my ($count, $np, $key, $work );
101 my $limit = scalar(@$self);
102 my $child;
103 $key = 0;
104 for ($count = 3; $count < $limit; $count += 4) {
105 $key++;
106 $ilimit = scalar @{$self->[$count+1]};
107 if (" $repeated " =~ / $self->[$count] /) {
108 # this is a repeated container so use a sequence no to preserve order
109 if ($ilimit <= 3) {
110 # $class->diag_print(4, "Found a scalar called '".
111 # "$self->[$count]' which contains '$self->[$count+1][2]'".
112 # " in a repeated container type element !" );
113 $np->{$self->[$count]} = ($self->[$count+1][2]);
114  
115 } else {
116 # call ourself to expand nested xml but use sequence no
117 $work = $class->Proto_from_XML_Parser_Tree($self->[$count + 1],
118 ++$depth, $repeated, $special, $encoding );
119 $work->{&typeKey} = $self->[$count];
120 # prefix with tilde to force to end (alphabetically)
121 $tk = "~$self->[$count]-".sprintf(&keyFormat, $key, $self->[$count] );
122 #print "$tk\n";
123 $np->{$tk} = $work;
124 }
125  
126 } elsif (" $special " =~ / $self->[$count] /) {
127 # this is a unique container definition (eg Glade <project>)
128 # so just expand and store it without a sequence no
129 $work = $class->Proto_from_XML_Parser_Tree($self->[$count + 1],
130 ++$depth, $repeated, $special, $encoding );
131 $work->{&typeKey} = $self->[$count];
132 $np->{$self->[$count]} = $work;
133  
134 } elsif ($ilimit > 3) {
135 # We have several (widget) attributes to store
136 $work = {};
137 for ($i = 3; $i < $ilimit; $i += 4) {
138 $work->{$self->[$count+1][$i]} = $self->[$count+1][$i+1][2];
139 }
140 $work->{&typeKey} = $work->{'class'} || $self->[$count];
141 # prefix with tilde to force to end (alphabetically)
142 $tk = "~$self->[$count]-".
143 sprintf(&keyFormat, $key, $self->[$count] );
144 $np->{$tk} = $work;
145  
146 } elsif ($ilimit == 1) {
147 # this is an empty (nul string) element
148 $np->{$self->[$count]} = '';
149  
150 } else {
151 # this is a simple element to add with
152 # key in $self->[$count] and val in $self->[$count+1][2]
153 # FIXME make this general for all encodings
154 if ($encoding && ($encoding eq 'ISO-8859-1')) {
155 # We use line below if you are using european characters
156 $np->{$self->[$count]} = &utf8($self->[$count+1][2])->latin1;
157 } else {
158 # We use the line below if you are NOT using european characters
159 $np->{$self->[$count]} = $self->[$count+1][2];
160 }
161 }
162 }
163 return $np;
164 }
165  
166 1;
167  
168 __END__