common/tools/lib/XML/Printer.pm
author Simon Howkins <simonh@symbian.org>
Wed, 27 Oct 2010 16:22:14 +0100
changeset 1316 0b4a09013baf
parent 961 94716c328941
permissions -rw-r--r--
Added copyright messages
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
814
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
     1
#!perl -w
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
     2
#
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
     3
# Copyright (c) 2009 Symbian Foundation Ltd
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
     4
# This component and the accompanying materials are made available
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
     5
# under the terms of the License "Eclipse Public License v1.0"
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
     6
# which accompanies this distribution, and is available
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
     7
# at the URL "http://www.eclipse.org/legal/epl-v10.html".
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
     8
#
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
     9
# Initial Contributors:
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    10
# Symbian Foundation Ltd - initial contribution.
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    11
#
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    12
# Contributors:
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    13
#
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    14
# Description:
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    15
# Functionality common to BRAG file generation
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    16
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    17
use strict;
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    18
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    19
package XML::Printer;
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    20
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    21
# Prints out the XML tree to STDOUT
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    22
sub printTree
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    23
{
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    24
	my $tree = shift or die;
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    25
	die unless ref $tree;
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    26
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    27
	my $tagName = ref $tree;
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    28
	$tagName =~ s{^main::}{};
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    29
	if ($tagName eq "Characters")
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    30
	{
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    31
		if ($tree->{Text} =~ m{[<>&]})
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    32
		{
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    33
			print "<![CDATA[$tree->{Text}]]>";
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    34
		}
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    35
		else
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    36
		{
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    37
			print $tree->{Text};
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    38
		}
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    39
		return;
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    40
	}
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    41
	
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    42
	print "<$tagName";
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    43
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    44
	foreach my $attr (
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    45
		sort {
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    46
			# TODO: This is hard coded for the case of a BRAG XML merge - ought to be passed in as a parameter
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    47
			my $order = "name level start stop href package effect";
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    48
			my $ixA = index $order, $a;
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    49
			my $ixB = index $order, $b;
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    50
			die "$a $b" if $ixA + $ixB == -2;
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    51
			$ixA - $ixB;
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    52
		}
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    53
		grep {
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    54
			! ref $tree->{$_}
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    55
		}
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    56
		keys %$tree)
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    57
	{
961
94716c328941 Fix canonical_system_definition.xml doesn't have XML special characters escaped
Dario Sestito <darios@symbian.org>
parents: 814
diff changeset
    58
		my $value_escaped = $tree->{$attr};
94716c328941 Fix canonical_system_definition.xml doesn't have XML special characters escaped
Dario Sestito <darios@symbian.org>
parents: 814
diff changeset
    59
		$value_escaped =~ s/&/&amp;/g;
94716c328941 Fix canonical_system_definition.xml doesn't have XML special characters escaped
Dario Sestito <darios@symbian.org>
parents: 814
diff changeset
    60
		$value_escaped =~ s/</&lt;/g;
94716c328941 Fix canonical_system_definition.xml doesn't have XML special characters escaped
Dario Sestito <darios@symbian.org>
parents: 814
diff changeset
    61
		$value_escaped =~ s/>/&gt;/g;
94716c328941 Fix canonical_system_definition.xml doesn't have XML special characters escaped
Dario Sestito <darios@symbian.org>
parents: 814
diff changeset
    62
		print " $attr=\"$value_escaped\"";
814
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    63
	}
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    64
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    65
	my $children = $tree->{Kids} || [];
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    66
	if (scalar @$children)
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    67
	{
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    68
		print ">";
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    69
		foreach my $child (@$children)
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    70
		{
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    71
			printTree($child);
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    72
		}
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    73
		print "</$tagName";
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    74
	}
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    75
	else
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    76
	{
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    77
		print "/"
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    78
	}
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    79
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    80
	print ">";
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    81
}
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    82
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    83
1;
11c237c9ad00 Changed mergeXML.pl to use a better version of the XML printing algorithm, so it can generate well-formed content if the input includes some rogue &s or <>s.
Simon Howkins <simonh@symbian.org>
parents:
diff changeset
    84