common/tools/lib/XML/Printer.pm
author Simon Howkins <simonh@symbian.org>
Wed, 31 Mar 2010 10:20:44 +0100
changeset 955 28714977dccb
parent 814 11c237c9ad00
child 961 94716c328941
permissions -rw-r--r--
Bug 2274 - Detection of physical drives doesn't work on Vista/Win7
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
	{
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
    58
		print " $attr=\"$tree->{$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
    59
	}
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
    60
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
    61
	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
    62
	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
    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
		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
    65
		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
    66
		{
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
			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
    68
		}
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
		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
    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
	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
    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 "/"
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
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
	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
    77
}
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
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
    80