-
Notifications
You must be signed in to change notification settings - Fork 0
/
WriteXML.pm
executable file
·166 lines (106 loc) · 2.91 KB
/
WriteXML.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/perl -w
#
#
###############################################################
package WriteXML;
=pod
=head1 NAME
WriteXML
=head1 AUTHOR
Kelvin Li
=head1 DESCRIPTION
Function calls to generate XML
=cut
use Carp;
use strict;
###############################################################################
sub element{
=head2 str element(str name, hash attributes, int indent, str data);
This subroutine will generate an XML element.
<name> is the name of the element
<attributes> is a hash that contains the key/value of the attributes
<indent> is a boolean that tells whether to indent the contents of the element
<data> is the data that should go inside of the element.
If you are not sure if your data has & or <, then you should apply the
escape function to your string.
=cut
my $name=shift;
my $attributes=shift;
my $indent=shift;
my $data=shift;
my $xml;
# Start the element
$xml="<$name";
# Output the attributes
foreach my $key(sort keys %{$attributes}){
$xml .= " $key=\"${$attributes}{$key}\"";
}
if(!defined($data) || $data eq ""){
# If data is null, output the shortened element type
$xml.="/>\n"
}else{
if($indent){
# If using the indent option, indent all new lines
my $last_data_char=chop $data;
if($last_data_char ne "\n"){
$data.=$last_data_char;
}
$data=~s/\n/\n\t/g;
$data="\n\t".$data;
$xml.=">$data\n</$name>\n";
}else{
#If not using the indent option, just slap the end tag on
$xml.=">$data</$name>\n";
}
}
return $xml;
}
###############################################################################
sub version{
=head2 str version(str version, str encoding);
Returns a XML version string with the version/encoding information specified.
=cut
my $version=shift;
my $encoding=shift;
return "<?xml version=\"$version\" encoding=\"$encoding\"?>\n";
}
###############################################################################
sub comment{
=head2 str comment(str comment);
Returns a XML comment string with your comment inside
=cut
my $comment=shift;
return "<!--$comment-->\n";
}
###############################################################################
sub escape{
=head2 str escape(str data);
Returns the string you entered with the & and < escaped
with the & and <, respectively.
=cut
my $data=shift;
$data=~s/&/&/g;
$data=~s/</</g;
return($data);
}
###############################################################################
sub wrapLongStr{
=head2 str wrapLongStr(str data, int width);
Returns the input string with line returns.
Each line becomes limited to width specified.
Useful for printing out sequences.
=cut
my $data=shift;
my $width=shift;
my $out_data="";
my $length=length($data);
my $pos=0;
do{
my $out_width=($width>$length)?$length:$width;
$out_data.=substr($data, $pos, $width) . "\n";
$pos+=$width;
$length-=$width;
}while($length>0);
return $out_data;
}
1;