File: xmlcharmap2utf8trans

package info (click to toggle)
docbook2x 0.8.8-9
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 4,900 kB
  • sloc: xml: 16,229; sh: 3,684; perl: 3,461; ansic: 639; makefile: 403; sed: 11
file content (184 lines) | stat: -rwxr-xr-x 4,663 bytes parent folder | download | duplicates (5)
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/env perl

=head1 NAME

xmlchar2utf8trans - convert XSLT 2.0 character maps to utf8trans character maps

=head1 SYNOPSIS

xmlchar2utf8trans [xslt-charmap-file]

=head1 DESCRIPTION

This script converts a XML character map in XSLT 2.0 format
to the plain text format used by utf8trans(1).

Note:  The parsing of the XSLT 2.0 character map is really
minimal, and does not conform to the full specification.

=cut

use XML::SAX::ParserFactory;
use strict;

package CharmapProcessor;
use base qw(XML::SAX::Base);

sub start_document {
    my ($self, $elem) = @_;
    $self->{imported_charmaps} = {};
    $self->{local_charmaps} = {};
}

sub xsl_import_element {
    my ($self, $elem) = @_;

    my $href = $elem->{Attributes}->{'{}href'};
    if(not defined $href) {
        $self->report_static_error("<xsl:import> does not have required 'href' attribute");
        return;
    }

    my $import_charmap_proc = CharmapProcessor->new;
    my $import_parser = XML::SAX::ParserFactory->parser(
                        DocumentHandler => $import_charmap_proc);
    $import_parser->parse_uri($href->{Value});
    
    copy_hash_entries($self->{imported_charmaps},
                      $import_charmap_proc->{imported_charmaps});
    copy_hash_entries($self->{imported_charmaps},
                      $import_charmap_proc->{local_charmaps});
}

sub copy_hash_entries
{
    my ($to, $from) = @_;
    my @conflicts = 0;
    while( my ($k,$v) = each %{$from} ) {
        push(@conflicts, $k) if exists($to->{$k});
        $to->{$k} = $v;
    }
    return @conflicts;
}

sub xsl_include_element {
    my ($self, $elem) = @_;

    my $href = $elem->{Attributes}->{'{}href'};
    if(not defined $href) {
        $self->report_static_error("<xsl:import> does not have required 'href' attribute");
        return;
    }

    my $include_parser = XML::SAX::ParserFactory->parser(
                        DocumentHandler => $self);
    $include_parser->parse_uri($href->{Value});
}

sub xsl_output_character_element {
    my ($self, $elem) = @_;

    if(not defined $self->{current_charmap}) {
        print STDERR "no current charmap\n";
        return;
    }

    my $char = ord($elem->{Attributes}->{'{}character'}->{Value});
    my $string = $elem->{Attributes}->{'{}string'}->{Value};

    push(@{$self->{current_charmap}}, [ $char, $string ]);
}

sub xsl_character_map_element {
    my ($self, $elem) = @_;

    my $charmap_name = $elem->{Attributes}->{'{}name'}->{Value};

    $self->{current_charmap} = [];
    $self->{local_charmaps}->{$charmap_name} = $self->{current_charmap};
}

sub xsl_output_element {
    my ($self, $elem) = @_;

    if(exists $elem->{Attributes}->{'{}use-character-maps'}) {
      # FIXME doesn't handle namespaced names yet !
      $_ = $elem->{Attributes}->{'{}use-character-maps'}->{Value};
      my @k = split;

      $self->{use_charmaps} = \@k;
    }
}

sub start_element {
    my ($self, $elem) = @_;
    
    if($elem->{NamespaceURI} eq 'http://www.w3.org/1999/XSL/Transform')
    {
        if($elem->{LocalName} eq 'output-character') {
            &xsl_output_character_element;
        } elsif($elem->{LocalName} eq 'character-map') {
            &xsl_character_map_element;
        } elsif($elem->{LocalName} eq 'import') {
            &xsl_import_element;
        } elsif($elem->{LocalName} eq 'include') {
            &xsl_include_element;
        } elsif($elem->{LocalName} eq 'output') {
            &xsl_output_element;
        } 
    }
}

sub report_static_error {
    my ($self, $error_code, $message) = @_;
    print STDERR "$0: $error_code: $message\n";
}

sub set_document_locator {
    my ($self, $arg) = @_;
    $self->{locator} = $arg->{Locator};
}

# sub comment {
#     my ($self, $node) = @_;
#     my @lines = split(/\n/, $node->{Data});
#     
#     print "\n" if @lines > 1;
#     
#     foreach my $line (@lines) {
#         $line =~ s/^[ \t]+//;
#         print "# $line\n";
#     }
# }

package main;

my $charmap_proc = CharmapProcessor->new;

unshift(@ARGV, '-') unless @ARGV;
my $parser = XML::SAX::ParserFactory->parser(
                DocumentHandler => $charmap_proc);

binmode(STDOUT, ":utf8") unless $] < 5.008;

foreach my $file (@ARGV)
{
    if($file eq '-') {
        $parser->parse_file(\*STDIN);
    } else {
        $parser->parse_uri($file);
    }
}

if(!exists $charmap_proc->{use_charmaps}) {
  print STDERR "no character maps are indicated to be used";
}

foreach my $k (@{$charmap_proc->{use_charmaps}}) {
  my $charmap = $charmap_proc->{local_charmaps}->{$k};

  foreach my $entry (@$charmap) {
    $entry->[1] =~ tr/\n\0/  /;
    printf "%x\t%s\n", $entry->[0], $entry->[1];
  }
}