| 1 | # Copyright (C) 2004 Jörg Tiedemann <joerg@stp.ling.uu.se> | ||
| 2 | # | ||
| 3 | # This program is free software; you can redistribute it and/or modify | ||
| 4 | # it under the terms of the GNU General Public License as published by | ||
| 5 | # the Free Software Foundation; either version 2 of the License, or | ||
| 6 | # (at your option) any later version. | ||
| 7 | # | ||
| 8 | # This program is distributed in the hope that it will be useful, | ||
| 9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 11 | # GNU General Public License for more details. | ||
| 12 | # | ||
| 13 | # You should have received a copy of the GNU General Public License | ||
| 14 | # along with this program; if not, write to the Free Software | ||
| 15 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
| 16 | |||
| 17 | ##################################################################### | ||
| 18 | # | ||
| 19 | # Uplug::IO::PlugXML - bitext in PLUG XML | ||
| 20 | # | ||
| 21 | ##################################################################### | ||
| 22 | # $Author$ | ||
| 23 | # $Id$ | ||
| 24 | |||
| 25 | package Uplug::IO::PlugXML; | ||
| 26 | |||
| 27 | use strict; | ||
| 28 | use vars qw(@ISA); | ||
| 29 | use vars qw(%StreamOptions); | ||
| 30 | use Uplug::IO::XML; | ||
| 31 | use Uplug::Data::Lang; | ||
| 32 | # use Uplug::IO::Any; | ||
| 33 | |||
| 34 | @ISA = qw( Uplug::IO::XML ); | ||
| 35 | |||
| 36 | # stream options and their default values!! | ||
| 37 | |||
| 38 | %StreamOptions = ('DocRootTag' => 'PLUG', | ||
| 39 | 'DocHeaderTag' => 'header', | ||
| 40 | 'DocBodyTag' => 'corpus', | ||
| 41 | 'root' => 'align', | ||
| 42 | 'InternalEncoding' => 'iso-8859-1', | ||
| 43 | 'DTDname' => 'PLUG', | ||
| 44 | 'DTDsystemID' => 'plugXML.dtd', | ||
| 45 | 'DefaultEncoding' => 'iso-8859-1', | ||
| 46 | |||
| 47 | # 'encoding' => 'iso-8859-1', | ||
| 48 | # 'DataStructure' => 'complex', | ||
| 49 | # 'encoding' => $Uplug::IO::DEFAULTENCODING, | ||
| 50 | # 'DTDpublicID' => '...' | ||
| 51 | ); | ||
| 52 | $StreamOptions{DocRoot}{version}='1.0'; | ||
| 53 | |||
| 54 | sub new{ | ||
| 55 | my $class=shift; | ||
| 56 | my $self=$class->SUPER::new($class); | ||
| 57 | &Uplug::IO::AddHash2Hash($self->{StreamOptions},\%StreamOptions); | ||
| 58 | return $self; | ||
| 59 | } | ||
| 60 | |||
| 61 | sub read{ | ||
| 62 | my $self=shift; | ||
| 63 | my ($data)=@_; | ||
| 64 | |||
| 65 | if (not ref($data->{source})){$data->{source}=Uplug::Data::Lang->new();} | ||
| 66 | if (not ref($data->{target})){$data->{target}=Uplug::Data::Lang->new();} | ||
| 67 | |||
| 68 | if ($self->SUPER::read($data)){ | ||
| 69 | my @seg=$data->findNodes('seg'); | ||
| 70 | if (defined $seg[0]){ | ||
| 71 | $seg[0]->setLabel('source'); | ||
| 72 | $data->{source}->setRoot($seg[0]); | ||
| 73 | } | ||
| 74 | if (defined $seg[1]){ | ||
| 75 | $seg[1]->setLabel('target'); | ||
| 76 | $data->{target}->setRoot($seg[1]); | ||
| 77 | } | ||
| 78 | return 1; | ||
| 79 | } | ||
| 80 | return 0; | ||
| 81 | } | ||
| 82 | |||
| 83 | sub write{ | ||
| 84 | my $self=shift; | ||
| 85 | my ($data)=@_; | ||
| 86 | $self->Uplug::IO::write($data); | ||
| 87 | my @seg=$data->getNodes('seg'); | ||
| 88 | if (not @seg){ | ||
| 89 | |||
| 90 | my ($source)=$data->getNodes('source'); | ||
| 91 | my ($target)=$data->getNodes('target'); | ||
| 92 | |||
| 93 | if (not ref($source)){return 0;} | ||
| 94 | if (not ref($target)){return 0;} | ||
| 95 | |||
| 96 | my $srcLang=$data->attribute($source,'lang'); | ||
| 97 | my $trgLang=$data->attribute($target,'lang'); | ||
| 98 | if (not $srcLang){$data->setAttribute($source,'lang','src');} | ||
| 99 | if (not $trgLang){$data->setAttribute($target,'lang','trg');} | ||
| 100 | |||
| 101 | $source->setTagName('seg'); | ||
| 102 | $target->setTagName('seg'); | ||
| 103 | |||
| 104 | } | ||
| 105 | |||
| 106 | return $self->SUPER::write($data); | ||
| 107 | } | ||
| 108 | |||
| 109 | |||
| 110 | sub readFromHandle{ | ||
| 111 | my $self=shift; | ||
| 112 | my $txt=$self->SUPER::readFromHandle(@_); | ||
| 113 | if ($txt=~/\<align\s+id\=[^\"\']/){ | ||
| 114 | $txt=~s/(\<align\s+id\=)([^\"\'])/$1\'$2/; | ||
| 115 | } | ||
| 116 | if ($txt=~/\<\?xml/){ | ||
| 117 | if ($txt!~/encoding/){ | ||
| 118 | $txt=~s/\?\>/ encoding=\"iso-8859-1\"\?\>/; | ||
| 119 | } | ||
| 120 | } | ||
| 121 | my @ent=(); | ||
| 122 | while ($txt=~/\&([^\s\;]+)\;/g){ | ||
| 123 | if ($1 eq 'lt'){next;} | ||
| 124 | if ($1 eq 'gt'){next;} | ||
| 125 | if ($1 eq 'amp'){next;} | ||
| 126 | push (@ent,$1); | ||
| 127 | } | ||
| 128 | foreach (@ent){ | ||
| 129 | print STDERR "replace \&$_\; with \[$_\]!\n"; | ||
| 130 | $txt=~s/\&$_\;/ [$_] /gs; | ||
| 131 | } | ||
| 132 | return $txt; | ||
| 133 | } |