| 1 | ## -*- Mode: CPerl -*- | ||
| 2 | ## | ||
| 3 | ## File: DTA::CAB::Format::TEI.pm | ||
| 4 | ## Author: Bryan Jurish <moocow@cpan.org> | ||
| 5 | ## Description: Datum parser|formatter: XML: TEI (with or without //c elements), using DTA::TokWrap | ||
| 6 | ## + uses DTA::CAB::Format::XmlTokWrap for output | ||
| 7 | |||
| 8 | package DTA::CAB::Format::TEI; | ||
| 9 | use DTA::CAB::Format::XmlTokWrap; | ||
| 10 | use DTA::CAB::Datum ':all'; | ||
| 11 | use DTA::CAB::Utils ':temp'; | ||
| 12 | use DTA::TokWrap; | ||
| 13 | use DTA::TokWrap::Utils qw(); | ||
| 14 | use File::Path; | ||
| 15 | use XML::LibXML; | ||
| 16 | use IO::File; | ||
| 17 | #use File::Copy qw(); | ||
| 18 | use Carp; | ||
| 19 | use strict; | ||
| 20 | |||
| 21 | ##============================================================================== | ||
| 22 | ## Globals | ||
| 23 | ##============================================================================== | ||
| 24 | |||
| 25 | our @ISA = qw(DTA::CAB::Format::XmlTokWrap); | ||
| 26 | |||
| 27 | BEGIN { | ||
| 28 | DTA::CAB::Format->registerFormat(name=>__PACKAGE__, filenameRegex=>qr/\.(?i:(?:c|chr|txt|tei(?:[\.\-_]?p[45])?)[\.\-_]xml|xml)$/); | ||
| 29 | DTA::CAB::Format->registerFormat(name=>__PACKAGE__, short=>$_) | ||
| 30 | foreach (qw(chr-xml c-xml cxml tei-xml teixml tei xml)); | ||
| 31 | |||
| 32 | DTA::CAB::Format->registerFormat(name=>__PACKAGE__, short=>$_, opts=>{txmlfmt=>'DTA::CAB::Format::XmlTokWrapFast'}) | ||
| 33 | foreach (qw(tei-fast tei+fast fast-tei-xml ftei-xml fteixml ftei)); | ||
| 34 | DTA::CAB::Format->registerFormat(name=>__PACKAGE__, short=>$_, opts=>{'att.linguistic'=>1}) | ||
| 35 | foreach (qw(tei-ling tei+ling ling-tei-xml ltei-xml lteixml ltei teiling)); | ||
| 36 | } | ||
| 37 | |||
| 38 | BEGIN { | ||
| 39 | *isa = \&UNIVERSAL::isa; | ||
| 40 | *can = \&UNIVERSAL::can; | ||
| 41 | } | ||
| 42 | |||
| 43 | ##-- HACK for broken tokenizer on services.dwds.de (2011-07-27) | ||
| 44 | #$DTA::TokWrap::Document::TOKENIZE_CLASS = 'http'; | ||
| 45 | $DTA::TokWrap::Document::TOKENIZE_CLASS = 'auto'; ##-- fixed (?) 2013-06-21; changed 2015-11-12 | ||
| 46 | |||
| 47 | ##-- default parser/formatter for *.t.xml files | ||
| 48 | our $TXML_CLASS_DEFAULT = 'DTA::CAB::Format::XmlTokWrap'; | ||
| 49 | #our $TXML_CLASS_DEFAULT = 'DTA::CAB::Format::XmlTokWrapFast'; ##-- ca. 2x faster, but doesn't support all data-keys | ||
| 50 | |||
| 51 | ##============================================================================== | ||
| 52 | ## Constructors etc. | ||
| 53 | ##============================================================================== | ||
| 54 | |||
| 55 | ## $fmt = CLASS_OR_OBJ->new(%args) | ||
| 56 | ## + object structure: HASH ref | ||
| 57 | ## { | ||
| 58 | ## ##-- new in TEI | ||
| 59 | ## tmpdir => $dir, ##-- temporary directory for this object (default: new) | ||
| 60 | ## keeptmp => $bool, ##-- keep temporary directory open | ||
| 61 | ## addc => $bool_or_guess, ##-- (input) whether to add //c elements (slow no-op if already present; default=0) | ||
| 62 | ## spliceback => $bool, ##-- (output) if true (default), return .cws.cab.xml ; otherwise just .cab.t.xml [requires doc 'teibufr' attribute] | ||
| 63 | ## keeptext => $bool, ##-- (input) if true (default), include 'textbufr' element for extract TEI text | ||
| 64 | ## keepc => $bool, ##-- (output) whether to include //c elements in spliceback-mode output (default=0) | ||
| 65 | ## tw => $tw, ##-- underlying DTA::TokWrap object | ||
| 66 | ## twopen => \%opts, ##-- options for $tw->open() | ||
| 67 | ## teibufr => \$buf, ##-- raw tei+c buffer, for spliceback mode | ||
| 68 | ## textbufr => \$buf, ##-- raw text buffer, for keeptext mode | ||
| 69 | ## | ||
| 70 | ## txmlfmt => $fmt, ##-- classname or object for parsing tokwrap *.t.xml files (default: DTA::CAB::Format::TokWrap) | ||
| 71 | ## txmlopts => \%opts, ##-- options for *.t.xml sub-formatter (clobbers %$fmt options) | ||
| 72 | ## att.linguistic => $bool, ##-- use att.linguistic features (forces txmlfmt, txmlopts, twopts) | ||
| 73 | ## | ||
| 74 | ## ##-- input: inherited from XmlNative | ||
| 75 | ## xdoc => $xdoc, ##-- XML::LibXML::Document | ||
| 76 | ## xprs => $xprs, ##-- XML::LibXML parser | ||
| 77 | ## | ||
| 78 | ## ##-- output: new | ||
| 79 | ## #outfile => $filename, ##-- final output file (flushed with File::Copy::copy) | ||
| 80 | ## | ||
| 81 | ## ##-- output: inherited from XmlTokWrap | ||
| 82 | ## arrayEltKeys => \%akey2ekey, ##-- maps array keys to element keys for output | ||
| 83 | ## arrayImplicitKeys => \%akey2undef, ##-- pseudo-hash of array keys NOT mapped to explicit elements | ||
| 84 | ## key2xml => \%key2xml, ##-- maps keys to XML-safe names | ||
| 85 | ## xml2key => \%xml2key, ##-- maps xml keys to internal keys | ||
| 86 | ## ## | ||
| 87 | ## ##-- output: inherited from XmlNative | ||
| 88 | ## #encoding => $inputEncoding, ##-- default: UTF-8; applies to output only! | ||
| 89 | ## level => $level, ##-- output formatting level (default=0) | ||
| 90 | ## | ||
| 91 | ## ##-- common: safety | ||
| 92 | ## safe => $bool, ##-- if true (default), no "unsafe" token data will be generated (_xmlnod,etc.) | ||
| 93 | ## } | ||
| 94 | sub new { | ||
| 95 | my $that = shift; | ||
| 96 | my $fmt = $that->SUPER::new( | ||
| 97 | ##-- local | ||
| 98 | tmpdir => undef, ##-- see tmpdir() method | ||
| 99 | keeptmp=>0, | ||
| 100 | teilog => 'off', ##-- tei format debug log level | ||
| 101 | twlog => 'off', ##-- DTA::TokWrap debug log level | ||
| 102 | ## | ||
| 103 | addc => 0, | ||
| 104 | keepc => 0, | ||
| 105 | spliceback => 1, | ||
| 106 | keeptext => 1, | ||
| 107 | ## | ||
| 108 | txmlfmt => $TXML_CLASS_DEFAULT, | ||
| 109 | #txmlopts => {}, | ||
| 110 | 'att.linguistic' => 0, ##-- use att.linguistic features (alters txmlfmt, txmlopts, twopts) | ||
| 111 | |||
| 112 | ##-- tokwrap | ||
| 113 | tw => undef, ##-- see tw() method | ||
| 114 | twopts => { | ||
| 115 | procOpts=>{ | ||
| 116 | soIgnoreAttrs=>[qw(c xb)], | ||
| 117 | spliceInfo=>'off', | ||
| 118 | addwsInfo=>'off', | ||
| 119 | wExtAttrs=>'^(?:t|b|mapclass)', | ||
| 120 | }, | ||
| 121 | }, | ||
| 122 | twopen => {}, | ||
| 123 | twTokenizeClass => $DTA::TokWrap::Document::TOKENIZE_CLASS, | ||
| 124 | |||
| 125 | ##-- overrides (XmlTokWrap, XmlNative, XmlCommon) | ||
| 126 | ignoreKeys => { | ||
| 127 | teibufr=>undef, | ||
| 128 | textbufr=>undef, | ||
| 129 | }, | ||
| 130 | |||
| 131 | ##-- user args | ||
| 132 | @_ | ||
| 133 | ); | ||
| 134 | |||
| 135 | if ($fmt->{twlog}) { | ||
| 136 | ##-- set DTA::TokWrap logging options (also consider specifying '-lo=twLevel=TRACE' on the command-line) | ||
| 137 | $fmt->{twopen}{"trace$_"} = $fmt->{twlog} foreach (qw(Proc Open Close Load Gen Subproc Run)); | ||
| 138 | $fmt->{twopts}{procOpts}{$_} = $fmt->{twlog} foreach (qw(traceLevel)); | ||
| 139 | $DTA::TokWrap::Utils::TRACE_RUNCMD = 'debug'; | ||
| 140 | $fmt->{twopts}{$_} = uc($fmt->{twlog}) foreach (qw(addwsInfo spliceInfo)); | ||
| 141 | } | ||
| 142 | if (0) { | ||
| 143 | ##-- DEBUG: also consider setting $DTA::CAB::Logger::defaultLogOpts{twLevel}='TRACE', e.g. with '-lo twLevel=TRACE' on the command-line | ||
| 144 | $fmt->{tmpdir} = "cab_tei_tmp"; | ||
| 145 | $fmt->{keeptmp} = 1; | ||
| 146 | } | ||
| 147 | |||
| 148 | if ($fmt->{'att.linguistic'}) { | ||
| 149 | $fmt->{txmlfmt} = 'DTA::CAB::Format::XmlLing'; | ||
| 150 | $fmt->{txmlopts}{twcompat} = 1; | ||
| 151 | $fmt->{twopts}{procOpts}{wIdAttr} = 'xml:id'; | ||
| 152 | $fmt->{twopts}{procOpts}{sIdAttr} = 'xml:id'; | ||
| 153 | $fmt->{twopts}{procOpts}{wExtAttrs} = '^(?:lemma|pos|norm|join)='; | ||
| 154 | $fmt->{twopts}{procOpts}{sExtAttrs} = '^$'; | ||
| 155 | } | ||
| 156 | |||
| 157 | ##-- tmpdir: see tmpdir() method | ||
| 158 | |||
| 159 | ##-- tw: TokWrap object : depends on tmpdir(): see tw() method | ||
| 160 | |||
| 161 | return $fmt; | ||
| 162 | } | ||
| 163 | |||
| 164 | ## $fmt->DESTROY() | ||
| 165 | ## + destructor implicitly calls $fmt->rmtmpdir() | ||
| 166 | sub DESTROY { | ||
| 167 | my $fmt = shift; | ||
| 168 | $fmt->rmtmpdir(); | ||
| 169 | $fmt->SUPER::DESTROY(); | ||
| 170 | } | ||
| 171 | |||
| 172 | |||
| 173 | ##============================================================================= | ||
| 174 | ## Methods: Generic | ||
| 175 | |||
| 176 | ## $dir = $fmt->tmpdir() | ||
| 177 | ## + get/generate name of temporary directory, ensures $fmt->{tmpdir} is set | ||
| 178 | sub tmpdir { | ||
| 179 | return $_[0]{tmpdir} if (defined($_[0]{tmpdir})); | ||
| 180 | return $_[0]{tmpdir} = mktmpfsdir("cab_tei_${$}_XXXX", CLEAN=>(!$_[0]{keeptmp})) | ||
| 181 | } | ||
| 182 | |||
| 183 | ## $tmpdir = $fmt->mktmpdir() | ||
| 184 | ## + ensures $fmt->tmpdir() exists | ||
| 185 | sub mktmpdir { | ||
| 186 | my $fmt = shift; | ||
| 187 | my $tmpdir = $fmt->tmpdir(); | ||
| 188 | $fmt->vlog($fmt->{teilog}, "mktmpdir $tmpdir"); | ||
| 189 | mkdir($tmpdir,0700) if (!-d $tmpdir); | ||
| 190 | (-d $tmpdir) or $fmt->logconfess("could not create directory '$tmpdir': $!"); | ||
| 191 | return $tmpdir; | ||
| 192 | } | ||
| 193 | |||
| 194 | ## $fmt = $fmt->rmtmpdir() | ||
| 195 | ## + removes $fmt->{tmpdir} unless $fmt->{keeptmp} is true | ||
| 196 | sub rmtmpdir { | ||
| 197 | my $fmt = shift; | ||
| 198 | if (defined($fmt->{tmpdir}) && -d $fmt->{tmpdir} && !$fmt->{keeptmp}) { | ||
| 199 | $fmt->vlog($fmt->{teilog}, "rmtree $fmt->{tmpdir}") if (Log::Log4perl->initialized);; | ||
| 200 | File::Path::rmtree($fmt->{tmpdir}) | ||
| 201 | or $fmt->logconfess("could not rmtree() temp directory '$fmt->{tmpdir}': $!"); | ||
| 202 | } | ||
| 203 | return $fmt; | ||
| 204 | } | ||
| 205 | |||
| 206 | ## $txmlfmt = $fmt->txmlfmt() | ||
| 207 | ## + gets cached $fmt->{txmlfmt} or creates it | ||
| 208 | sub txmlfmt { | ||
| 209 | return $_[0]{txmlfmt} if (ref $_[0]{txmlfmt}); | ||
| 210 | my %txmlopts = (%{$_[0]}, %{$_[0]{txmlopts}||{}}); | ||
| 211 | delete @txmlopts{qw(xdoc xprs txmlfmt fh tmpfh)}; | ||
| 212 | return $_[0]{txmlfmt} = $_[0]->txmlclass->new(%txmlopts); | ||
| 213 | } | ||
| 214 | |||
| 215 | ## $class = $fmt->txmlclass() | ||
| 216 | sub txmlclass { | ||
| 217 | return ref($_[0]{txmlfmt}) if (ref($_[0]{txmlfmt})); | ||
| 218 | return "DTA::CAB::Format::$_[0]{txmlfmt}" if (UNIVERSAL::isa("DTA::CAB::Format::$_[0]{txmlfmt}",'DTA::CAB::Format')); | ||
| 219 | return $DTA::CAB::Format::REG->formatClass($_[0]{txmlfmt}) || $_[0]{txmlfmt} || $TXML_CLASS_DEFAULT; | ||
| 220 | } | ||
| 221 | |||
| 222 | ## $tw = $fmt->tw() | ||
| 223 | ## + returns DTA::TokWrap object for $fmt | ||
| 224 | ## + calls $fmt->tmpdir() | ||
| 225 | sub tw { | ||
| 226 | return $_[0]{tw} if (defined($_[0]{tw})); | ||
| 227 | my $tw = $_[0]{tw} = DTA::TokWrap->new(%{$_[0]{twopts}||{}}, tokenizeClass=>$_[0]{twTokenizeClass}); | ||
| 228 | $tw->{keeptmp} = $_[0]{keeptmp}; | ||
| 229 | $tw->{tmpdir} = $tw->{outdir} = $_[0]->tmpdir(); | ||
| 230 | $tw->init(); | ||
| 231 | |||
| 232 | return $tw; | ||
| 233 | } | ||
| 234 | |||
| 235 | ##============================================================================= | ||
| 236 | ## Methods: Input | ||
| 237 | ##============================================================================== | ||
| 238 | |||
| 239 | ##-------------------------------------------------------------- | ||
| 240 | ## Methods: Input: Generic API | ||
| 241 | |||
| 242 | ## $fmt = $fmt->close() | ||
| 243 | ## + close current input source, if any | ||
| 244 | sub close { | ||
| 245 | my $fmt = shift; | ||
| 246 | $fmt->{twdoc}->close() if ($fmt->{twdoc}); | ||
| 247 | $fmt->{txmlfmt}->close(@_) if (ref($fmt->{txmlfmt})); | ||
| 248 | delete @$fmt{qw(teibufr textbufr)}; | ||
| 249 | $fmt->rmtmpdir(); | ||
| 250 | return $fmt->SUPER::close(@_); | ||
| 251 | } | ||
| 252 | |||
| 253 | ## $fmt = $fmt->fromString(\$string) | ||
| 254 | ## + select input from string $string | ||
| 255 | sub fromString { | ||
| 256 | my $fmt = shift; | ||
| 257 | my $str = ref($_[0]) ? $_[0] : \$_[0]; | ||
| 258 | $fmt->close(); | ||
| 259 | |||
| 260 | ##-- ensure tmpdir exists | ||
| 261 | $fmt->vlog($fmt->{teilog}, "fromString()"); | ||
| 262 | my $tmpdir = $fmt->mktmpdir; | ||
| 263 | |||
| 264 | ##-- prepare tei buffer with //c elements | ||
| 265 | utf8::encode($$str) if (utf8::is_utf8($$str)); | ||
| 266 | |||
| 267 | if (!$fmt->{addc}) { | ||
| 268 | ##-- dump document with predefined //c elements, or rely on dta-tokwrap >= v0.38 to handle both //c and text() | ||
| 269 | $fmt->vlog($fmt->{teilog}, "write $tmpdir/tmp.chr.xml"); | ||
| 270 | DTA::TokWrap::Utils::ref2file($str,"$tmpdir/tmp.chr.xml") | ||
| 271 | or $fmt->logdie("couldn't create temporary file $tmpdir/tmp.chr.xml: $!"); | ||
| 272 | $fmt->{teibufr} = $str if ($fmt->{spliceback}); | ||
| 273 | } | ||
| 274 | else { | ||
| 275 | ##-- dump raw document | ||
| 276 | $fmt->vlog($fmt->{teilog}, "add-c: write $tmpdir/tmp.raw.xml"); | ||
| 277 | DTA::TokWrap::Utils::ref2file($str,"$tmpdir/tmp.raw.xml") | ||
| 278 | or $fmt->logdie("couldn't create temporary file $tmpdir/tmp.raw.xml: $!"); | ||
| 279 | |||
| 280 | ##-- ensure //c elements | ||
| 281 | $fmt->vlog($fmt->{teilog}, "add-c: dtatw-add-c.perl"); | ||
| 282 | my $addc_args = '-rmns '.($fmt->{addc} eq 'guess' ? '-guess' : '-noguess'); | ||
| 283 | DTA::TokWrap::Utils::runcmd("dtatw-add-c.perl $addc_args $tmpdir/tmp.raw.xml > $tmpdir/tmp.chr.xml")==0 | ||
| 284 | or $fmt->logdie("dtatw-add-c.perl failed: $!"); | ||
| 285 | |||
| 286 | ##-- grab tei buffer | ||
| 287 | $fmt->vlog($fmt->{teilog}, "add-c: slurp tmp.chr.xml"); | ||
| 288 | $fmt->{teibufr} = DTA::TokWrap::Utils::slurp_file("$tmpdir/tmp.chr.xml") | ||
| 289 | if ($fmt->{spliceback}); | ||
| 290 | } | ||
| 291 | |||
| 292 | ##-- run tokwrap | ||
| 293 | $fmt->vlog($fmt->{teilog}, "tokwrap: tmp.chr.xml -> tmp.chr.t.xml"); | ||
| 294 | my $twdoc = $fmt->tw->open("$tmpdir/tmp.chr.xml",%{$fmt->{twopen}||{}}) | ||
| 295 | or $fmt->logdie("could not open $tmpdir/tmp.chr.xml as TokWrap document: $!"); | ||
| 296 | $twdoc->genKey('tei2txml') | ||
| 297 | or $fmt->logdie("could generate $tmpdir/tmp.chr.t.xml with DTA::TokWrap: $!"); | ||
| 298 | if ($fmt->{keeptext}) { | ||
| 299 | $fmt->vlog($fmt->{teilog}, "keeptext: slurp textbufr < $tmpdir/tmp.chr.txt"); | ||
| 300 | $fmt->{textbufr} = DTA::TokWrap::Utils::slurp_file("$tmpdir/tmp.chr.txt"); | ||
| 301 | } | ||
| 302 | $twdoc->close(); | ||
| 303 | |||
| 304 | ##-- now process the tokwrap document | ||
| 305 | $fmt->vlog($fmt->{teilog}, $fmt->txmlclass()."->fromFile(tmp.chr.t.xml)"); | ||
| 306 | $fmt->{txmlfmt}->close(@_) if (ref $fmt->{txmlfmt}); | ||
| 307 | my $rc = $fmt->txmlfmt->fromFile("$tmpdir/tmp.chr.t.xml"); | ||
| 308 | |||
| 309 | ##-- ... and cleanup | ||
| 310 | $fmt->rmtmpdir(); | ||
| 311 | |||
| 312 | $fmt->vlog($fmt->{teilog}, "fromString(): returning"); | ||
| 313 | return $rc ? $fmt : undef; | ||
| 314 | } | ||
| 315 | |||
| 316 | |||
| 317 | ## $fmt = $fmt->fromFile($filename_or_handle) | ||
| 318 | ## + calls $fmt->fromFh() | ||
| 319 | sub fromFile { | ||
| 320 | return $_[0]->DTA::CAB::Format::fromFile(@_[1..$#_]); | ||
| 321 | } | ||
| 322 | |||
| 323 | ## $fmt = $fmt->fromFh($handle) | ||
| 324 | ## + just calls $fmt->fromString() | ||
| 325 | sub fromFh { | ||
| 326 | return $_[0]->DTA::CAB::Format::fromFh_str(@_[1..$#_]); | ||
| 327 | } | ||
| 328 | |||
| 329 | ## $doc = $fmt->parseDocument() | ||
| 330 | ## + parses buffered XML::LibXML::Document | ||
| 331 | ## + override inserts $doc->{teibufr}, $doc->{textbufr} attributes for spliceback mode | ||
| 332 | sub parseDocument { | ||
| 333 | my $fmt = shift; | ||
| 334 | $fmt->vlog($fmt->{teilog}, "parseDocument()"); | ||
| 335 | my $doc = $fmt->txmlfmt->parseDocument(@_) or return undef; | ||
| 336 | $doc->{teibufr} = $fmt->{teibufr} if ($fmt->{spliceback}); | ||
| 337 | $doc->{textbufr} = $fmt->{textbufr} if ($fmt->{keeptext}); | ||
| 338 | return $doc; | ||
| 339 | } | ||
| 340 | |||
| 341 | ##============================================================================= | ||
| 342 | ## Methods: Output | ||
| 343 | ##============================================================================== | ||
| 344 | |||
| 345 | ##-------------------------------------------------------------- | ||
| 346 | ## Methods: Output: MIME & HTTP stuff | ||
| 347 | |||
| 348 | ## $short = $fmt->shortName() | ||
| 349 | ## + returns "official" short name for this format | ||
| 350 | ## + default just returns package suffix | ||
| 351 | sub shortName { | ||
| 352 | return 'tei'; | ||
| 353 | } | ||
| 354 | |||
| 355 | ## $ext = $fmt->defaultExtension() | ||
| 356 | ## + returns default filename extension for this format (default='.cab') | ||
| 357 | sub defaultExtension { return '.tei.xml'; } | ||
| 358 | |||
| 359 | ##-------------------------------------------------------------- | ||
| 360 | ## Methods: Output: output selection | ||
| 361 | |||
| 362 | ## $fmt = $fmt->flush() | ||
| 363 | ## + flush any buffered output to selected output source | ||
| 364 | ## + override calls $fmt->buf2fh(\$fmt->{outbuf}, $fmt->{fh}) | ||
| 365 | sub flush { | ||
| 366 | my $fmt = shift; | ||
| 367 | $fmt->vlog($fmt->{teilog}, "flush()") if (Log::Log4perl->initialized); | ||
| 368 | #File::Copy::copy($fmt->{outfile},$fmt->{fh}) if (defined($fmt->{outfile}) && defined($fmt->{fh})); | ||
| 369 | $fmt->buf2fh(\$fmt->{outbuf}, $fmt->{fh}) | ||
| 370 | if (defined($fmt->{outbuf}) && defined($fmt->{fh}) && $fmt->{fh}->opened); | ||
| 371 | #$fmt->SUPER::flush(@_); ##-- not here, since this writes literal {xdoc} to the output file! | ||
| 372 | delete @$fmt{qw(outfile outbuf xdoc)}; | ||
| 373 | return $fmt; | ||
| 374 | } | ||
| 375 | |||
| 376 | ## $fmt = $fmt->toString(\$str) | ||
| 377 | ## $fmt = $fmt->toString(\$str,$formatLevel) | ||
| 378 | ## + select output to byte-string | ||
| 379 | ## + override reverts to DTA::CAB::Format::toString() | ||
| 380 | sub toString { | ||
| 381 | return $_[0]->DTA::CAB::Format::toString(@_[1..$#_]); | ||
| 382 | } | ||
| 383 | |||
| 384 | ## $fmt_or_undef = $fmt->toFile($filename, $formatLevel) | ||
| 385 | ## + select output to $filename | ||
| 386 | ## + override reverts to DTA::CAB::Format::toFile() | ||
| 387 | sub toFile { | ||
| 388 | return $_[0]->DTA::CAB::Format::toFile(@_[1..$#_]); | ||
| 389 | } | ||
| 390 | |||
| 391 | ## $fmt_or_undef = $fmt->toFh($fh,$formatLevel) | ||
| 392 | ## + select output to filehandle $fh | ||
| 393 | ## + override reverts to DTA::CAB::Format::toFh() | ||
| 394 | sub toFh { | ||
| 395 | return $_[0]->DTA::CAB::Format::toFh(@_[1..$#_]); | ||
| 396 | } | ||
| 397 | |||
| 398 | ##-------------------------------------------------------------- | ||
| 399 | ## Methods: Output: Generic API | ||
| 400 | |||
| 401 | ## $fmt = $fmt->putDocument($doc) | ||
| 402 | ## + override respects local 'keepc' and 'spliceback' flags | ||
| 403 | sub putDocument { | ||
| 404 | my ($fmt,$doc) = @_; | ||
| 405 | |||
| 406 | ##-- get original TEI-XML buffer | ||
| 407 | my $teibufr = $fmt->{spliceback} ? ($doc->{teibufr} || $fmt->{teibufr}) : undef; | ||
| 408 | if (!defined($teibufr) || !$$teibufr) { | ||
| 409 | $fmt->logwarn("spliceback mode requested but no 'teibufr' document property - using XmlTokWrap format"); | ||
| 410 | $fmt->vlog($fmt->{teilog}, $fmt->txmlclass."->putDocument()"); | ||
| 411 | return $fmt->txmlfmt->toString(\$fmt->{outbuf})->putDocument($doc)->flush(); | ||
| 412 | } | ||
| 413 | |||
| 414 | ##-- get temp directory | ||
| 415 | my $tmpdir = $fmt->mktmpdir(); | ||
| 416 | |||
| 417 | ##-- dump base data | ||
| 418 | $fmt->vlog($fmt->{teilog}, "putDocument(): write $tmpdir/tmp.tei.xml"); | ||
| 419 | DTA::TokWrap::Utils::ref2file($teibufr,"$tmpdir/tmp.tei.xml") | ||
| 420 | or $fmt->logconfess("couldn't create temporary file $tmpdir/tmp.tei.xml: $!"); | ||
| 421 | |||
| 422 | ##-- get tokwrap object | ||
| 423 | my $twdoc = $fmt->tw->open("$tmpdir/tmp.tei.xml",%{$fmt->{twopen}||{}}) | ||
| 424 | or $fmt->logdie("could not open $tmpdir/tmp.tei.xml as TokWrap document: $!"); | ||
| 425 | $twdoc->{xmldata} = $$teibufr; | ||
| 426 | $twdoc->{xtokfile} = "$tmpdir/tmp.cab.t.xml"; | ||
| 427 | |||
| 428 | ##-- dump underlying txml data | ||
| 429 | my ($rc); | ||
| 430 | $fmt->vlog($fmt->{teilog}, "putDocument(): underlying ".$fmt->txmlclass."->putDocument()"); | ||
| 431 | if ($fmt->txmlfmt->can('putDocument') eq DTA::CAB::Format::XmlNative->can('putDocument')) { | ||
| 432 | ##-- XmlNative-style putDocument uses underlying libxml xdoc | ||
| 433 | $rc = $fmt->txmlfmt->putDocument($doc); | ||
| 434 | $twdoc->{xtokdata} = $fmt->txmlfmt->{xdoc}->toString(0); | ||
| 435 | } else { | ||
| 436 | ##-- XmlTokWrapFast-style putDocument to buffer | ||
| 437 | $rc = $fmt->txmlfmt->toString(\$twdoc->{xtokdata}, 0)->putDocument($doc)->flush; | ||
| 438 | } | ||
| 439 | return $rc if (!$rc); | ||
| 440 | |||
| 441 | $fmt->vlog($fmt->{teilog}, "putDocument(): splice to $tmpdir/tmp.tei.cws.xml"); | ||
| 442 | $twdoc->{cwsfile} = "$tmpdir/tmp.tei.cws.xml"; | ||
| 443 | #$twdoc->{cwstfile} = "$tmpdir/tmp.tei.cwst.xml"; | ||
| 444 | #$twdoc->{cwstbufr} = \$fmt->{outbuf}; ##-- output to string | ||
| 445 | $twdoc->saveXtokFile() | ||
| 446 | or $fmt->logconfess("could not save intermediate cab.t.xml file: $!"); | ||
| 447 | $twdoc->genKey('addws') | ||
| 448 | or $fmt->logconfess("could not generate intermediate cws xml data: $!"); | ||
| 449 | |||
| 450 | ##-- optionally remove //c elements | ||
| 451 | $fmt->vlog($fmt->{teilog}, "putDocument(): remove //c -> $tmpdir/tmp.tei.cws.noc.xml"); | ||
| 452 | my $cwsfile = "$tmpdir/tmp.tei.cws.xml"; | ||
| 453 | if (!$fmt->{keepc}) { | ||
| 454 | DTA::TokWrap::Utils::runcmd("dtatw-rm-c.perl $cwsfile > $tmpdir/tmp.tei.cws.noc.xml")==0 | ||
| 455 | or $fmt->logdie("dtatw-rm-c.perl failed: $!"); | ||
| 456 | $cwsfile = "$tmpdir/tmp.tei.cws.noc.xml"; | ||
| 457 | } | ||
| 458 | |||
| 459 | ##-- splice in cab analysis data (should already be there in tokwrap >= v0.37) | ||
| 460 | #$twdoc->genKey('idsplice'); | ||
| 461 | |||
| 462 | ##-- slurp the buffer back in | ||
| 463 | $fmt->vlog($fmt->{teilog}, "putDocument(): re-slurp from $cwsfile"); | ||
| 464 | DTA::TokWrap::Utils::slurp_file("$cwsfile",\$fmt->{outbuf}) | ||
| 465 | or $fmt->logdie("slurp_file() failed for '$cwsfile': $!"); | ||
| 466 | utf8::decode($fmt->{outbuf}) if (!utf8::is_utf8($fmt->{outbuf})); ##-- decode UTF-8 (hack) | ||
| 467 | $fmt->{outbuf} =~ s|(<[^>]*)\sXMLNS=|$1 xmlns=|g; ##-- decode default namespaces (hack) | ||
| 468 | |||
| 469 | ##-- cleanup | ||
| 470 | $twdoc->close(); | ||
| 471 | $fmt->rmtmpdir(); | ||
| 472 | |||
| 473 | $fmt->vlog($fmt->{teilog}, "putDocument(): returning"); | ||
| 474 | return $fmt; | ||
| 475 | } | ||
| 476 | |||
| 477 | |||
| 478 | |||
| 479 | |||
| 480 | |||
| 481 | 1; ##-- be happy | ||
| 482 | |||
| 483 | __END__ |