imToken v20.9.11.17版本的 MD5 值为:f7135d2ec367bd3b61d8914bc13d03cb
以下内容为反编译后的 XMPSerializerRDF.java 源代码,内容仅作参考
package com.adobe.xmp.impl;
import com.adobe.xmp.XMPConst;
import com.adobe.xmp.XMPError;
import com.adobe.xmp.XMPException;
import com.adobe.xmp.XMPMeta;
import com.adobe.xmp.XMPMetaFactory;
import com.adobe.xmp.XMPSchemaRegistry;
import com.adobe.xmp.options.SerializeOptions;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class XMPSerializerRDF {
private static final int DEFAULT_PAD = 2048;
private static final String PACKET_HEADER = "<?xpacket begin=\"\ufeff\" id=\"W5M0MpCehiHzreSzNTczkc9d\"?>";
private static final String PACKET_TRAILER = "<?xpacket end=\"";
private static final String PACKET_TRAILER2 = "\"?>";
static final Set RDF_ATTR_QUALIFIER = new HashSet(Arrays.asList(XMPConst.XML_LANG, "rdf:resource", "rdf:ID", "rdf:bagID", "rdf:nodeID"));
private static final String RDF_EMPTY_STRUCT = "<rdf:Description/>";
private static final String RDF_RDF_END = "</rdf:RDF>";
private static final String RDF_RDF_START = "<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">";
private static final String RDF_SCHEMA_END = "</rdf:Description>";
private static final String RDF_SCHEMA_START = "<rdf:Description rdf:about=";
private static final String RDF_STRUCT_END = "</rdf:Description>";
private static final String RDF_STRUCT_START = "<rdf:Description";
private static final String RDF_XMPMETA_END = "</x:xmpmeta>";
private static final String RDF_XMPMETA_START = "<x:xmpmeta xmlns:x=\"adobe:ns:meta/\" x:xmptk=\"";
private SerializeOptions options;
private CountOutputStream outputStream;
private int padding;
private int unicodeSize = 1;
private OutputStreamWriter writer;
private XMPMetaImpl xmp;
private void addPadding(int i) throws XMPException, IOException {
if (this.options.getExactPacketLength()) {
int bytesWritten = this.outputStream.getBytesWritten() + (i * this.unicodeSize);
int i2 = this.padding;
if (bytesWritten > i2) {
throw new XMPException("Can't fit into specified packet size", 107);
}
this.padding = i2 - bytesWritten;
}
this.padding /= this.unicodeSize;
int length = this.options.getNewline().length();
int i3 = this.padding;
if (i3 < length) {
writeChars(i3, ' ');
return;
}
int i4 = i3 - length;
while (true) {
this.padding = i4;
int i5 = this.padding;
int i6 = length + 100;
if (i5 < i6) {
writeChars(i5, ' ');
writeNewline();
return;
}
writeChars(100, ' ');
writeNewline();
i4 = this.padding - i6;
}
}
private void appendNodeValue(String str, boolean z) throws IOException {
if (str == null) {
str = "";
}
write(Utils.escapeXML(str, z, true));
}
private boolean canBeRDFAttrProp(XMPNode xMPNode) {
return (xMPNode.hasQualifier() || xMPNode.getOptions().isURI() || xMPNode.getOptions().isCompositeProperty() || "[]".equals(xMPNode.getName())) ? false : true;
}
private void declareNamespace(String str, String str2, Set set, int i) throws IOException {
if (str2 == null) {
QName qName = new QName(str);
if (!qName.hasPrefix()) {
return;
}
str = qName.getPrefix();
XMPSchemaRegistry schemaRegistry = XMPMetaFactory.getSchemaRegistry();
str2 = schemaRegistry.getNamespaceURI(str + ":");
declareNamespace(str, str2, set, i);
}
if (set.contains(str)) {
return;
}
writeNewline();
writeIndent(i);
write("xmlns:");
write(str);
write("=\"");
write(str2);
write(34);
set.add(str);
}
private void declareUsedNamespaces(XMPNode xMPNode, Set set, int i) throws IOException {
if (xMPNode.getOptions().isSchemaNode()) {
declareNamespace(xMPNode.getValue().substring(0, xMPNode.getValue().length() - 1), xMPNode.getName(), set, i);
} else if (xMPNode.getOptions().isStruct()) {
Iterator iterateChildren = xMPNode.iterateChildren();
while (iterateChildren.hasNext()) {
declareNamespace(((XMPNode) iterateChildren.next()).getName(), null, set, i);
}
}
Iterator iterateChildren2 = xMPNode.iterateChildren();
while (iterateChildren2.hasNext()) {
declareUsedNamespaces((XMPNode) iterateChildren2.next(), set, i);
}
Iterator iterateQualifier = xMPNode.iterateQualifier();
while (iterateQualifier.hasNext()) {
XMPNode xMPNode2 = (XMPNode) iterateQualifier.next();
declareNamespace(xMPNode2.getName(), null, set, i);
declareUsedNamespaces(xMPNode2, set, i);
}
}
private void emitRDFArrayTag(XMPNode xMPNode, boolean z, int i) throws IOException {
if (z || xMPNode.hasChildren()) {
writeIndent(i);
write(z ? "<rdf:" : "</rdf:");
write(xMPNode.getOptions().isArrayAlternate() ? "Alt" : xMPNode.getOptions().isArrayOrdered() ? "Seq" : "Bag");
write((!z || xMPNode.hasChildren()) ? ">" : "/>");
writeNewline();
}
}
private void endOuterRDFDescription(int i) throws IOException {
writeIndent(i + 1);
write("</rdf:Description>");
writeNewline();
}
private String serializeAsRDF() throws IOException, XMPException {
int i = 0;
if (!this.options.getOmitPacketWrapper()) {
writeIndent(0);
write(PACKET_HEADER);
writeNewline();
}
if (!this.options.getOmitXmpMetaElement()) {
writeIndent(0);
write(RDF_XMPMETA_START);
if (!this.options.getOmitVersionAttribute()) {
write(XMPMetaFactory.getVersionInfo().getMessage());
}
write("\">");
writeNewline();
i = 1;
}
writeIndent(i);
write(RDF_RDF_START);
writeNewline();
if (this.options.getUseCanonicalFormat()) {
serializeCanonicalRDFSchemas(i);
} else {
serializeCompactRDFSchemas(i);
}
writeIndent(i);
write(RDF_RDF_END);
writeNewline();
if (!this.options.getOmitXmpMetaElement()) {
writeIndent(i - 1);
write(RDF_XMPMETA_END);
writeNewline();
}
String str = "";
if (this.options.getOmitPacketWrapper()) {
return "";
}
for (int baseIndent = this.options.getBaseIndent(); baseIndent > 0; baseIndent--) {
str = str + this.options.getIndent();
}
StringBuilder sb = new StringBuilder();
sb.append(str + PACKET_TRAILER);
sb.append(this.options.getReadOnlyPacket() ? 'r' : 'w');
return sb.toString() + PACKET_TRAILER2;
}
private void serializeCanonicalRDFProperty(XMPNode xMPNode, boolean z, boolean z2, int i) throws IOException, XMPException {
boolean z3;
String str;
int i2 = i;
String name = xMPNode.getName();
if (z2) {
name = "rdf:value";
} else if ("[]".equals(name)) {
name = "rdf:li";
}
writeIndent(i2);
write(60);
write(name);
Iterator iterateQualifier = xMPNode.iterateQualifier();
boolean z4 = false;
boolean z5 = false;
boolean z6 = false;
while (true) {
z3 = true;
if (!iterateQualifier.hasNext()) {
break;
}
XMPNode xMPNode2 = (XMPNode) iterateQualifier.next();
if (RDF_ATTR_QUALIFIER.contains(xMPNode2.getName())) {
z6 = "rdf:resource".equals(xMPNode2.getName());
if (!z2) {
write(32);
write(xMPNode2.getName());
write("=\"");
appendNodeValue(xMPNode2.getValue(), true);
write(34);
}
} else {
z5 = true;
}
}
if (!z5 || z2) {
if (xMPNode.getOptions().isCompositeProperty()) {
if (xMPNode.getOptions().isArray()) {
write(62);
writeNewline();
int i3 = i2 + 1;
emitRDFArrayTag(xMPNode, true, i3);
if (xMPNode.getOptions().isArrayAltText()) {
XMPNodeUtils.normalizeLangArray(xMPNode);
}
Iterator iterateChildren = xMPNode.iterateChildren();
while (iterateChildren.hasNext()) {
serializeCanonicalRDFProperty((XMPNode) iterateChildren.next(), z, false, i2 + 2);
}
emitRDFArrayTag(xMPNode, false, i3);
} else if (z6) {
Iterator iterateChildren2 = xMPNode.iterateChildren();
while (iterateChildren2.hasNext()) {
XMPNode xMPNode3 = (XMPNode) iterateChildren2.next();
if (!canBeRDFAttrProp(xMPNode3)) {
throw new XMPException("Can't mix rdf:resource and complex fields", XMPError.BADRDF);
}
writeNewline();
writeIndent(i2 + 1);
write(32);
write(xMPNode3.getName());
write("=\"");
appendNodeValue(xMPNode3.getValue(), true);
write(34);
}
write("/>");
} else if (xMPNode.hasChildren()) {
if (z) {
write(">");
writeNewline();
i2++;
writeIndent(i2);
write(RDF_STRUCT_START);
write(">");
} else {
write(" rdf:parseType=\"Resource\">");
}
writeNewline();
Iterator iterateChildren3 = xMPNode.iterateChildren();
while (iterateChildren3.hasNext()) {
serializeCanonicalRDFProperty((XMPNode) iterateChildren3.next(), z, false, i2 + 1);
}
} else if (z) {
write(">");
writeNewline();
writeIndent(i2 + 1);
write(RDF_EMPTY_STRUCT);
z4 = true;
} else {
str = " rdf:parseType=\"Resource\"/>";
write(str);
}
z4 = true;
} else if (xMPNode.getOptions().isURI()) {
write(" rdf:resource=\"");
appendNodeValue(xMPNode.getValue(), true);
str = "\"/>";
write(str);
} else {
if (xMPNode.getValue() != null && !"".equals(xMPNode.getValue())) {
write(62);
appendNodeValue(xMPNode.getValue(), false);
z4 = true;
z3 = false;
}
write("/>");
}
writeNewline();
} else if (z6) {
throw new XMPException("Can't mix rdf:resource and general qualifiers", XMPError.BADRDF);
} else {
if (z) {
write(">");
writeNewline();
i2++;
writeIndent(i2);
write(RDF_STRUCT_START);
write(">");
} else {
write(" rdf:parseType=\"Resource\">");
}
writeNewline();
int i4 = i2 + 1;
serializeCanonicalRDFProperty(xMPNode, z, true, i4);
Iterator iterateQualifier2 = xMPNode.iterateQualifier();
while (iterateQualifier2.hasNext()) {
XMPNode xMPNode4 = (XMPNode) iterateQualifier2.next();
if (!RDF_ATTR_QUALIFIER.contains(xMPNode4.getName())) {
serializeCanonicalRDFProperty(xMPNode4, z, false, i4);
}
}
}
if (z4) {
if (z3) {
writeIndent(i2);
}
write("</");
write(name);
write(62);
writeNewline();
}
}
private void serializeCanonicalRDFSchema(XMPNode xMPNode, int i) throws IOException, XMPException {
Iterator iterateChildren = xMPNode.iterateChildren();
while (iterateChildren.hasNext()) {
serializeCanonicalRDFProperty((XMPNode) iterateChildren.next(), this.options.getUseCanonicalFormat(), false, i + 2);
}
}
private void serializeCanonicalRDFSchemas(int i) throws IOException, XMPException {
if (this.xmp.getRoot().getChildrenLength() > 0) {
startOuterRDFDescription(this.xmp.getRoot(), i);
Iterator iterateChildren = this.xmp.getRoot().iterateChildren();
while (iterateChildren.hasNext()) {
serializeCanonicalRDFSchema((XMPNode) iterateChildren.next(), i);
}
endOuterRDFDescription(i);
return;
}
writeIndent(i + 1);
write(RDF_SCHEMA_START);
writeTreeName();
write("/>");
writeNewline();
}
private void serializeCompactRDFArrayProp(XMPNode xMPNode, int i) throws IOException, XMPException {
write(62);
writeNewline();
int i2 = i + 1;
emitRDFArrayTag(xMPNode, true, i2);
if (xMPNode.getOptions().isArrayAltText()) {
XMPNodeUtils.normalizeLangArray(xMPNode);
}
serializeCompactRDFElementProps(xMPNode, i + 2);
emitRDFArrayTag(xMPNode, false, i2);
}
private boolean serializeCompactRDFAttrProps(XMPNode xMPNode, int i) throws IOException {
Iterator iterateChildren = xMPNode.iterateChildren();
boolean z = true;
while (iterateChildren.hasNext()) {
XMPNode xMPNode2 = (XMPNode) iterateChildren.next();
if (canBeRDFAttrProp(xMPNode2)) {
writeNewline();
writeIndent(i);
write(xMPNode2.getName());
write("=\"");
appendNodeValue(xMPNode2.getValue(), true);
write(34);
} else {
z = false;
}
}
return z;
}
private void serializeCompactRDFElementProps(XMPNode xMPNode, int i) throws IOException, XMPException {
boolean z;
boolean booleanValue;
Iterator iterateChildren = xMPNode.iterateChildren();
while (iterateChildren.hasNext()) {
XMPNode xMPNode2 = (XMPNode) iterateChildren.next();
if (!canBeRDFAttrProp(xMPNode2)) {
String name = xMPNode2.getName();
if ("[]".equals(name)) {
name = "rdf:li";
}
writeIndent(i);
write(60);
write(name);
Iterator iterateQualifier = xMPNode2.iterateQualifier();
boolean z2 = false;
boolean z3 = false;
while (true) {
z = true;
if (!iterateQualifier.hasNext()) {
break;
}
XMPNode xMPNode3 = (XMPNode) iterateQualifier.next();
if (RDF_ATTR_QUALIFIER.contains(xMPNode3.getName())) {
z3 = "rdf:resource".equals(xMPNode3.getName());
write(32);
write(xMPNode3.getName());
write("=\"");
appendNodeValue(xMPNode3.getValue(), true);
write(34);
} else {
z2 = true;
}
}
if (z2) {
serializeCompactRDFGeneralQualifier(i, xMPNode2);
} else if (!xMPNode2.getOptions().isCompositeProperty()) {
Object[] serializeCompactRDFSimpleProp = serializeCompactRDFSimpleProp(xMPNode2);
boolean booleanValue2 = ((Boolean) serializeCompactRDFSimpleProp[0]).booleanValue();
booleanValue = ((Boolean) serializeCompactRDFSimpleProp[1]).booleanValue();
z = booleanValue2;
if (z) {
if (booleanValue) {
writeIndent(i);
}
write("</");
write(name);
write(62);
writeNewline();
}
} else if (xMPNode2.getOptions().isArray()) {
serializeCompactRDFArrayProp(xMPNode2, i);
} else {
z = serializeCompactRDFStructProp(xMPNode2, i, z3);
}
booleanValue = true;
if (z) {
}
}
}
}
private void serializeCompactRDFGeneralQualifier(int i, XMPNode xMPNode) throws IOException, XMPException {
write(" rdf:parseType=\"Resource\">");
writeNewline();
int i2 = i + 1;
serializeCanonicalRDFProperty(xMPNode, false, true, i2);
Iterator iterateQualifier = xMPNode.iterateQualifier();
while (iterateQualifier.hasNext()) {
serializeCanonicalRDFProperty((XMPNode) iterateQualifier.next(), false, false, i2);
}
}
private void serializeCompactRDFSchemas(int i) throws IOException, XMPException {
String str;
int i2 = i + 1;
writeIndent(i2);
write(RDF_SCHEMA_START);
writeTreeName();
HashSet hashSet = new HashSet();
hashSet.add("xml");
hashSet.add("rdf");
Iterator iterateChildren = this.xmp.getRoot().iterateChildren();
while (iterateChildren.hasNext()) {
declareUsedNamespaces((XMPNode) iterateChildren.next(), hashSet, i + 3);
}
Iterator iterateChildren2 = this.xmp.getRoot().iterateChildren();
boolean z = true;
while (iterateChildren2.hasNext()) {
z &= serializeCompactRDFAttrProps((XMPNode) iterateChildren2.next(), i + 2);
}
if (z) {
str = "/>";
} else {
write(62);
writeNewline();
Iterator iterateChildren3 = this.xmp.getRoot().iterateChildren();
while (iterateChildren3.hasNext()) {
serializeCompactRDFElementProps((XMPNode) iterateChildren3.next(), i + 2);
}
writeIndent(i2);
str = "</rdf:Description>";
}
write(str);
writeNewline();
}
private Object[] serializeCompactRDFSimpleProp(XMPNode xMPNode) throws IOException {
String str;
Boolean bool = Boolean.TRUE;
Boolean bool2 = Boolean.TRUE;
if (xMPNode.getOptions().isURI()) {
write(" rdf:resource=\"");
appendNodeValue(xMPNode.getValue(), true);
str = "\"/>";
} else if (xMPNode.getValue() != null && xMPNode.getValue().length() != 0) {
write(62);
appendNodeValue(xMPNode.getValue(), false);
bool2 = Boolean.FALSE;
return new Object[]{bool, bool2};
} else {
str = "/>";
}
write(str);
writeNewline();
bool = Boolean.FALSE;
return new Object[]{bool, bool2};
}
private boolean serializeCompactRDFStructProp(XMPNode xMPNode, int i, boolean z) throws XMPException, IOException {
String str;
Iterator iterateChildren = xMPNode.iterateChildren();
boolean z2 = false;
boolean z3 = false;
while (iterateChildren.hasNext()) {
if (canBeRDFAttrProp((XMPNode) iterateChildren.next())) {
z2 = true;
} else {
z3 = true;
}
if (z2 && z3) {
break;
}
}
if (z && z3) {
throw new XMPException("Can't mix rdf:resource qualifier and element fields", XMPError.BADRDF);
}
if (!xMPNode.hasChildren()) {
str = " rdf:parseType=\"Resource\"/>";
} else if (z3) {
if (z2) {
write(62);
writeNewline();
int i2 = i + 1;
writeIndent(i2);
write(RDF_STRUCT_START);
serializeCompactRDFAttrProps(xMPNode, i + 2);
write(">");
writeNewline();
serializeCompactRDFElementProps(xMPNode, i2);
writeIndent(i2);
write("</rdf:Description>");
writeNewline();
} else {
write(" rdf:parseType=\"Resource\">");
writeNewline();
serializeCompactRDFElementProps(xMPNode, i + 1);
}
return true;
} else {
serializeCompactRDFAttrProps(xMPNode, i + 1);
str = "/>";
}
write(str);
writeNewline();
return false;
}
private void startOuterRDFDescription(XMPNode xMPNode, int i) throws IOException {
writeIndent(i + 1);
write(RDF_SCHEMA_START);
writeTreeName();
HashSet hashSet = new HashSet();
hashSet.add("xml");
hashSet.add("rdf");
declareUsedNamespaces(xMPNode, hashSet, i + 3);
write(62);
writeNewline();
}
private void write(int i) throws IOException {
this.writer.write(i);
}
private void write(String str) throws IOException {
this.writer.write(str);
}
private void writeChars(int i, char c) throws IOException {
while (i > 0) {
this.writer.write(c);
i--;
}
}
private void writeIndent(int i) throws IOException {
for (int baseIndent = this.options.getBaseIndent() + i; baseIndent > 0; baseIndent--) {
this.writer.write(this.options.getIndent());
}
}
private void writeNewline() throws IOException {
this.writer.write(this.options.getNewline());
}
private void writeTreeName() throws IOException {
write(34);
String name = this.xmp.getRoot().getName();
if (name != null) {
appendNodeValue(name, true);
}
write(34);
}
protected void checkOptionsConsistence() throws XMPException {
if (this.options.getEncodeUTF16BE() | this.options.getEncodeUTF16LE()) {
this.unicodeSize = 2;
}
if (this.options.getExactPacketLength()) {
if (this.options.getOmitPacketWrapper() || this.options.getIncludeThumbnailPad()) {
throw new XMPException("Inconsistent options for exact size serialize", 103);
}
if ((this.options.getPadding() & (this.unicodeSize - 1)) != 0) {
throw new XMPException("Exact size must be a multiple of the Unicode element", 103);
}
return;
}
if (this.options.getReadOnlyPacket()) {
if (this.options.getOmitPacketWrapper() | this.options.getIncludeThumbnailPad()) {
throw new XMPException("Inconsistent options for read-only packet", 103);
}
} else if (!this.options.getOmitPacketWrapper()) {
if (this.padding == 0) {
this.padding = this.unicodeSize * 2048;
}
if (!this.options.getIncludeThumbnailPad() || this.xmp.doesPropertyExist("http://ns.adobe.com/xap/1.0/", "Thumbnails")) {
return;
}
this.padding += this.unicodeSize * 10000;
return;
} else if (this.options.getIncludeThumbnailPad()) {
throw new XMPException("Inconsistent options for non-packet serialize", 103);
}
this.padding = 0;
}
public void serialize(XMPMeta xMPMeta, OutputStream outputStream, SerializeOptions serializeOptions) throws XMPException {
try {
this.outputStream = new CountOutputStream(outputStream);
this.writer = new OutputStreamWriter(this.outputStream, serializeOptions.getEncoding());
this.xmp = (XMPMetaImpl) xMPMeta;
this.options = serializeOptions;
this.padding = serializeOptions.getPadding();
this.writer = new OutputStreamWriter(this.outputStream, serializeOptions.getEncoding());
checkOptionsConsistence();
String serializeAsRDF = serializeAsRDF();
this.writer.flush();
addPadding(serializeAsRDF.length());
write(serializeAsRDF);
this.writer.flush();
this.outputStream.close();
} catch (IOException unused) {
throw new XMPException("Error writing to the OutputStream", 0);
}
}
}