summaryrefslogtreecommitdiff
path: root/java/XMPCore/src/com/adobe/xmp/impl/XMPNodeUtils.java
blob: 853746936ee2c0ab8659d503abe7c185ece52fb0 (plain)
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
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
// =================================================================================================
// ADOBE SYSTEMS INCORPORATED
// Copyright 2006 Adobe Systems Incorporated
// All Rights Reserved
//
// NOTICE:  Adobe permits you to use, modify, and distribute this file in accordance with the terms
// of the Adobe license agreement accompanying it.
// =================================================================================================

package com.adobe.xmp.impl;

import java.util.GregorianCalendar;
import java.util.Iterator;

import com.adobe.xmp.XMPConst;
import com.adobe.xmp.XMPDateTime;
import com.adobe.xmp.XMPDateTimeFactory;
import com.adobe.xmp.XMPError;
import com.adobe.xmp.XMPException;
import com.adobe.xmp.XMPMetaFactory;
import com.adobe.xmp.XMPUtils;
import com.adobe.xmp.impl.xpath.XMPPath;
import com.adobe.xmp.impl.xpath.XMPPathSegment;
import com.adobe.xmp.options.AliasOptions;
import com.adobe.xmp.options.PropertyOptions;


/**
 * Utilities for <code>XMPNode</code>.
 * 
 * @since   Aug 28, 2006
 */
public class XMPNodeUtils implements XMPConst
{
	/** */
	static final int CLT_NO_VALUES = 0;
	/** */
	static final int CLT_SPECIFIC_MATCH = 1;
	/** */
	static final int CLT_SINGLE_GENERIC = 2;
	/** */
	static final int CLT_MULTIPLE_GENERIC = 3;
	/** */
	static final int CLT_XDEFAULT = 4;
	/** */
	static final int CLT_FIRST_ITEM = 5;


	/**
	 * Private Constructor
	 */
	private XMPNodeUtils()
	{
		// EMPTY
	}

	
	/**
	 * Find or create a schema node if <code>createNodes</code> is false and
	 *  
	 * @param tree the root of the xmp tree. 
	 * @param namespaceURI a namespace
	 * @param createNodes a flag indicating if the node shall be created if not found.
	 * 		  <em>Note:</em> The namespace must be registered prior to this call.
	 * 
	 * @return Returns the schema node if found, <code>null</code> otherwise.
	 * 		   Note: If <code>createNodes</code> is <code>true</code>, it is <b>always</b>
	 * 		   returned a valid node. 
	 * @throws XMPException An exception is only thrown if an error occurred, not if a
	 *         		node was not found.
	 */
	static XMPNode findSchemaNode(XMPNode tree, String namespaceURI,
			boolean createNodes)
			throws XMPException
	{
		return findSchemaNode(tree, namespaceURI, null, createNodes);
	}
	
	
	/**
	 * Find or create a schema node if <code>createNodes</code> is true.
	 *  
	 * @param tree the root of the xmp tree. 
	 * @param namespaceURI a namespace
	 * @param suggestedPrefix If a prefix is suggested, the namespace is allowed to be registered.
	 * @param createNodes a flag indicating if the node shall be created if not found.
	 * 		  <em>Note:</em> The namespace must be registered prior to this call.
	 * 
	 * @return Returns the schema node if found, <code>null</code> otherwise.
	 * 		   Note: If <code>createNodes</code> is <code>true</code>, it is <b>always</b>
	 * 		   returned a valid node. 
	 * @throws XMPException An exception is only thrown if an error occurred, not if a
	 *         		node was not found.
	 */
	static XMPNode findSchemaNode(XMPNode tree, String namespaceURI, String suggestedPrefix,
			boolean createNodes)
			throws XMPException
	{
		assert tree.getParent() == null; // make sure that its the root
		XMPNode schemaNode = tree.findChildByName(namespaceURI);
		
		if (schemaNode == null  &&  createNodes)
		{
			schemaNode = new XMPNode(namespaceURI, 
				new PropertyOptions()
					.setSchemaNode(true));
			schemaNode.setImplicit(true);
			
			// only previously registered schema namespaces are allowed in the XMP tree.
			String prefix = XMPMetaFactory.getSchemaRegistry().getNamespacePrefix(namespaceURI);
			if (prefix == null)
			{	
				if (suggestedPrefix != null  &&  suggestedPrefix.length() != 0)
				{
					prefix = XMPMetaFactory.getSchemaRegistry().registerNamespace(namespaceURI,
							suggestedPrefix);
				}
				else
				{
					throw new XMPException("Unregistered schema namespace URI",
							XMPError.BADSCHEMA);
				}	
			}	
				
			schemaNode.setValue(prefix);
	
			tree.addChild(schemaNode);
		}
		
		return schemaNode;
	}

	
	/**
	 * Find or create a child node under a given parent node. If the parent node is no 
	 * Returns the found or created child node.
	 * 
	 * @param parent
	 *            the parent node
	 * @param childName
	 *            the node name to find
	 * @param createNodes
	 *            flag, if new nodes shall be created.
	 * @return Returns the found or created node or <code>null</code>.
	 * @throws XMPException Thrown if 
	 */
	static XMPNode findChildNode(XMPNode parent, String childName, boolean createNodes)
			throws XMPException
	{
		if (!parent.getOptions().isSchemaNode() && !parent.getOptions().isStruct())
		{
			if (!parent.isImplicit())
			{
				throw new XMPException("Named children only allowed for schemas and structs",
						XMPError.BADXPATH);
			}	
			else if (parent.getOptions().isArray())
			{
				throw new XMPException("Named children not allowed for arrays",
						XMPError.BADXPATH);
			}
			else if (createNodes)
			{	
				parent.getOptions().setStruct(true);
			}	
		}
			
		XMPNode childNode = parent.findChildByName(childName); 
		
		if (childNode == null  &&  createNodes)
		{
			PropertyOptions options = new PropertyOptions();
			childNode = new XMPNode(childName, options);
			childNode.setImplicit(true);
			parent.addChild(childNode);
		}
		
		assert childNode != null ||  !createNodes;
	
		return childNode;
	}


	/**
	 * Follow an expanded path expression to find or create a node.
	 * 
	 * @param xmpTree the node to begin the search. 
	 * @param xpath the complete xpath
	 * @param createNodes flag if nodes shall be created 
	 * 			(when called by <code>setProperty()</code>)
	 * @param leafOptions the options for the created leaf nodes (only when
	 *			<code>createNodes == true</code>).
	 * @return Returns the node if found or created or <code>null</code>.
	 * @throws XMPException An exception is only thrown if an error occurred, 
	 * 			not if a node was not found.
	 */
	static XMPNode findNode(XMPNode xmpTree, XMPPath xpath, boolean createNodes,
		PropertyOptions leafOptions) throws XMPException
	{
		// check if xpath is set.
		if (xpath == null  ||  xpath.size() == 0)
		{
			throw new XMPException("Empty XMPPath", XMPError.BADXPATH);
		}

		// Root of implicitly created subtree to possible delete it later. 
		// Valid only if leaf is new.
		XMPNode rootImplicitNode = null; 
		XMPNode currNode = null;
		
		// resolve schema step
		currNode = findSchemaNode(xmpTree,
			xpath.getSegment(XMPPath.STEP_SCHEMA).getName(), createNodes);
		if (currNode == null) 
		{
			return null;
		}
		else if (currNode.isImplicit())
		{
			currNode.setImplicit(false);	// Clear the implicit node bit.
			rootImplicitNode = currNode;	// Save the top most implicit node.
		}


		// Now follow the remaining steps of the original XMPPath.
		try
		{
			for (int i = 1; i < xpath.size(); i++)
			{
				currNode = followXPathStep(currNode, xpath.getSegment(i), createNodes);
				if (currNode == null) 
				{
					if (createNodes)
					{	
						// delete implicitly created nodes
						deleteNode(rootImplicitNode);
					}	
					return null;
				}
				else if (currNode.isImplicit())
				{
					// clear the implicit node flag
					currNode.setImplicit(false);

					// if node is an ALIAS (can be only in root step, auto-create array 
					// when the path has been resolved from a not simple alias type
					if (i == 1  &&  
						xpath.getSegment(i).isAlias()  &&
						xpath.getSegment(i).getAliasForm() != 0)
					{
						currNode.getOptions().setOption(xpath.getSegment(i).getAliasForm(), true);
					}
					// "CheckImplicitStruct" in C++
					else if (i < xpath.size() - 1  &&
						xpath.getSegment(i).getKind() == XMPPath.STRUCT_FIELD_STEP  &&	
						!currNode.getOptions().isCompositeProperty())
					{
						currNode.getOptions().setStruct(true);
					}					
					
					if (rootImplicitNode == null)
					{
						rootImplicitNode = currNode;	// Save the top most implicit node.
					}
				}
			}
		}
		catch (XMPException e)
		{
			// if new notes have been created prior to the error, delete them
			if (rootImplicitNode != null)
			{
				deleteNode(rootImplicitNode);
			}
			throw e;
		}
		
		
		if (rootImplicitNode != null)
		{
			// set options only if a node has been successful created
			currNode.getOptions().mergeWith(leafOptions);
			currNode.setOptions(currNode.getOptions());
		}
		
		return currNode;
	}


	/**
	 * Deletes the the given node and its children from its parent.
	 * Takes care about adjusting the flags.
	 * @param node the top-most node to delete.
	 */
	static void deleteNode(XMPNode node)
	{
		XMPNode parent = node.getParent();
		
		if (node.getOptions().isQualifier())
		{
			// root is qualifier
			parent.removeQualifier(node);
		}
		else
		{
			// root is NO qualifier
			parent.removeChild(node);
		}
		
		// delete empty Schema nodes
		if (!parent.hasChildren()  &&  parent.getOptions().isSchemaNode())
		{
			parent.getParent().removeChild(parent);
		}
	}


	/**
	 * This is setting the value of a leaf node.
	 * 
	 * @param node an XMPNode
	 * @param value a value
	 */
	static void setNodeValue(XMPNode node, Object value)
	{
		String strValue = serializeNodeValue(value);
		if (!(node.getOptions().isQualifier()  &&  XML_LANG.equals(node.getName()))) 
		{	
			node.setValue(strValue);
		}
		else
		{
			node.setValue(Utils.normalizeLangValue(strValue));
		}
	}
	
	
	/**
	 * Verifies the PropertyOptions for consistancy and updates them as needed. 
	 * If options are <code>null</code> they are created with default values.
	 *  
	 * @param options the <code>PropertyOptions</code>
	 * @param itemValue the node value to set
	 * @return Returns the updated options.
	 * @throws XMPException If the options are not consistant. 
	 */
	static PropertyOptions verifySetOptions(PropertyOptions options, Object itemValue)
			throws XMPException
	{
		// create empty and fix existing options
		if (options == null)
		{
			// set default options
			options = new PropertyOptions();
		}
		
		if (options.isArrayAltText())
		{
			options.setArrayAlternate(true);
		}
	
		if (options.isArrayAlternate())
		{
			options.setArrayOrdered(true);
		}
	
		if (options.isArrayOrdered())
		{
			options.setArray(true);
		}
	
		if (options.isCompositeProperty() && itemValue != null && itemValue.toString().length() > 0)
		{
			throw new XMPException("Structs and arrays can't have values",
				XMPError.BADOPTIONS);
		}
	
		options.assertConsistency(options.getOptions());
		
		return options;
	}


	/**
	 * Converts the node value to String, apply special conversions for defined
	 * types in XMP.
	 * 
	 * @param value
	 *            the node value to set
	 * @return Returns the String representation of the node value.
	 */
	static String serializeNodeValue(Object value)
	{
		String strValue;
		if (value == null)
		{
			strValue = null;
		}
		else if (value instanceof Boolean)
		{
			strValue = XMPUtils.convertFromBoolean(((Boolean) value).booleanValue());
		}
		else if (value instanceof Integer)
		{
			strValue = XMPUtils.convertFromInteger(((Integer) value).intValue());
		}
		else if (value instanceof Long)
		{
			strValue = XMPUtils.convertFromLong(((Long) value).longValue());
		}
		else if (value instanceof Double)
		{
			strValue = XMPUtils.convertFromDouble(((Double) value).doubleValue());
		}
		else if (value instanceof XMPDateTime)
		{
			strValue = XMPUtils.convertFromDate((XMPDateTime) value);
		}
		else if (value instanceof GregorianCalendar)
		{
			XMPDateTime dt = XMPDateTimeFactory.createFromCalendar((GregorianCalendar) value);
			strValue = XMPUtils.convertFromDate(dt);
		}
		else if (value instanceof byte[])
		{
			strValue = XMPUtils.encodeBase64((byte[]) value);
		}
		else
		{
			strValue = value.toString();
		}
	
		return strValue != null ? Utils.removeControlChars(strValue) : null;
	}
	
	
	/** 
	 * After processing by ExpandXPath, a step can be of these forms:
	 * <ul>
	 * 	<li>qualName - A top level property or struct field.
	 * <li>[index] - An element of an array.
	 * <li>[last()] - The last element of an array.
	 * <li>[qualName="value"] - An element in an array of structs, chosen by a field value.
	 * <li>[?qualName="value"] - An element in an array, chosen by a qualifier value.
	 * <li>?qualName - A general qualifier.
	 * </ul>
	 * Find the appropriate child node, resolving aliases, and optionally creating nodes.
	 * 
	 * @param parentNode the node to start to start from 
	 * @param nextStep the xpath segment 
	 * @param createNodes 
	 * @return returns the found or created XMPPath node 
	 * @throws XMPException 
	 */
	private static XMPNode followXPathStep(
				XMPNode parentNode, 
				XMPPathSegment nextStep,
				boolean createNodes) throws XMPException
	{
		XMPNode nextNode = null;
		int index = 0;
		int stepKind = nextStep.getKind();
		
		if (stepKind == XMPPath.STRUCT_FIELD_STEP)
		{
			nextNode = findChildNode(parentNode, nextStep.getName(), createNodes);
		}
		else if (stepKind == XMPPath.QUALIFIER_STEP)
		{
			nextNode = findQualifierNode(
				parentNode, nextStep.getName().substring(1), createNodes);
		}
		else
		{
			// This is an array indexing step. First get the index, then get the node.

			if (!parentNode.getOptions().isArray())
			{
				throw new XMPException("Indexing applied to non-array", XMPError.BADXPATH);
			}

			if (stepKind == XMPPath.ARRAY_INDEX_STEP)
			{
				index = findIndexedItem(parentNode, nextStep.getName(), createNodes);
			}
			else if (stepKind == XMPPath.ARRAY_LAST_STEP)
			{
				index = parentNode.getChildrenLength();
			}
			else if (stepKind == XMPPath.FIELD_SELECTOR_STEP)
			{
				String[] result = Utils.splitNameAndValue(nextStep.getName());
				String fieldName = result[0];
				String fieldValue = result[1];
				index = lookupFieldSelector(parentNode, fieldName, fieldValue);
			}
			else if (stepKind == XMPPath.QUAL_SELECTOR_STEP)
			{
				String[] result = Utils.splitNameAndValue(nextStep.getName());
				String qualName = result[0];
				String qualValue = result[1];
				index = lookupQualSelector(
					parentNode, qualName, qualValue, nextStep.getAliasForm());
			}
			else
			{
				throw new XMPException("Unknown array indexing step in FollowXPathStep",
						XMPError.INTERNALFAILURE);
			}

			if (1 <= index  &&  index <=  parentNode.getChildrenLength())
			{
				nextNode = parentNode.getChild(index);
			}
		}
	
		return nextNode;		
	}


	/**
	 * Find or create a qualifier node under a given parent node. Returns a pointer to the 
	 * qualifier node, and optionally an iterator for the node's position in 
	 * the parent's vector of qualifiers. The iterator is unchanged if no qualifier node (null) 
	 * is returned.
	 * <em>Note:</em> On entry, the qualName parameter must not have the leading '?' from the 
	 * XMPPath step.
	 * 
	 * @param parent the parent XMPNode 
	 * @param qualName the qualifier name
	 * @param createNodes flag if nodes shall be created
	 * @return Returns the qualifier node if found or created, <code>null</code> otherwise.
	 * @throws XMPException 
	 */
	private static XMPNode findQualifierNode(XMPNode parent, String qualName, boolean createNodes)
			throws XMPException
	{
		assert !qualName.startsWith("?");
		
		XMPNode qualNode = parent.findQualifierByName(qualName);
		
		if (qualNode == null  &&  createNodes)
		{
			qualNode = new XMPNode(qualName, null);
			qualNode.setImplicit(true);
	
			parent.addQualifier(qualNode);				
		}
		
		return qualNode;
	}
	
	
	/**
	 * @param arrayNode an array node
	 * @param segment the segment containing the array index
	 * @param createNodes flag if new nodes are allowed to be created.
	 * @return Returns the index or index = -1 if not found
	 * @throws XMPException Throws Exceptions
	 */
	private static int findIndexedItem(XMPNode arrayNode, String segment, boolean createNodes)
			throws XMPException
	{
		int index = 0;
	
		try
		{
			segment = segment.substring(1, segment.length() - 1);
			index = Integer.parseInt(segment);
			if (index < 1)
			{
				throw new XMPException("Array index must be larger than zero",
						XMPError.BADXPATH);
			}
		}
		catch (NumberFormatException e)
		{
			throw new XMPException("Array index not digits.", XMPError.BADXPATH);
		}
	
		if (createNodes  &&  index == arrayNode.getChildrenLength() + 1)
		{
			// Append a new last + 1 node.
			XMPNode newItem = new XMPNode(ARRAY_ITEM_NAME, null);
			newItem.setImplicit(true);
			arrayNode.addChild(newItem);
		}

		return index;
	}
	
	
	/**
	 * Searches for a field selector in a node:
	 * [fieldName="value] - an element in an array of structs, chosen by a field value.
	 * No implicit nodes are created by field selectors. 
	 * 
	 * @param arrayNode
	 * @param fieldName
	 * @param fieldValue
	 * @return Returns the index of the field if found, otherwise -1.
	 * @throws XMPException 
	 */
	private static int lookupFieldSelector(XMPNode arrayNode, String fieldName, String fieldValue)
		throws XMPException
	{
		int result = -1;
		
		for (int index = 1; index <= arrayNode.getChildrenLength()  &&  result < 0; index++)
		{
			XMPNode currItem = arrayNode.getChild(index);
	
			if (!currItem.getOptions().isStruct())
			{
				throw new XMPException("Field selector must be used on array of struct",
						XMPError.BADXPATH);
			}
	
			for (int f = 1; f <= currItem.getChildrenLength(); f++)
			{
				XMPNode currField = currItem.getChild(f);
				if (!fieldName.equals(currField.getName()))
				{
					continue;
				}
				if (fieldValue.equals(currField.getValue())) 
				{
					result = index;
					break;
				}
			}
		}
		
		return result;
	}
	
	
	/**
	 * Searches for a qualifier selector in a node:
	 * [?qualName="value"] - an element in an array, chosen by a qualifier value.
	 * No implicit nodes are created for qualifier selectors, 
	 * except for an alias to an x-default item.
	 * 
	 * @param arrayNode an array node 
	 * @param qualName the qualifier name
	 * @param qualValue the qualifier value
	 * @param aliasForm in case the qual selector results from an alias,
	 * 		  an x-default node is created if there has not been one. 
	 * @return Returns the index of th
	 * @throws XMPException 
	 */
	private static int lookupQualSelector(XMPNode arrayNode, String qualName, 
		String qualValue, int aliasForm) throws XMPException
	{
		if (XML_LANG.equals(qualName))
		{
			qualValue = Utils.normalizeLangValue(qualValue);
			int index = XMPNodeUtils.lookupLanguageItem(arrayNode, qualValue);
			if (index < 0  &&  (aliasForm & AliasOptions.PROP_ARRAY_ALT_TEXT) > 0)
			{	
				XMPNode langNode = new XMPNode(ARRAY_ITEM_NAME, null);
				XMPNode xdefault = new XMPNode(XML_LANG, X_DEFAULT, null);
				langNode.addQualifier(xdefault);
				arrayNode.addChild(1, langNode);
				return 1;
			}
			else
			{
				return index;
			}
		}
		else
		{
			for (int index = 1; index < arrayNode.getChildrenLength(); index++)
			{
				XMPNode currItem = arrayNode.getChild(index);
		
				for (Iterator it = currItem.iterateQualifier(); it.hasNext();)
				{
					XMPNode qualifier = (XMPNode) it.next();
					if (qualName.equals(qualifier.getName())  &&
						qualValue.equals(qualifier.getValue()))
					{
						return index;
					}
				}
			}
			return -1;
		}	
	}


	/**
	 * Make sure the x-default item is first. Touch up &quot;single value&quot;
	 * arrays that have a default plus one real language. This case should have
	 * the same value for both items. Older Adobe apps were hardwired to only
	 * use the &quot;x-default&quot; item, so we copy that value to the other
	 * item.
	 * 
	 * @param arrayNode
	 *            an alt text array node
	 */
	static void normalizeLangArray(XMPNode arrayNode)
	{
		if (!arrayNode.getOptions().isArrayAltText())
		{
			return;
		}
	
		// check if node with x-default qual is first place
		for (int i = 2; i <= arrayNode.getChildrenLength(); i++)
		{
			XMPNode child = arrayNode.getChild(i);
			if (child.hasQualifier() && X_DEFAULT.equals(child.getQualifier(1).getValue()))
			{
				// move node to first place
				try
				{
					arrayNode.removeChild(i);
					arrayNode.addChild(1, child);
				}
				catch (XMPException e)
				{
					// cannot occur, because same child is removed before
					assert false;
				}
				
				if (i == 2)
				{
					arrayNode.getChild(2).setValue(child.getValue());
				}
				break;
			}
		}
	}


	/**
	 * See if an array is an alt-text array. If so, make sure the x-default item
	 * is first.
	 * 
	 * @param arrayNode
	 *            the array node to check if its an alt-text array
	 */
	static void detectAltText(XMPNode arrayNode)
	{
		if (arrayNode.getOptions().isArrayAlternate() && arrayNode.hasChildren())
		{
			boolean isAltText = false;
			for (Iterator it = arrayNode.iterateChildren(); it.hasNext();)
			{
				XMPNode child = (XMPNode) it.next();
				if (child.getOptions().getHasLanguage())
				{
					isAltText = true;
					break;
				}
			}
	
			if (isAltText)
			{
				arrayNode.getOptions().setArrayAltText(true);
				normalizeLangArray(arrayNode);
			}
		}
	}


	/**
	 * Appends a language item to an alt text array.
	 * 
	 * @param arrayNode the language array
	 * @param itemLang the language of the item
	 * @param itemValue the content of the item
	 * @throws XMPException Thrown if a duplicate property is added
	 */
	static void appendLangItem(XMPNode arrayNode, String itemLang, String itemValue)
			throws XMPException
	{
		XMPNode newItem = new XMPNode(ARRAY_ITEM_NAME, itemValue, null);
		XMPNode langQual = new XMPNode(XML_LANG, itemLang, null);
		newItem.addQualifier(langQual);
	
		if (!X_DEFAULT.equals(langQual.getValue()))
		{
			arrayNode.addChild(newItem);
		}
		else
		{
			arrayNode.addChild(1, newItem);
		}
	}


	/**
	 * <ol>
	 * <li>Look for an exact match with the specific language.
	 * <li>If a generic language is given, look for partial matches.
	 * <li>Look for an "x-default"-item.
	 * <li>Choose the first item.
	 * </ol>
	 * 
	 * @param arrayNode
	 *            the alt text array node
	 * @param genericLang
	 *            the generic language
	 * @param specificLang
	 *            the specific language
	 * @return Returns the kind of match as an Integer and the found node in an
	 *         array.
	 * 
	 * @throws XMPException
	 */
	static Object[] chooseLocalizedText(XMPNode arrayNode, String genericLang, String specificLang)
			throws XMPException
	{
		// See if the array has the right form. Allow empty alt arrays,
		// that is what parsing returns.
		if (!arrayNode.getOptions().isArrayAltText())
		{
			throw new XMPException("Localized text array is not alt-text", XMPError.BADXPATH);
		}
		else if (!arrayNode.hasChildren())
		{
			return new Object[] { new Integer(XMPNodeUtils.CLT_NO_VALUES), null };
		}
	
		int foundGenericMatches = 0;
		XMPNode resultNode = null;
		XMPNode xDefault = null;
	
		// Look for the first partial match with the generic language.
		for (Iterator it = arrayNode.iterateChildren(); it.hasNext();)
		{
			XMPNode currItem = (XMPNode) it.next();
	
			// perform some checks on the current item
			if (currItem.getOptions().isCompositeProperty())
			{
				throw new XMPException("Alt-text array item is not simple", XMPError.BADXPATH);
			}
			else if (!currItem.hasQualifier()
					|| !XML_LANG.equals(currItem.getQualifier(1).getName()))
			{
				throw new XMPException("Alt-text array item has no language qualifier",
						XMPError.BADXPATH);
			}
	
			String currLang = currItem.getQualifier(1).getValue();
	
			// Look for an exact match with the specific language.
			if (specificLang.equals(currLang))
			{
				return new Object[] { new Integer(XMPNodeUtils.CLT_SPECIFIC_MATCH), currItem };
			}
			else if (genericLang != null && currLang.startsWith(genericLang))
			{
				if (resultNode == null)
				{
					resultNode = currItem;
				}
				// ! Don't return/break, need to look for other matches.
				foundGenericMatches++;
			}
			else if (X_DEFAULT.equals(currLang))
			{
				xDefault = currItem;
			}
		}
	
		// evaluate loop
		if (foundGenericMatches == 1)
		{
			return new Object[] { new Integer(XMPNodeUtils.CLT_SINGLE_GENERIC), resultNode };
		}
		else if (foundGenericMatches > 1)
		{
			return new Object[] { new Integer(XMPNodeUtils.CLT_MULTIPLE_GENERIC), resultNode };
		}
		else if (xDefault != null)
		{
			return new Object[] { new Integer(XMPNodeUtils.CLT_XDEFAULT), xDefault };
		}
		else
		{
			// Everything failed, choose the first item.
			return new Object[] { new Integer(XMPNodeUtils.CLT_FIRST_ITEM), arrayNode.getChild(1) };
		}
	}


	/**
	 * Looks for the appropriate language item in a text alternative array.item
	 * 
	 * @param arrayNode
	 *            an array node
	 * @param language
	 *            the requested language
	 * @return Returns the index if the language has been found, -1 otherwise.
	 * @throws XMPException
	 */
	static int lookupLanguageItem(XMPNode arrayNode, String language) throws XMPException
	{
		if (!arrayNode.getOptions().isArray())
		{
			throw new XMPException("Language item must be used on array", XMPError.BADXPATH);
		}
	
		for (int index = 1; index <= arrayNode.getChildrenLength(); index++)
		{
			XMPNode child = arrayNode.getChild(index);
			if (!child.hasQualifier() || !XML_LANG.equals(child.getQualifier(1).getName()))
			{
				continue;
			}
			else if (language.equals(child.getQualifier(1).getValue()))
			{
				return index;
			}
		}
	
		return -1;
	}
}