File: canonical

package info (click to toggle)
libxml-parser-perl 2.47-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,268 kB
  • sloc: xml: 3,937; perl: 2,026; makefile: 38; ansic: 27
file content (126 lines) | stat: -rwxr-xr-x 2,847 bytes parent folder | download | duplicates (3)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/local/bin/perl -w
#
# Copyright 1999 Clark Cooper <coopercc@netheaven.com>
# All rights reserved.
#
# This program is free software; you can redistribute it and/or
# modify it under the same terms as Perl itself.
#
# $Revision: 1.1.1.1 $
#
# $Date: 2003-07-27 11:07:11 $
#
#	This program take an XML document (either on standard input or
#	from a filename supplied as an argument) and generates corresponding
#	canonical XML document on the standard output. The definition of
#	"Canonical XML" that I'm using is taken from the working draft
#	published by W3C on 19-Jan-2000:
#
#		http://www.w3.org/TR/2000/WD-xml-c14n-20000119.html
#
#	The latest version of this document is at:
#
#		http://www.w3.org/TR/xml-c14n
#

use XML::Parser;

my $indoctype = 0;
my $inroot    = 0;
my $p         = new XML::Parser(
    ErrorContext  => 2,
    Namespaces    => 1,
    ParseParamEnt => 1,
    Handlers      => {
        Start      => \&sthndl,
        End        => \&endhndl,
        Char       => \&chrhndl,
        Proc       => \&proc,
        Doctype    => sub { $indoctype = 1 },
        DoctypeFin => sub { $indoctype = 0 }
    }
);

my $file = shift;
if ( defined $file ) {
    $p->parsefile($file);
}
else {
    $p->parse(*STDIN);
}

################
## End main
################

sub sthndl {
    my $xp = shift;
    my $el = shift;

    $inroot = 1 unless $inroot;
    my $ns_index = 1;

    my $elns = $xp->namespace($el);
    if ( defined $elns ) {
        my $pfx = 'n' . $ns_index++;
        print "<$pfx:$el xmlns:$pfx=\"$elns\"";
    }
    else {
        print "<$el";
    }

    if (@_) {
        for ( my $i = 0; $i < @_; $i += 2 ) {
            my $nm = $_[$i];
            my $ns = $xp->namespace($nm);
            $_[$i] = defined($ns) ? "$ns\01$nm" : "\01$nm";
        }

        my %atts = @_;
        my @ids  = sort keys %atts;
        foreach my $id (@ids) {
            my ( $ns, $nm ) = split( /\01/, $id );
            my $val = $xp->xml_escape( $atts{$id}, '"', "\x9", "\xA", "\xD" );
            if ( length($ns) ) {
                my $pfx = 'n' . $ns_index++;
                print " $pfx:$nm=\"$val\" xmlns:$pfx=\"$ns\"";
            }
            else {
                print " $nm=\"$val\"";
            }
        }
    }

    print '>';
}    # End sthndl

sub endhndl {
    my ( $xp, $el ) = @_;

    my $nm = $xp->namespace($el) ? "n1:$el" : $el;
    print "</$nm>";
    if ( $xp->depth == 0 ) {
        $inroot = 0;
        print "\n";
    }
}    # End endhndl

sub chrhndl {
    my ( $xp, $data ) = @_;

    print $xp->xml_escape( $data, '>', "\xD" );
}    # End chrhndl

sub proc {
    my ( $xp, $target, $data ) = @_;

    unless ($indoctype) {
        print "<?$target $data?>";
        print "\n" unless $inroot;
    }
}

# Tell emacs that this is really a perl script
#Local Variables:
#mode: perl
#End: