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 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
|
#!@PERL_PROGRAM@
# Copyright (C) 2009 Michael Bell <michael.bell@opensync.org>
use strict;
use warnings FATAL => qw( all );
use English;
# check params
# 1. original file
# 2. new file
# 3. no more params
my $ignore_attribute = "";
if ($ARGV[0] eq "--delete-attribute") {
shift;
die "The attribute which must be deleted is missing."
if (not $ARGV[0]);
$ignore_attribute = shift;
}
die "There must be two arguments (old and new file)."
if (scalar(@ARGV) != 2);
my $org_filename = $ARGV[0];
my $new_filename = $ARGV[1];
die "The original file does not exist."
if (not $org_filename or not -e $org_filename);
die "The new file is not valid filename."
if (not $new_filename);
die "The new file exists already."
if (-e $new_filename);
# open files
die "The original file is not readable."
if (not open(my $ORG_FD, "<", $org_filename));
die "The new file is not writeable."
if (not open(my $NEW_FD, ">", $new_filename));
# look for the XML tree
# - version and encoding
# - DTD
# - XML tree
my $line = <$ORG_FD>;
my $state = "IGNORE";
do {
## determine state
## XML detection
if ($state eq "IGNORE" and $line =~ q{^\s*<[a-zA-Z]}) {
$state = "XML_TREE";
}
## version and encoding detection
if ($state eq "IGNORE" and $line =~ q{^\s*<\?}) {
$state = "ENC_OPEN";
}
if ($state eq "ENC_OPEN" and $line =~ q{^\s*<\?.*\?>\s*$} and $line !~ q{\sencoding="[^"]*"}) {
# add default encoding
$line =~ s{\s*\?>\s*$}{ encoding="UTF-8"?>\n};
}
if ($state eq "ENC_OPEN" and $line =~ q{\?>\s*$}) {
## uppercase encoding
my $encoding = $line;
$encoding =~ s{.*\sencoding="([^"]*)".*}{$1};
$encoding = uc($encoding);
$line =~ s{\sencoding="[^"]*"}{ encoding="${encoding}"};
$state = "WRITE";
}
## DTD detection
if ($state eq "IGNORE" and $line =~ q{^\s*<!DOCTYPE}) {
$state = "DTD_OPEN";
}
if ($state eq "DTD_OPEN" and $line =~ q{\s\[<\?.*\?>\]}) {
## such special XML stuff is lost in WBXML
$line =~ s{\s\[<\?.*\?>\]}{};
}
if ($state eq "DTD_OPEN" and $line =~ q{>\s*$}) {
$state = "WRITE";
}
## comment detection
if ($state eq "IGNORE" and $line =~ q{^\s*<!--}) {
$state = "COMMENT_OPEN";
}
if ($state eq "COMMENT_OPEN" and $line =~ q{-->\s*$}) {
$state = "IGNORE";
}
## handle data
if ($state eq "IGNORE") {
$line = <$ORG_FD>;
}
if ($state eq "WRITE") {
$line =~ s{[\s\r\n]*$}{\n}s;
print $NEW_FD $line;
$line = <$ORG_FD>;
$state = "IGNORE";
}
if ($state =~ q{_OPEN$}) {
$line .= <$ORG_FD>;
$line =~ s{\s*[\n\r]+\s*}{ }sg;
$line .= "\n";
}
} while ($state ne "XML_TREE");
# XML tree state
# - element
# <
# element name
# blank
# attribute name
# = attribute assignment
# attribute value
# >
# - data
# - comment
my $indent = 0;
my $char = "";
my $text = "";
my $element = "";
my $expected = "";
$state = "NEUTRAL";
while (1) {
## $line works line oriented
## but the parser works character oriented
last if (not defined $line);
if (length ($line) == 0) {
$line = <$ORG_FD>;
last if (not defined $line or length($line) < 1);
}
$char = substr($line, 0, 1);
$line = substr($line, 1);
# check state
# reset text state
if ($state eq "TEXT_NEWLINE" and $char !~ q{[\s\n\r]}) {
$state = "NEUTRAL";
}
# ignore leading blanks (normalization)
if ($state eq "NEUTRAL" and $char =~ q{\s}) {
next;
}
# handle comment or element which starts in a text line
if ($state eq "TEXT" and $char eq "<") {
if (length($expected)) {
# the original text must be replaced for test validation
$text = $expected;
$expected = "";
}
$text =~ s{\s*$}{};
print $NEW_FD "${text}\n";
$state = "NEUTRAL";
}
# try to handle a new comment or element
if ($state eq "NEUTRAL" and $char eq "<") {
# let's look forward (element or comment)
die "A standalone smaller than "<" sign is not allowed in XML."
if (length($line) < 1);
$char = substr($line, 0, 1);
$line = substr($line, 1);
if ($char eq "!") {
if ($line =~ q{^--}) {
## this should be a comment
$char = substr($line, 0, 2);
$line = substr($line, 2);
$state = "COMMENT";
$text = "";
} elsif ($line =~ q{^\[CDATA\[}) {
## CDATA section detected
for (my $i = 0; $i < $indent; $i++) {
print $NEW_FD " ";
}
print $NEW_FD "<![CDATA[";
$line =~ s{^\[CDATA\[}{};
$state = "CDATA";
} else {
die "Only comments and CDATA sections are supported and not with '<!${line}'."
if ($char ne "--");
}
} elsif ($char =~ q{[a-zA-Z]}) {
## this is an element
for (my $i = 0; $i < $indent; $i++) {
print $NEW_FD " ";
}
$indent++;
print $NEW_FD "<".$char;
$element = $char;
$state = "ELEMENT";
} elsif ($char eq "/") {
## this is a closing element
$indent--;
for (my $i = 0; $i < $indent; $i++) {
print $NEW_FD " ";
}
print $NEW_FD "</";
$state = "ELEMENT";
} else {
## this is illegal
die "A smaller than "<" sign must be a tag or a comment.";
}
next;
}
# ignore comments
if ($state eq "COMMENT") {
if ($char eq "-" and substr($line, 0, 2) eq "->") {
# end of comment
$line = substr($line, 2);
$state = "NEUTRAL";
# check if this is a special action configuration
if ($text =~ m{^\sEXPECTED\s::=\s.*\s$}) {
# This is the value for the next text data.
$expected = $text;
$expected =~ s{^\sEXPECTED\s::=\s(.*)\s$}{$1};
$text = "";
}
} else {
$text .= $char;
}
next;
}
# read and write text data
if ($state eq "NEUTRAL" and $char ne "<") {
# new text data
for (my $i = 0; $i < $indent; $i++) {
print $NEW_FD " ";
}
$text = $char;
$state = "TEXT";
next;
}
if ($state eq "TEXT" and $char eq "\r") {
next;
}
if ($state eq "TEXT" and $char eq "\n") {
$text =~ s{\s*$}{};
print $NEW_FD "${text}\n";
$state = "TEXT_NEWLINE";
next;
}
# if & is not handled as & then this error cannot be recovered
if ($state eq "TEXT" and $char eq '"') {
$text .= """;
next;
}
if ($state eq "TEXT" and $char eq "'") {
$text .= "'";
next;
}
if ($state eq "TEXT" and $char eq '<') {
$text .= "<";
next;
}
if ($state eq "TEXT" and $char eq '>') {
$text .= ">";
next;
}
if ($state eq "TEXT") {
$text .= $char;
next;
}
if ($state eq "TEXT_NEWLINE" and $char =~ q{\s\r\n}) {
next;
}
# read element name
if ($state eq "ELEMENT" and $char =~ q{[a-zA-Z0-9_]}) {
print $NEW_FD $char;
$element .= $char;
next;
}
# detect space for potential attribute
if ($state eq "ELEMENT" and $char =~ q{[\s\r\n]}) {
$state = "POTENTIAL_ATTRIBUTE";
next;
}
# detect and read attribute
if ($state eq "POTENTIAL_ATTRIBUTE" and $char =~ q{[a-zA-Z0-9_]}) {
if ($ignore_attribute and
substr($ignore_attribute, 0, 1) eq $char and
substr($ignore_attribute, 1) eq substr($line, 0, length(substr($ignore_attribute, 1))))
{
# let's ingore the attribute
$line =~ s{^[a-zA-Z_1-9]*=}{};
if (substr($line, 0, 1) eq "'") {
$line =~ s{^'[^']*'}{};
} else {
$line =~ s{^"[^"]*"}{};
}
next;
}
$state = "ATTRIBUTE_NAME";
print $NEW_FD " ";
}
if ($state eq "ATTRIBUTE_NAME" and $char =~ q{[a-zA-Z0-9_]}) {
print $NEW_FD $char;
next;
}
if ($state eq "ATTRIBUTE_NAME" and $char eq "=") {
print $NEW_FD $char;
$state = "ATTRIBUTE_ASSIGN";
next;
}
if ($state eq "ATTRIBUTE_ASSIGN" and $char eq '"') {
print $NEW_FD $char;
$state = "ATTRIBUTE_VALUE_QUOT";
next;
}
if ($state eq "ATTRIBUTE_VALUE_QUOT" and $char ne '"') {
print $NEW_FD $char;
next;
}
if ($state eq "ATTRIBUTE_VALUE_QUOT" and $char eq '"') {
print $NEW_FD $char;
$state = "POTENTIAL_ATTRIBUTE";
next;
}
if ($state eq "ATTRIBUTE_ASSIGN" and $char eq "'") {
print $NEW_FD $char;
$state = "ATTRIBUTE_VALUE_APOS";
next;
}
if ($state eq "ATTRIBUTE_VALUE_APOS" and $char ne "'") {
print $NEW_FD $char;
next;
}
if ($state eq "ATTRIBUTE_VALUE_APOS" and $char eq "'") {
print $NEW_FD $char;
$state = "POTENTIAL_ATTRIBUTE";
next;
}
# handle standalone element
if (($state eq "ELEMENT" or $state eq "POTENTIAL_ATTRIBUTE")
and $char eq "/")
{
# consume the closing ">" and add the complete closing tag
$char = ">";
$line =~ s{^\s*>}{};
$indent--;
print $NEW_FD ">\n";
for (my $i = 0; $i < $indent; $i++) {
print $NEW_FD " ";
}
print $NEW_FD "</${element}>\n";
$state = "NEUTRAL";
next;
}
# read element end
if (($state eq "ELEMENT" or $state eq "POTENTIAL_ATTRIBUTE")
and $char eq ">")
{
print $NEW_FD $char."\n";
$state = "NEUTRAL";
next;
}
# handle CDATA
if ($state eq "CDATA" and $char eq "]" and substr($line , 0, 2) eq "]>") {
$line = substr($line, 2);
print $NEW_FD "]]>\n";
$state = "NEUTRAL";
next;
}
if ($state eq "CDATA") {
print $NEW_FD $char;
next;
}
};
# close files
close $ORG_FD;
close $NEW_FD;
|