| 1 | # -*- perl -*- | ||
| 2 | |||
| 3 | # | ||
| 4 | # $Id: XMLText.pm,v 1.5 2004/12/21 23:19:46 eserte Exp $ | ||
| 5 | # Author: Slaven Rezic | ||
| 6 | # | ||
| 7 | # Copyright (C) 2004 Slaven Rezic. | ||
| 8 | # This is free software; you can redistribute it and/or modify it under the | ||
| 9 | # terms of the GNU General Public License, see the file COPYING. | ||
| 10 | |||
| 11 | # | ||
| 12 | # Mail: slaven@rezic.de | ||
| 13 | # WWW: http://we-framework.sourceforge.net | ||
| 14 | # | ||
| 15 | |||
| 16 | package WE_Content::XMLText; | ||
| 17 | use base qw(WE_Content::Base); | ||
| 18 | |||
| 19 | use strict; | ||
| 20 | use vars qw($VERSION); | ||
| 21 | $VERSION = sprintf("%d.%02d", q$Revision: 1.5 $ =~ /(\d+)\.(\d+)/); | ||
| 22 | |||
| 23 | use Carp; | ||
| 24 | use XML::Parser (); | ||
| 25 | use Storable qw(dclone); | ||
| 26 | |||
| 27 | { | ||
| 28 | # Simple minded writer which is sufficient for this module, | ||
| 29 | # but better than XML::Writer! | ||
| 30 | package My::XML::Writer; | ||
| 31 | sub new { | ||
| 32 | my $class = shift; | ||
| 33 | my $self = bless { T => "" }, $class; | ||
| 34 | } | ||
| 35 | sub as_string { | ||
| 36 | shift->{T}; | ||
| 37 | } | ||
| 38 | sub startTag { | ||
| 39 | my($self, $tag, @attr) = @_; | ||
| 40 | my $t = \$self->{T}; | ||
| 41 | $$t .= "<$tag"; | ||
| 42 | for(my $i=0; $i<$#attr; $i+=2) { | ||
| 43 | $$t .= " $attr[$i]='$attr[$i+1]'"; | ||
| 44 | } | ||
| 45 | $$t .= ">"; | ||
| 46 | push @{ $self->{LastOpen} }, $tag; | ||
| 47 | } | ||
| 48 | sub endTag { | ||
| 49 | my($self, $tag) = @_; | ||
| 50 | my $last_open = pop @{ $self->{LastOpen} }; | ||
| 51 | if (defined $tag) { | ||
| 52 | if ($tag ne $last_open) { | ||
| 53 | die "Mismatch tag: $tag != $last_open"; | ||
| 54 | } | ||
| 55 | } else { | ||
| 56 | $tag = $last_open; | ||
| 57 | } | ||
| 58 | $self->{T} .= "</$tag>\n"; | ||
| 59 | } | ||
| 60 | sub characters { | ||
| 61 | my($self, $data) = @_; | ||
| 62 | if ($data =~ /[\&\<\>]/) { | ||
| 63 | $data =~ s/\&/\&\;/g; | ||
| 64 | $data =~ s/\</\<\;/g; | ||
| 65 | $data =~ s/\>/\>\;/g; | ||
| 66 | } | ||
| 67 | # Unicodify | ||
| 68 | if ($] >= 5.008) { | ||
| 69 | require Encode; | ||
| 70 | # Make sure the data is characters, but with utf8 flag turned on | ||
| 71 | if (!Encode::is_utf8($data)) { | ||
| 72 | $data = Encode::decode("iso-8859-1", $data); | ||
| 73 | } | ||
| 74 | } elsif ($] >= 5.006) { | ||
| 75 | $data .= "\x{0100}"; | ||
| 76 | $data = substr($data, 0, -1); | ||
| 77 | } else { | ||
| 78 | warn "XXX not yet tested"; | ||
| 79 | require Unicode::String; | ||
| 80 | my $us = Unicode::String->new; | ||
| 81 | $us->latin1($data); | ||
| 82 | $data = $us->utf8; | ||
| 83 | } | ||
| 84 | $self->{T} .= $data; | ||
| 85 | } | ||
| 86 | } | ||
| 87 | |||
| 88 | sub new { | ||
| 89 | my($class, %args) = @_; | ||
| 90 | my $self = {}; | ||
| 91 | while(my($k,$v) = each %args) { | ||
| 92 | croak "Option does not start with a dash: $k" if $k !~ /^-/; | ||
| 93 | $self->{ucfirst(substr($k,1))} = $v; | ||
| 94 | } | ||
| 95 | bless $self, $class; | ||
| 96 | if ($self->{File}) { | ||
| 97 | $self->parse(-file => $self->{File}); | ||
| 98 | } elsif ($self->{String}) { | ||
| 99 | $self->parse(-string => $self->{String}); | ||
| 100 | } | ||
| 101 | $self; | ||
| 102 | } | ||
| 103 | |||
| 104 | sub _create_parser { | ||
| 105 | my $self = shift; | ||
| 106 | my $p = $self->{P} = XML::Parser->new; | ||
| 107 | $p->setHandlers(Start => sub { $self->_start_handler(@_) }, | ||
| 108 | End => sub { $self->_end_handler(@_) }, | ||
| 109 | Char => sub { $self->_char_handler(@_) }, | ||
| 110 | ); | ||
| 111 | $p; | ||
| 112 | } | ||
| 113 | |||
| 114 | sub _start_handler { | ||
| 115 | my($self, undef, $elem, %attr) = @_; | ||
| 116 | $self->{CurrentTag} = $elem; | ||
| 117 | if ($elem eq 'webeditordoc') { | ||
| 118 | my $lang = $self->{CurrentLang} = $attr{"lang"}; | ||
| 119 | my $oldlang = $attr{"oldlang"}; | ||
| 120 | $oldlang = $self->{Oldlang} if !defined $oldlang; | ||
| 121 | if (!$oldlang) { | ||
| 122 | die "Missing -oldlang parameter"; | ||
| 123 | } | ||
| 124 | if (!$self->{Templateobject}{data}{$oldlang}) { | ||
| 125 | die "Missing language $oldlang in template object, found only: " . join(", ", keys %{ $self->{Templateobject}{data} }); | ||
| 126 | } | ||
| 127 | $self->{Templateobject}{data}{$lang} = | ||
| 128 | dclone $self->{Templateobject}{data}{$oldlang}; | ||
| 129 | } else { | ||
| 130 | $self->{CurrentPath} = $attr{"path"}; | ||
| 131 | } | ||
| 132 | $self->{CurrentCharacters} = ""; | ||
| 133 | } | ||
| 134 | |||
| 135 | sub _end_handler { | ||
| 136 | my($self, undef, $elem) = @_; | ||
| 137 | if ($elem eq 'val') { | ||
| 138 | my $path = $self->{CurrentPath} || die "path missing"; | ||
| 139 | my(@path) = split /\./, $path; | ||
| 140 | if (shift(@path) ne "root") { | ||
| 141 | die "path should start with 'root'"; | ||
| 142 | } | ||
| 143 | my $ref = $self->{Templateobject}{data}; | ||
| 144 | my $lang = $self->{CurrentLang} || die "lang missing"; | ||
| 145 | $ref = $ref->{$lang}; | ||
| 146 | for my $i (0 .. $#path-1) { | ||
| 147 | my $elem = $path[$i]; | ||
| 148 | if (ref $ref eq 'ARRAY') { | ||
| 149 | $ref = $ref->[$elem]; | ||
| 150 | } elsif (ref $ref eq 'HASH') { | ||
| 151 | $ref = $ref->{$elem}; | ||
| 152 | } else { | ||
| 153 | undef $ref; | ||
| 154 | } | ||
| 155 | if (!$ref) { | ||
| 156 | local $^W = 0; | ||
| 157 | die "Could not resolve path `$path', failed at `$elem'. Element is `$ref'"; | ||
| 158 | } | ||
| 159 | } | ||
| 160 | my $s = $self->{CurrentCharacters}; | ||
| 161 | if ($self->debug) { | ||
| 162 | warn "Set `$path' to `$s'\n"; | ||
| 163 | } | ||
| 164 | if (ref $ref eq 'ARRAY') { | ||
| 165 | $ref->[$path[-1]] = $s; | ||
| 166 | } else { | ||
| 167 | $ref->{$path[-1]} = $s; | ||
| 168 | } | ||
| 169 | } | ||
| 170 | } | ||
| 171 | |||
| 172 | sub _char_handler { | ||
| 173 | my($self, undef, $string) = @_; | ||
| 174 | $self->{CurrentCharacters} .= $string; | ||
| 175 | } | ||
| 176 | |||
| 177 | sub parse { | ||
| 178 | my($self, %args) = @_; | ||
| 179 | |||
| 180 | if (!$self->{Templateobject}) { | ||
| 181 | croak "Missing -templateobject parameter"; | ||
| 182 | } | ||
| 183 | |||
| 184 | my $buf = $self->get_string(%args); | ||
| 185 | if (!$self->{P}) { $self->_create_parser } | ||
| 186 | eval { | ||
| 187 | $self->{P}->parse($buf); | ||
| 188 | }; | ||
| 189 | if ($@) { | ||
| 190 | my $line = 1; | ||
| 191 | warn join("\n", map { sprintf("%3d: %s", $line++, $_) } split /\n/, $buf); | ||
| 192 | croak $@; | ||
| 193 | } | ||
| 194 | |||
| 195 | $self->{Object} = $self->{Templateobject}; | ||
| 196 | |||
| 197 | $self->{Object}; | ||
| 198 | } | ||
| 199 | |||
| 200 | sub serialize { | ||
| 201 | my($self, %args) = @_; | ||
| 202 | my $lang = $args{-lang} || croak "-lang parameter is missing"; | ||
| 203 | my $oldlang = $args{-oldlang}; | ||
| 204 | my $xmlw = My::XML::Writer->new; | ||
| 205 | $xmlw->startTag("webeditordoc", "lang", $lang, | ||
| 206 | (defined $oldlang ? ("oldlang", $oldlang) : ()), | ||
| 207 | ); | ||
| 208 | $self->_serialize($self->{Object}{'data'}{$lang}, "root", $xmlw); | ||
| 209 | $xmlw->endTag("webeditordoc"); | ||
| 210 | my $data = "<?xml version='1.0' encoding='utf-8'?>\n" . | ||
| 211 | "<!DOCTYPE webeditordoc SYSTEM 'webeditordoc.dtd'>\n" . | ||
| 212 | $xmlw->as_string; | ||
| 213 | if ($] >= 5.008) { | ||
| 214 | # Write out utf-8 as octets. | ||
| 215 | $data = Encode::encode("utf-8", $data); | ||
| 216 | } else { | ||
| 217 | warn "XXX not yet tested"; | ||
| 218 | } | ||
| 219 | $data; | ||
| 220 | } | ||
| 221 | |||
| 222 | sub _serialize { | ||
| 223 | my($self, $ref, $path, $xmlw) = @_; | ||
| 224 | if (ref $ref eq 'HASH') { | ||
| 225 | while(my($k,$v) = each %$ref) { | ||
| 226 | if ($k =~ /^(text|title)$/) { # XXX add here more translatable keys (or make configurable) | ||
| 227 | $xmlw->startTag("val", | ||
| 228 | "path" => "$path.$k"); | ||
| 229 | $xmlw->characters($v); | ||
| 230 | $xmlw->endTag; | ||
| 231 | } elsif (ref $v) { | ||
| 232 | $self->_serialize($v, "$path.$k", $xmlw); | ||
| 233 | } | ||
| 234 | } | ||
| 235 | } elsif (ref $ref eq 'ARRAY') { | ||
| 236 | for my $i (0 .. $#$ref) { | ||
| 237 | my $v = $ref->[$i]; | ||
| 238 | if (ref $v) { | ||
| 239 | $self->_serialize($v, "$path.$i", $xmlw); | ||
| 240 | } | ||
| 241 | } | ||
| 242 | } | ||
| 243 | } | ||
| 244 | |||
| 245 | sub ext { "xml" } | ||
| 246 | |||
| 247 | 1; | ||
| 248 | |||
| 249 | __END__ |