6 my $NS = "http://www.daa.com.au/~james/dia-shape-ns";
9 my ($node, $name) = @_;
11 for my $attr ($node->attributes()) {
12 if ($attr->nodeName eq $name) {
18 while (my $filename = shift) {
19 my $parser = XML
::LibXML
->new();
20 my $tree = $parser->parse_file($filename);
21 my $root = $tree->getDocumentElement; # shape element
23 my @svg = $root->getElementsByTagName('svg:svg');
31 my $svg = $svg[0]; # should only be one
33 my $widthval = $svg->getAttribute("width");
37 my $heightval = $svg->getAttribute("height");
41 if ($widthval && $heightval) {
42 my $newline = $tree->createTextNode( "\n" );
43 my $midx = $widthval/2;
44 my $midy = $heightval/2;
45 # For unknown reasons, this doesn't find the connections child
47 # my @connections = $root->getElementsByTagName('connections');
48 # if (@connections > 1) {
49 # die "Too many connections";
51 # my $conn2 = $connections[0];
52 # print "Old conn: " . $conn2 . "\n";
54 my @points = $tree->getElementsByTagNameNS($NS, 'point');
56 my @conn = $tree->getElementsByTagNameNS($NS, 'connections');
58 die "Too many connection groups";
63 my @points = $tree->getElementsByTagNameNS($NS, 'point');
64 my $mainPoint = $tree->createElement("point");
65 $mainPoint->setAttribute("x", $midx);
66 $mainPoint->setAttribute("y", $midy);
67 $mainPoint->setAttribute("main", "yes");
69 foreach my $point (@points) {
70 my $x = getAttribute
($point, "x");
71 my $y = getAttribute
($point, "y");
72 my $main = getAttribute
($point, "main");
74 # $conn->removeChild($point);
75 $point->replaceNode($mainPoint);
78 if ($x == $midx && $y == $midy) {
79 print "Replacing nonmain\n";
80 $conn->removeChild($point);
85 $conn->addChild($mainPoint);
86 $root->insertAfter( $newline, $mainPoint);
89 $conn = $tree->createElement("connections");
90 my $mainPoint = $tree->createElement("point");
91 $mainPoint->setAttribute("x", $midx);
92 $mainPoint->setAttribute("y", $midy);
93 $mainPoint->setAttribute("main", "yes");
94 $conn->addChild($mainPoint);
95 my $spacing = $tree->createTextNode( " " );
96 $root->insertBefore($spacing, $svg);
97 $root->insertBefore($conn, $svg);
98 $root->insertBefore($newline, $svg);
100 open(OUT
, ">$filename");
101 print OUT
$tree->toString;
106 #my @svg = $shape[0]->getElementsByTagName('svg:svg');