aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/share/perl/lib/XML/Serializer.pm
diff options
context:
space:
mode:
Diffstat (limited to 'share/perl/lib/XML/Serializer.pm')
-rw-r--r--share/perl/lib/XML/Serializer.pm163
1 files changed, 163 insertions, 0 deletions
diff --git a/share/perl/lib/XML/Serializer.pm b/share/perl/lib/XML/Serializer.pm
new file mode 100644
index 0000000..6e64f17
--- /dev/null
+++ b/share/perl/lib/XML/Serializer.pm
@@ -0,0 +1,163 @@
1package XML::Serializer;
2
3use strict;
4
5my $root_element = "root";
6my $indent = " ";
7#my $XML_HEADER = << "XMLHEADER";
8#<?xml version="1.0" encoding="__CHARSET__"?>
9#<?xml-stylesheet type="text/xsl" href="__XSLT__" ?>
10#XMLHEADER
11my $XML_HEADER = << "XMLHEADER";
12<?xml version="1.0" encoding="__CHARSET__"?>
13XMLHEADER
14
15sub WITH_HEADER {
16 return 1;
17}
18
19sub new {
20 my ($this, $data, $root_name, $xslt) = @_;
21 my %fields = (
22 _charset => "utf-8",
23 _data => "",
24 _output => "",
25 _root_name => $root_name ? $root_name : "root",
26 _xslt => $xslt ? $xslt : ""
27 );
28 if (defined $data) {
29 $fields{_data} = $data;
30 }
31 return bless \%fields, $this;
32}
33
34sub set_root_name {
35 my ($this, $root_name) = @_;
36 $this->{_root_name} = $root_name;
37}
38
39sub set_data {
40 my ($this, $data) = @_;
41 $this->{_data} = $data;
42}
43
44sub set_charset {
45 my ($this, $charset) = @_;
46 $this->{_charset} = $charset;
47}
48
49sub set_xslt {
50 my ($this, $xslt) = @_;
51 $this->{_xslt} = $xslt;
52}
53
54sub to_string{
55 my ($this, $header) = @_;
56 if ($header) {
57 $this->{_output} = &_make_xml_header($this->{_charset}, $this->{_xslt});
58 }
59 $this->{_output} .= &_to_string($this->{_data}, $this->{_root_name});
60}
61
62sub to_formatted{
63 my ($this, $header) = @_;
64 if ($header) {
65 $this->{_output} = &_make_xml_header($this->{_charset}, $this->{_xslt});
66 }
67 $this->{_output} .= &_to_formatted($this->{_root_name}, $this->{_data});
68}
69
70sub _make_xml_header {
71 my $header = $XML_HEADER;
72 $header =~ s/__CHARSET__/$_[0]/;
73 $header =~ s/__XSLT__/$_[1]/;
74 return $header;
75}
76
77sub _to_string {
78 my ($obj, $name) = @_;
79 my $output = "";
80
81 if (ref($obj) eq "HASH") {
82 my $attr_list = "";
83 my $tmp_mid = "";
84 foreach (sort keys %$obj) {
85 if ($_ =~ /^@/) {
86 $attr_list = &_to_string($_, $obj->{$_});
87 }
88 $tmp_mid .= &_to_string($_, $obj->{$_});
89 }
90 $output = &_start_node($name, $attr_list) . $tmp_mid . &_end_node($name);
91 }
92 elsif (ref($obj) eq "ARRAY") {
93 foreach (@$obj) {
94 $output .= &_to_string($_, $name);
95 }
96 }
97 else {
98 if ($_ =~ /^@(.+)$/) {
99 return "$1=\"$obj\" ";
100 } else {
101 $output = &_start_node($name) . $obj . &_end_node($name);
102 }
103 }
104 return $output;
105}
106
107sub _to_formatted {
108 my ($name, $obj, $depth) = @_;
109# if (!$obj) { $obj = ""; }
110 if (!defined($depth)) { $depth = 0; }
111 my $output = "";
112 if (ref($obj) eq "HASH") {
113 my $attr_list = "";
114 my $tmp_mid = "";
115 foreach (sort keys %$obj) {
116 if ($_ =~ /^@/) {
117 $attr_list = &_to_string($_, $obj->{$_});
118 }
119 $tmp_mid .= &_to_formatted($_, $obj->{$_}, $depth+1);
120 }
121 $output = &_start_node($name, $attr_list, $depth) . "\n" . $tmp_mid . &_end_node($name, $depth);
122 }
123 elsif (ref($obj) eq "ARRAY") {
124 foreach (@$obj) {
125 $output .= &_to_formatted($name, $_, $depth);
126 }
127 }
128 else {
129 if ($_ =~ /^@(.+)$/) {
130 #return "$1=\"$obj\" ";
131 } else {
132 $output .= &_start_node($name, "", $depth);
133 $output .= $obj;
134 $output .= &_end_node($name);
135 }
136 }
137 return $output;
138}
139
140sub _start_node {
141 my $ret = "";
142 if (defined $_[2]) {
143 for(1..$_[2]) { $ret .= $indent; }
144 }
145 my $tag = $_[0] ? $_[0] : "";
146 my $attr = $_[1] ? $_[1] : "";
147 $ret .= "<$tag $attr>";
148 return $ret;
149}
150
151sub _end_node {
152 my $ret = "";
153 if (defined $_[1]) {
154 for(1..$_[1]) { $ret .= $indent; }
155 }
156 if (defined $_[0]) {
157 $ret .= "</$_[0]>\n";
158 }
159 return $ret;
160}
161
1621;
163