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.
authorSimon Howkins <simonh@symbian.org>
Mon, 07 Dec 2009 17:21:55 +0000
changeset 814 11c237c9ad00
parent 813 66884b6df45d
child 815 3ae590eac695
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.
common/tools/lib/XML/Printer.pm
common/tools/mergeXML.pl
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/common/tools/lib/XML/Printer.pm	Mon Dec 07 17:21:55 2009 +0000
@@ -0,0 +1,80 @@
+#!perl -w
+#
+# Copyright (c) 2009 Symbian Foundation Ltd
+# This component and the accompanying materials are made available
+# under the terms of the License "Eclipse Public License v1.0"
+# which accompanies this distribution, and is available
+# at the URL "http://www.eclipse.org/legal/epl-v10.html".
+#
+# Initial Contributors:
+# Symbian Foundation Ltd - initial contribution.
+#
+# Contributors:
+#
+# Description:
+# Functionality common to BRAG file generation
+
+use strict;
+
+package XML::Printer;
+
+# Prints out the XML tree to STDOUT
+sub printTree
+{
+	my $tree = shift or die;
+	die unless ref $tree;
+
+	my $tagName = ref $tree;
+	$tagName =~ s{^main::}{};
+	if ($tagName eq "Characters")
+	{
+		if ($tree->{Text} =~ m{[<>&]})
+		{
+			print "<![CDATA[$tree->{Text}]]>";
+		}
+		else
+		{
+			print $tree->{Text};
+		}
+		return;
+	}
+	
+	print "<$tagName";
+
+	foreach my $attr (
+		sort {
+			# TODO: This is hard coded for the case of a BRAG XML merge - ought to be passed in as a parameter
+			my $order = "name level start stop href package effect";
+			my $ixA = index $order, $a;
+			my $ixB = index $order, $b;
+			die "$a $b" if $ixA + $ixB == -2;
+			$ixA - $ixB;
+		}
+		grep {
+			! ref $tree->{$_}
+		}
+		keys %$tree)
+	{
+		print " $attr=\"$tree->{$attr}\"";
+	}
+
+	my $children = $tree->{Kids} || [];
+	if (scalar @$children)
+	{
+		print ">";
+		foreach my $child (@$children)
+		{
+			printTree($child);
+		}
+		print "</$tagName";
+	}
+	else
+	{
+		print "/"
+	}
+
+	print ">";
+}
+
+1;
+
--- a/common/tools/mergeXML.pl	Mon Dec 07 15:22:47 2009 +0000
+++ b/common/tools/mergeXML.pl	Mon Dec 07 17:21:55 2009 +0000
@@ -18,6 +18,11 @@
 use XML::Parser;
 use Getopt::Long;
 
+use FindBin;
+use lib "$FindBin::Bin/lib";
+
+use XML::Printer;
+
 # Read option arguments
 my $howtoString;
 my $xslLink;
@@ -61,7 +66,7 @@
 # Output total tree
 print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
 print "<?xml-stylesheet type=\"text/xsl\" href=\"$xslLink\"?>\n" if $xslLink;
-printTree($outTree->[0]);
+XML::Printer::printTree($outTree->[0]);
 print "\n";
 
 exit(0);
@@ -147,47 +152,4 @@
 	return undef;
 }
 
-sub printTree
-{
-	my $tree = shift or die;
-	die unless ref $tree;
 
-	my $tagName = ref $tree;
-	$tagName =~ s{^main::}{};
-	if ($tagName eq "Characters")
-	{
-		print $tree->{Text};
-		return;
-	}
-	
-	print "<$tagName";
-
-	foreach my $attr (
-		sort
-		grep {
-			! ref $tree->{$_}
-		}
-		keys %$tree)
-	{
-		print " $attr=\"$tree->{$attr}\"";
-	}
-
-	my $children = $tree->{Kids};
-	if (scalar @$children)
-	{
-		print ">";
-		foreach my $child (@$children)
-		{
-			printTree($child);
-		}
-		print "</$tagName";
-	}
-	else
-	{
-		print "/"
-	}
-
-	print ">";
-}
-
-