| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
package com.adobe.internal.xmp.impl; |
| 11 |
|
| 12 |
import java.io.IOException; |
| 13 |
import java.io.InputStream; |
| 14 |
import java.io.InputStreamReader; |
| 15 |
import java.io.Reader; |
| 16 |
import java.io.StringReader; |
| 17 |
import java.io.UnsupportedEncodingException; |
| 18 |
|
| 19 |
import javax.xml.XMLConstants; |
| 20 |
import javax.xml.parsers.DocumentBuilder; |
| 21 |
import javax.xml.parsers.DocumentBuilderFactory; |
| 22 |
import javax.xml.parsers.ParserConfigurationException; |
| 23 |
|
| 24 |
import org.w3c.dom.Document; |
| 25 |
import org.w3c.dom.Node; |
| 26 |
import org.w3c.dom.NodeList; |
| 27 |
import org.w3c.dom.ProcessingInstruction; |
| 28 |
import org.xml.sax.InputSource; |
| 29 |
import org.xml.sax.SAXException; |
| 30 |
|
| 31 |
import com.adobe.internal.xmp.XMPConst; |
| 32 |
import com.adobe.internal.xmp.XMPError; |
| 33 |
import com.adobe.internal.xmp.XMPException; |
| 34 |
import com.adobe.internal.xmp.XMPMeta; |
| 35 |
import com.adobe.internal.xmp.options.ParseOptions; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
public class XMPMetaParser |
| 50 |
{ |
| 51 |
|
| 52 |
|
| 53 |
private static final Object XMP_RDF = new Object(); |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
private static DocumentBuilderFactory factory = createDocumentBuilderFactory(); |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
private XMPMetaParser() |
| 66 |
{ |
| 67 |
|
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
public static XMPMeta parse(Object input, ParseOptions options) throws XMPException |
| 83 |
{ |
| 84 |
ParameterAsserts.assertNotNull(input); |
| 85 |
options = options != null ? options : new ParseOptions(); |
| 86 |
|
| 87 |
Document document = parseXml(input, options); |
| 88 |
|
| 89 |
boolean xmpmetaRequired = options.getRequireXMPMeta(); |
| 90 |
Object[] result = new Object[3]; |
| 91 |
result = findRootNode(document, xmpmetaRequired, result); |
| 92 |
|
| 93 |
if (result != null && result[1] == XMP_RDF) |
| 94 |
{ |
| 95 |
XMPMetaImpl xmp = ParseRDF.parse((Node) result[0], options); |
| 96 |
xmp.setPacketHeader((String) result[2]); |
| 97 |
|
| 98 |
|
| 99 |
if (!options.getOmitNormalization()) |
| 100 |
{ |
| 101 |
return XMPNormalizer.process(xmp, options); |
| 102 |
} |
| 103 |
else |
| 104 |
{ |
| 105 |
return xmp; |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
else |
| 118 |
{ |
| 119 |
|
| 120 |
return new XMPMetaImpl(); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
private static Document parseXml(Object input, ParseOptions options) |
| 143 |
throws XMPException |
| 144 |
{ |
| 145 |
if (input instanceof InputStream) |
| 146 |
{ |
| 147 |
return parseXmlFromInputStream((InputStream) input, options); |
| 148 |
} |
| 149 |
else if (input instanceof byte[]) |
| 150 |
{ |
| 151 |
return parseXmlFromBytebuffer(new ByteBuffer((byte[]) input), options); |
| 152 |
} |
| 153 |
else |
| 154 |
{ |
| 155 |
return parseXmlFromString((String) input, options); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
private static Document parseXmlFromInputStream(InputStream stream, ParseOptions options) |
| 170 |
throws XMPException |
| 171 |
{ |
| 172 |
if (!options.getAcceptLatin1() && |
| 173 |
!options.getFixControlChars() && |
| 174 |
!options.getDisallowDoctype()) |
| 175 |
{ |
| 176 |
return parseInputSource(new InputSource(stream)); |
| 177 |
} |
| 178 |
else |
| 179 |
{ |
| 180 |
|
| 181 |
try |
| 182 |
{ |
| 183 |
ByteBuffer buffer = new ByteBuffer(stream); |
| 184 |
return parseXmlFromBytebuffer(buffer, options); |
| 185 |
} |
| 186 |
catch (IOException e) |
| 187 |
{ |
| 188 |
throw new XMPException("Error reading the XML-file", |
| 189 |
XMPError.BADSTREAM, e); |
| 190 |
} |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
private static Document parseXmlFromBytebuffer(ByteBuffer buffer, ParseOptions options) |
| 207 |
throws XMPException |
| 208 |
{ |
| 209 |
try |
| 210 |
{ |
| 211 |
InputSource source = new InputSource(buffer.getByteStream()); |
| 212 |
try |
| 213 |
{ |
| 214 |
if (options.getDisallowDoctype()) |
| 215 |
{ |
| 216 |
try |
| 217 |
{ |
| 218 |
factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); |
| 219 |
} |
| 220 |
catch(Throwable e) |
| 221 |
{ |
| 222 |
|
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
return parseInputSource(source); |
| 227 |
} |
| 228 |
catch (XMPException e) |
| 229 |
{ |
| 230 |
if ("DOCTYPE is disallowed".equals(e.getCause().getMessage())) |
| 231 |
{ |
| 232 |
throw new XMPException(e.getCause().getMessage(), XMPError.BADXML); |
| 233 |
} |
| 234 |
else if (e.getErrorCode() == XMPError.BADXML || |
| 235 |
e.getErrorCode() == XMPError.BADSTREAM) |
| 236 |
{ |
| 237 |
if (options.getAcceptLatin1()) |
| 238 |
{ |
| 239 |
buffer = Latin1Converter.convert(buffer); |
| 240 |
} |
| 241 |
|
| 242 |
if (options.getFixControlChars()) |
| 243 |
{ |
| 244 |
String encoding = buffer.getEncoding(); |
| 245 |
Reader fixReader = new FixASCIIControlsReader( |
| 246 |
new InputStreamReader( |
| 247 |
buffer.getByteStream(), encoding)); |
| 248 |
return parseInputSource(new InputSource(fixReader)); |
| 249 |
} |
| 250 |
source = new InputSource(buffer.getByteStream()); |
| 251 |
return parseInputSource(source); |
| 252 |
} |
| 253 |
else |
| 254 |
{ |
| 255 |
throw e; |
| 256 |
} |
| 257 |
} |
| 258 |
} |
| 259 |
catch (UnsupportedEncodingException e) |
| 260 |
{ |
| 261 |
|
| 262 |
throw new XMPException("Unsupported Encoding", |
| 263 |
XMPError.INTERNALFAILURE, e); |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
private static Document parseXmlFromString(String input, ParseOptions options) |
| 278 |
throws XMPException |
| 279 |
{ |
| 280 |
InputSource source; |
| 281 |
try |
| 282 |
{ |
| 283 |
if (options.getDisallowDoctype()) |
| 284 |
{ |
| 285 |
try |
| 286 |
{ |
| 287 |
factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); |
| 288 |
} |
| 289 |
catch(Throwable e) |
| 290 |
{ |
| 291 |
|
| 292 |
} |
| 293 |
} |
| 294 |
source = new InputSource(new StringReader(input)); |
| 295 |
return parseInputSource(source); |
| 296 |
} |
| 297 |
catch (XMPException e) |
| 298 |
{ |
| 299 |
if (e.getErrorCode() == XMPError.BADXML && options.getFixControlChars()) |
| 300 |
{ |
| 301 |
source = new InputSource(new FixASCIIControlsReader(new StringReader(input))); |
| 302 |
return parseInputSource(source); |
| 303 |
} |
| 304 |
else |
| 305 |
{ |
| 306 |
throw e; |
| 307 |
} |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
private static Document parseInputSource(InputSource source) throws XMPException |
| 319 |
{ |
| 320 |
try |
| 321 |
{ |
| 322 |
DocumentBuilder builder = factory.newDocumentBuilder(); |
| 323 |
builder.setErrorHandler(null); |
| 324 |
|
| 325 |
return builder.parse(source); |
| 326 |
} |
| 327 |
catch (SAXException e) |
| 328 |
{ |
| 329 |
throw new XMPException("XML parsing failure", XMPError.BADXML, e); |
| 330 |
} |
| 331 |
catch (ParserConfigurationException e) |
| 332 |
{ |
| 333 |
throw new XMPException("XML Parser not correctly configured", |
| 334 |
XMPError.UNKNOWN, e); |
| 335 |
} |
| 336 |
catch (IOException e) |
| 337 |
{ |
| 338 |
throw new XMPException("Error reading the XML-file", XMPError.BADSTREAM, e); |
| 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 |
private static Object[] findRootNode(Node root, boolean xmpmetaRequired, Object[] result) |
| 373 |
{ |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
NodeList children = root.getChildNodes(); |
| 378 |
for (int i = 0; i < children.getLength(); i++) |
| 379 |
{ |
| 380 |
root = children.item(i); |
| 381 |
if (Node.PROCESSING_INSTRUCTION_NODE == root.getNodeType() && |
| 382 |
XMPConst.XMP_PI.equals(((ProcessingInstruction) root).getTarget())) |
| 383 |
|
| 384 |
{ |
| 385 |
|
| 386 |
if (result != null) |
| 387 |
{ |
| 388 |
result[2] = ((ProcessingInstruction) root).getData(); |
| 389 |
} |
| 390 |
} |
| 391 |
else if (Node.TEXT_NODE != root.getNodeType() && |
| 392 |
Node.PROCESSING_INSTRUCTION_NODE != root.getNodeType()) |
| 393 |
{ |
| 394 |
String rootNS = root.getNamespaceURI(); |
| 395 |
String rootLocal = root.getLocalName(); |
| 396 |
if ( |
| 397 |
( |
| 398 |
XMPConst.TAG_XMPMETA.equals(rootLocal) || |
| 399 |
XMPConst.TAG_XAPMETA.equals(rootLocal) |
| 400 |
) && |
| 401 |
XMPConst.NS_X.equals(rootNS) |
| 402 |
) |
| 403 |
{ |
| 404 |
|
| 405 |
return findRootNode(root, false, result); |
| 406 |
} |
| 407 |
else if (!xmpmetaRequired && |
| 408 |
"RDF".equals(rootLocal) && |
| 409 |
XMPConst.NS_RDF.equals(rootNS)) |
| 410 |
{ |
| 411 |
if (result != null) |
| 412 |
{ |
| 413 |
result[0] = root; |
| 414 |
result[1] = XMP_RDF; |
| 415 |
} |
| 416 |
return result; |
| 417 |
} |
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
else |
| 432 |
{ |
| 433 |
|
| 434 |
Object[] newResult = findRootNode(root, xmpmetaRequired, result); |
| 435 |
if (newResult != null) |
| 436 |
{ |
| 437 |
return newResult; |
| 438 |
} |
| 439 |
else |
| 440 |
{ |
| 441 |
continue; |
| 442 |
} |
| 443 |
} |
| 444 |
} |
| 445 |
} |
| 446 |
|
| 447 |
|
| 448 |
return null; |
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
} |
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
private static DocumentBuilderFactory createDocumentBuilderFactory() |
| 460 |
{ |
| 461 |
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
| 462 |
factory.setNamespaceAware(true); |
| 463 |
factory.setIgnoringComments(true); |
| 464 |
factory.setExpandEntityReferences(false); |
| 465 |
|
| 466 |
try |
| 467 |
{ |
| 468 |
String FEATURE = "http://apache.org/xml/features/disallow-doctype-decl"; |
| 469 |
factory.setFeature(FEATURE, true); |
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
FEATURE = "http://xml.org/sax/features/external-general-entities"; |
| 478 |
factory.setFeature(FEATURE, false); |
| 479 |
FEATURE = "http://xerces.apache.org/xerces2-j/features.html#disallow-doctype-decl"; |
| 480 |
factory.setFeature(FEATURE, false); |
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
FEATURE = "http://xml.org/sax/features/external-parameter-entities"; |
| 488 |
factory.setFeature(FEATURE, false); |
| 489 |
|
| 490 |
FEATURE = "http://xerces.apache.org/xerces2-j/features.html#external-parameter-entities"; |
| 491 |
factory.setFeature(FEATURE, false); |
| 492 |
|
| 493 |
|
| 494 |
FEATURE = "http://apache.org/xml/features/nonvalidating/load-external-dtd"; |
| 495 |
factory.setFeature(FEATURE, false); |
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
factory.setXIncludeAware(false); |
| 500 |
factory.setExpandEntityReferences(false); |
| 501 |
|
| 502 |
} |
| 503 |
catch (Throwable e) |
| 504 |
{ |
| 505 |
|
| 506 |
|
| 507 |
} |
| 508 |
return factory; |
| 509 |
} |
| 510 |
} |