| 1 | #! perl | ||||||||||||
| 2 | |||||||||||||
| 3 | package Text::FakeXML; | ||||||||||||
| 4 | |||||||||||||
| 5 | 10 | ▶ | use 5.008; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 10 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 10 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 10 | ▶ | use Carp; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | |||||||||||||
| 10 | =head1 NAME | ||||||||||||
| 11 | |||||||||||||
| 12 | Text::FakeXML - Creating text with E<lt>thingsE<gt>. | ||||||||||||
| 13 | |||||||||||||
| 14 | =head1 VERSION | ||||||||||||
| 15 | |||||||||||||
| 16 | Version 0.02 | ||||||||||||
| 17 | |||||||||||||
| 18 | =cut | ||||||||||||
| 19 | |||||||||||||
| 20 | 10 | our $VERSION = '0.02'; | |||||||||||
| 21 | |||||||||||||
| 22 | |||||||||||||
| 23 | =head1 SYNOPSIS | ||||||||||||
| 24 | |||||||||||||
| 25 | Many applications use XML-style data, e.g., for configuration. | ||||||||||||
| 26 | However, very often this data is not 'real' XML, but just text with | ||||||||||||
| 27 | some XML-like markups. Therefore is it not necessary to pull in the | ||||||||||||
| 28 | whole vast XML machinery to create these files. A simple 'fake' module | ||||||||||||
| 29 | is sufficient. | ||||||||||||
| 30 | |||||||||||||
| 31 | For example, consider this real-life config file for eye-of-gnome: | ||||||||||||
| 32 | |||||||||||||
| 33 | <?xml version='1.0'?> | ||||||||||||
| 34 | <gconf> | ||||||||||||
| 35 | <entry name='geometry_collection' mtime='1164190071' type='string'> | ||||||||||||
| 36 | <stringvalue>440x350+1063+144</stringvalue> | ||||||||||||
| 37 | </entry> | ||||||||||||
| 38 | </gconf> | ||||||||||||
| 39 | |||||||||||||
| 40 | This doesn't require anything fancy: | ||||||||||||
| 41 | |||||||||||||
| 42 | use Text::FakeXML; | ||||||||||||
| 43 | my $cfg = Text::FakeXML->new(version => "1.0"); | ||||||||||||
| 44 | $cfg->xml_elt_open("gconf"); | ||||||||||||
| 45 | $cfg->xml_elt("entry", name => "geometry_collection", | ||||||||||||
| 46 | mtime => "1164190071", type => "string"); | ||||||||||||
| 47 | $cfg->xml_elt("stringvalue", "440x350+1063+144" ); | ||||||||||||
| 48 | $cfg->xml_elt_close("gconf"); | ||||||||||||
| 49 | |||||||||||||
| 50 | =head1 METHODS | ||||||||||||
| 51 | |||||||||||||
| 52 | =head2 new | ||||||||||||
| 53 | |||||||||||||
| 54 | Constructor. Takes an optional series of key/value pairs: | ||||||||||||
| 55 | |||||||||||||
| 56 | =over 4 | ||||||||||||
| 57 | |||||||||||||
| 58 | =item fh | ||||||||||||
| 59 | |||||||||||||
| 60 | The file handle where to write the output to. If not specified, the | ||||||||||||
| 61 | currently selected file handle is used. | ||||||||||||
| 62 | |||||||||||||
| 63 | =item version | ||||||||||||
| 64 | |||||||||||||
| 65 | If specified, a leading C<< <?xml version=...?> >> is emitted. | ||||||||||||
| 66 | |||||||||||||
| 67 | =item indent | ||||||||||||
| 68 | |||||||||||||
| 69 | Indentation for each level of tags. Must be a string (e.g., two spaces | ||||||||||||
| 70 | C<< " " >>) or a number that indicates the desired number of spaces. | ||||||||||||
| 71 | Default is two spaces. | ||||||||||||
| 72 | |||||||||||||
| 73 | =item level | ||||||||||||
| 74 | |||||||||||||
| 75 | The starting level of indentation. Defaults to zero. | ||||||||||||
| 76 | |||||||||||||
| 77 | =back | ||||||||||||
| 78 | |||||||||||||
| 79 | Example: | ||||||||||||
| 80 | |||||||||||||
| 81 | my $o = Text::FakeXML::new version => '1.0'; | ||||||||||||
| 82 | |||||||||||||
| 83 | =cut | ||||||||||||
| 84 | |||||||||||||
| 85 | 10 | ▶ | sub new { | ||||||||||
|
Subroutine
new: called CC=11 SCAR=24.9
Pod
documented
| |||||||||||||
| 86 | 10 | my ($pkg, %args) = @_; | |||||||||||
| 87 | 10 | my $self = bless | |||||||||||
| 88 | { | ||||||||||||
| 89 | _level => 0, | ||||||||||||
| 90 | _indent => " ", | ||||||||||||
| 91 | _fh => select, | ||||||||||||
| 92 | }, $pkg; | ||||||||||||
| 93 | |||||||||||||
| 94 | 10 | my $version; | |||||||||||
| 95 | my $encoding; | ||||||||||||
| 96 | 10 | ▶ | if ( exists $args{fh} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 97 | 10 | $self->{_fh} = delete $args{fh}; | |||||||||||
| 98 | } | ||||||||||||
| 99 | 10 | ▶ | if ( exists $args{version} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 100 | 1 | $version = delete $args{version}; | |||||||||||
| 101 | } | ||||||||||||
| 102 | 10 | ▶ | if ( exists $args{encoding} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 103 | 0 | $encoding = delete $args{encoding}; | |||||||||||
| 104 | 0 | ▶ | $version ||= '1.0'; | ||||||||||
|
Condition0%0/2
$version ||= "1.0"
MC/DC0%0/1
$version ||= "1.0"
$version
Truth table0%0/2
A
A= $version
| |||||||||||||
| 105 | } | ||||||||||||
| 106 | 10 | ▶ | if ( exists $args{indent} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 107 | 2 | $self->{_indent} = delete $args{indent}; | |||||||||||
| 108 | $self->{_indent} = " " x $self->{_indent} | ||||||||||||
| 109 | 2 | ▶ | if $self->{_indent} =~ /^\d+$/; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 110 | } | ||||||||||||
| 111 | 10 | ▶ | if ( exists $args{level} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 112 | 3 | $self->{_level} = delete $args{level}; | |||||||||||
| 113 | } | ||||||||||||
| 114 | |||||||||||||
| 115 | 10 | ▶ | croak(__PACKAGE__, ": Unhandled constructor attributes: ", | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 116 | join(" ", sort keys %args)) | ||||||||||||
| 117 | if %args; | ||||||||||||
| 118 | |||||||||||||
| 119 | 10 | ▶ | if ( $version ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 120 | 1 | ▶ | $self->print("<?xml version='$version'", | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 121 | $encoding ? " encoding='$encoding'" : "", | ||||||||||||
| 122 | "?>\n"); | ||||||||||||
| 123 | } | ||||||||||||
| 124 | |||||||||||||
| 125 | 10 | $self; | |||||||||||
| 126 | } | ||||||||||||
| 127 | |||||||||||||
| 128 | 22 | ▶ | sub indent { $_[0]->{_indent} x $_[0]->{_level} } | ||||||||||
|
Subroutine
indent: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 129 | 7 | ▶ | sub indent_incr { $_[0]->{_level}++ } | ||||||||||
|
Subroutine
indent_incr: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 130 | 6 | ▶ | sub indent_decr { $_[0]->{_level}-- } | ||||||||||
|
Subroutine
indent_decr: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 131 | 0 | ▶ | sub indent_init { $_[0]->{_level} = 0 } | ||||||||||
|
Subroutine
indent_init: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 132 | |||||||||||||
| 133 | 23 | ▶ | sub print { | ||||||||||
|
Subroutine
print: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 134 | 23 | my ($self, @args) = @_; | |||||||||||
| 135 | 23 | my $fh = select($self->{_fh}); | |||||||||||
| 136 | 23 | print(@args); | |||||||||||
| 137 | 23 | select($fh); | |||||||||||
| 138 | } | ||||||||||||
| 139 | |||||||||||||
| 140 | 22 | ▶ | sub printi { | ||||||||||
|
Subroutine
printi: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 141 | 22 | my ($self, @args) = @_; | |||||||||||
| 142 | 22 | $self->print($self->indent, @args); | |||||||||||
| 143 | } | ||||||||||||
| 144 | |||||||||||||
| 145 | =head2 xml_elt_open | ||||||||||||
| 146 | |||||||||||||
| 147 | Emits the opening tag for a new element. | ||||||||||||
| 148 | First argument is the name of the element. It may | ||||||||||||
| 149 | be followed by a series of key/value pairs that will be used as | ||||||||||||
| 150 | attributes for this element. | ||||||||||||
| 151 | |||||||||||||
| 152 | =cut | ||||||||||||
| 153 | |||||||||||||
| 154 | 7 | ▶ | sub xml_elt_open { | ||||||||||
|
Subroutine
xml_elt_open: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 155 | 7 | my ($self, $tag, @atts) = @_; | |||||||||||
| 156 | 7 | ▶ | croak("xml_elt_open: odd number of attribute arguments") | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 157 | if @atts % 2; | ||||||||||||
| 158 | 7 | my $t = "<$tag"; | |||||||||||
| 159 | 7 | ▶ | while ( @atts ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 160 | 5 | $t .= " " . shift(@atts) . "=" . xml_quote(xml_text(shift(@atts))) . ""; | |||||||||||
| 161 | } | ||||||||||||
| 162 | 7 | $t .= ">"; | |||||||||||
| 163 | 7 | $self->printi("$t\n"); | |||||||||||
| 164 | 7 | $self->indent_incr; | |||||||||||
| 165 | 7 | unshift(@{$self->{elts}}, $tag); | |||||||||||
| 166 | } | ||||||||||||
| 167 | |||||||||||||
| 168 | =head2 xml_elt_close | ||||||||||||
| 169 | |||||||||||||
| 170 | Closes the current element. First (and only) argument is the name of | ||||||||||||
| 171 | the element. | ||||||||||||
| 172 | |||||||||||||
| 173 | =cut | ||||||||||||
| 174 | |||||||||||||
| 175 | 6 | ▶ | sub xml_elt_close { | ||||||||||
|
Subroutine
xml_elt_close: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||
| 176 | 6 | my ($self, $tag) = @_; | |||||||||||
| 177 | 6 | ▶ | if ( $tag eq $self->{elts}->[0] ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 178 | 6 | shift(@{$self->{elts}}); | |||||||||||
| 179 | } | ||||||||||||
| 180 | else { | ||||||||||||
| 181 | warn("XML ERROR: closing element $tag while in ", | ||||||||||||
| 182 | 0 | $self->{elts}->[0], "\n"); | |||||||||||
| 183 | } | ||||||||||||
| 184 | 6 | $self->indent_decr; | |||||||||||
| 185 | 6 | $self->printi("</$tag>\n"); | |||||||||||
| 186 | } | ||||||||||||
| 187 | |||||||||||||
| 188 | =head2 xml_elt | ||||||||||||
| 189 | |||||||||||||
| 190 | Outputs a simple element. First argument is the name of the element, | ||||||||||||
| 191 | the second argument (if present) is the value. This may be followed by | ||||||||||||
| 192 | a series of key/value pairs that will be used as attributes for this | ||||||||||||
| 193 | element. | ||||||||||||
| 194 | |||||||||||||
| 195 | $o->xml_elt("foo") -> <foo /> | ||||||||||||
| 196 | $o->xml_elt("foo", "bar") -> <foo>bar</foo> | ||||||||||||
| 197 | $o->xml_elt("foo", "bar", | ||||||||||||
| 198 | id => 1) -> <foo id='1'>bar</foo> | ||||||||||||
| 199 | $o->xml_elt("foo", undef, | ||||||||||||
| 200 | id => 1) -> <foo id='1' /> | ||||||||||||
| 201 | |||||||||||||
| 202 | =cut | ||||||||||||
| 203 | |||||||||||||
| 204 | 9 | ▶ | sub xml_elt { | ||||||||||
|
Subroutine
xml_elt: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||
| 205 | 9 | my ($self, $tag, $val, @atts) = @_; | |||||||||||
| 206 | 9 | ▶ | croak("xml_elt: odd number of attribute arguments") | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 207 | if @atts % 2; | ||||||||||||
| 208 | 9 | my $t = "<$tag"; | |||||||||||
| 209 | 9 | ▶ | while ( @atts ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 210 | 2 | $t .= " " . shift(@atts) . "=" . | |||||||||||
| 211 | xml_quote(xml_text(shift(@atts))) . ""; | ||||||||||||
| 212 | } | ||||||||||||
| 213 | 9 | ▶ | if ( defined $val ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 214 | 8 | $self->printi($t, ">", xml_text($val), "</$tag>\n"); | |||||||||||
| 215 | } | ||||||||||||
| 216 | else { | ||||||||||||
| 217 | 1 | $self->printi("$t />\n"); | |||||||||||
| 218 | } | ||||||||||||
| 219 | } | ||||||||||||
| 220 | |||||||||||||
| 221 | =head2 xml_comment | ||||||||||||
| 222 | |||||||||||||
| 223 | Outputs a comment. Arguments contain the comment text. | ||||||||||||
| 224 | |||||||||||||
| 225 | =cut | ||||||||||||
| 226 | |||||||||||||
| 227 | 0 | ▶ | sub xml_comment { | ||||||||||
|
Subroutine
xml_comment: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 228 | 0 | my ($self, @a) = @_; | |||||||||||
| 229 | 0 | $self->printi("<!-- ", xml_text("@a"), " -->\n"); | |||||||||||
| 230 | } | ||||||||||||
| 231 | |||||||||||||
| 232 | # XMLise text. | ||||||||||||
| 233 | 15 | ▶ | sub xml_text { | ||||||||||
|
Subroutine
xml_text: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||
| 234 | 15 | ▶ | return "" unless defined $_[0]; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 235 | 15 | for ( $_[0] ) { | |||||||||||
| 236 | 15 | s/&/&/g; | |||||||||||
| 237 | 15 | s/'/'/g; | |||||||||||
| 238 | 15 | s/</</g; | |||||||||||
| 239 | 15 | s/>/>/g; | |||||||||||
| 240 | 15 | return $_; | |||||||||||
| 241 | } | ||||||||||||
| 242 | } | ||||||||||||
| 243 | |||||||||||||
| 244 | 7 | ▶ | sub xml_quote { | ||||||||||
|
Subroutine
xml_quote: called CC=2 SCAR=8.1
Pod
undocumented
| |||||||||||||
| 245 | 7 | my $t = shift; | |||||||||||
| 246 | 7 | ▶ | return '"'.$t.'"' unless $t =~ /"/; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 247 | 0 | return "'".$t."'"; | |||||||||||
| 248 | } | ||||||||||||
| 249 | |||||||||||||
| 250 | =head1 AUTHOR | ||||||||||||
| 251 | |||||||||||||
| 252 | Johan Vromans, C<< <jv at cpan.org> >> | ||||||||||||
| 253 | |||||||||||||
| 254 | =head1 BUGS | ||||||||||||
| 255 | |||||||||||||
| 256 | Please report any bugs or feature requests to C<bug-text-fakexml at rt.cpan.org>, or through | ||||||||||||
| 257 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Text-FakeXML>. I will be notified, and then you'll | ||||||||||||
| 258 | automatically be notified of progress on your bug as I make changes. | ||||||||||||
| 259 | |||||||||||||
| 260 | =head1 SUPPORT | ||||||||||||
| 261 | |||||||||||||
| 262 | You can find documentation for this module with the perldoc command. | ||||||||||||
| 263 | |||||||||||||
| 264 | perldoc Text::FakeXML | ||||||||||||
| 265 | |||||||||||||
| 266 | You can also look for information at: | ||||||||||||
| 267 | |||||||||||||
| 268 | =over 4 | ||||||||||||
| 269 | |||||||||||||
| 270 | =item * RT: CPAN's request tracker | ||||||||||||
| 271 | |||||||||||||
| 272 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Text-FakeXML> | ||||||||||||
| 273 | |||||||||||||
| 274 | =item * CPAN Ratings | ||||||||||||
| 275 | |||||||||||||
| 276 | L<http://cpanratings.perl.org/d/Text-FakeXML> | ||||||||||||
| 277 | |||||||||||||
| 278 | =item * Search CPAN | ||||||||||||
| 279 | |||||||||||||
| 280 | L<http://search.cpan.org/dist/Text-FakeXML> | ||||||||||||
| 281 | |||||||||||||
| 282 | =back | ||||||||||||
| 283 | |||||||||||||
| 284 | =head1 COPYRIGHT & LICENSE | ||||||||||||
| 285 | |||||||||||||
| 286 | Copyright 2008 Johan Vromans, all rights reserved. | ||||||||||||
| 287 | |||||||||||||
| 288 | This program is free software; you can redistribute it and/or modify it | ||||||||||||
| 289 | under the same terms as Perl itself. | ||||||||||||
| 290 | |||||||||||||
| 291 | =cut | ||||||||||||
| 292 | |||||||||||||
| 293 | 10 | 1; | |||||||||||