blib/lib/XML/GenericJSON.pm

filter: Statementstmt 25.5% 22 / 86 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 38.4% 5 / 13 Podpod 100% 8 / 8 totaltotal 24.3% 35 / 144 CC 20 SCAR 32.8
CC
20
Cov
18%
dom2perlish
72.0
list_children
30.0
string2file
12.0
CRAP 26.6
↑ Summary
1 package XML::GenericJSON;
2 2 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 2 @ISA = ('Exporter');
5 2 @EXPORT = qw();
6 2 our $VERSION = 0.05;
7  
8 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 =head1 NAME
15  
16 XML::GenericJSON - for turning XML into JSON, preserving as much XMLness as possible.
17  
18 =head1 SYNOPSIS
19  
20 my $json_string = XML::GenericJSON::string2string($xml_string);
21  
22 my $json_string = XML::GenericJSON::file2string($xml_filename);
23  
24 XML::GenericJSON::string2file($xml_string,$json_filename);
25  
26 XML::GenericJSON::file2file($xml_filename,$json_filename);
27  
28 =head1 DESCRIPTION
29  
30 XML::GenericJSON provides functions for turning XML into JSON. It uses LibXML to parse
31 the XML and JSON::XS to turn a perlish data structure into JSON. The perlish data structure
32 preserves as much XML information as possible. (In other words, an application-specific JSON filter
33 would almost certainly produce more compact JSON.)
34  
35 The module was initially developed as part of the Xcruciate project (F<http://www.xcruciate.co.uk>)
36 to produce JSON output via the Xteriorize webserver. It turns the entire XML document into a DOM tree,
37 which may not be what you want to do if your XML document is 3 buzillion lines long.
38  
39 =head1 AUTHOR
40  
41 Mark Howe, E<lt>melonman@cpan.orgE<gt>
42  
43 =head2 EXPORT
44  
45 None
46  
47 =head1 BUGS
48  
49 The best way to report bugs is via the Xcruciate bugzilla site (F<http://www.xcruciate.co.uk/bugzilla>).
50  
51 =head1 PREVIOUS VERSIONS
52  
53 =cut
54  
55 2 my @types=(0,
56 'element', #1
57 'attribute', #2
58 'text', #3
59 'cdata', #4
60 'entity_ref', #5
61 'entity_node', #6
62 'pi', #7
63 'comment', #8
64 'document', #9
65 'document_type', #10
66 'document_frag', #11
67 'notation', #12
68 'html_document', #13
69 'dtd', #14
70 'element_decl', #15
71 'attribute_decl', #16
72 'entity_decl', #17
73 'namespace_decl', #18
74 'xinclude_start', #19
75 'xinclude_end', #20
76 'docb_document' #21
77 );
78  
79 2 my $simple_types = {4=>1,
80 7=>1,
81 8=>1};
82  
83 2 my $xml_parser = new XML::LibXML;
84  
85 =head2 string2string(xml_string [,preserve_whitespace])
86  
87 Returns a JSON representation of an XML string. The second argument should be false if you want to preserve non-semantic whitespace.
88  
89 =cut
90  
91 0 ▶sub string2string {
Subroutine string2string: not called CC=2 SCAR=17.9
Pod documented
92 0 my $xml_string = shift;
93 0 my $strip_whitespace = 1;
94 0 ▶ $strip_whitespace = shift if defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0] 0 0 0
95 0 my $dom = $xml_parser->parse_string($xml_string);
96 0 return (encode_json dom2perlish($dom->getDocumentElement,$strip_whitespace));
97 }
98  
99 =head2 file2string(xml_filename [,preserve_whitespace])
100  
101 Returns a JSON representation of an XML file. The second argument should be false if you want to preserve non-semantic whitespace.
102  
103 =cut
104  
105 0 ▶sub file2string {
Subroutine file2string: not called CC=2 SCAR=17.9
Pod documented
106 0 my $xml_filename = shift;
107 0 my $strip_whitespace = 1;
108 0 ▶ $strip_whitespace = shift if defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0] 0 0 0
109 0 my $dom = $xml_parser->parse_file($xml_filename);
110 0 return (encode_json dom2perlish($dom->getDocumentElement,$strip_whitespace));
111 }
112  
113 =head2 string2file(xml_string, json_filename [,preserve_whitespace])
114  
115 Writes a JSON file based on an XML string. The third argument should be false if you want to preserve non-semantic whitespace.
116  
117 =cut
118  
119 0 ▶sub string2file {
Subroutine string2file: not called CC=3 SCAR=24.8
Pod documented
120 0 my $xml_string = shift;
121 0 my $json_filename = shift;
122 0 my $strip_whitespace = 1;
123 0 ▶ $strip_whitespace = shift if defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0] 0 0 0
124 0 my $dom = $xml_parser->parse_string($xml_string);
125 0 my $json = (encode_json dom2perlish($dom->getDocumentElement,$strip_whitespace));
126 0 ▶ open(OUT,">$json_filename") or die "Could not write JSON to '$json_filename' :$!";
Branch: 1 branch
BranchTrueFalseTotal
open OUT, ">$json_filename" or die "Could not write JSON to '${json_filename}' :$!" 0 0 0
127 0 print OUT $json;
128 0 close OUT;
129 }
130  
131 =head2 file2file(xml_filename, json_filename [,preserve_whitespace])
132  
133 Writes a JSON file based on an XML file. The third argument should be false if you want to preserve non-semantic whitespace.
134  
135 =cut
136  
137 0 ▶sub file2file {
Subroutine file2file: not called CC=3 SCAR=24.8
Pod documented
138 0 my $xml_filename = shift;
139 0 my $json_filename = shift;
140 0 my $strip_whitespace = 1;
141 0 ▶ $strip_whitespace = shift if defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0] 0 0 0
142 0 my $dom = $xml_parser->parse_file($xml_filename);
143 0 my $json = (encode_json dom2perlish($dom->getDocumentElement,$strip_whitespace));
144 0 ▶ open(OUT,">$json_filename") or die "Could not write JSON to '$json_filename' :$!";
Branch: 1 branch
BranchTrueFalseTotal
open OUT, ">$json_filename" or die "Could not write JSON to '${json_filename}' :$!" 0 0 0
145 0 print OUT $json;
146 0 close OUT;
147 }
148  
149 =head2 dom2perlish(node)
150  
151 The function that does the work of turning XML into a perlish data structure suitable for treatment by JSON::XS.
152  
153 =cut
154  
155 0 ▶sub dom2perlish {
Subroutine dom2perlish: not called CC=8 SCAR=42.8
Pod documented
156 0 my $xml_node = shift;
157 0 my $strip_whitespace = 1;
158 0 ▶ $strip_whitespace = shift if defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0] 0 0 0
159 0 my $perlish_node = {};
160 0 ▶ if ($xml_node->nodeType == 3) {#text - just store the scalar
Branch: 2 branches
BranchTrueFalseTotal
if ($xml_node->nodeType == 3) { } 0 0 0
elsif (defined $$simple_types{$xml_node->nodeType}) { } 0 0 0
161 0 return $xml_node->data;
162 }elsif (defined $simple_types->{$xml_node->nodeType}) {#cdata,pi,comment - store type plus data
163 0 $perlish_node->{type} = $types[$xml_node->nodeType];
164 0 $perlish_node->{data} = $xml_node->nodeValue;
165 0 return $perlish_node;
166 } else {#Probably an element, but it should work regardless
167 0 $perlish_node->{type} = $types[$xml_node->nodeType];
168 0 ▶ $perlish_node->{namespaces} = list_namespaces($xml_node) if $xml_node->getNamespaces;
Branch: 1 branch
BranchTrueFalseTotal
if $xml_node->getNamespaces 0 0 0
169 0 ▶ $perlish_node->{prefix} = $xml_node->prefix if $xml_node->prefix;
Branch: 1 branch
BranchTrueFalseTotal
if $xml_node->prefix 0 0 0
170 0 $perlish_node->{name} = $xml_node->localname;
171 0 ▶ $perlish_node->{attributes} = hash_attributes($xml_node) if $xml_node->hasAttributes;
Branch: 1 branch
BranchTrueFalseTotal
if $xml_node->hasAttributes 0 0 0
172 0 ▶ $perlish_node->{children} = list_children($xml_node,$strip_whitespace) if $xml_node->hasChildNodes;
Branch: 1 branch
BranchTrueFalseTotal
if $xml_node->hasChildNodes 0 0 0
173 0 return $perlish_node
174 }
175 }
176  
177 =head2 hash_attributes(node)
178  
179 Makes a hash of attributes.
180  
181 =cut
182  
183 0 ▶sub hash_attributes {
Subroutine hash_attributes: not called CC=2 SCAR=17.9
Pod documented
184 0 my $xml_node = shift;
185 0 my $hash = {};
186 0 foreach ($xml_node->attributes) {
187 0 $hash->{$_->name}=$_->value}
188 0 return $hash;
189 }
190  
191 =head2 list_namespaces($node)
192  
193 Makes a list of namespaces.
194  
195 =cut
196  
197 0 ▶sub list_namespaces {
Subroutine list_namespaces: not called CC=2 SCAR=17.9
Pod documented
198 0 my $xml_node = shift;
199 0 my @namespaces_list = ();
200 0 foreach ($xml_node->namespaces) {
201 0 my $namespace_hash={};
202 0 $namespace_hash->{prefix} = $_->getLocalName;
203 0 $namespace_hash->{uri} = $_->getData;
204 0 push @namespaces_list,$namespace_hash;
205 }
206 0 return [@namespaces_list];
207 }
208  
209 =head2 list_children(node)
210  
211 Makes a list of child nodes.
212  
213 =cut
214  
215 0 ▶sub list_children {
Subroutine list_children: not called CC=5 SCAR=34.0
Pod documented
216 0 my $xml_node = shift;
217 0 my $strip_whitespace = shift;
218 0 my @children = ();
219 0 foreach ($xml_node->childNodes) {
220 0 ▶ next if $strip_whitespace and ($_->nodeType == 3) and ($_->textContent=~/^\s*$/s);
Branch: 1 branch
BranchTrueFalseTotal
if $strip_whitespace and $_->nodeType == 3 and $_->textContent =~ /^\s*$/s 0 0 0
Condition0%0/6
$strip_whitespace and $_->nodeType == 3 and $_->textContent =~ /^\s*$/s
!ll&&!rl&&r
000
$strip_whitespace and $_->nodeType == 3
!ll&&!rl&&r
000
MC/DC0%0/3
$strip_whitespace and $_->nodeType == 3 and $_->textContent =~ /^\s*$/s
$strip_whitespace $_->nodeType == 3 $_->textContent =~ /^\s*$/s
Truth table0%0/4
A and B and C
A= $strip_whitespaceB= $_->nodeType == 3C= $_->textContent =~ /^\s*$/s
ABCresult
0XX0
10X0
1100
1111
221 0 push @children,dom2perlish($_,$strip_whitespace);
222 }
223 0 return [@children];
224 }
225  
226 =head1 PREVIOUS VERSIONS
227  
228 =over
229  
230 B<0.01>: First upload
231  
232 B<0.02>: Get dependencies right
233  
234 B<0.03>: Get path to abstract right
235  
236 B<0.04>: ported to use Module::Build
237  
238 B<0.05>: fixed unit test
239  
240 =back
241  
242 =head1 COPYRIGHT AND LICENSE
243  
244 Copyright (C) 2009 by SARL Cyberporte/Menteith Consulting
245  
246 This library is distributed under BSD licence (F<http://www.xcruciate.co.uk/licence-code>).
247  
248 =cut
249  
250 2 1;
↑ Summary