Debian Codegraph @

src/com/adobe/internal/xmp/impl/XMPMetaImpl.java

raw symbols SCIP
1 // =================================================================================================
2 // ADOBE SYSTEMS INCORPORATED
3 // Copyright 2006 Adobe Systems Incorporated
4 // All Rights Reserved
5 //
6 // NOTICE: Adobe permits you to use, modify, and distribute this file in accordance with the terms
7 // of the Adobe license agreement accompanying it.
8 // =================================================================================================
9
10 package com.adobe.internal.xmp.impl;
11
12 import java.util.Calendar;
13 import java.util.Iterator;
14
15 import com.adobe.internal.xmp.XMPConst;
16 import com.adobe.internal.xmp.XMPDateTime;
17 import com.adobe.internal.xmp.XMPError;
18 import com.adobe.internal.xmp.XMPException;
19 import com.adobe.internal.xmp.XMPIterator;
20 import com.adobe.internal.xmp.XMPMeta;
21 import com.adobe.internal.xmp.XMPPathFactory;
22 import com.adobe.internal.xmp.XMPUtils;
23 import com.adobe.internal.xmp.impl.xpath.XMPPath;
24 import com.adobe.internal.xmp.impl.xpath.XMPPathParser;
25 import com.adobe.internal.xmp.options.IteratorOptions;
26 import com.adobe.internal.xmp.options.ParseOptions;
27 import com.adobe.internal.xmp.options.PropertyOptions;
28 import com.adobe.internal.xmp.properties.XMPProperty;
29
30
31 /**
32 * Implementation for {@link XMPMeta}.
33 *
34 * @author Stefan Makswit
35 * @version $Revision$
36 * @since 17.02.2006
37 */
38 public class XMPMetaImpl implements XMPMeta, XMPConst
39 {
40 /** Property values are Strings by default */
41 private static final int VALUE_STRING = 0;
42 /** */
43 private static final int VALUE_BOOLEAN = 1;
44 /** */
45 private static final int VALUE_INTEGER = 2;
46 /** */
47 private static final int VALUE_LONG = 3;
48 /** */
49 private static final int VALUE_DOUBLE = 4;
50 /** */
51 private static final int VALUE_DATE = 5;
52 /** */
53 private static final int VALUE_CALENDAR = 6;
54 /** */
55 private static final int VALUE_BASE64 = 7;
56
57 /** root of the metadata tree */
58 private XMPNode tree;
59 /** the xpacket processing instructions content */
60 private String packetHeader = null;
61
62
63 /**
64 * Constructor for an empty metadata object.
65 */
66 public XMPMetaImpl()
67 {
68 // create root node
69 tree = new XMPNode(null, null, null);
70 }
71
72
73 /**
74 * Constructor for a cloned metadata tree.
75 *
76 * @param tree
77 * an prefilled metadata tree which fulfills all
78 * <code>XMPNode</code> contracts.
79 */
80 public XMPMetaImpl(XMPNode tree)
81 {
82 this.tree = tree;
83 }
84
85
86 /**
87 * @see XMPMeta#appendArrayItem(String, String, PropertyOptions, String,
88 * PropertyOptions)
89 */
90 public void appendArrayItem(String schemaNS, String arrayName, PropertyOptions arrayOptions,
91 String itemValue, PropertyOptions itemOptions) throws XMPException
92 {
93 ParameterAsserts.assertSchemaNS(schemaNS);
94 ParameterAsserts.assertArrayName(arrayName);
95
96 if (arrayOptions == null)
97 {
98 arrayOptions = new PropertyOptions();
99 }
100 if (!arrayOptions.isOnlyArrayOptions())
101 {
102 throw new XMPException("Only array form flags allowed for arrayOptions",
103 XMPError.BADOPTIONS);
104 }
105
106 // Check if array options are set correctly.
107 // FfF: move logic into PropertyOptions?
108 arrayOptions = XMPNodeUtils.verifySetOptions(arrayOptions, null);
109
110
111 // Locate or create the array. If it already exists, make sure the array
112 // form from the options
113 // parameter is compatible with the current state.
114 XMPPath arrayPath = XMPPathParser.expandXPath(schemaNS, arrayName);
115
116
117 // Just lookup, don't try to create.
118 XMPNode arrayNode = XMPNodeUtils.findNode(tree, arrayPath, false, null);
119
120 if (arrayNode != null)
121 {
122 // The array exists, make sure the form is compatible. Zero
123 // arrayForm means take what exists.
124 if (!arrayNode.getOptions().isArray())
125 {
126 throw new XMPException("The named property is not an array", XMPError.BADXPATH);
127 }
128
129 // FfF: Disable for now. Need to do some general rethinking of semantic checks.
130 // if (arrayOptions != null && !arrayOptions.equalArrayTypes(arrayNode.getOptions()))
131 // {
132 // throw new XMPException("Mismatch of existing and specified array form", BADOPTIONS);
133 // }
134 }
135 else
136 {
137 // The array does not exist, try to create it.
138 if (arrayOptions.isArray())
139 {
140 // FfF: Must not call findNode twice
141 arrayNode = XMPNodeUtils.findNode(tree, arrayPath, true, arrayOptions);
142 if (arrayNode == null)
143 {
144 throw new XMPException("Failure creating array node", XMPError.BADXPATH);
145 }
146 }
147 else
148 {
149 // array options missing
150 throw new XMPException("Explicit arrayOptions required to create new array",
151 XMPError.BADOPTIONS);
152 }
153 }
154
155 doSetArrayItem(arrayNode, ARRAY_LAST_ITEM, itemValue, itemOptions, true);
156 }
157
158
159 /**
160 * @see XMPMeta#appendArrayItem(String, String, String)
161 */
162 public void appendArrayItem(String schemaNS, String arrayName, String itemValue)
163 throws XMPException
164 {
165 appendArrayItem(schemaNS, arrayName, null, itemValue, null);
166 }
167
168
169 /**
170 * @throws XMPException
171 * @see XMPMeta#countArrayItems(String, String)
172 */
173 public int countArrayItems(String schemaNS, String arrayName) throws XMPException
174 {
175 ParameterAsserts.assertSchemaNS(schemaNS);
176 ParameterAsserts.assertArrayName(arrayName);
177
178 XMPPath arrayPath = XMPPathParser.expandXPath(schemaNS, arrayName);
179 XMPNode arrayNode = XMPNodeUtils.findNode(tree, arrayPath, false, null);
180
181 if (arrayNode == null)
182 {
183 return 0;
184 }
185
186 if (arrayNode.getOptions().isArray())
187 {
188 return arrayNode.getChildrenLength();
189 }
190 else
191 {
192 throw new XMPException("The named property is not an array", XMPError.BADXPATH);
193 }
194 }
195
196
197 /**
198 * @see XMPMeta#deleteArrayItem(String, String, int)
199 */
200 public void deleteArrayItem(String schemaNS, String arrayName, int itemIndex)
201 {
202 try
203 {
204 ParameterAsserts.assertSchemaNS(schemaNS);
205 ParameterAsserts.assertArrayName(arrayName);
206
207 String itemPath = XMPPathFactory.composeArrayItemPath(arrayName, itemIndex);
208 deleteProperty(schemaNS, itemPath);
209 }
210 catch (XMPException e)
211 {
212 // EMPTY, exceptions are ignored within delete
213 }
214 }
215
216
217 /**
218 * @see XMPMeta#deleteProperty(String, String)
219 */
220 public void deleteProperty(String schemaNS, String propName)
221 {
222 try
223 {
224 ParameterAsserts.assertSchemaNS(schemaNS);
225 ParameterAsserts.assertPropName(propName);
226
227 XMPPath expPath = XMPPathParser.expandXPath(schemaNS, propName);
228
229 XMPNode propNode = XMPNodeUtils.findNode(tree, expPath, false, null);
230 if (propNode != null)
231 {
232 XMPNodeUtils.deleteNode(propNode);
233 }
234 }
235 catch (XMPException e)
236 {
237 // EMPTY, exceptions are ignored within delete
238 }
239 }
240
241
242 /**
243 * @see XMPMeta#deleteQualifier(String, String, String, String)
244 */
245 public void deleteQualifier(String schemaNS, String propName, String qualNS, String qualName)
246 {
247 try
248 {
249 // Note: qualNS and qualName are checked inside composeQualfierPath
250 ParameterAsserts.assertSchemaNS(schemaNS);
251 ParameterAsserts.assertPropName(propName);
252
253 String qualPath = propName + XMPPathFactory.composeQualifierPath(qualNS, qualName);
254 deleteProperty(schemaNS, qualPath);
255 }
256 catch (XMPException e)
257 {
258 // EMPTY, exceptions within delete are ignored
259 }
260 }
261
262
263 /**
264 * @see XMPMeta#deleteStructField(String, String, String, String)
265 */
266 public void deleteStructField(String schemaNS, String structName, String fieldNS,
267 String fieldName)
268 {
269 try
270 {
271 // fieldNS and fieldName are checked inside composeStructFieldPath
272 ParameterAsserts.assertSchemaNS(schemaNS);
273 ParameterAsserts.assertStructName(structName);
274
275 String fieldPath = structName
276 + XMPPathFactory.composeStructFieldPath(fieldNS, fieldName);
277 deleteProperty(schemaNS, fieldPath);
278 }
279 catch (XMPException e)
280 {
281 // EMPTY, exceptions within delete are ignored
282 }
283 }
284
285
286 /**
287 * @see XMPMeta#doesPropertyExist(String, String)
288 */
289 public boolean doesPropertyExist(String schemaNS, String propName)
290 {
291 try
292 {
293 ParameterAsserts.assertSchemaNS(schemaNS);
294 ParameterAsserts.assertPropName(propName);
295
296 XMPPath expPath = XMPPathParser.expandXPath(schemaNS, propName);
297 final XMPNode propNode = XMPNodeUtils.findNode(tree, expPath, false, null);
298 return propNode != null;
299 }
300 catch (XMPException e)
301 {
302 return false;
303 }
304 }
305
306
307 /**
308 * @see XMPMeta#doesArrayItemExist(String, String, int)
309 */
310 public boolean doesArrayItemExist(String schemaNS, String arrayName, int itemIndex)
311 {
312 try
313 {
314 ParameterAsserts.assertSchemaNS(schemaNS);
315 ParameterAsserts.assertArrayName(arrayName);
316
317 String path = XMPPathFactory.composeArrayItemPath(arrayName, itemIndex);
318 return doesPropertyExist(schemaNS, path);
319 }
320 catch (XMPException e)
321 {
322 return false;
323 }
324 }
325
326
327 /**
328 * @see XMPMeta#doesStructFieldExist(String, String, String, String)
329 */
330 public boolean doesStructFieldExist(String schemaNS, String structName, String fieldNS,
331 String fieldName)
332 {
333 try
334 {
335 // fieldNS and fieldName are checked inside composeStructFieldPath()
336 ParameterAsserts.assertSchemaNS(schemaNS);
337 ParameterAsserts.assertStructName(structName);
338
339 String path = XMPPathFactory.composeStructFieldPath(fieldNS, fieldName);
340 return doesPropertyExist(schemaNS, structName + path);
341 }
342 catch (XMPException e)
343 {
344 return false;
345 }
346 }
347
348
349 /**
350 * @see XMPMeta#doesQualifierExist(String, String, String, String)
351 */
352 public boolean doesQualifierExist(String schemaNS, String propName, String qualNS,
353 String qualName)
354 {
355 try
356 {
357 // qualNS and qualName are checked inside composeQualifierPath()
358 ParameterAsserts.assertSchemaNS(schemaNS);
359 ParameterAsserts.assertPropName(propName);
360
361 String path = XMPPathFactory.composeQualifierPath(qualNS, qualName);
362 return doesPropertyExist(schemaNS, propName + path);
363 }
364 catch (XMPException e)
365 {
366 return false;
367 }
368 }
369
370
371 /**
372 * @see XMPMeta#getArrayItem(String, String, int)
373 */
374 public XMPProperty getArrayItem(String schemaNS, String arrayName, int itemIndex)
375 throws XMPException
376 {
377 ParameterAsserts.assertSchemaNS(schemaNS);
378 ParameterAsserts.assertArrayName(arrayName);
379
380 String itemPath = XMPPathFactory.composeArrayItemPath(arrayName, itemIndex);
381 return getProperty(schemaNS, itemPath);
382 }
383
384
385 /**
386 * @throws XMPException
387 * @see XMPMeta#getLocalizedText(String, String, String, String)
388 */
389 public XMPProperty getLocalizedText(String schemaNS, String altTextName, String genericLang,
390 String specificLang) throws XMPException
391 {
392 ParameterAsserts.assertSchemaNS(schemaNS);
393 ParameterAsserts.assertArrayName(altTextName);
394 ParameterAsserts.assertSpecificLang(specificLang);
395
396 genericLang = genericLang != null ? Utils.normalizeLangValue(genericLang) : null;
397 specificLang = Utils.normalizeLangValue(specificLang);
398
399 XMPPath arrayPath = XMPPathParser.expandXPath(schemaNS, altTextName);
400 // *** This expand/find idiom is used in 3 Getters.
401 XMPNode arrayNode = XMPNodeUtils.findNode(tree, arrayPath, false, null);
402 if (arrayNode == null)
403 {
404 return null;
405 }
406
407 Object[] result = XMPNodeUtils.chooseLocalizedText(arrayNode, genericLang, specificLang);
408 int match = ((Integer) result[0]).intValue();
409 final XMPNode itemNode = (XMPNode) result[1];
410
411 if (match != XMPNodeUtils.CLT_NO_VALUES)
412 {
413 return new XMPProperty()
414 {
415 public String getValue()
416 {
417 return itemNode.getValue();
418 }
419
420
421 public PropertyOptions getOptions()
422 {
423 return itemNode.getOptions();
424 }
425
426
427 public String getLanguage()
428 {
429 return itemNode.getQualifier(1).getValue();
430 }
431
432
433 public String toString()
434 {
435 return itemNode.getValue().toString();
436 }
437 };
438 }
439 else
440 {
441 return null;
442 }
443 }
444
445
446 /**
447 * @see XMPMeta#setLocalizedText(String, String, String, String, String,
448 * PropertyOptions)
449 */
450 public void setLocalizedText(String schemaNS, String altTextName, String genericLang,
451 String specificLang, String itemValue, PropertyOptions options) throws XMPException
452 {
453 ParameterAsserts.assertSchemaNS(schemaNS);
454 ParameterAsserts.assertArrayName(altTextName);
455 ParameterAsserts.assertSpecificLang(specificLang);
456
457 genericLang = genericLang != null ? Utils.normalizeLangValue(genericLang) : null;
458 specificLang = Utils.normalizeLangValue(specificLang);
459
460 XMPPath arrayPath = XMPPathParser.expandXPath(schemaNS, altTextName);
461
462 // Find the array node and set the options if it was just created.
463 XMPNode arrayNode = XMPNodeUtils.findNode(tree, arrayPath, true, new PropertyOptions(
464 PropertyOptions.ARRAY | PropertyOptions.ARRAY_ORDERED
465 | PropertyOptions.ARRAY_ALTERNATE | PropertyOptions.ARRAY_ALT_TEXT));
466
467 if (arrayNode == null)
468 {
469 throw new XMPException("Failed to find or create array node", XMPError.BADXPATH);
470 }
471 else if (!arrayNode.getOptions().isArrayAltText())
472 {
473 if (!arrayNode.hasChildren() && arrayNode.getOptions().isArrayAlternate())
474 {
475 arrayNode.getOptions().setArrayAltText(true);
476 }
477 else
478 {
479 throw new XMPException(
480 "Specified property is no alt-text array", XMPError.BADXPATH);
481 }
482 }
483
484 // Make sure the x-default item, if any, is first.
485 boolean haveXDefault = false;
486 XMPNode xdItem = null;
487
488 for (Iterator it = arrayNode.iterateChildren(); it.hasNext();)
489 {
490 XMPNode currItem = (XMPNode) it.next();
491 if (!currItem.hasQualifier()
492 || !XMPConst.XML_LANG.equals(currItem.getQualifier(1).getName()))
493 {
494 throw new XMPException("Language qualifier must be first", XMPError.BADXPATH);
495 }
496 else if (XMPConst.X_DEFAULT.equals(currItem.getQualifier(1).getValue()))
497 {
498 xdItem = currItem;
499 haveXDefault = true;
500 break;
501 }
502 }
503
504 // Moves x-default to the beginning of the array
505 if (xdItem != null && arrayNode.getChildrenLength() > 1)
506 {
507 arrayNode.removeChild(xdItem);
508 arrayNode.addChild(1, xdItem);
509 }
510
511 // Find the appropriate item.
512 // chooseLocalizedText will make sure the array is a language
513 // alternative.
514 Object[] result = XMPNodeUtils.chooseLocalizedText(arrayNode, genericLang, specificLang);
515 int match = ((Integer) result[0]).intValue();
516 XMPNode itemNode = (XMPNode) result[1];
517
518 boolean specificXDefault = XMPConst.X_DEFAULT.equals(specificLang);
519
520 switch (match)
521 {
522 case XMPNodeUtils.CLT_NO_VALUES:
523
524 // Create the array items for the specificLang and x-default, with
525 // x-default first.
526 XMPNodeUtils.appendLangItem(arrayNode, XMPConst.X_DEFAULT, itemValue);
527 haveXDefault = true;
528 if (!specificXDefault)
529 {
530 XMPNodeUtils.appendLangItem(arrayNode, specificLang, itemValue);
531 }
532 break;
533
534 case XMPNodeUtils.CLT_SPECIFIC_MATCH:
535
536 if (!specificXDefault)
537 {
538 // Update the specific item, update x-default if it matches the
539 // old value.
540 if (haveXDefault && xdItem != itemNode && xdItem != null
541 && xdItem.getValue().equals(itemNode.getValue()))
542 {
543 xdItem.setValue(itemValue);
544 }
545 // ! Do this after the x-default check!
546 itemNode.setValue(itemValue);
547 }
548 else
549 {
550 // Update all items whose values match the old x-default value.
551 assert haveXDefault && xdItem == itemNode;
552 for (Iterator it = arrayNode.iterateChildren(); it.hasNext();)
553 {
554 XMPNode currItem = (XMPNode) it.next();
555 if (currItem == xdItem
556 || !currItem.getValue().equals(
557 xdItem != null ? xdItem.getValue() : null))
558 {
559 continue;
560 }
561 currItem.setValue(itemValue);
562 }
563 // And finally do the x-default item.
564 if (xdItem != null)
565 {
566 xdItem.setValue(itemValue);
567 }
568 }
569 break;
570
571 case XMPNodeUtils.CLT_SINGLE_GENERIC:
572
573 // Update the generic item, update x-default if it matches the old
574 // value.
575 if (haveXDefault && xdItem != itemNode && xdItem != null
576 && xdItem.getValue().equals(itemNode.getValue()))
577 {
578 xdItem.setValue(itemValue);
579 }
580 itemNode.setValue(itemValue); // ! Do this after
581 // the x-default
582 // check!
583 break;
584
585 case XMPNodeUtils.CLT_MULTIPLE_GENERIC:
586
587 // Create the specific language, ignore x-default.
588 XMPNodeUtils.appendLangItem(arrayNode, specificLang, itemValue);
589 if (specificXDefault)
590 {
591 haveXDefault = true;
592 }
593 break;
594
595 case XMPNodeUtils.CLT_XDEFAULT:
596
597 // Create the specific language, update x-default if it was the only
598 // item.
599 if (xdItem != null && arrayNode.getChildrenLength() == 1)
600 {
601 xdItem.setValue(itemValue);
602 }
603 XMPNodeUtils.appendLangItem(arrayNode, specificLang, itemValue);
604 break;
605
606 case XMPNodeUtils.CLT_FIRST_ITEM:
607
608 // Create the specific language, don't add an x-default item.
609 XMPNodeUtils.appendLangItem(arrayNode, specificLang, itemValue);
610 if (specificXDefault)
611 {
612 haveXDefault = true;
613 }
614 break;
615
616 default:
617 // does not happen under normal circumstances
618 throw new XMPException("Unexpected result from ChooseLocalizedText",
619 XMPError.INTERNALFAILURE);
620
621 }
622
623 // Add an x-default at the front if needed.
624 if (!haveXDefault && arrayNode.getChildrenLength() == 1)
625 {
626 XMPNodeUtils.appendLangItem(arrayNode, XMPConst.X_DEFAULT, itemValue);
627 }
628 }
629
630
631 /**
632 * @see XMPMeta#setLocalizedText(String, String, String, String, String)
633 */
634 public void setLocalizedText(String schemaNS, String altTextName, String genericLang,
635 String specificLang, String itemValue) throws XMPException
636 {
637 setLocalizedText(schemaNS, altTextName, genericLang, specificLang, itemValue, null);
638 }
639
640
641 /**
642 * @throws XMPException
643 * @see XMPMeta#getProperty(String, String)
644 */
645 public XMPProperty getProperty(String schemaNS, String propName) throws XMPException
646 {
647 return getProperty(schemaNS, propName, VALUE_STRING);
648 }
649
650
651 /**
652 * Returns a property, but the result value can be requested. It can be one
653 * of {@link XMPMetaImpl#VALUE_STRING}, {@link XMPMetaImpl#VALUE_BOOLEAN},
654 * {@link XMPMetaImpl#VALUE_INTEGER}, {@link XMPMetaImpl#VALUE_LONG},
655 * {@link XMPMetaImpl#VALUE_DOUBLE}, {@link XMPMetaImpl#VALUE_DATE},
656 * {@link XMPMetaImpl#VALUE_CALENDAR}, {@link XMPMetaImpl#VALUE_BASE64}.
657 *
658 * @see XMPMeta#getProperty(String, String)
659 * @param schemaNS
660 * a schema namespace
661 * @param propName
662 * a property name or path
663 * @param valueType
664 * the type of the value, see VALUE_...
665 * @return Returns an <code>XMPProperty</code>
666 * @throws XMPException
667 * Collects any exception that occurs.
668 */
669 protected XMPProperty getProperty(String schemaNS, String propName, int valueType)
670 throws XMPException
671 {
672 ParameterAsserts.assertSchemaNS(schemaNS);
673 ParameterAsserts.assertPropName(propName);
674
675 final XMPPath expPath = XMPPathParser.expandXPath(schemaNS, propName);
676 final XMPNode propNode = XMPNodeUtils.findNode(tree, expPath, false, null);
677
678 if (propNode != null)
679 {
680 if (valueType != VALUE_STRING && propNode.getOptions().isCompositeProperty())
681 {
682 throw new XMPException("Property must be simple when a value type is requested",
683 XMPError.BADXPATH);
684 }
685
686 final Object value = evaluateNodeValue(valueType, propNode);
687
688 return new XMPProperty()
689 {
690 public String getValue()
691 {
692 return value != null ? value.toString() : null;
693 }
694
695
696 public PropertyOptions getOptions()
697 {
698 return propNode.getOptions();
699 }
700
701
702 public String getLanguage()
703 {
704 return null;
705 }
706
707
708 public String toString()
709 {
710 return value.toString();
711 }
712 };
713 }
714 else
715 {
716 return null;
717 }
718 }
719
720
721 /**
722 * Returns a property, but the result value can be requested.
723 *
724 * @see XMPMeta#getProperty(String, String)
725 * @param schemaNS
726 * a schema namespace
727 * @param propName
728 * a property name or path
729 * @param valueType
730 * the type of the value, see VALUE_...
731 * @return Returns the node value as an object according to the
732 * <code>valueType</code>.
733 * @throws XMPException
734 * Collects any exception that occurs.
735 */
736 protected Object getPropertyObject(String schemaNS, String propName, int valueType)
737 throws XMPException
738 {
739 ParameterAsserts.assertSchemaNS(schemaNS);
740 ParameterAsserts.assertPropName(propName);
741
742 final XMPPath expPath = XMPPathParser.expandXPath(schemaNS, propName);
743 final XMPNode propNode = XMPNodeUtils.findNode(tree, expPath, false, null);
744
745 if (propNode != null)
746 {
747 if (valueType != VALUE_STRING && propNode.getOptions().isCompositeProperty())
748 {
749 throw new XMPException("Property must be simple when a value type is requested",
750 XMPError.BADXPATH);
751 }
752
753 return evaluateNodeValue(valueType, propNode);
754 }
755 else
756 {
757 return null;
758 }
759 }
760
761
762 /**
763 * @see XMPMeta#getPropertyBoolean(String, String)
764 */
765 public Boolean getPropertyBoolean(String schemaNS, String propName) throws XMPException
766 {
767 return (Boolean) getPropertyObject(schemaNS, propName, VALUE_BOOLEAN);
768 }
769
770
771 /**
772 * @throws XMPException
773 * @see XMPMeta#setPropertyBoolean(String, String, boolean, PropertyOptions)
774 */
775 public void setPropertyBoolean(String schemaNS, String propName, boolean propValue,
776 PropertyOptions options) throws XMPException
777 {
778 setProperty(schemaNS, propName, propValue ? TRUESTR : FALSESTR, options);
779 }
780
781
782 /**
783 * @see XMPMeta#setPropertyBoolean(String, String, boolean)
784 */
785 public void setPropertyBoolean(String schemaNS, String propName, boolean propValue)
786 throws XMPException
787 {
788 setProperty(schemaNS, propName, propValue ? TRUESTR : FALSESTR, null);
789 }
790
791
792 /**
793 * @see XMPMeta#getPropertyInteger(String, String)
794 */
795 public Integer getPropertyInteger(String schemaNS, String propName) throws XMPException
796 {
797 return (Integer) getPropertyObject(schemaNS, propName, VALUE_INTEGER);
798 }
799
800
801 /**
802 * @see XMPMeta#setPropertyInteger(String, String, int, PropertyOptions)
803 */
804 public void setPropertyInteger(String schemaNS, String propName, int propValue,
805 PropertyOptions options) throws XMPException
806 {
807 setProperty(schemaNS, propName, new Integer(propValue), options);
808 }
809
810
811 /**
812 * @see XMPMeta#setPropertyInteger(String, String, int)
813 */
814 public void setPropertyInteger(String schemaNS, String propName, int propValue)
815 throws XMPException
816 {
817 setProperty(schemaNS, propName, new Integer(propValue), null);
818 }
819
820
821 /**
822 * @see XMPMeta#getPropertyLong(String, String)
823 */
824 public Long getPropertyLong(String schemaNS, String propName) throws XMPException
825 {
826 return (Long) getPropertyObject(schemaNS, propName, VALUE_LONG);
827 }
828
829
830 /**
831 * @see XMPMeta#setPropertyLong(String, String, long, PropertyOptions)
832 */
833 public void setPropertyLong(String schemaNS, String propName, long propValue,
834 PropertyOptions options) throws XMPException
835 {
836 setProperty(schemaNS, propName, new Long(propValue), options);
837 }
838
839
840 /**
841 * @see XMPMeta#setPropertyLong(String, String, long)
842 */
843 public void setPropertyLong(String schemaNS, String propName, long propValue)
844 throws XMPException
845 {
846 setProperty(schemaNS, propName, new Long(propValue), null);
847 }
848
849
850 /**
851 * @see XMPMeta#getPropertyDouble(String, String)
852 */
853 public Double getPropertyDouble(String schemaNS, String propName) throws XMPException
854 {
855 return (Double) getPropertyObject(schemaNS, propName, VALUE_DOUBLE);
856 }
857
858
859 /**
860 * @see XMPMeta#setPropertyDouble(String, String, double, PropertyOptions)
861 */
862 public void setPropertyDouble(String schemaNS, String propName, double propValue,
863 PropertyOptions options) throws XMPException
864 {
865 setProperty(schemaNS, propName, new Double(propValue), options);
866 }
867
868
869 /**
870 * @see XMPMeta#setPropertyDouble(String, String, double)
871 */
872 public void setPropertyDouble(String schemaNS, String propName, double propValue)
873 throws XMPException
874 {
875 setProperty(schemaNS, propName, new Double(propValue), null);
876 }
877
878
879 /**
880 * @see XMPMeta#getPropertyDate(String, String)
881 */
882 public XMPDateTime getPropertyDate(String schemaNS, String propName) throws XMPException
883 {
884 return (XMPDateTime) getPropertyObject(schemaNS, propName, VALUE_DATE);
885 }
886
887
888 /**
889 * @see XMPMeta#setPropertyDate(String, String, XMPDateTime,
890 * PropertyOptions)
891 */
892 public void setPropertyDate(String schemaNS, String propName, XMPDateTime propValue,
893 PropertyOptions options) throws XMPException
894 {
895 setProperty(schemaNS, propName, propValue, options);
896 }
897
898
899 /**
900 * @see XMPMeta#setPropertyDate(String, String, XMPDateTime)
901 */
902 public void setPropertyDate(String schemaNS, String propName, XMPDateTime propValue)
903 throws XMPException
904 {
905 setProperty(schemaNS, propName, propValue, null);
906 }
907
908
909 /**
910 * @see XMPMeta#getPropertyCalendar(String, String)
911 */
912 public Calendar getPropertyCalendar(String schemaNS, String propName) throws XMPException
913 {
914 return (Calendar) getPropertyObject(schemaNS, propName, VALUE_CALENDAR);
915 }
916
917
918 /**
919 * @see XMPMeta#setPropertyCalendar(String, String, Calendar,
920 * PropertyOptions)
921 */
922 public void setPropertyCalendar(String schemaNS, String propName, Calendar propValue,
923 PropertyOptions options) throws XMPException
924 {
925 setProperty(schemaNS, propName, propValue, options);
926 }
927
928
929 /**
930 * @see XMPMeta#setPropertyCalendar(String, String, Calendar)
931 */
932 public void setPropertyCalendar(String schemaNS, String propName, Calendar propValue)
933 throws XMPException
934 {
935 setProperty(schemaNS, propName, propValue, null);
936 }
937
938
939 /**
940 * @see XMPMeta#getPropertyBase64(String, String)
941 */
942 public byte[] getPropertyBase64(String schemaNS, String propName) throws XMPException
943 {
944 return (byte[]) getPropertyObject(schemaNS, propName, VALUE_BASE64);
945 }
946
947
948 /**
949 * @see XMPMeta#getPropertyString(String, String)
950 */
951 public String getPropertyString(String schemaNS, String propName) throws XMPException
952 {
953 return (String) getPropertyObject(schemaNS, propName, VALUE_STRING);
954 }
955
956
957 /**
958 * @see XMPMeta#setPropertyBase64(String, String, byte[], PropertyOptions)
959 */
960 public void setPropertyBase64(String schemaNS, String propName, byte[] propValue,
961 PropertyOptions options) throws XMPException
962 {
963 setProperty(schemaNS, propName, propValue, options);
964 }
965
966
967 /**
968 * @see XMPMeta#setPropertyBase64(String, String, byte[])
969 */
970 public void setPropertyBase64(String schemaNS, String propName, byte[] propValue)
971 throws XMPException
972 {
973 setProperty(schemaNS, propName, propValue, null);
974 }
975
976
977 /**
978 * @throws XMPException
979 * @see XMPMeta#getQualifier(String, String, String, String)
980 */
981 public XMPProperty getQualifier(String schemaNS, String propName, String qualNS,
982 String qualName) throws XMPException
983 {
984 // qualNS and qualName are checked inside composeQualfierPath
985 ParameterAsserts.assertSchemaNS(schemaNS);
986 ParameterAsserts.assertPropName(propName);
987
988 String qualPath = propName + XMPPathFactory.composeQualifierPath(qualNS, qualName);
989 return getProperty(schemaNS, qualPath);
990 }
991
992
993 /**
994 * @see XMPMeta#getStructField(String, String, String, String)
995 */
996 public XMPProperty getStructField(String schemaNS, String structName, String fieldNS,
997 String fieldName) throws XMPException
998 {
999 // fieldNS and fieldName are checked inside composeStructFieldPath
1000 ParameterAsserts.assertSchemaNS(schemaNS);
1001 ParameterAsserts.assertStructName(structName);
1002
1003 String fieldPath = structName + XMPPathFactory.composeStructFieldPath(fieldNS, fieldName);
1004 return getProperty(schemaNS, fieldPath);
1005 }
1006
1007
1008 /**
1009 * @throws XMPException
1010 * @see XMPMeta#iterator()
1011 */
1012 public XMPIterator iterator() throws XMPException
1013 {
1014 return iterator(null, null, null);
1015 }
1016
1017
1018 /**
1019 * @see XMPMeta#iterator(IteratorOptions)
1020 */
1021 public XMPIterator iterator(IteratorOptions options) throws XMPException
1022 {
1023 return iterator(null, null, options);
1024 }
1025
1026
1027 /**
1028 * @see XMPMeta#iterator(String, String, IteratorOptions)
1029 */
1030 public XMPIterator iterator(String schemaNS, String propName, IteratorOptions options)
1031 throws XMPException
1032 {
1033 return new XMPIteratorImpl(this, schemaNS, propName, options);
1034 }
1035
1036
1037 /**
1038 * @throws XMPException
1039 * @see XMPMeta#setArrayItem(String, String, int, String, PropertyOptions)
1040 */
1041 public void setArrayItem(String schemaNS, String arrayName, int itemIndex, String itemValue,
1042 PropertyOptions options) throws XMPException
1043 {
1044 ParameterAsserts.assertSchemaNS(schemaNS);
1045 ParameterAsserts.assertArrayName(arrayName);
1046
1047 // Just lookup, don't try to create.
1048 XMPPath arrayPath = XMPPathParser.expandXPath(schemaNS, arrayName);
1049 XMPNode arrayNode = XMPNodeUtils.findNode(tree, arrayPath, false, null);
1050
1051 if (arrayNode != null)
1052 {
1053 doSetArrayItem(arrayNode, itemIndex, itemValue, options, false);
1054 }
1055 else
1056 {
1057 throw new XMPException("Specified array does not exist", XMPError.BADXPATH);
1058 }
1059 }
1060
1061
1062 /**
1063 * @see XMPMeta#setArrayItem(String, String, int, String)
1064 */
1065 public void setArrayItem(String schemaNS, String arrayName, int itemIndex, String itemValue)
1066 throws XMPException
1067 {
1068 setArrayItem(schemaNS, arrayName, itemIndex, itemValue, null);
1069 }
1070
1071
1072 /**
1073 * @throws XMPException
1074 * @see XMPMeta#insertArrayItem(String, String, int, String,
1075 * PropertyOptions)
1076 */
1077 public void insertArrayItem(String schemaNS, String arrayName, int itemIndex, String itemValue,
1078 PropertyOptions options) throws XMPException
1079 {
1080 ParameterAsserts.assertSchemaNS(schemaNS);
1081 ParameterAsserts.assertArrayName(arrayName);
1082
1083 // Just lookup, don't try to create.
1084 XMPPath arrayPath = XMPPathParser.expandXPath(schemaNS, arrayName);
1085 XMPNode arrayNode = XMPNodeUtils.findNode(tree, arrayPath, false, null);
1086
1087 if (arrayNode != null)
1088 {
1089 doSetArrayItem(arrayNode, itemIndex, itemValue, options, true);
1090 }
1091 else
1092 {
1093 throw new XMPException("Specified array does not exist", XMPError.BADXPATH);
1094 }
1095 }
1096
1097
1098 /**
1099 * @see XMPMeta#insertArrayItem(String, String, int, String)
1100 */
1101 public void insertArrayItem(String schemaNS, String arrayName, int itemIndex, String itemValue)
1102 throws XMPException
1103 {
1104 insertArrayItem(schemaNS, arrayName, itemIndex, itemValue, null);
1105 }
1106
1107
1108 /**
1109 * @throws XMPException
1110 * @see XMPMeta#setProperty(String, String, Object, PropertyOptions)
1111 */
1112 public void setProperty(String schemaNS, String propName, Object propValue,
1113 PropertyOptions options) throws XMPException
1114 {
1115 ParameterAsserts.assertSchemaNS(schemaNS);
1116 ParameterAsserts.assertPropName(propName);
1117
1118 options = XMPNodeUtils.verifySetOptions(options, propValue);
1119
1120 XMPPath expPath = XMPPathParser.expandXPath(schemaNS, propName);
1121
1122 XMPNode propNode = XMPNodeUtils.findNode(tree, expPath, true, options);
1123 if (propNode != null)
1124 {
1125 setNode(propNode, propValue, options, false);
1126 }
1127 else
1128 {
1129 throw new XMPException("Specified property does not exist", XMPError.BADXPATH);
1130 }
1131 }
1132
1133
1134 /**
1135 * @see XMPMeta#setProperty(String, String, Object)
1136 */
1137 public void setProperty(String schemaNS, String propName, Object propValue) throws XMPException
1138 {
1139 setProperty(schemaNS, propName, propValue, null);
1140 }
1141
1142
1143 /**
1144 * @throws XMPException
1145 * @see XMPMeta#setQualifier(String, String, String, String, String,
1146 * PropertyOptions)
1147 */
1148 public void setQualifier(String schemaNS, String propName, String qualNS, String qualName,
1149 String qualValue, PropertyOptions options) throws XMPException
1150 {
1151 ParameterAsserts.assertSchemaNS(schemaNS);
1152 ParameterAsserts.assertPropName(propName);
1153
1154 if (!doesPropertyExist(schemaNS, propName))
1155 {
1156 throw new XMPException("Specified property does not exist!", XMPError.BADXPATH);
1157 }
1158
1159 String qualPath = propName + XMPPathFactory.composeQualifierPath(qualNS, qualName);
1160 setProperty(schemaNS, qualPath, qualValue, options);
1161 }
1162
1163
1164 /**
1165 * @see XMPMeta#setQualifier(String, String, String, String, String)
1166 */
1167 public void setQualifier(String schemaNS, String propName, String qualNS, String qualName,
1168 String qualValue) throws XMPException
1169 {
1170 setQualifier(schemaNS, propName, qualNS, qualName, qualValue, null);
1171
1172 }
1173
1174
1175 /**
1176 * @see XMPMeta#setStructField(String, String, String, String, String,
1177 * PropertyOptions)
1178 */
1179 public void setStructField(String schemaNS, String structName, String fieldNS,
1180 String fieldName, String fieldValue, PropertyOptions options) throws XMPException
1181 {
1182 ParameterAsserts.assertSchemaNS(schemaNS);
1183 ParameterAsserts.assertStructName(structName);
1184
1185 String fieldPath = structName + XMPPathFactory.composeStructFieldPath(fieldNS, fieldName);
1186 setProperty(schemaNS, fieldPath, fieldValue, options);
1187 }
1188
1189
1190 /**
1191 * @see XMPMeta#setStructField(String, String, String, String, String)
1192 */
1193 public void setStructField(String schemaNS, String structName, String fieldNS,
1194 String fieldName, String fieldValue) throws XMPException
1195 {
1196 setStructField(schemaNS, structName, fieldNS, fieldName, fieldValue, null);
1197 }
1198
1199
1200 /**
1201 * @see XMPMeta#getObjectName()
1202 */
1203 public String getObjectName()
1204 {
1205 return tree.getName() != null ? tree.getName() : "";
1206 }
1207
1208
1209 /**
1210 * @see XMPMeta#setObjectName(String)
1211 */
1212 public void setObjectName(String name)
1213 {
1214 tree.setName(name);
1215 }
1216
1217
1218 /**
1219 * @see XMPMeta#getPacketHeader()
1220 */
1221 public String getPacketHeader()
1222 {
1223 return packetHeader;
1224 }
1225
1226
1227 /**
1228 * Sets the packetHeader attributes, only used by the parser.
1229 * @param packetHeader the processing instruction content
1230 */
1231 public void setPacketHeader(String packetHeader)
1232 {
1233 this.packetHeader = packetHeader;
1234 }
1235
1236
1237 /**
1238 * Performs a deep clone of the XMPMeta-object
1239 *
1240 * @see java.lang.Object#clone()
1241 */
1242 public Object clone()
1243 {
1244 XMPNode clonedTree = (XMPNode) tree.clone();
1245 return new XMPMetaImpl(clonedTree);
1246 }
1247
1248
1249 /**
1250 * @see XMPMeta#dumpObject()
1251 */
1252 public String dumpObject()
1253 {
1254 // renders tree recursively
1255 return getRoot().dumpNode(true);
1256 }
1257
1258
1259 /**
1260 * @see XMPMeta#sort()
1261 */
1262 public void sort()
1263 {
1264 this.tree.sort();
1265 }
1266
1267
1268 /**
1269 * @see XMPMeta#normalize(ParseOptions)
1270 */
1271 public void normalize(ParseOptions options) throws XMPException
1272 {
1273 if (options == null)
1274 {
1275 options = new ParseOptions();
1276 }
1277 XMPNormalizer.process(this, options);
1278 }
1279
1280
1281 /**
1282 * @return Returns the root node of the XMP tree.
1283 */
1284 public XMPNode getRoot()
1285 {
1286 return tree;
1287 }
1288
1289
1290
1291 // -------------------------------------------------------------------------------------
1292 // private
1293
1294
1295 /**
1296 * Locate or create the item node and set the value. Note the index
1297 * parameter is one-based! The index can be in the range [1..size + 1] or
1298 * "last()", normalize it and check the insert flags. The order of the
1299 * normalization checks is important. If the array is empty we end up with
1300 * an index and location to set item size + 1.
1301 *
1302 * @param arrayNode an array node
1303 * @param itemIndex the index where to insert the item
1304 * @param itemValue the item value
1305 * @param itemOptions the options for the new item
1306 * @param insert insert oder overwrite at index position?
1307 * @throws XMPException
1308 */
1309 private void doSetArrayItem(XMPNode arrayNode, int itemIndex, String itemValue,
1310 PropertyOptions itemOptions, boolean insert) throws XMPException
1311 {
1312 XMPNode itemNode = new XMPNode(ARRAY_ITEM_NAME, null);
1313 itemOptions = XMPNodeUtils.verifySetOptions(itemOptions, itemValue);
1314
1315 // in insert mode the index after the last is allowed,
1316 // even ARRAY_LAST_ITEM points to the index *after* the last.
1317 int maxIndex = insert ? arrayNode.getChildrenLength() + 1 : arrayNode.getChildrenLength();
1318 if (itemIndex == ARRAY_LAST_ITEM)
1319 {
1320 itemIndex = maxIndex;
1321 }
1322
1323 if (1 <= itemIndex && itemIndex <= maxIndex)
1324 {
1325 if (!insert)
1326 {
1327 arrayNode.removeChild(itemIndex);
1328 }
1329 arrayNode.addChild(itemIndex, itemNode);
1330 setNode(itemNode, itemValue, itemOptions, false);
1331 }
1332 else
1333 {
1334 throw new XMPException("Array index out of bounds", XMPError.BADINDEX);
1335 }
1336 }
1337
1338
1339 /**
1340 * The internals for setProperty() and related calls, used after the node is
1341 * found or created.
1342 *
1343 * @param node
1344 * the newly created node
1345 * @param value
1346 * the node value, can be <code>null</code>
1347 * @param newOptions
1348 * options for the new node, must not be <code>null</code>.
1349 * @param deleteExisting flag if the existing value is to be overwritten
1350 * @throws XMPException thrown if options and value do not correspond
1351 */
1352 void setNode(XMPNode node, Object value, PropertyOptions newOptions, boolean deleteExisting)
1353 throws XMPException
1354 {
1355 int compositeMask = (PropertyOptions.ARRAY | PropertyOptions.ARRAY_ALT_TEXT | PropertyOptions.ARRAY_ALTERNATE
1356 | PropertyOptions.ARRAY_ORDERED | PropertyOptions.STRUCT);
1357 if (deleteExisting)
1358 {
1359 node.clear();
1360 }
1361
1362 // its checked by setOptions(), if the merged result is a valid options set
1363 node.getOptions().mergeWith(newOptions);
1364
1365 if ((node.getOptions().getOptions() & compositeMask) == 0)
1366 {
1367 // This is setting the value of a leaf node.
1368 XMPNodeUtils.setNodeValue(node, value);
1369 }
1370 else
1371 {
1372 if (value != null && value.toString().length() > 0)
1373 {
1374 throw new XMPException("Composite nodes can't have values", XMPError.BADXPATH);
1375 }
1376 if ((node.getOptions().getOptions() & compositeMask) != 0) { // Can't change an array to a struct, or vice versa.
1377 if ( (newOptions.getOptions() & compositeMask) != (node.getOptions().getOptions() & compositeMask) ) {
1378 throw new XMPException( "Requested and existing composite form mismatch", XMPError.BADXPATH );
1379 }
1380 }
1381
1382 node.removeChildren();
1383 }
1384
1385 }
1386
1387
1388 /**
1389 * Evaluates a raw node value to the given value type, apply special
1390 * conversions for defined types in XMP.
1391 *
1392 * @param valueType
1393 * an int indicating the value type
1394 * @param propNode
1395 * the node containing the value
1396 * @return Returns a literal value for the node.
1397 * @throws XMPException
1398 */
1399 private Object evaluateNodeValue(int valueType, final XMPNode propNode) throws XMPException
1400 {
1401 final Object value;
1402 String rawValue = propNode.getValue();
1403 switch (valueType)
1404 {
1405 case VALUE_BOOLEAN:
1406 value = new Boolean(XMPUtils.convertToBoolean(rawValue));
1407 break;
1408 case VALUE_INTEGER:
1409 value = new Integer(XMPUtils.convertToInteger(rawValue));
1410 break;
1411 case VALUE_LONG:
1412 value = new Long(XMPUtils.convertToLong(rawValue));
1413 break;
1414 case VALUE_DOUBLE:
1415 value = new Double(XMPUtils.convertToDouble(rawValue));
1416 break;
1417 case VALUE_DATE:
1418 value = XMPUtils.convertToDate(rawValue);
1419 break;
1420 case VALUE_CALENDAR:
1421 XMPDateTime dt = XMPUtils.convertToDate(rawValue);
1422 value = dt.getCalendar();
1423 break;
1424 case VALUE_BASE64:
1425 value = XMPUtils.decodeBase64(rawValue);
1426 break;
1427 case VALUE_STRING:
1428 default:
1429 // leaf values return empty string instead of null
1430 // for the other cases the converter methods provides a "null"
1431 // value.
1432 // a default value can only occur if this method is made public.
1433 value = rawValue != null || propNode.getOptions().isCompositeProperty() ? rawValue : "";
1434 break;
1435 }
1436 return value;
1437 }
1438 }