SerializerImpl.java
28.4 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
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
/*
* Decompiled with CFR 0_118.
*/
package com.adobe.xmp.core.serializer.impl;
import com.adobe.xmp.core.XMPArray;
import com.adobe.xmp.core.XMPException;
import com.adobe.xmp.core.XMPMetadata;
import com.adobe.xmp.core.XMPMetadataFactory;
import com.adobe.xmp.core.XMPNode;
import com.adobe.xmp.core.XMPQualifiers;
import com.adobe.xmp.core.XMPSimple;
import com.adobe.xmp.core.XMPStruct;
import com.adobe.xmp.core.serializer.SerializeOptions;
import com.adobe.xmp.core.serializer.SerializeOptionsException;
import com.adobe.xmp.core.serializer.SerializeXMPException;
import com.adobe.xmp.core.serializer.XMPSerializer;
import com.adobe.xmp.core.serializer.impl.CountOutputStream;
import com.adobe.xmp.core.serializer.impl.NamespaceResolver;
import com.adobe.xmp.core.serializer.impl.PacketWriter;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
/*
* This class specifies class file version 49.0 but uses Java 6 signatures. Assumed Java 6.
*/
public class SerializerImpl
implements XMPSerializer {
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 = "\"?>";
private static final String RDF_XMPMETA_START = "<x:xmpmeta xmlns:x=\"adobe:ns:meta/\" x:xmptk=\"";
private static final String RDF_XMPMETA_END = "</x:xmpmeta>";
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_RDF_END = "</rdf:RDF>";
private static final String RDF_SCHEMA_END = "</rdf:Description>";
private static final String RDF_STRUCT_START = "<rdf:Description";
private static final String RDF_STRUCT_END = "</rdf:Description>";
private static final String RDF_EMPTY_STRUCT = "<rdf:Description/>";
private XMPMetadata xmp;
private CountOutputStream outputStream;
private PacketWriter writer;
private SerializeOptions options;
private int unicodeSize = 1;
private int remainingPadding;
private NamespaceResolver nsResolver;
@Override
public void serialize(XMPMetadata xmp, OutputStream out, Map<String, Object> serializeContext) throws IOException, XMPException {
this.options = this.getOptionsFromContext(serializeContext);
this.outputStream = new CountOutputStream(out);
this.writer = new PacketWriter(this.outputStream, this.options);
this.nsResolver = new NamespaceResolver(this.getPrefixContractFromContext(serializeContext));
this.xmp = xmp;
this.remainingPadding = this.options.getPadding();
this.checkOptionsConsistence();
String tailStr = this.serializeAsRDF();
this.writer.flush();
this.addPadding(tailStr.length());
this.writer.write(tailStr);
this.writer.flush();
this.outputStream.close();
}
@Override
public String serializeToString(XMPMetadata xmp, Map<String, Object> serializeContext) throws IOException, XMPException {
SerializeOptions optionsCopy;
this.options = this.getOptionsFromContext(serializeContext);
try {
optionsCopy = (SerializeOptions)this.options.clone();
}
catch (CloneNotSupportedException e1) {
optionsCopy = new SerializeOptions(new SerializeOptions.Option[0]);
}
optionsCopy.add(SerializeOptions.Option.ENCODE_UTF16BE);
HashMap<String, Object> newSerializeContext = new HashMap<String, Object>();
newSerializeContext.putAll(serializeContext);
newSerializeContext.put("serializeOptions", optionsCopy);
ByteArrayOutputStream out = new ByteArrayOutputStream(4096);
this.serialize(xmp, out, newSerializeContext);
try {
return out.toString(optionsCopy.getEncoding());
}
catch (UnsupportedEncodingException e) {
return out.toString();
}
}
@Override
public byte[] serializeToBuffer(XMPMetadata xmp, Map<String, Object> serializeContext) throws IOException, XMPException {
ByteArrayOutputStream out = new ByteArrayOutputStream(4096);
this.serialize(xmp, out, serializeContext);
return out.toByteArray();
}
private void addPadding(int tailLength) throws XMPException, IOException {
if (this.options.hasOption(SerializeOptions.Option.EXACT_PACKET_LENGTH)) {
int minSize = this.outputStream.getBytesWritten() + tailLength * this.unicodeSize;
if (minSize > this.options.getPadding()) {
throw new SerializeXMPException("Can't fit into specified packet size");
}
this.remainingPadding -= minSize;
}
this.remainingPadding /= this.unicodeSize;
int newlineLen = this.options.getNewline().length();
if (this.remainingPadding >= newlineLen) {
this.remainingPadding -= newlineLen;
while (this.remainingPadding >= 100 + newlineLen) {
this.writer.fillWithChar(100, ' ');
this.writer.newline();
this.remainingPadding -= 100 + newlineLen;
}
this.writer.fillWithChar(this.remainingPadding, ' ');
this.writer.newline();
} else {
this.writer.fillWithChar(this.remainingPadding, ' ');
}
}
protected void checkOptionsConsistence() throws XMPException {
if (this.options.hasOption(SerializeOptions.Option.ENCODE_UTF16BE) || this.options.hasOption(SerializeOptions.Option.ENCODE_UTF16LE)) {
this.unicodeSize = 2;
}
if (this.options.hasOption(SerializeOptions.Option.EXACT_PACKET_LENGTH)) {
if (this.options.hasOption(SerializeOptions.Option.NO_PACKET_WRAPPER) || this.options.hasOption(SerializeOptions.Option.INCLUDE_THUMBNAIL_PAD)) {
throw new SerializeOptionsException("Inconsistent options for exact size serialize");
}
if ((this.options.getPadding() & this.unicodeSize - 1) != 0) {
throw new SerializeOptionsException("Exact size must be a multiple of the Unicode element");
}
} else if (this.options.hasOption(SerializeOptions.Option.READONLY_PACKET)) {
if (this.options.hasOption(SerializeOptions.Option.NO_PACKET_WRAPPER) || this.options.hasOption(SerializeOptions.Option.INCLUDE_THUMBNAIL_PAD)) {
throw new SerializeOptionsException("Inconsistent options for read-only packet");
}
this.remainingPadding = 0;
} else if (this.options.hasOption(SerializeOptions.Option.NO_PACKET_WRAPPER)) {
if (this.options.hasOption(SerializeOptions.Option.INCLUDE_THUMBNAIL_PAD)) {
throw new SerializeOptionsException("Inconsistent options for non-packet serialize");
}
this.remainingPadding = 0;
} else {
if (this.remainingPadding == 0) {
this.remainingPadding = 2048 * this.unicodeSize;
}
if (this.options.hasOption(SerializeOptions.Option.INCLUDE_THUMBNAIL_PAD) && this.xmp.get("http://ns.adobe.com/xap/1.0/", "Thumbnails") != null) {
this.remainingPadding += 10000 * this.unicodeSize;
}
}
}
private String serializeAsRDF() throws IOException, XMPException {
int level = 0;
if (!this.options.hasOption(SerializeOptions.Option.NO_PACKET_WRAPPER)) {
this.writer.indent(level);
this.writer.write("<?xpacket begin=\"\ufeff\" id=\"W5M0MpCehiHzreSzNTczkc9d\"?>");
this.writer.newline();
}
if (!this.options.hasOption(SerializeOptions.Option.NO_XMPMETA_ELEMENT)) {
String versionString;
this.writer.indent(level);
this.writer.write("<x:xmpmeta xmlns:x=\"adobe:ns:meta/\" x:xmptk=\"");
if (!this.options.hasOption(SerializeOptions.Option.NO_VERSION_ATTRIBUTE) && (versionString = XMPMetadataFactory.getVersionString()) != null) {
this.writer.write(versionString);
}
this.writer.write("\">");
this.writer.newline();
++level;
}
this.writer.indent(level);
this.writer.write("<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">");
this.writer.newline();
if (this.options.hasOption(SerializeOptions.Option.CANONICAL_FORMAT)) {
this.serializeCanonicalRDFProperties(level);
} else {
this.serializeCompactRDFSchemas(level);
}
this.writer.indent(level);
this.writer.write("</rdf:RDF>");
this.writer.newline();
if (!this.options.hasOption(SerializeOptions.Option.NO_XMPMETA_ELEMENT)) {
this.writer.indent(--level);
this.writer.write("</x:xmpmeta>");
this.writer.newline();
}
String tailStr = "";
if (!this.options.hasOption(SerializeOptions.Option.NO_PACKET_WRAPPER)) {
for (level = this.options.getBaseIndent(); level > 0; --level) {
tailStr = tailStr + this.options.getIndent();
}
tailStr = tailStr + "<?xpacket end=\"";
tailStr = tailStr + (this.options.hasOption(SerializeOptions.Option.READONLY_PACKET) ? 'r' : 'w');
tailStr = tailStr + "\"?>";
}
return tailStr;
}
private void serializeCanonicalRDFProperties(int level) throws IOException, XMPException {
++level;
if (this.xmp.size() > 0) {
this.startOuterRDFDescription(this.xmp, level, true);
for (XMPNode property : this.xmp) {
this.serializeCanonicalRDFProperty(property, this.options.hasOption(SerializeOptions.Option.CANONICAL_FORMAT), false, level + 1);
}
this.endOuterRDFDescription(level);
} else {
this.writer.indent(level);
this.writer.write("<rdf:Description");
this.writer.write(" ");
this.writer.writeAttribute(this.createRDFQName("about"), this.xmp.getAboutURI());
this.writer.write("/>");
this.writer.newline();
}
}
private void serializeCanonicalRDFProperty(XMPNode node, boolean useCanonicalRDF, boolean emitAsRDFValue, int level) throws IOException, XMPException {
boolean emitEndTag = true;
boolean indentEndTag = true;
String elemName = emitAsRDFValue ? this.nsResolver.getCreatePrefix("http://www.w3.org/1999/02/22-rdf-syntax-ns#") + ":value" : (node.getParent() instanceof XMPArray ? this.nsResolver.getCreatePrefix("http://www.w3.org/1999/02/22-rdf-syntax-ns#") + ":li" : this.createQName(node));
this.writer.indent(level);
this.writer.write(60);
this.writer.write(elemName);
boolean hasGeneralQualifiers = false;
boolean hasRDFResourceQual = false;
if (node.hasQualifiers()) {
for (XMPNode qualifier : node.accessQualifiers()) {
if (!this.isRDFAttrQualifier(qualifier)) {
hasGeneralQualifiers = true;
continue;
}
boolean bl = hasRDFResourceQual = "http://www.w3.org/1999/02/22-rdf-syntax-ns#".equals(qualifier.getNamespace()) && "resource".equals(qualifier.getName());
XMPSimple simpleQual = qualifier.adaptTo(XMPSimple.class);
if (simpleQual == null || emitAsRDFValue) continue;
this.writer.write(32);
this.writer.writeAttribute(this.createQName(simpleQual), simpleQual.getValue());
}
}
if (hasGeneralQualifiers && !emitAsRDFValue) {
if (hasRDFResourceQual) {
// empty if block
}
if (useCanonicalRDF) {
this.writer.write(">");
this.writer.newline();
this.writer.indent(++level);
this.writer.write("<rdf:Description");
this.writer.write(">");
} else {
this.writer.write(" ");
this.writer.writeAttribute(this.createRDFQName("parseType"), "Resource");
this.writer.write(">");
}
this.writer.newline();
this.serializeCanonicalRDFProperty(node, useCanonicalRDF, true, level + 1);
if (node.hasQualifiers()) {
for (XMPNode qualifier : node.accessQualifiers()) {
if (this.isRDFAttrQualifier(qualifier)) continue;
this.serializeCanonicalRDFProperty(qualifier, useCanonicalRDF, false, level + 1);
}
}
if (useCanonicalRDF) {
this.writer.indent(level);
this.writer.write("</rdf:Description>");
this.writer.newline();
--level;
}
} else if (node instanceof XMPSimple) {
XMPSimple simpleProp = node.adaptTo(XMPSimple.class);
if (simpleProp.isURI()) {
this.writer.write(" ");
this.writer.writeAttribute(this.createRDFQName("resource"), simpleProp.getValue());
this.writer.write("/>");
this.writer.newline();
emitEndTag = false;
} else if (simpleProp.getValue().length() == 0) {
this.writer.write("/>");
this.writer.newline();
emitEndTag = false;
} else {
this.writer.write(62);
this.writer.writeTagBody(simpleProp.getValue());
indentEndTag = false;
}
} else if (node instanceof XMPArray) {
this.writer.write(62);
this.writer.newline();
this.emitRDFArrayTag(node, true, level + 1);
for (XMPNode child : node) {
this.serializeCanonicalRDFProperty(child, useCanonicalRDF, false, level + 2);
}
this.emitRDFArrayTag(node, false, level + 1);
} else if (!hasRDFResourceQual) {
XMPStruct structProp = node.adaptTo(XMPStruct.class);
if (structProp == null || structProp.size() == 0) {
if (useCanonicalRDF) {
this.writer.write(">");
this.writer.newline();
this.writer.indent(level + 1);
this.writer.write("<rdf:Description/>");
} else {
this.writer.write(" ");
this.writer.writeAttribute(this.createRDFQName("parseType"), "Resource");
this.writer.write("/>");
emitEndTag = false;
}
this.writer.newline();
} else {
if (useCanonicalRDF) {
this.writer.write(">");
this.writer.newline();
this.writer.indent(++level);
this.writer.write("<rdf:Description");
this.writer.write(">");
} else {
this.writer.write(" ");
this.writer.writeAttribute(this.createRDFQName("parseType"), "Resource");
this.writer.write(">");
}
this.writer.newline();
for (XMPNode child : node) {
this.serializeCanonicalRDFProperty(child, useCanonicalRDF, false, level + 1);
}
if (useCanonicalRDF) {
this.writer.indent(level);
this.writer.write("</rdf:Description>");
this.writer.newline();
--level;
}
}
} else {
for (XMPNode child : node) {
if (!SerializerImpl.canBeRDFAttrProp(child)) {
throw new SerializeXMPException("Can't mix rdf:resource and complex fields");
}
XMPSimple simpleProp = child.adaptTo(XMPSimple.class);
this.writer.newline();
this.writer.indent(level + 1);
this.writer.write(32);
this.writer.writeAttribute(this.createQName(node), simpleProp.getValue());
}
this.writer.write("/>");
this.writer.newline();
emitEndTag = false;
}
if (emitEndTag) {
if (indentEndTag) {
this.writer.indent(level);
}
this.writer.write("</");
this.writer.write(elemName);
this.writer.write(62);
this.writer.newline();
}
}
private String createQName(XMPNode node) {
return this.nsResolver.getCreatePrefix(node.getNamespace()) + ":" + node.getName();
}
private String createRDFQName(String localName) {
return this.nsResolver.getCreatePrefix("http://www.w3.org/1999/02/22-rdf-syntax-ns#") + ":" + localName;
}
private void serializeCompactRDFSchemas(int level) throws IOException, XMPException {
this.startOuterRDFDescription(this.xmp, ++level, false);
boolean allAreAttrs = true;
if (allAreAttrs &= this.serializeCompactRDFAttrProps(this.xmp, level + 1)) {
if (this.xmp.size() > 0) {
this.writer.write("/>");
this.writer.newline();
}
return;
}
this.writer.write(">");
this.writer.newline();
this.serializeCompactRDFElementProps(this.xmp, level + 1);
this.endOuterRDFDescription(level);
--level;
}
private void serializeCompactRDFElementProps(Iterable<XMPNode> parentProperty, int level) throws IOException, XMPException {
for (XMPNode property : parentProperty) {
if (SerializerImpl.canBeRDFAttrProp(property)) continue;
boolean emitEndTag = true;
boolean indentEndTag = true;
String elemName = property.isArrayItem() ? this.createRDFQName("li") : this.createQName(property);
this.writer.indent(level);
this.writer.write(60);
this.writer.write(elemName);
boolean hasGeneralQualifiers = false;
boolean hasRDFResourceQual = false;
if (property.hasQualifiers()) {
for (XMPNode qualifier : property.accessQualifiers()) {
if (!this.isRDFAttrQualifier(qualifier)) {
hasGeneralQualifiers = true;
continue;
}
XMPSimple simpleQual = qualifier.adaptTo(XMPSimple.class);
if (simpleQual == null) continue;
hasRDFResourceQual = "http://www.w3.org/1999/02/22-rdf-syntax-ns#".equals(simpleQual.getNamespace()) && "resource".equals(simpleQual.getName());
this.writer.write(32);
this.writer.writeAttribute(this.createQName(qualifier), simpleQual.getValue());
}
}
if (hasGeneralQualifiers) {
this.serializeCompactRDFGeneralQualifier(property, level);
} else if (property instanceof XMPSimple) {
Object[] result = this.serializeCompactRDFSimpleProp(property.adaptTo(XMPSimple.class));
emitEndTag = (Boolean)result[0];
indentEndTag = (Boolean)result[1];
} else if (property instanceof XMPArray) {
this.serializeCompactRDFArrayProp(property, level);
} else {
emitEndTag = this.serializeCompactRDFStructProp(property.adaptTo(XMPStruct.class), level, hasRDFResourceQual);
}
if (!emitEndTag) continue;
if (indentEndTag) {
this.writer.indent(level);
}
this.writer.write("</");
this.writer.write(elemName);
this.writer.write(62);
this.writer.newline();
}
}
private Object[] serializeCompactRDFSimpleProp(XMPSimple simpleProp) throws IOException {
Boolean emitEndTag = Boolean.TRUE;
Boolean indentEndTag = Boolean.TRUE;
if (simpleProp == null || simpleProp.getValue() == null || simpleProp.getValue().length() == 0) {
this.writer.write("/>");
this.writer.newline();
emitEndTag = Boolean.FALSE;
} else if (simpleProp.isURI()) {
this.writer.write(" ");
this.writer.writeAttribute(this.createRDFQName("resource"), simpleProp.getValue());
this.writer.write("/>");
this.writer.newline();
emitEndTag = Boolean.FALSE;
} else {
this.writer.write(62);
this.writer.writeTagBody(simpleProp.getValue());
indentEndTag = Boolean.FALSE;
}
return new Object[]{emitEndTag, indentEndTag};
}
private void serializeCompactRDFArrayProp(XMPNode array, int level) throws IOException, XMPException {
this.writer.write(62);
this.writer.newline();
this.emitRDFArrayTag(array, true, ++level);
this.serializeCompactRDFElementProps(array, level + 1);
this.emitRDFArrayTag(array, false, level);
--level;
}
private boolean serializeCompactRDFStructProp(XMPStruct struct, int level, boolean hasRDFResourceQual) throws IOException, XMPException {
++level;
boolean hasAttrFields = false;
boolean hasElemFields = false;
boolean emitEndTag = true;
for (XMPNode field : struct) {
if (SerializerImpl.canBeRDFAttrProp(field)) {
hasAttrFields = true;
} else {
hasElemFields = true;
}
if (!hasAttrFields || !hasElemFields) continue;
break;
}
if (hasRDFResourceQual && hasElemFields) {
throw new IOException("Can't mix rdf:resource qualifier and element fields");
}
if (struct.size() == 0) {
this.writer.write(" ");
this.writer.writeAttribute(this.createRDFQName("parseType"), "Resource");
this.writer.write("/>");
this.writer.newline();
emitEndTag = false;
} else if (!hasElemFields) {
this.serializeCompactRDFAttrProps(struct, level);
this.writer.write("/>");
this.writer.newline();
emitEndTag = false;
} else if (!hasAttrFields) {
this.writer.write(" ");
this.writer.writeAttribute(this.createRDFQName("parseType"), "Resource");
this.writer.write(">");
this.writer.newline();
this.serializeCompactRDFElementProps(struct, level);
} else {
this.writer.write(62);
this.writer.newline();
this.writer.indent(level);
this.writer.write("<rdf:Description");
this.serializeCompactRDFAttrProps(struct, level + 2);
this.writer.write(">");
this.writer.newline();
this.serializeCompactRDFElementProps(struct, level + 1);
this.writer.indent(level);
this.writer.write("</rdf:Description>");
this.writer.newline();
}
--level;
return emitEndTag;
}
private void serializeCompactRDFGeneralQualifier(XMPNode property, int level) throws IOException, XMPException {
this.writer.write(" ");
this.writer.writeAttribute(this.createRDFQName("parseType"), "Resource");
this.writer.write(">");
this.writer.newline();
this.serializeCanonicalRDFProperty(property, false, true, level + 1);
if (property.hasQualifiers()) {
for (XMPNode qualifier : property.accessQualifiers()) {
this.serializeCanonicalRDFProperty(qualifier, false, false, level + 1);
}
}
}
private boolean serializeCompactRDFAttrProps(Iterable<XMPNode> struct, int level) throws IOException {
boolean allAreAttrs = true;
for (XMPNode property : struct) {
if (SerializerImpl.canBeRDFAttrProp(property) && property instanceof XMPSimple) {
this.writer.newline();
this.writer.indent(level);
this.writer.writeAttribute(this.createQName(property), ((XMPSimple)property).getValue());
continue;
}
allAreAttrs = false;
}
return allAreAttrs;
}
private void startOuterRDFDescription(XMPMetadata xmp, int level, boolean closeTag) throws IOException {
this.writer.indent(level);
this.writer.write("<rdf:Description");
this.writer.write(" ");
this.writer.writeAttribute(this.createRDFQName("about"), xmp.getAboutURI());
HashSet<String> usedNamespaces = new HashSet<String>();
this.declareUsedNamespaces(xmp, usedNamespaces, level + 2);
if (xmp.size() == 0) {
this.writer.write("/>");
this.writer.newline();
} else if (closeTag) {
this.writer.write(62);
this.writer.newline();
}
}
private void endOuterRDFDescription(int level) throws IOException {
this.writer.indent(level);
this.writer.write("</rdf:Description>");
this.writer.newline();
}
private void emitRDFArrayTag(XMPNode node, boolean isStartTag, int indent) throws IOException {
XMPArray.Form form = XMPArray.Form.ALTERNATIVE;
if (node instanceof XMPArray) {
form = ((XMPArray)node).getForm();
}
if (isStartTag || node.size() > 0) {
this.writer.indent(indent);
this.writer.write(isStartTag ? "<" : "</");
if (form == XMPArray.Form.UNORDERED) {
this.writer.write(this.createRDFQName("Bag"));
} else if (form == XMPArray.Form.ORDERED) {
this.writer.write(this.createRDFQName("Seq"));
} else {
this.writer.write(this.createRDFQName("Alt"));
}
if (isStartTag && node.size() == 0) {
this.writer.write("/>");
} else {
this.writer.write(">");
}
this.writer.newline();
}
}
private void declareUsedNamespaces(XMPNode node, Set<String> usedNamespaces, int indent) throws IOException {
if (node instanceof XMPStruct) {
for (XMPNode child : node) {
this.declareNamespace(child.getNamespace(), usedNamespaces, indent);
}
}
for (XMPNode child : node) {
this.declareUsedNamespaces(child, usedNamespaces, indent);
}
if (node.hasQualifiers()) {
for (XMPNode qualifier : node.accessQualifiers()) {
this.declareNamespace(qualifier.getNamespace(), usedNamespaces, indent);
this.declareUsedNamespaces(qualifier, usedNamespaces, indent);
}
}
}
private void declareNamespace(String namespace, Set<String> usedNamespaces, int indent) throws IOException {
if (namespace != null && !usedNamespaces.contains(namespace)) {
String prefix = this.nsResolver.getCreatePrefix(namespace);
usedNamespaces.add(namespace);
this.writer.newline();
this.writer.indent(indent);
this.writer.writeAttribute("xmlns:" + prefix, namespace);
}
}
private static boolean canBeRDFAttrProp(XMPNode node) {
return node instanceof XMPSimple && !node.adaptTo(XMPSimple.class).isURI() && node.accessQualifiers().size() == 0 && !node.isArrayItem();
}
private boolean isRDFAttrQualifier(XMPNode qualifier) {
if ("http://www.w3.org/XML/1998/namespace".equals(qualifier.getNamespace())) {
return "lang".equals(qualifier.getName());
}
if ("http://www.w3.org/1999/02/22-rdf-syntax-ns#".equals(qualifier.getNamespace())) {
return "resource:ID:bagID:nodeID".indexOf(qualifier.getName()) >= 0;
}
return false;
}
private Map<String, String> getPrefixContractFromContext(Map<String, Object> serializeContext) {
Object prefixContract;
if (serializeContext != null && (prefixContract = serializeContext.get("prefixContract")) instanceof Map) {
return (Map)prefixContract;
}
return null;
}
private SerializeOptions getOptionsFromContext(Map<String, Object> serializeContext) {
Object serializeOptions;
if (serializeContext != null && (serializeOptions = serializeContext.get("serializeOptions")) instanceof SerializeOptions) {
return (SerializeOptions)serializeOptions;
}
return new SerializeOptions(new SerializeOptions.Option[0]);
}
}