Debian Codegraph @

src/com/adobe/internal/xmp/impl/XMPNode.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.ArrayList;
13 import java.util.Arrays;
14 import java.util.Collections;
15 import java.util.Iterator;
16 import java.util.List;
17 import java.util.ListIterator;
18
19 import com.adobe.internal.xmp.XMPConst;
20 import com.adobe.internal.xmp.XMPError;
21 import com.adobe.internal.xmp.XMPException;
22 import com.adobe.internal.xmp.options.PropertyOptions;
23
24
25 /**
26 * A node in the internally XMP tree, which can be a schema node, a property node, an array node,
27 * an array item, a struct node or a qualifier node (without '?').
28 *
29 * Possible improvements:
30 *
31 * 1. The kind Node of node might be better represented by a class-hierarchy of different nodes.
32 * 2. The array type should be an enum
33 * 3. isImplicitNode should be removed completely and replaced by return values of fi.
34 * 4. hasLanguage, hasType should be automatically maintained by XMPNode
35 *
36 * @author Stefan Makswit
37 * @version $Revision$
38 * @since 21.02.2006
39 */
40 class XMPNode implements Comparable
41 {
42 /** name of the node, contains different information depending of the node kind */
43 private String name;
44 /** value of the node, contains different information depending of the node kind */
45 private String value;
46 /** link to the parent node */
47 private XMPNode parent;
48 /** list of child nodes, lazy initialized */
49 private List children = null;
50 /** list of qualifier of the node, lazy initialized */
51 private List qualifier = null;
52 /** options describing the kind of the node */
53 private PropertyOptions options = null;
54
55 // internal processing options
56
57 /** flag if the node is implicitly created */
58 private boolean implicit;
59 /** flag if the node has aliases */
60 private boolean hasAliases;
61 /** flag if the node is an alias */
62 private boolean alias;
63 /** flag if the node has an "rdf:value" child node. */
64 private boolean hasValueChild;
65
66
67
68 /**
69 * Creates an <code>XMPNode</code> with initial values.
70 *
71 * @param name the name of the node
72 * @param value the value of the node
73 * @param options the options of the node
74 */
75 public XMPNode(String name, String value, PropertyOptions options)
76 {
77 this.name = name;
78 this.value = value;
79 this.options = options;
80 }
81
82
83 /**
84 * Constructor for the node without value.
85 *
86 * @param name the name of the node
87 * @param options the options of the node
88 */
89 public XMPNode(String name, PropertyOptions options)
90 {
91 this(name, null, options);
92 }
93
94
95 /**
96 * Resets the node.
97 */
98 public void clear()
99 {
100 options = null;
101 name = null;
102 value = null;
103 children = null;
104 qualifier = null;
105 }
106
107
108 /**
109 * @return Returns the parent node.
110 */
111 public XMPNode getParent()
112 {
113 return parent;
114 }
115
116
117 /**
118 * @param index an index [1..size]
119 * @return Returns the child with the requested index.
120 */
121 public XMPNode getChild(int index)
122 {
123 return (XMPNode) getChildren().get(index - 1);
124 }
125
126
127 /**
128 * Adds a node as child to this node.
129 * @param node an XMPNode
130 * @throws XMPException
131 */
132 public void addChild(XMPNode node) throws XMPException
133 {
134 // check for duplicate properties
135 assertChildNotExisting(node.getName());
136 node.setParent(this);
137 getChildren().add(node);
138 }
139
140
141 /**
142 * Adds a node as child to this node.
143 * @param index the index of the node <em>before</em> which the new one is inserted.
144 * <em>Note:</em> The node children are indexed from [1..size]!
145 * An index of size + 1 appends a node.
146 * @param node an XMPNode
147 * @throws XMPException
148 */
149 public void addChild(int index, XMPNode node) throws XMPException
150 {
151 assertChildNotExisting(node.getName());
152 node.setParent(this);
153 getChildren().add(index - 1, node);
154 }
155
156
157 /**
158 * Replaces a node with another one.
159 * @param index the index of the node that will be replaced.
160 * <em>Note:</em> The node children are indexed from [1..size]!
161 * @param node the replacement XMPNode
162 */
163 public void replaceChild(int index, XMPNode node)
164 {
165 node.setParent(this);
166 getChildren().set(index - 1, node);
167 }
168
169
170 /**
171 * Removes a child at the requested index.
172 * @param itemIndex the index to remove [1..size]
173 */
174 public void removeChild(int itemIndex)
175 {
176 getChildren().remove(itemIndex - 1);
177 cleanupChildren();
178 }
179
180
181 /**
182 * Removes a child node.
183 * If its a schema node and doesn't have any children anymore, its deleted.
184 *
185 * @param node the child node to delete.
186 */
187 public void removeChild(XMPNode node)
188 {
189 getChildren().remove(node);
190 cleanupChildren();
191 }
192
193
194 /**
195 * Removes the children list if this node has no children anymore;
196 * checks if the provided node is a schema node and doesn't have any children anymore,
197 * its deleted.
198 */
199 protected void cleanupChildren()
200 {
201 if (children.isEmpty())
202 {
203 children = null;
204 }
205 }
206
207
208 /**
209 * Removes all children from the node.
210 */
211 public void removeChildren()
212 {
213 children = null;
214 }
215
216
217 /**
218 * @return Returns the number of children without neccessarily creating a list.
219 */
220 public int getChildrenLength()
221 {
222 return children != null ?
223 children.size() :
224 0;
225 }
226
227
228 /**
229 * @param expr child node name to look for
230 * @return Returns an <code>XMPNode</code> if node has been found, <code>null</code> otherwise.
231 */
232 public XMPNode findChildByName(String expr)
233 {
234 return find(getChildren(), expr);
235 }
236
237
238 /**
239 * @param index an index [1..size]
240 * @return Returns the qualifier with the requested index.
241 */
242 public XMPNode getQualifier(int index)
243 {
244 return (XMPNode) getQualifier().get(index - 1);
245 }
246
247
248 /**
249 * @return Returns the number of qualifier without neccessarily creating a list.
250 */
251 public int getQualifierLength()
252 {
253 return qualifier != null ?
254 qualifier.size() :
255 0;
256 }
257
258
259 /**
260 * Appends a qualifier to the qualifier list and sets respective options.
261 * @param qualNode a qualifier node.
262 * @throws XMPException
263 */
264 public void addQualifier(XMPNode qualNode) throws XMPException
265 {
266 assertQualifierNotExisting(qualNode.getName());
267 qualNode.setParent(this);
268 qualNode.getOptions().setQualifier(true);
269 getOptions().setHasQualifiers(true);
270
271 // contraints
272 if (qualNode.isLanguageNode())
273 {
274 // "xml:lang" is always first and the option "hasLanguage" is set
275 options.setHasLanguage(true);
276 getQualifier().add(0, qualNode);
277 }
278 else if (qualNode.isTypeNode())
279 {
280 // "rdf:type" must be first or second after "xml:lang" and the option "hasType" is set
281 options.setHasType(true);
282 getQualifier().add(
283 !options.getHasLanguage() ? 0 : 1,
284 qualNode);
285 }
286 else
287 {
288 // other qualifiers are appended
289 getQualifier().add(qualNode);
290 }
291 }
292
293
294 /**
295 * Removes one qualifier node and fixes the options.
296 * @param qualNode qualifier to remove
297 */
298 public void removeQualifier(XMPNode qualNode)
299 {
300 PropertyOptions opts = getOptions();
301 if (qualNode.isLanguageNode())
302 {
303 // if "xml:lang" is removed, remove hasLanguage-flag too
304 opts.setHasLanguage(false);
305 }
306 else if (qualNode.isTypeNode())
307 {
308 // if "rdf:type" is removed, remove hasType-flag too
309 opts.setHasType(false);
310 }
311
312 getQualifier().remove(qualNode);
313 if (qualifier.isEmpty())
314 {
315 opts.setHasQualifiers(false);
316 qualifier = null;
317 }
318
319 }
320
321
322 /**
323 * Removes all qualifiers from the node and sets the options appropriate.
324 */
325 public void removeQualifiers()
326 {
327 PropertyOptions opts = getOptions();
328 // clear qualifier related options
329 opts.setHasQualifiers(false);
330 opts.setHasLanguage(false);
331 opts.setHasType(false);
332 qualifier = null;
333 }
334
335
336 /**
337 * @param expr qualifier node name to look for
338 * @return Returns a qualifier <code>XMPNode</code> if node has been found,
339 * <code>null</code> otherwise.
340 */
341 public XMPNode findQualifierByName(String expr)
342 {
343 return find(qualifier, expr);
344 }
345
346
347 /**
348 * @return Returns whether the node has children.
349 */
350 public boolean hasChildren()
351 {
352 return children != null && children.size() > 0;
353 }
354
355
356 /**
357 * @return Returns an iterator for the children.
358 * <em>Note:</em> take care to use it.remove(), as the flag are not adjusted in that case.
359 */
360 public Iterator iterateChildren()
361 {
362 if (children != null)
363 {
364 return getChildren().iterator();
365 }
366 else
367 {
368 return Collections.EMPTY_LIST.listIterator();
369 }
370 }
371
372
373 /**
374 * @return Returns whether the node has qualifier attached.
375 */
376 public boolean hasQualifier()
377 {
378 return qualifier != null && qualifier.size() > 0;
379 }
380
381
382 /**
383 * @return Returns an iterator for the qualifier.
384 * <em>Note:</em> take care to use it.remove(), as the flag are not adjusted in that case.
385 */
386 public Iterator iterateQualifier()
387 {
388 if (qualifier != null)
389 {
390 final Iterator it = getQualifier().iterator();
391
392 return new Iterator()
393 {
394 public boolean hasNext()
395 {
396 return it.hasNext();
397 }
398
399 public Object next()
400 {
401 return it.next();
402 }
403
404 public void remove()
405 {
406 throw new UnsupportedOperationException(
407 "remove() is not allowed due to the internal contraints");
408 }
409
410 };
411 }
412 else
413 {
414 return Collections.EMPTY_LIST.iterator();
415 }
416 }
417
418 /**
419 * Performs a <b>deep clone</b> of the node and the complete subtree.
420 * it will clone those nodes also which has empty child and empty value.
421 * @see java.lang.Object#clone(), XMPNode#clone(boolean skipEmpty)
422 */
423
424 public Object clone(){
425 return this.clone(false);
426 }
427
428
429 /**
430 * Performs a <b>deep clone</b> of the node and the complete subtree.
431 * if <code>skipEmpty</code> is true, it will not clone node which has empty child and empty value.
432 * @param skipEmpty If true, it will not clone those nodes with empty value and empty childern
433 * @see java.lang.Object#clone()
434 */
435 public Object clone(boolean skipEmpty)
436 {
437 PropertyOptions newOptions;
438 try
439 {
440 newOptions = new PropertyOptions(getOptions().getOptions());
441 }
442 catch (XMPException e)
443 {
444 // cannot happen
445 newOptions = new PropertyOptions();
446 }
447
448 XMPNode newNode = new XMPNode(name, value, newOptions);
449 cloneSubtree(newNode, skipEmpty);
450 if(skipEmpty && (newNode.getValue() == null || newNode.getValue().length() == 0)
451 &&(! newNode.hasChildren())){
452 newNode = null;
453 }
454
455 return newNode;
456 }
457
458
459 /**
460 * Performs a <b>deep clone</b> of the complete subtree (children and
461 * qualifier )into and add it to the destination node.
462 *
463 * @param destination the node to add the cloned subtree
464 * @param skipEmpty If true, it will not clone nodes with empty values and empty childern
465 */
466 public void cloneSubtree(XMPNode destination, boolean skipEmpty)
467 {
468 try
469 {
470 for (Iterator it = iterateChildren(); it.hasNext();)
471 {
472 XMPNode child = (XMPNode) it.next();
473 if(skipEmpty && (child.getValue() == null || child.getValue().length() == 0)
474 && (!child.hasChildren()))
475 continue;
476 XMPNode childNode = (XMPNode) child.clone(skipEmpty);
477 if(childNode == null)
478 continue;
479 destination.addChild(childNode);
480 }
481
482 for (Iterator it = iterateQualifier(); it.hasNext();)
483 {
484 XMPNode qualifier = (XMPNode) it.next();
485 if(skipEmpty && (qualifier.getValue() == null || qualifier.getValue().length() == 0)
486 && (!qualifier.hasChildren()))
487 continue;
488 XMPNode qualNode = (XMPNode) qualifier.clone(skipEmpty);
489 if(qualNode == null)
490 continue;
491 destination.addQualifier(qualNode);
492 }
493 }
494 catch (XMPException e)
495 {
496 // cannot happen (duplicate childs/quals do not exist in this node)
497 assert false;
498 }
499
500 }
501
502
503 /**
504 * Renders this node and the tree unter this node in a human readable form.
505 * @param recursive Flag is qualifier and child nodes shall be rendered too
506 * @return Returns a multiline string containing the dump.
507 */
508 public String dumpNode(boolean recursive)
509 {
510 StringBuffer result = new StringBuffer(512);
511 this.dumpNode(result, recursive, 0, 0);
512 return result.toString();
513 }
514
515
516 /**
517 * @see Comparable#compareTo(Object)
518 */
519 public int compareTo(Object xmpNode)
520 {
521 if (getOptions().isSchemaNode())
522 {
523 return this.value.compareTo(((XMPNode) xmpNode).getValue());
524 }
525 else
526 {
527 return this.name.compareTo(((XMPNode) xmpNode).getName());
528 }
529 }
530
531
532 /**
533 * @return Returns the name.
534 */
535 public String getName()
536 {
537 return name;
538 }
539
540
541 /**
542 * @param name The name to set.
543 */
544 public void setName(String name)
545 {
546 this.name = name;
547 }
548
549
550 /**
551 * @return Returns the value.
552 */
553 public String getValue()
554 {
555 return value;
556 }
557
558
559 /**
560 * @param value The value to set.
561 */
562 public void setValue(String value)
563 {
564 this.value = value;
565 }
566
567
568 /**
569 * @return Returns the options.
570 */
571 public PropertyOptions getOptions()
572 {
573 if (options == null)
574 {
575 options = new PropertyOptions();
576 }
577 return options;
578 }
579
580
581 /**
582 * Updates the options of the node.
583 * @param options the options to set.
584 */
585 public void setOptions(PropertyOptions options)
586 {
587 this.options = options;
588 }
589
590
591 /**
592 * @return Returns the implicit flag
593 */
594 public boolean isImplicit()
595 {
596 return implicit;
597 }
598
599
600 /**
601 * @param implicit Sets the implicit node flag
602 */
603 public void setImplicit(boolean implicit)
604 {
605 this.implicit = implicit;
606 }
607
608
609 /**
610 * @return Returns if the node contains aliases (applies only to schema nodes)
611 */
612 public boolean getHasAliases()
613 {
614 return hasAliases;
615 }
616
617
618 /**
619 * @param hasAliases sets the flag that the node contains aliases
620 */
621 public void setHasAliases(boolean hasAliases)
622 {
623 this.hasAliases = hasAliases;
624 }
625
626
627 /**
628 * @return Returns if the node contains aliases (applies only to schema nodes)
629 */
630 public boolean isAlias()
631 {
632 return alias;
633 }
634
635
636 /**
637 * @param alias sets the flag that the node is an alias
638 */
639 public void setAlias(boolean alias)
640 {
641 this.alias = alias;
642 }
643
644
645 /**
646 * @return the hasValueChild
647 */
648 public boolean getHasValueChild()
649 {
650 return hasValueChild;
651 }
652
653
654 /**
655 * @param hasValueChild the hasValueChild to set
656 */
657 public void setHasValueChild(boolean hasValueChild)
658 {
659 this.hasValueChild = hasValueChild;
660 }
661
662
663
664 /**
665 * Sorts the complete datamodel according to the following rules:
666 * <ul>
667 * <li>Nodes at one level are sorted by name, that is prefix + local name
668 * <li>Starting at the root node the children and qualifier are sorted recursively,
669 * which the following exceptions.
670 * <li>Sorting will not be used for arrays.
671 * <li>Within qualifier "xml:lang" and/or "rdf:type" stay at the top in that order,
672 * all others are sorted.
673 * </ul>
674 */
675 public void sort()
676 {
677 // sort qualifier
678 if (hasQualifier())
679 {
680 XMPNode[] quals = (XMPNode[]) getQualifier()
681 .toArray(new XMPNode[getQualifierLength()]);
682 int sortFrom = 0;
683 while (
684 quals.length > sortFrom &&
685 (XMPConst.XML_LANG.equals(quals[sortFrom].getName()) ||
686 "rdf:type".equals(quals[sortFrom].getName()))
687 )
688 {
689 quals[sortFrom].sort();
690 sortFrom++;
691 }
692
693 Arrays.sort(quals, sortFrom, quals.length);
694 ListIterator it = qualifier.listIterator();
695 for (int j = 0; j < quals.length; j++)
696 {
697 it.next();
698 it.set(quals[j]);
699 quals[j].sort();
700 }
701 }
702
703 // sort children
704 if (hasChildren())
705 {
706 if (!getOptions().isArray())
707 {
708 Collections.sort(children);
709 }
710 for (Iterator it = iterateChildren(); it.hasNext();)
711 {
712 ((XMPNode) it.next()).sort();
713
714 }
715 }
716 }
717
718
719
720 //------------------------------------------------------------------------------ private methods
721
722
723 /**
724 * Dumps this node and its qualifier and children recursively.
725 * <em>Note:</em> It creats empty options on every node.
726 * FfF: sort schemas and properties on each level if and only if it would increase performance
727 *
728 * @param result the buffer to append the dump.
729 * @param recursive Flag is qualifier and child nodes shall be rendered too
730 * @param indent the current indent level.
731 * @param index the index within the parent node (important for arrays)
732 */
733 private void dumpNode(StringBuffer result, boolean recursive, int indent, int index)
734 {
735 // write indent
736 for (int i = 0; i < indent; i++)
737 {
738 result.append('\t');
739 }
740
741 // render Node
742 if (parent != null)
743 {
744 if (getOptions().isQualifier())
745 {
746 result.append('?');
747 result.append(name);
748 }
749 else if (getParent().getOptions().isArray())
750 {
751 result.append('[');
752 result.append(index);
753 result.append(']');
754 }
755 else
756 {
757 result.append(name);
758 }
759 }
760 else
761 {
762 // applies only to the root node
763 result.append("ROOT NODE");
764 if (name != null && name.length() > 0)
765 {
766 // the "about" attribute
767 result.append(" (");
768 result.append(name);
769 result.append(')');
770 }
771 }
772
773 if (value != null && value.length() > 0)
774 {
775 result.append(" = \"");
776 result.append(value);
777 result.append('"');
778 }
779
780 // render options if at least one is set
781 if (getOptions().containsOneOf(0xffffffff))
782 {
783 result.append("\t(");
784 result.append(getOptions().toString());
785 result.append(" : ");
786 result.append(getOptions().getOptionsString());
787 result.append(')');
788 }
789
790 result.append('\n');
791
792 // render qualifier
793 if (recursive && hasQualifier())
794 {
795 XMPNode[] quals = (XMPNode[]) getQualifier()
796 .toArray(new XMPNode[getQualifierLength()]);
797 int i = 0;
798 while (quals.length > i &&
799 (XMPConst.XML_LANG.equals(quals[i].getName()) ||
800 "rdf:type".equals(quals[i].getName()))
801 )
802 {
803 i++;
804 }
805 Arrays.sort(quals, i, quals.length);
806 for (i = 0; i < quals.length; i++)
807 {
808 XMPNode qualifier = quals[i];
809 qualifier.dumpNode(result, recursive, indent + 2, i + 1);
810 }
811 }
812
813 // render children
814 if (recursive && hasChildren())
815 {
816 XMPNode[] children = (XMPNode[]) getChildren()
817 .toArray(new XMPNode[getChildrenLength()]);
818 if (!getOptions().isArray())
819 {
820 Arrays.sort(children);
821 }
822 for (int i = 0; i < children.length; i++)
823 {
824 XMPNode child = children[i];
825 child.dumpNode(result, recursive, indent + 1, i + 1);
826 }
827 }
828 }
829
830
831 /**
832 * @return Returns whether this node is a language qualifier.
833 */
834 private boolean isLanguageNode()
835 {
836 return XMPConst.XML_LANG.equals(name);
837 }
838
839
840 /**
841 * @return Returns whether this node is a type qualifier.
842 */
843 private boolean isTypeNode()
844 {
845 return "rdf:type".equals(name);
846 }
847
848
849 /**
850 * <em>Note:</em> This method should always be called when accessing 'children' to be sure
851 * that its initialized.
852 * @return Returns list of children that is lazy initialized.
853 */
854 private List getChildren()
855 {
856 if (children == null)
857 {
858 children = new ArrayList(0);
859 }
860 return children;
861 }
862
863
864 /**
865 * @return Returns a read-only copy of child nodes list.
866 */
867 public List getUnmodifiableChildren()
868 {
869 return Collections.unmodifiableList(new ArrayList(getChildren()));
870 }
871
872
873 /**
874 * @return Returns list of qualifier that is lazy initialized.
875 */
876 private List getQualifier()
877 {
878 if (qualifier == null)
879 {
880 qualifier = new ArrayList(0);
881 }
882 return qualifier;
883 }
884
885
886 /**
887 * Sets the parent node, this is solely done by <code>addChild(...)</code>
888 * and <code>addQualifier()</code>.
889 *
890 * @param parent
891 * Sets the parent node.
892 */
893 protected void setParent(XMPNode parent)
894 {
895 this.parent = parent;
896 }
897
898
899 /**
900 * Internal find.
901 * @param list the list to search in
902 * @param expr the search expression
903 * @return Returns the found node or <code>nulls</code>.
904 */
905 private XMPNode find(List list, String expr)
906 {
907
908 if (list != null)
909 {
910 for (Iterator it = list.iterator(); it.hasNext();)
911 {
912 XMPNode child = (XMPNode) it.next();
913 if (child.getName().equals(expr))
914 {
915 return child;
916 }
917 }
918 }
919 return null;
920 }
921
922
923 /**
924 * Checks that a node name is not existing on the same level, except for array items.
925 * @param childName the node name to check
926 * @throws XMPException Thrown if a node with the same name is existing.
927 */
928 private void assertChildNotExisting(String childName) throws XMPException
929 {
930 if (!XMPConst.ARRAY_ITEM_NAME.equals(childName) &&
931 findChildByName(childName) != null)
932 {
933 throw new XMPException("Duplicate property or field node '" + childName + "'",
934 XMPError.BADXMP);
935 }
936 }
937
938
939 /**
940 * Checks that a qualifier name is not existing on the same level.
941 * @param qualifierName the new qualifier name
942 * @throws XMPException Thrown if a node with the same name is existing.
943 */
944 private void assertQualifierNotExisting(String qualifierName) throws XMPException
945 {
946 if (!XMPConst.ARRAY_ITEM_NAME.equals(qualifierName) &&
947 findQualifierByName(qualifierName) != null)
948 {
949 throw new XMPException("Duplicate '" + qualifierName + "' qualifier", XMPError.BADXMP);
950 }
951 }
952 }