/usr/bin/pmlupgrade is in photoml 0.28-0ubuntu1.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
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 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 | #! /usr/bin/perl
# This script upgrades PhotoML files to the most recent DTD versions
# Copyright © 2003-2010 Brendt Wohlberg <photoml@wohlberg.net>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of version 2 of the GNU General Public License at
# http://www.gnu.org/licenses/gpl-2.0.txt.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
# Most recent modification: 22 October 2010
use strict;
use File::Basename;
use File::Copy;
use Getopt::Std;
# Set up path variables
my $pmlpath="/usr/share/photoml";
my $xsl = "$pmlpath/xsl/upgrade";
my $tmp = '/tmp';
if (-r '/etc/xml/catalog' and $ENV{'XML_CATALOG_FILES'} eq '') {
$ENV{'XML_CATALOG_FILES'} = "/etc/xml/catalog";
}
$ENV{'XML_CATALOG_FILES'} = "$pmlpath/dtd/catalog.xml " .
$ENV{'XML_CATALOG_FILES'};
undef $ENV{'SGML_CATALOG_FILES'};
# Ensure xsltproc and xmllint are available
die "pmlupgrade: error executing xstlproc\n"
if (`which xsltproc 2>/dev/null` eq '');
die "pmlupgrade: error executing xmllint\n"
if (`which xmllint 2>/dev/null` eq '');
my $ustr = <<EOF;
usage: pmlupgrade [-h] infile [infile] ...
-h Display usage information
-n Preserve numeric character references
EOF
my $opts = {};
getopts('hn', $opts);
if ($opts->{'h'}) { print STDERR "$ustr"; exit(0); }
die "$ustr" if (@ARGV == 0);
# Declare variables used within main loop
my $nerr = 0;
my ($x, $tmp0, $tmp1, $tflag, $trans, $cmd, $dtd, $root, $path, $sra);
my ($dtdcl, $dtpub, $dttyp, $dtver, $dtint);
# Work through each file specified on the command line
foreach $x ( @ARGV ) {
# Ensure current file is readable
if (! -r $x) {
warn "pmlupgrade: could not read file $x\n";
$nerr++;
next;
}
# Determine document type information
$dtdcl = getdoctyp($x);
$dtpub = getdtpub($dtdcl);
($dttyp,$dtver) = getpubtypver($dtpub);
$dtint = getdtint($dtdcl);
# Determine the relevant DTD and skip to next file if not recognised
if ($dttyp eq "PhotoML") {
$dtd = 'photo';
$root = 'photo';
} elsif ($dttyp eq "DigImageML") {
$dtd = 'digim';
$root = 'digimage';
} else {
warn "pmlupgrade: unrecognised document type for file $x\n";
$nerr++;
$dtd = '';
next;
}
warn "Upgrading $x\n";
$path = dirname($x);
# Flag indicating whether current file has been transformed
$tflag = 0;
# Find the relevant XSL upgrade transform
$trans = `find $xsl -name "$dtd-$dtver-*.xsl"`;
chomp($trans);
# Temporary file names
$tmp0 = "$tmp/upgrade.$$.0.xml";
$tmp1 = "$tmp/upgrade.$$.1.xml";
# If DTD is one of the versions with ISOlat1 and ISOlat2 character
# entity references, convert them to the corresponding numeric
# character references
if ($dtver <= "0.11") {
substncr($x, $tmp0);
} else {
copy($x,$tmp0);
}
$sra = ($opts->{'n'})?"'s/\&/\&/g'":"'s/\\&\\([^#]\\)/\\&\\1/g'";
# Increment version numbers while transforms available
while ($trans ne '') {
# Preprocessing to prevent namespace errors (since the DTD, in
# which the namespace prefixes are defined, is not loaded), and to
# prevent parsing and replacement of entity definitions.
$cmd = "sed -e 's|<$root>|<$root xmlns:xlink=\"http://www.w3.org".
"/1999/xlink\">|g' -e $sra $tmp0 > $tmp1";
system($cmd);
move($tmp1,$tmp0);
# Remove invalid namespace prefix from merge reject PI when upgrading
# from DTD version 0.12
if ($dtver == "0.12") {
$cmd = "sed -e 's|<\?merge:reject|<\?merge-reject|g' $tmp0 > $tmp1";
system($cmd);
move($tmp1,$tmp0);
}
# Apply the XSL transform
$cmd = "xsltproc --path $path --novalid -o $tmp1 $trans $tmp0";
system($cmd);
move($tmp1,$tmp0);
# In applying the last upgrade for digim files, the root element
# is changed to photo
$root = 'photo' if ($dtd eq 'digim' && $dtver eq '0.02');
# Undo preprocessing changes
$cmd = "sed -e 's|<$root xmlns:xlink=\"http://www.w3.org/1999/".
"xlink\">|<$root>|g' -e 's/\&/\\&/g' $tmp0 > $tmp1";
system($cmd);
move($tmp1,$tmp0);
# Find the next relevant XSL transform
if ($trans =~ /-([\d\.]+)\.xsl$/) {
$dtver = $1;
} else {
die "pmlupgrade: error in accessing upgrade XSL transforms\n";
}
$trans = `find $xsl -name "$dtd-$dtver-*.xsl"`;
chomp($trans);
$tflag = 1;
}
# If an upgrade transform has been applied, insert the internal
# subset removed by XSL processing, and validate the result
if ($tflag == 1) {
setdtint($dtint, $tmp0, "$x.tmp");
$cmd = "xmllint --valid --noout $x.tmp 2>/dev/null";
if (!system($cmd)) {
move($x, "$x.bak");
move("$x.tmp",$x);
} else {
move("$x.tmp","$x.fail");
warn "pmlupgrade: validity error in upgrading file $x\n";
$nerr++;
}
}
# Remove temporary files
unlink($tmp0);
unlink($tmp1);
}
exit ($nerr > 0)?1:0;
# Parse strings delimited by (possibly nested) angle brackets
sub abmatch {
my $fh = shift;
my $lt = shift;
my $mt = '';
my $line;
my $n = 1;
while ($n > 0) {
while (($lt !~ /<|>/) and ($line = <$fh>)) {
$lt .= $line;
}
if ($lt =~ /<|>/) {
$mt .= $` . $&;
$lt = $';
$n = ($& eq ">")?($n-1):($n+1);
} else {
return undef;
}
}
return $mt;
}
# Extract the document type declaration from an XML file
sub getdoctyp {
my $fn = shift;
my $line;
open(FH, "< $fn");
my $fh = *FH;
while ($line = <$fh>) {
last if ($line =~ /<!DOCTYPE/);
}
if ($line =~ /<!DOCTYPE/) {
return $& . abmatch($fh, $');
} else {
return undef;
}
}
# Extract the public identifier from a document type declaration
sub getdtpub {
my $dt = shift;
if ($dt =~ /DOCTYPE\s+\w+\s+PUBLIC\s+\"([^\"]+)\"/) {
return $1;
} else {
return undef;
}
}
# Extract the type and version number from a PhotoML/DigImageML file
sub getpubtypver {
my $dtp = shift;
if ($dtp =~ /DTD\s+(\w+)\s+([\d|\.]+)\/\/\w{2}$/) {
return ($1,$2);
} else {
return undef;
}
}
# Extract the internal subset from a document type declaration
sub getdtint {
my $dt = shift;
if ($dt =~ /\[([^\]]+)\]/) {
return $1;
} else {
return undef;
}
}
# Insert an internal subset into the document type declaration in a
# XML file
sub setdtint {
my $iss = shift;
my $fn0 = shift;
my $fn1 = shift;
if ($iss eq '') {
copy($fn0, $fn1);
return 1;
}
local(*FH, $/);
open(FH, "<$fn0") or return undef;
my $ft = <FH>;
close(FH);
$ft =~ s/(<!DOCTYPE\s+[^>]+)>/$1 \[$iss\]>/;
open(FH, ">$fn1") or return undef;
print FH $ft;
close(FH);
return 1;
}
# Replace ISOlat1 and ISOlat2 character entity references with the
# corresponding numeric character references
sub substncr {
my $fn0 = shift;
my $fn1 = shift;
open(IFH, "<$fn0") or return undef;
open(OFH, ">$fn1") or return undef;
my $line;
while ($line = <IFH>) {
$line =~ s/\&([^\;]+)\;/@{[getncr($1)]}/g;
print OFH $line;
}
close(OFH);
close(IFH);
return 1;
}
# Convert ISOlat1 and ISOlat2 character entity references to numeric
# character references, returning other entity references unchanged
sub getncr {
my $cer = shift;
my $ncr = {"Agrave" => "À", "Aacute" => "Á", "Acirc" => "Â",
"Atilde" => "Ã" , "Auml" => "Ä" , "Aring" => "Å" ,
"AElig" => "Æ" , "Ccedil" => "Ç" , "Egrave" => "È" ,
"Eacute" => "É" , "Ecirc" => "Ê" , "Euml" => "Ë" ,
"Igrave" => "Ì" , "Iacute" => "Í" , "Icirc" => "Î" ,
"Iuml" => "Ï" , "ETH" => "Ð" , "Ntilde" => "Ñ" ,
"Ograve" => "Ò" , "Oacute" => "Ó" , "Ocirc" => "Ô" ,
"Otilde" => "Õ" , "Ouml" => "Ö" , "Oslash" => "Ø" ,
"Ugrave" => "Ù" , "Uacute" => "Ú" , "Ucirc" => "Û" ,
"Uuml" => "Ü" , "Yacute" => "Ý" , "THORN" => "Þ" ,
"szlig" => "ß" , "agrave" => "à" , "aacute" => "á" ,
"acirc" => "â" , "atilde" => "ã" , "auml" => "ä" ,
"aring" => "å" , "aelig" => "æ" , "ccedil" => "ç" ,
"egrave" => "è" , "eacute" => "é" , "ecirc" => "ê" ,
"euml" => "ë" , "igrave" => "ì" , "iacute" => "í" ,
"icirc" => "î" , "iuml" => "ï" , "eth" => "ð" ,
"ntilde" => "ñ" , "ograve" => "ò" , "oacute" => "ó" ,
"ocirc" => "ô" , "otilde" => "õ" , "ouml" => "ö" ,
"oslash" => "ø" , "ugrave" => "ù" , "uacute" => "ú" ,
"ucirc" => "û" , "uuml" => "ü" , "yacute" => "ý" ,
"thorn" => "þ" , "yuml" => "ÿ" , "Amacr" => "Ā",
"amacr" => "ā", "Abreve" => "Ă", "abreve" => "ă",
"Aogon" => "Ą", "aogon" => "ą", "Cacute" => "Ć",
"cacute" => "ć", "Ccirc" => "Ĉ", "ccirc" => "ĉ",
"Cdot" => "Ċ", "cdot" => "ċ", "Ccaron" => "Č",
"ccaron" => "č", "Dcaron" => "Ď", "dcaron" => "ď",
"Dstrok" => "Đ", "dstrok" => "đ", "Emacr" => "Ē",
"emacr" => "ē", "Edot" => "Ė", "edot" => "ė",
"Eogon" => "Ę", "eogon" => "ę", "Ecaron" => "Ě",
"ecaron" => "ě", "Gcirc" => "Ĝ", "gcirc" => "ĝ",
"Gbreve" => "Ğ", "gbreve" => "ğ", "Gdot" => "Ġ",
"gdot" => "ġ", "Gcedil" => "Ģ", "Hcirc" => "Ĥ",
"hcirc" => "ĥ", "Hstrok" => "Ħ", "hstrok" => "ħ",
"Itilde" => "Ĩ", "itilde" => "ĩ", "Imacr" => "Ī",
"imacr" => "ī", "Iogon" => "Į", "iogon" => "į",
"Idot" => "İ", "inodot" => "ı", "IJlig" => "IJ",
"ijlig" => "ij", "Jcirc" => "Ĵ", "jcirc" => "ĵ",
"Kcedil" => "Ķ", "kcedil" => "ķ", "kgreen" => "ĸ",
"Lacute" => "Ĺ", "Lmidot" => "Ĺ", "lacute" => "ĺ",
"Lcedil" => "Ļ", "lcedil" => "ļ", "Lcaron" => "Ľ",
"lcaron" => "ľ", "lmidot" => "ŀ", "Lstrok" => "Ł",
"lstrok" => "ł", "Nacute" => "Ń", "nacute" => "ń",
"Ncedil" => "Ņ", "ncedil" => "ņ", "Ncaron" => "Ň",
"ncaron" => "ň", "napos" => "ʼn", "ENG" => "Ŋ",
"eng" => "ŋ", "Omacr" => "Ō", "omacr" => "ō",
"Odblac" => "Ő", "odblac" => "ő", "OElig" => "Œ",
"oelig" => "œ", "Racute" => "Ŕ", "racute" => "ŕ",
"Rcedil" => "Ŗ", "rcedil" => "ŗ", "Rcaron" => "Ř",
"rcaron" => "ř", "Sacute" => "Ś", "sacute" => "ś",
"scirc" => "Ŝ", "Scirc" => "ŝ", "Scedil" => "Ş",
"scedil" => "ş", "Scaron" => "Š", "scaron" => "š",
"tcedil" => "Ţ", "Tcedil" => "ţ", "Tcaron" => "Ť",
"tcaron" => "ť", "Tstrok" => "Ŧ", "tstrok" => "ŧ",
"Utilde" => "Ũ", "utilde" => "ũ", "Umacr" => "Ū",
"umacr" => "ū", "Ubreve" => "Ŭ", "ubreve" => "ŭ",
"Uring" => "Ů", "uring" => "ů", "Udblac" => "Ű",
"udblac" => "ű", "Uogon" => "Ų", "uogon" => "ų",
"Wcirc" => "Ŵ", "wcirc" => "ŵ", "Ycirc" => "Ŷ",
"ycirc" => "ŷ", "Yuml" => "Ÿ", "Zacute" => "Ź",
"zacute" => "ź", "Zdot" => "Ż", "zdot" => "ż",
"Zcaron" => "Ž", "zcaron" => "ž", "gacute" => "ǵ"};
my $val = $ncr->{$cer};
return (defined $val)?$val:"&$cer;";
}
|