正在查看: Harvest v2.32 应用的 XMPMetaParser.java JAVA 源代码文件
本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。
正在查看: Harvest v2.32 应用的 XMPMetaParser.java JAVA 源代码文件
本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。
package com.itextpdf.xmp.impl;
import com.itextpdf.xmp.XMPConst;
import com.itextpdf.xmp.XMPError;
import com.itextpdf.xmp.XMPException;
import com.itextpdf.xmp.XMPMeta;
import com.itextpdf.xmp.options.ParseOptions;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.ProcessingInstruction;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
public class XMPMetaParser {
private static final Object XMP_RDF = new Object();
private static DocumentBuilderFactory factory = createDocumentBuilderFactory();
private XMPMetaParser() {
}
public static XMPMeta parse(Object obj, ParseOptions parseOptions) throws XMPException {
ParameterAsserts.assertNotNull(obj);
if (parseOptions == null) {
parseOptions = new ParseOptions();
}
Object[] findRootNode = findRootNode(parseXml(obj, parseOptions), parseOptions.getRequireXMPMeta(), new Object[3]);
if (findRootNode != null && findRootNode[1] == XMP_RDF) {
XMPMetaImpl parse = ParseRDF.parse((Node) findRootNode[0]);
parse.setPacketHeader((String) findRootNode[2]);
return !parseOptions.getOmitNormalization() ? XMPNormalizer.process(parse, parseOptions) : parse;
}
return new XMPMetaImpl();
}
private static Document parseXml(Object obj, ParseOptions parseOptions) throws XMPException {
if (obj instanceof InputStream) {
return parseXmlFromInputStream((InputStream) obj, parseOptions);
}
if (obj instanceof byte[]) {
return parseXmlFromBytebuffer(new ByteBuffer((byte[]) obj), parseOptions);
}
return parseXmlFromString((String) obj, parseOptions);
}
private static Document parseXmlFromInputStream(InputStream inputStream, ParseOptions parseOptions) throws XMPException {
if (!parseOptions.getAcceptLatin1() && !parseOptions.getFixControlChars()) {
return parseInputSource(new InputSource(inputStream));
}
try {
return parseXmlFromBytebuffer(new ByteBuffer(inputStream), parseOptions);
} catch (IOException e) {
throw new XMPException("Error reading the XML-file", XMPError.BADSTREAM, e);
}
}
private static Document parseXmlFromBytebuffer(ByteBuffer byteBuffer, ParseOptions parseOptions) throws XMPException {
try {
return parseInputSource(new InputSource(byteBuffer.getByteStream()));
} catch (XMPException e) {
if (e.getErrorCode() == 201 || e.getErrorCode() == 204) {
if (parseOptions.getAcceptLatin1()) {
byteBuffer = Latin1Converter.convert(byteBuffer);
}
if (parseOptions.getFixControlChars()) {
try {
return parseInputSource(new InputSource(new FixASCIIControlsReader(new InputStreamReader(byteBuffer.getByteStream(), byteBuffer.getEncoding()))));
} catch (UnsupportedEncodingException unused) {
throw new XMPException("Unsupported Encoding", 9, e);
}
}
return parseInputSource(new InputSource(byteBuffer.getByteStream()));
}
throw e;
}
}
private static Document parseXmlFromString(String str, ParseOptions parseOptions) throws XMPException {
try {
return parseInputSource(new InputSource(new StringReader(str)));
} catch (XMPException e) {
if (e.getErrorCode() == 201 && parseOptions.getFixControlChars()) {
return parseInputSource(new InputSource(new FixASCIIControlsReader(new StringReader(str))));
}
throw e;
}
}
private static Document parseInputSource(InputSource inputSource) throws XMPException {
try {
DocumentBuilder newDocumentBuilder = factory.newDocumentBuilder();
newDocumentBuilder.setErrorHandler(null);
return newDocumentBuilder.parse(inputSource);
} catch (IOException e) {
throw new XMPException("Error reading the XML-file", XMPError.BADSTREAM, e);
} catch (ParserConfigurationException e2) {
throw new XMPException("XML Parser not correctly configured", 0, e2);
} catch (SAXException e3) {
throw new XMPException("XML parsing failure", XMPError.BADXML, e3);
}
}
private static Object[] findRootNode(Node node, boolean z, Object[] objArr) {
NodeList childNodes = node.getChildNodes();
for (int i = 0; i < childNodes.getLength(); i++) {
Node item = childNodes.item(i);
if (7 == item.getNodeType()) {
ProcessingInstruction processingInstruction = (ProcessingInstruction) item;
if (XMPConst.XMP_PI.equals(processingInstruction.getTarget())) {
if (objArr != null) {
objArr[2] = processingInstruction.getData();
}
}
}
if (3 != item.getNodeType() && 7 != item.getNodeType()) {
String namespaceURI = item.getNamespaceURI();
String localName = item.getLocalName();
if ((XMPConst.TAG_XMPMETA.equals(localName) || XMPConst.TAG_XAPMETA.equals(localName)) && XMPConst.NS_X.equals(namespaceURI)) {
return findRootNode(item, false, objArr);
}
if (!z && "RDF".equals(localName) && XMPConst.NS_RDF.equals(namespaceURI)) {
if (objArr != null) {
objArr[0] = item;
objArr[1] = XMP_RDF;
}
return objArr;
}
Object[] findRootNode = findRootNode(item, z, objArr);
if (findRootNode != null) {
return findRootNode;
}
}
}
return null;
}
private static DocumentBuilderFactory createDocumentBuilderFactory() {
DocumentBuilderFactory newInstance = DocumentBuilderFactory.newInstance();
newInstance.setNamespaceAware(true);
newInstance.setIgnoringComments(true);
try {
newInstance.setFeature("http://javax.xml.XMLConstants/feature/secure-processing", true);
newInstance.setFeature("http://xml.org/sax/features/external-general-entities", false);
newInstance.setFeature("http://apache.org/xml/features/disallow-doctype-decl", false);
newInstance.setXIncludeAware(false);
newInstance.setExpandEntityReferences(false);
} catch (Exception unused) {
}
return newInstance;
}
}