WE_Content/XML.pm untested

filter: Statementstmt 0.0% 0 / 89 Branchbran 0.0% 0 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 138 CC 12 SCAR 27.5
CC
12
Cov
0%
parse
42.0
new
20.0
serialize
20.0
CRAP 15.7
1 # -*- perl -*-
2  
3 #
4 # $Id: XML.pm,v 1.7 2004/03/21 23:11:32 eserte Exp $
5 # Author: Slaven Rezic
6 #
7 # Copyright (C) 2003 Slaven Rezic.
8 # This is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License, see the file COPYING.
10  
11 #
12 # Mail: slaven@rezic.de
13 # WWW: http://we-framework.sourceforge.net
14 #
15  
16 package WE_Content::XML;
17 use base qw(WE_Content::Base);
18  
19 use strict;
20 use vars qw($VERSION);
21 $VERSION = sprintf("%d.%02d", q$Revision: 1.7 $ =~ /(\d+)\.(\d+)/);
22  
23 use XML::Dumper 0.71 (); # earlier versions were not reliable
24  
25 sub new {
26 my($class, %args) = @_;
27 my $self = {};
28 while(my($k,$v) = each %args) {
29 die "Option does not start with a dash: $k" if $k !~ /^-/;
30 $self->{ucfirst(substr($k,1))} = $v;
31 }
32 bless $self, $class;
33 if ($self->{File}) {
34 $self->parse(-file => $self->{File});
35 } elsif ($self->{String}) {
36 $self->parse(-string => $self->{String});
37 }
38 $self->_create_parser;
39 $self;
40 }
41  
42 sub _create_parser {
43 my $self = shift;
44 $self->{P} = XML::Dumper->new;
45 }
46  
47 sub parse {
48 my($self, %args) = @_;
49 my $buf = $self->get_string(%args);
50  
51 my $emptydata;
52 if (!$self->{P}) { $self->_create_parser }
53 my $outdata = eval { local $SIG{__DIE__}; $self->{P}->xml2pl($buf) };
54 if ($@) {
55 my $line = 1;
56 warn join("\n", map { sprintf("%3d: %s", $line++, $_) } split /\n/, $buf);
57 die $@;
58 }
59 if (!defined $outdata) {
60 die "Loading emptydata not yet supported...";
61 }
62  
63 if (defined $outdata) {
64 $self->{Object} = $outdata;
65 $self->{Type} = 'content';
66 } elsif (defined $emptydata) {
67 $self->{Object} = eval $emptydata; # XXX should use Safe!
68 $self->{Type} = 'template';
69 } else {
70 die "No data found!";
71 }
72  
73 $self->{Object};
74 }
75  
76 sub serialize {
77 my $self = shift;
78 if (!$self->{P}) { $self->_create_parser() }
79 my $xml = '<?xml version="1.0" encoding="utf-8"?>' . $self->{P}->pl2xml($self->{Object});
80 if ($self->_can_XML_LibXSLT) {
81 $xml = $self->_beautify_with_XML_LibXSLT($xml);
82 } elsif ($self->_can_XML_XSLT) {
83 $xml = $self->_beautify_with_XML_XSLT($xml);
84 }
85 $xml;
86 }
87  
88 sub ext { "xml" }
89  
90 sub _can_XML_XSLT {
91 my $self = shift;
92 warn "Can't use the identity idiom with XML::XSLT (yet)";
93 return 0;
94 eval { require XML::XSLT; 1 };
95 }
96  
97 sub _beautify_with_XML_XSLT {
98 my($self, $xml) = @_;
99 my $xslt = XML::XSLT->new($self->stylesheet);
100 $xslt->transform($xml);
101 $xml = $xslt->toString;
102 $xslt->dispose;
103 $xml;
104 }
105  
106 sub _can_XML_LibXSLT {
107 my $self = shift;
108 eval {
109 require XML::LibXML;
110 require XML::LibXSLT;
111 1;
112 };
113 }
114  
115 sub _beautify_with_XML_LibXSLT {
116 my($self, $xml) = @_;
117  
118 my $parser = XML::LibXML->new();
119 my $xslt = XML::LibXSLT->new();
120  
121 my $source = $parser->parse_string($xml);
122 my $style_doc = $parser->parse_string($self->stylesheet);
123  
124 my $stylesheet = $xslt->parse_stylesheet($style_doc);
125  
126 my $results = $stylesheet->transform($source);
127  
128 $stylesheet->output_string($results);
129 }
130  
131 sub stylesheet {
132 <<'EOF';
133 <?xml version="1.0" encoding="iso-8859-1"?>
134 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
135 <xsl:template match="@memory_address" />
136 <xsl:template match="@*|node()">
137 <xsl:copy>
138 <xsl:apply-templates select="@*|node()"/>
139 </xsl:copy>
140 </xsl:template>
141 </xsl:stylesheet>
142 EOF
143 }
144  
145 1;
146  
147 __END__