common/tools/brag/toBrag.pm
author Simon Howkins <simonh@symbian.org>
Tue, 10 Nov 2009 16:52:27 +0000
changeset 752 1f07674ec99f
child 753 5069de517698
permissions -rw-r--r--
Moved common code to shared module.

#!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 ToBrag;

# A useful constant
our $xmlNewline = bless { Text => "\n" }, "Characters";

sub createBuildStatus
{
	return [
		bless
		{
			Kids =>
			[ $ToBrag::xmlNewline ]
		}, "buildStatus"
	];
}

sub ensurePhase
{
	my $buildStatus = shift;
	my $phaseName = shift;

	my ($phase) = grep { ref $_ eq "phase" && $_->{name} eq $phaseName } @{$buildStatus->[-1]->{Kids}};
	unless ($phase)
	{
		$phase = bless
		{
			name => $phaseName,
			Kids => [ $ToBrag::xmlNewline ]
		}, "phase";
		push @{$buildStatus->[-1]->{Kids}}, $phase, $ToBrag::xmlNewline;
	}
	return $phase;
}

sub ensureStep
{
	my $phase = shift;
	my $stepName = shift;

	my ($step) = grep { ref $_ eq "step" && $_->{name} eq $stepName } @{$phase->{Kids}};
	unless ($step)
	{
		$step = bless
		{
			name => $stepName,
			Kids => [ $ToBrag::xmlNewline ]
		}, "step";
		push @{$phase->{Kids}}, $step, $ToBrag::xmlNewline;
	}
	return $step;
}

sub ensureFailureSet
{
	my $step = shift;
	my $level = shift;

	my ($failureSet) = grep { ref $_ eq "failures" && $_->{level} eq $level } @{$step->{Kids}};
	unless ($failureSet)
	{
		$failureSet = bless
		{
			level => $level,
			Kids => [ $ToBrag::xmlNewline ]
		}, "failures";
		push @{$step->{Kids}}, $failureSet, $ToBrag::xmlNewline;
	}
	return $failureSet;
}

# 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")
	{
		print $tree->{Text};
		return;
	}
	
	print "<$tagName";

	foreach my $attr (
		sort {
			my $order = "name level start stop href";
			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;