XMPNodeCopyReplaceVisitor.java
5.08 KB
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
/*
* Decompiled with CFR 0_118.
*/
package com.adobe.xmp.core.impl;
import com.adobe.xmp.core.XMPArray;
import com.adobe.xmp.core.XMPNode;
import com.adobe.xmp.core.XMPNodeVisitor;
import com.adobe.xmp.core.XMPQualifiers;
import com.adobe.xmp.core.XMPSimple;
import com.adobe.xmp.core.XMPStruct;
import java.util.Stack;
class XMPNodeCopyReplaceVisitor
implements XMPNodeVisitor {
Stack<XMPNode> stack = new Stack();
public XMPNodeCopyReplaceVisitor(XMPNode root) {
this.stack.push(root);
}
public void visit(XMPSimple sSimple) {
XMPNode currentNode = this.stack.peek();
if (!(currentNode instanceof XMPSimple)) {
throw new IllegalStateException("Expected XMPSimple node type.");
}
XMPSimple simpleTarget = (XMPSimple)currentNode;
simpleTarget.setValue(sSimple.getValue());
simpleTarget.setURI(sSimple.isURI());
this.handleQualifiers(sSimple, simpleTarget);
}
public void visit(XMPStruct sStruct) {
XMPNode currentNode = this.stack.peek();
if (!(currentNode instanceof XMPStruct)) {
throw new IllegalStateException("Expected XMPStruct node type.");
}
XMPStruct tStruct = (XMPStruct)currentNode;
for (XMPNode sChild : sStruct) {
XMPStruct struct;
XMPNode newChild = null;
if (sChild instanceof XMPSimple) {
XMPSimple simple = (XMPSimple)sChild;
newChild = tStruct.setSimple(simple.getNamespace(), simple.getName(), simple.getValue());
} else if (sChild instanceof XMPArray) {
XMPArray array = (XMPArray)sChild;
newChild = tStruct.getArray(array.getNamespace(), array.getName());
if (newChild == null || ((XMPArray)newChild).getForm() != array.getForm()) {
newChild = tStruct.setArray(array.getNamespace(), array.getName(), array.getForm());
}
} else if (sChild instanceof XMPStruct && (newChild = tStruct.getStruct((struct = (XMPStruct)sChild).getNamespace(), struct.getName())) == null) {
newChild = tStruct.setStruct(struct.getNamespace(), struct.getName());
}
this.visitChild(sChild, newChild);
}
this.handleQualifiers(sStruct, tStruct);
}
public void visit(XMPArray sArray) {
XMPNode root = this.stack.peek();
if (!(root instanceof XMPArray)) {
throw new IllegalStateException("Expected XMPArray node type.");
}
XMPArray tArray = (XMPArray)root;
int index = 0;
for (XMPNode sChild : sArray) {
XMPNode newChild;
XMPNode xMPNode = newChild = index >= tArray.size() ? null : tArray.get(index);
if (sChild instanceof XMPSimple) {
newChild = tArray.setSimple(index, ((XMPSimple)sChild).getValue());
} else if (sChild instanceof XMPArray) {
if (!(newChild instanceof XMPArray) || ((XMPArray)newChild).getForm() != ((XMPArray)sChild).getForm()) {
newChild = tArray.setArray(index, ((XMPArray)sChild).getForm());
}
} else if (sChild instanceof XMPStruct && !(newChild instanceof XMPStruct)) {
newChild = tArray.setStruct(index);
}
this.visitChild(sChild, newChild);
++index;
}
this.handleQualifiers(sArray, tArray);
}
private void visitChild(XMPNode sourceChild, XMPNode copyTo) {
this.stack.push(copyTo);
sourceChild.accept(this);
this.stack.pop();
}
private void handleQualifiers(XMPNode sourceNode, XMPNode targetNode) {
if (sourceNode.hasQualifiers()) {
this.populateQualifiers(sourceNode.accessQualifiers(), targetNode.accessQualifiers());
}
}
private void populateQualifiers(XMPQualifiers sQuals, XMPQualifiers tQuals) {
for (XMPNode sChild : sQuals) {
XMPNode tChild = tQuals.get(sChild.getNamespace(), sChild.getName());
if (sChild instanceof XMPSimple) {
if (tChild instanceof XMPSimple) continue;
XMPSimple simple = (XMPSimple)sChild;
XMPSimple tSimple = tQuals.setSimple(simple.getNamespace(), simple.getName(), simple.getValue());
this.visitChild((XMPSimple)sChild, tSimple);
continue;
}
if (sChild instanceof XMPArray) {
if (tChild instanceof XMPArray && ((XMPArray)tChild).getForm() == ((XMPArray)sChild).getForm()) continue;
XMPArray array = (XMPArray)sChild;
XMPArray tChildArray = tQuals.setArray(array.getNamespace(), array.getName(), array.getForm());
this.visitChild((XMPArray)sChild, tChildArray);
continue;
}
if (!(sChild instanceof XMPStruct) || tChild instanceof XMPStruct) continue;
XMPStruct struct = (XMPStruct)sChild;
XMPStruct tChildStruct = tQuals.setStruct(struct.getNamespace(), struct.getName());
this.visitChild((XMPStruct)sChild, tChildStruct);
}
}
}