summaryrefslogtreecommitdiffstats
path: root/contrib/local/annotate-xml-lineno.pl
blob: f7e1a79bc18d522ff77e5060c9bf6f41d0dc40bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/perl -w

use strict;
use File::Spec;
use File::Basename;
use XML::LibXML;
use Data::Dumper;

my $xmlIn = shift or die("Expected *.xml file as input");

# absolutize and split into components
$xmlIn = File::Spec->rel2abs($xmlIn) or die($!);
my($filename, $dirs, $suffix) = fileparse($xmlIn) or die($!);

my $parser = XML::LibXML->new({'line_numbers' => 1 });
# my $xml = $parser->parse_file($xmlIn);
my $doc = $parser->load_xml('location' => $xmlIn, 'line_numbers' => 1) ;

my $lineOffset = 0;

sub lineNoNodes {
	my $node = shift;
	
	if ($node->nodeType == XML_ELEMENT_NODE) {
		$node->setAttribute("line_start", $node->line_number() + $lineOffset);
	}

	my $prevElem;
	for my $child ($node->childNodes()) {
		lineNoNodes($child);
		if ($prevElem) {
			$prevElem->setAttribute("line_end", $child->line_number() - 1 + $lineOffset);
			undef($prevElem);
		}
		if ($child->nodeType == XML_ELEMENT_NODE) {
			$prevElem = $child;
		}
	}
}

&lineNoNodes($doc->getDocumentElement());

print $doc->toString();