Subject: | user defined entity mapping and line break for output option |
This patch include for the feature of specification of entity mapping and
line break.
* Distribution name and version
XML-TreePP-0.41
* Perl version
This is perl, v5.10.1 (*) built for i486-linux-gnu-thread-multi
(with 53 registered patches, see perl -V for more detail)
* Operating System vendor and version
Linux www27376u 2.6.32-5-686 #1 SMP Fri Sep 9 20:51:05 UTC 2011 i686
GNU/Linux
Subject: | XML-TreePP-0.41a.patch |
diff -ur XML-TreePP-0.41/lib/XML/TreePP.pm XML-TreePP-0.41a/lib/XML/TreePP.pm
--- XML-TreePP-0.41/lib/XML/TreePP.pm 2010-10-31 19:47:03.000000000 +0900
+++ XML-TreePP-0.41a/lib/XML/TreePP.pm 2011-10-31 10:24:24.000000000 +0900
@@ -343,6 +343,12 @@
This doesn't strictly follow the XML specification but does looks nice.
+=head2 linebreak
+
+This option allows you to specify the output line break. Default value is "\n".
+
+ $tpp->set( linebreak => '' );
+
=head2 xml_decl
This module inserts an XML declaration on top of the XML document generated
@@ -350,6 +356,12 @@
$tpp->set( xml_decl => '' );
+=head2 escape_map
+
+This option allows you to specify a entity mapping from character to name or value
+
+ $tpp->set(escape_map => {"'" => '''});
+
=head2 output_encoding
This option allows you to specify a encoding of the XML document generated
@@ -502,6 +514,7 @@
my $from = $self->{internal_encoding} || $INTERNAL_ENCODING;
my $to = shift || $self->{output_encoding} || $XML_ENCODING;
my $decl = $self->{xml_decl};
+ my $linebreak = defined $self->{linebreak} ? $self->{linebreak} : "\n";
$decl = '<?xml version="1.0" encoding="' . $to . '" ?>' unless defined $decl;
local $self->{__first_out};
@@ -543,7 +556,7 @@
}
return $text if ( $decl eq '' );
- join( "\n", $decl, $text );
+ join( $linebreak, $decl, $text );
}
sub parsehttp {
@@ -976,6 +989,8 @@
my $out = [];
my $attr = [];
my $allkeys = [ keys %$hash ];
+ my $linebreak = defined $self->{linebreak} ? $self->{linebreak} : "\n";
+ my $escape_map = $self->{escape_map} if ref $self->{escape_map};
my $fo = $self->{__first_out} if ref $self->{__first_out};
my $lo = $self->{__last_out} if ref $self->{__last_out};
my $firstkeys = [ sort { $fo->{$a} <=> $fo->{$b} } grep { exists $fo->{$_} } @$allkeys ] if ref $fo;
@@ -1022,7 +1037,7 @@
foreach my $key ( @$attrkey ) {
my $name = substr( $key, $prelen );
- my $val = &xml_escape( $hash->{$key} );
+ my $val = &xml_escape( $hash->{$key}, $escape_map );
push( @$attr, ' ' . $name . '="' . $val . '"' );
}
}
@@ -1033,16 +1048,16 @@
if ( defined $self->{__indent} ) {
s/^(\s*<)/$self->{__indent}$1/mg foreach @$out;
}
- unshift( @$out, "\n" );
+ unshift( @$out, $linebreak );
}
my $text = join( '', @$out );
if ( defined $name ) {
if ( scalar @$out ) {
- $text = "<$name$jattr>$text</$name>\n";
+ $text = "<$name$jattr>$text</$name>$linebreak";
}
else {
- $text = "<$name$jattr />\n";
+ $text = "<$name$jattr />$linebreak";
}
}
$text;
@@ -1052,10 +1067,11 @@
my $self = shift;
my $name = shift;
my $array = shift;
+ my $linebreak = defined $self->{linebreak} ? $self->{linebreak} : "\n";
my $out = [];
foreach my $val (@$array) {
if ( !defined $val ) {
- push( @$out, "<$name />\n" );
+ push( @$out, "<$name />$linebreak" );
}
elsif ( UNIVERSAL::isa( $val, 'HASH' ) ) {
my $child = $self->hash_to_xml( $name, $val );
@@ -1085,10 +1101,11 @@
my $self = shift;
my $name = shift;
my $ref = shift;
+ my $linebreak = defined $self->{linebreak} ? $self->{linebreak} : "\n";
my $data = defined $$ref ? $$ref : '';
$data =~ s#(]])(>)#$1]]><![CDATA[$2#g;
my $text = '<![CDATA[' . $data . ']]>';
- $text = "<$name>$text</$name>\n" if ( $name ne $self->{text_node_key} );
+ $text = "<$name>$text</$name>$linebreak" if ( $name ne $self->{text_node_key} );
$text;
}
@@ -1096,9 +1113,11 @@
my $self = shift;
my $name = shift;
my $scalar = shift;
+ my $linebreak = defined $self->{linebreak} ? $self->{linebreak} : "\n";
+ my $escape_map = $self->{escape_map} if ref $self->{escape_map};
my $copy = $scalar;
- my $text = &xml_escape($copy);
- $text = "<$name>$text</$name>\n" if ( $name ne $self->{text_node_key} );
+ my $text = &xml_escape($copy, $escape_map);
+ $text = "<$name>$text</$name>$linebreak" if ( $name ne $self->{text_node_key} );
$text;
}
@@ -1253,6 +1272,7 @@
sub xml_escape {
my $str = shift;
+ my $escape_map = shift;
return '' unless defined $str;
# except for TAB(\x09),CR(\x0D),LF(\x0A)
$str =~ s{
@@ -1260,6 +1280,11 @@
}{
sprintf( '&#%d;', ord($1) );
}gex;
+ if (defined $escape_map && ref $escape_map eq "HASH") {
+ while (my ($key, $val) = each %{$escape_map}) {
+ $str =~ s/$key/$val/g;
+ }
+ }
$str =~ s/&(?!#(\d+;|x[\dA-Fa-f]+;))/&/g;
$str =~ s/</</g;
$str =~ s/>/>/g;