XMPNodeImpl.java
12.1 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
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
/*
* Decompiled with CFR 0_118.
*/
package com.adobe.xmp.core.impl;
import com.adobe.xmp.core.XMPArray;
import com.adobe.xmp.core.XMPException;
import com.adobe.xmp.core.XMPLanguageAlternative;
import com.adobe.xmp.core.XMPMetadata;
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 com.adobe.xmp.core.impl.XMPNodeCopyReplaceVisitor;
import com.adobe.xmp.core.impl.XMPQualifiersImpl;
import com.adobe.xmp.path.XMPPath;
import com.adobe.xmp.path.XMPPathSegment;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
abstract class XMPNodeImpl
implements XMPNode {
private String namespace;
private String name;
private XMPNode parent;
private XMPQualifiers qualifiers = null;
XMPNodeImpl(XMPNode parent, String namespace, String name) {
this.parent = parent;
this.namespace = namespace;
this.name = name;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getNamespace() {
return this.namespace;
}
public void setNamespace(String namespace) {
this.namespace = namespace;
}
public XMPNode getParent() {
return this.parent;
}
public XMPQualifiers accessQualifiers() {
if (this.qualifiers == null) {
this.qualifiers = new XMPQualifiersImpl(this);
}
return this.qualifiers;
}
public boolean isArrayItem() {
return this.getParent() != null && this.getParent() instanceof XMPArray;
}
public boolean hasQualifiers() {
return this.qualifiers != null && this.qualifiers.size() != 0;
}
public XMPPath getXMPPath() {
ArrayList<XMPNodeImpl> pathToParent = new ArrayList<XMPNodeImpl>();
XMPNode node = this;
while (node != null) {
pathToParent.add((XMPNodeImpl)node);
if (node instanceof XMPQualifiers) {
node = ((XMPQualifiers)node).getHost();
continue;
}
node = node.getParent();
}
XMPPath xmpPath = new XMPPath();
Collections.reverse(pathToParent);
XMPNode parent = null;
for (XMPNode pNode : pathToParent) {
if (pNode.getParent() == null) continue;
XMPPathSegment segment = this.createSegment(pNode, parent);
if (segment != null) {
xmpPath.add(segment);
}
parent = pNode;
}
return xmpPath;
}
private XMPPathSegment createSegment(XMPNode node, XMPNode parent) {
XMPPathSegment segment = null;
if (parent != null && parent.hasQualifiers()) {
XMPQualifiers qualifiers = parent.accessQualifiers();
for (XMPNode qualifier : qualifiers) {
if (qualifier != node) continue;
return XMPPathSegment.createQualifierSegment(node.getNamespace(), node.getName());
}
}
if (parent instanceof XMPArray) {
int index = 0;
for (XMPNode child : parent) {
++index;
if (child != node) continue;
break;
}
segment = XMPPathSegment.createArrayIndexSegment(parent.getNamespace(), index);
} else {
segment = XMPPathSegment.createPropertySegment(node.getNamespace(), node.getName());
}
return segment;
}
public XMPNode get(XMPPath path) {
XMPNodeImpl parent = this;
XMPNode current = this;
XMPNodeImpl res = null;
Iterator<XMPPathSegment> pathIterator = path.iterator();
while (pathIterator.hasNext() && current != null) {
XMPPathSegment seg = pathIterator.next();
switch (seg.getType()) {
case PROPERTY: {
if (parent instanceof XMPStruct) {
current = parent.adaptTo(XMPStruct.class).get(seg.getNamespace(), seg.getName());
break;
}
current = null;
break;
}
case ARRAY_INDEX: {
if (parent instanceof XMPArray) {
XMPArray array = parent.adaptTo(XMPArray.class);
if (array.size() >= seg.getIndex()) {
current = array.get(seg.getIndex() - 1);
break;
}
current = null;
break;
}
current = null;
break;
}
case QUALIFIER: {
if (parent.hasQualifiers()) {
current = parent.accessQualifiers().get(seg.getNamespace(), seg.getName());
break;
}
current = null;
break;
}
case QUALIFIER_SELECTOR: {
if (parent instanceof XMPArray) {
current = this.getArrayItemBasedOnSimpleQual(parent.adaptTo(XMPArray.class), seg.getNamespace(), seg.getName(), seg.getValue());
break;
}
current = null;
break;
}
default: {
current = null;
}
}
parent = res = current;
}
return res;
}
public XMPNode remove(XMPPath path) throws XMPException {
XMPNode parent;
XMPNode node = this.get(path);
XMPNode removedNode = null;
if (node != null && (parent = node.getParent()) != null) {
if (parent instanceof XMPArray) {
assert (node.isArrayItem());
XMPPathSegment selector = path.get(path.size() - 1);
if (selector.getType().equals((Object)XMPPathSegment.Type.ARRAY_INDEX)) {
int index = selector.getIndex() - 1;
removedNode = parent.adaptTo(XMPArray.class).remove(index);
} else if (selector.getType().equals((Object)XMPPathSegment.Type.QUALIFIER_SELECTOR)) {
removedNode = this.removeArrayItemBasedOnSimpleQual(parent.adaptTo(XMPArray.class), selector.getNamespace(), selector.getName(), selector.getValue());
}
} else if (parent instanceof XMPStruct) {
if (parent instanceof XMPQualifiers) {
XMPQualifiers quals = parent.adaptTo(XMPQualifiers.class);
removedNode = quals.remove(node.getNamespace(), node.getName());
} else {
removedNode = parent.adaptTo(XMPStruct.class).remove(node.getNamespace(), node.getName());
}
}
}
return removedNode;
}
public XMPSimple getSimple(XMPPath path) {
XMPNode node = this.get(path);
return node.adaptTo(XMPSimple.class);
}
public XMPStruct getStruct(XMPPath path) {
XMPNode node = this.get(path);
return node.adaptTo(XMPStruct.class);
}
public XMPArray getArray(XMPPath path) {
XMPNode node = this.get(path);
return node.adaptTo(XMPArray.class);
}
public XMPLanguageAlternative getLanguageAlternative(XMPPath path) {
XMPArray array = this.getArray(path);
if (array != null) {
return XMPLanguageAlternative.newInstance(array);
}
return null;
}
public String dump() {
StringBuffer result = new StringBuffer(512);
this.dumpNode(result, true, 0, 0);
return result.toString();
}
private void writeIndent(StringBuffer result, int indent) {
for (int i = 0; i < indent; ++i) {
result.append('\t');
}
}
private void writeNS(StringBuffer result) {
if (!(this.getParent() instanceof XMPArray)) {
result.append(this.namespace);
result.append(" | ");
}
}
protected void dumpNode(StringBuffer result, boolean recursive, int indent, int index) {
if (this.parent != null) {
this.writeIndent(result, indent);
if (this.getParent() instanceof XMPQualifiers) {
result.append('?');
result.append(this.name);
} else if (this.getParent() instanceof XMPArray) {
result.append('[');
result.append(index);
result.append(']');
} else {
result.append(this.name);
}
} else if (!(this instanceof XMPQualifiers)) {
String name;
result.append("ROOT NODE");
if (this instanceof XMPMetadata && (name = this.adaptTo(XMPMetadata.class).getAboutURI()) != null) {
result.append("\t( ");
result.append(name);
result.append(" )");
}
}
if (this instanceof XMPSimple) {
String value = this.adaptTo(XMPSimple.class).getValue();
if (value != null && value.length() > 0) {
result.append(" = \"");
result.append(value);
result.append('\"');
result.append("\t( ");
this.writeNS(result);
result.append("SIMPLE");
if (this.adaptTo(XMPSimple.class).isURI()) {
result.append(" | URI");
}
result.append(" )");
}
} else if (this instanceof XMPArray) {
result.append("\t( ");
this.writeNS(result);
result.append((Object)this.adaptTo(XMPArray.class).getForm());
result.append(" ARRAY");
result.append(" )");
} else if (this instanceof XMPStruct && this.parent != null) {
result.append("\t( ");
this.writeNS(result);
result.append("STRUCT");
result.append(" )");
}
if (!(this instanceof XMPQualifiers)) {
result.append('\n');
}
if (recursive) {
if (this.qualifiers != null && this.qualifiers.size() != 0) {
((XMPNodeImpl)((Object)this.qualifiers)).dumpNode(result, recursive, indent, 0);
}
Iterator<XMPNode> it = this.iterator();
int cnt = 0;
while (it.hasNext()) {
XMPNode node = it.next();
((XMPNodeImpl)node).dumpNode(result, recursive, indent + 1, cnt + 1);
++cnt;
}
}
}
public void copyReplace(XMPNode copyFrom) {
if (copyFrom == null) {
throw new IllegalArgumentException("XMPNode from which copying is to be done should not be null.");
}
if (this.getClass() != copyFrom.getClass()) {
throw new IllegalArgumentException("XMPNode from which copying is to be done should be of the same type as current node.");
}
XMPNodeCopyReplaceVisitor copyReplaceVisitor = new XMPNodeCopyReplaceVisitor(this);
copyFrom.accept(copyReplaceVisitor);
}
private XMPNode getArrayItemBasedOnSimpleQual(XMPArray array, String qualNS, String qualName, String qualValue) {
for (XMPNode node : array) {
XMPNode qual;
if (!node.hasQualifiers() || !((qual = node.accessQualifiers().get(qualNS, qualName)) instanceof XMPSimple) || !qual.adaptTo(XMPSimple.class).getValue().equals(qualValue)) continue;
return node;
}
return null;
}
private XMPNode removeArrayItemBasedOnSimpleQual(XMPArray array, String qualNS, String qualName, String qualValue) {
Iterator<XMPNode> it = array.iterator();
while (it.hasNext()) {
XMPNode qual;
XMPNode node = it.next();
if (!node.hasQualifiers() || !((qual = node.accessQualifiers().get(qualNS, qualName)) instanceof XMPSimple) || !qual.adaptTo(XMPSimple.class).getValue().equals(qualValue)) continue;
it.remove();
return node;
}
return null;
}
}