common/tools/lib/XML/Printer.pm
changeset 814 11c237c9ad00
child 959 94716c328941
equal deleted inserted replaced
813:66884b6df45d 814:11c237c9ad00
       
     1 #!perl -w
       
     2 #
       
     3 # Copyright (c) 2009 Symbian Foundation Ltd
       
     4 # This component and the accompanying materials are made available
       
     5 # under the terms of the License "Eclipse Public License v1.0"
       
     6 # which accompanies this distribution, and is available
       
     7 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 #
       
     9 # Initial Contributors:
       
    10 # Symbian Foundation Ltd - initial contribution.
       
    11 #
       
    12 # Contributors:
       
    13 #
       
    14 # Description:
       
    15 # Functionality common to BRAG file generation
       
    16 
       
    17 use strict;
       
    18 
       
    19 package XML::Printer;
       
    20 
       
    21 # Prints out the XML tree to STDOUT
       
    22 sub printTree
       
    23 {
       
    24 	my $tree = shift or die;
       
    25 	die unless ref $tree;
       
    26 
       
    27 	my $tagName = ref $tree;
       
    28 	$tagName =~ s{^main::}{};
       
    29 	if ($tagName eq "Characters")
       
    30 	{
       
    31 		if ($tree->{Text} =~ m{[<>&]})
       
    32 		{
       
    33 			print "<![CDATA[$tree->{Text}]]>";
       
    34 		}
       
    35 		else
       
    36 		{
       
    37 			print $tree->{Text};
       
    38 		}
       
    39 		return;
       
    40 	}
       
    41 	
       
    42 	print "<$tagName";
       
    43 
       
    44 	foreach my $attr (
       
    45 		sort {
       
    46 			# TODO: This is hard coded for the case of a BRAG XML merge - ought to be passed in as a parameter
       
    47 			my $order = "name level start stop href package effect";
       
    48 			my $ixA = index $order, $a;
       
    49 			my $ixB = index $order, $b;
       
    50 			die "$a $b" if $ixA + $ixB == -2;
       
    51 			$ixA - $ixB;
       
    52 		}
       
    53 		grep {
       
    54 			! ref $tree->{$_}
       
    55 		}
       
    56 		keys %$tree)
       
    57 	{
       
    58 		print " $attr=\"$tree->{$attr}\"";
       
    59 	}
       
    60 
       
    61 	my $children = $tree->{Kids} || [];
       
    62 	if (scalar @$children)
       
    63 	{
       
    64 		print ">";
       
    65 		foreach my $child (@$children)
       
    66 		{
       
    67 			printTree($child);
       
    68 		}
       
    69 		print "</$tagName";
       
    70 	}
       
    71 	else
       
    72 	{
       
    73 		print "/"
       
    74 	}
       
    75 
       
    76 	print ">";
       
    77 }
       
    78 
       
    79 1;
       
    80