Debian Codegraph @

src/com/adobe/internal/xmp/impl/XMPIteratorImpl.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.Collections;
13 import java.util.Iterator;
14 import java.util.NoSuchElementException;
15
16 import com.adobe.internal.xmp.XMPError;
17 import com.adobe.internal.xmp.XMPException;
18 import com.adobe.internal.xmp.XMPIterator;
19 import com.adobe.internal.xmp.XMPMetaFactory;
20 import com.adobe.internal.xmp.impl.xpath.XMPPath;
21 import com.adobe.internal.xmp.impl.xpath.XMPPathParser;
22 import com.adobe.internal.xmp.options.IteratorOptions;
23 import com.adobe.internal.xmp.options.PropertyOptions;
24 import com.adobe.internal.xmp.properties.XMPPropertyInfo;
25
26
27 /**
28 * The <code>XMPIterator</code> implementation.
29 * Iterates the XMP Tree according to a set of options.
30 * During the iteration the XMPMeta-object must not be changed.
31 * Calls to <code>skipSubtree()</code> / <code>skipSiblings()</code> will affect the iteration.
32 * FfF: Introduce a lock mechanism to prohibit changes in the XMPTree during the iteration;
33 * FfF: will throw a ConcurrentModificationException.
34 *
35 * @author Stefan Makswit
36 * @version $Revision$
37 * @since 29.06.2006
38 */
39 public class XMPIteratorImpl implements XMPIterator
40 {
41 /** stores the iterator options */
42 private IteratorOptions options;
43 /** the base namespace of the property path, will be changed during the iteration */
44 private String baseNS = null;
45 /** flag to indicate that skipSiblings() has been called. */
46 protected boolean skipSiblings = false;
47 /** flag to indicate that skipSiblings() has been called. */
48 protected boolean skipSubtree = false;
49 /** the node iterator doing the work */
50 private Iterator nodeIterator = null;
51
52
53 /**
54 * Constructor with optionsl initial values. If <code>propName</code> is provided,
55 * <code>schemaNS</code> has also be provided.
56 * @param xmp the iterated metadata object.
57 * @param schemaNS the iteration is reduced to this schema (optional)
58 * @param propPath the iteration is redurce to this property within the <code>schemaNS</code>
59 * @param options advanced iteration options, see {@link IteratorOptions}
60 * @throws XMPException If the node defined by the paramters is not existing.
61 */
62 public XMPIteratorImpl(XMPMetaImpl xmp, String schemaNS, String propPath,
63 IteratorOptions options) throws XMPException
64 {
65 // make sure that options is defined at least with defaults
66 this.options = options != null ? options : new IteratorOptions();
67
68 // the start node of the iteration depending on the schema and property filter
69 XMPNode startNode = null;
70 String initialPath = null;
71 boolean baseSchema = schemaNS != null && schemaNS.length() > 0;
72 boolean baseProperty = propPath != null && propPath.length() > 0;
73
74 if (!baseSchema && !baseProperty)
75 {
76 // complete tree will be iterated
77 startNode = xmp.getRoot();
78 }
79 else if (baseSchema && baseProperty)
80 {
81 // Schema and property node provided
82
83 // FfF: Shall the indices "last()" and selectors be converted to real indices?
84 // FfF: Need to wait for customer input.
85 // FfF: in case: findNode would have to change the path accordingly
86 XMPPath path = XMPPathParser.expandXPath(schemaNS, propPath);
87
88 // base path is the prop path without the property leaf
89 XMPPath basePath = new XMPPath();
90 for (int i = 0; i < path.size() - 1; i++)
91 {
92 basePath.add(path.getSegment(i));
93 }
94
95 startNode = XMPNodeUtils.findNode(xmp.getRoot(), path, false, null);
96 baseNS = schemaNS;
97 initialPath = basePath.toString();
98 }
99 else if (baseSchema && !baseProperty)
100 {
101 // Only Schema provided
102 startNode = XMPNodeUtils.findSchemaNode(xmp.getRoot(), schemaNS, false);
103 }
104 else // !baseSchema && baseProperty
105 {
106 // No schema but property provided -> error
107 throw new XMPException("Schema namespace URI is required", XMPError.BADSCHEMA);
108 }
109
110
111 // create iterator
112 if (startNode != null)
113 {
114 if (!this.options.isJustChildren())
115 {
116 nodeIterator = new NodeIterator(startNode, initialPath, 1);
117 }
118 else
119 {
120 nodeIterator = new NodeIteratorChildren(startNode, initialPath);
121 }
122 }
123 else
124 {
125 // create null iterator
126 nodeIterator = Collections.EMPTY_LIST.iterator();
127 }
128 }
129
130
131 /**
132 * @see XMPIterator#skipSubtree()
133 */
134 public void skipSubtree()
135 {
136 this.skipSubtree = true;
137 }
138
139
140 /**
141 * @see XMPIterator#skipSiblings()
142 */
143 public void skipSiblings()
144 {
145 skipSubtree();
146 this.skipSiblings = true;
147 }
148
149
150 /**
151 * @see java.util.Iterator#hasNext()
152 */
153 public boolean hasNext()
154 {
155 return nodeIterator.hasNext();
156 }
157
158
159 /**
160 * @see java.util.Iterator#next()
161 */
162 public Object next()
163 {
164 return nodeIterator.next();
165 }
166
167
168 /**
169 * @see java.util.Iterator#remove()
170 */
171 public void remove()
172 {
173 throw new UnsupportedOperationException("The XMPIterator does not support remove().");
174 }
175
176
177 /**
178 * @return Exposes the options for inner class.
179 */
180 protected IteratorOptions getOptions()
181 {
182 return options;
183 }
184
185
186 /**
187 * @return Exposes the options for inner class.
188 */
189 protected String getBaseNS()
190 {
191 return baseNS;
192 }
193
194
195 /**
196 * @param baseNS sets the baseNS from the inner class.
197 */
198 protected void setBaseNS(String baseNS)
199 {
200 this.baseNS = baseNS;
201 }
202
203
204
205
206
207
208 /**
209 * The <code>XMPIterator</code> implementation.
210 * It first returns the node itself, then recursivly the children and qualifier of the node.
211 *
212 * @author Stefan Makswit
213 * @version $Revision$
214 * @since 29.06.2006
215 */
216 private class NodeIterator implements Iterator
217 {
218 /** iteration state */
219 protected static final int ITERATE_NODE = 0;
220 /** iteration state */
221 protected static final int ITERATE_CHILDREN = 1;
222 /** iteration state */
223 protected static final int ITERATE_QUALIFIER = 2;
224
225 /** the state of the iteration */
226 private int state = ITERATE_NODE;
227 /** the currently visited node */
228 private XMPNode visitedNode;
229 /** the recursively accumulated path */
230 private String path;
231 /** the iterator that goes through the children and qualifier list */
232 private Iterator childrenIterator = null;
233 /** index of node with parent, only interesting for arrays */
234 private int index = 0;
235 /** the iterator for each child */
236 private Iterator subIterator = Collections.EMPTY_LIST.iterator();
237 /** the cached <code>PropertyInfo</code> to return */
238 private XMPPropertyInfo returnProperty = null;
239
240
241 /**
242 * Default constructor
243 */
244 public NodeIterator()
245 {
246 // EMPTY
247 }
248
249
250 /**
251 * Constructor for the node iterator.
252 * @param visitedNode the currently visited node
253 * @param parentPath the accumulated path of the node
254 * @param index the index within the parent node (only for arrays)
255 */
256 public NodeIterator(XMPNode visitedNode, String parentPath, int index)
257 {
258 this.visitedNode = visitedNode;
259 this.state = NodeIterator.ITERATE_NODE;
260 if (visitedNode.getOptions().isSchemaNode())
261 {
262 setBaseNS(visitedNode.getName());
263 }
264
265 // for all but the root node and schema nodes
266 path = accumulatePath(visitedNode, parentPath, index);
267 }
268
269
270 /**
271 * Prepares the next node to return if not already done.
272 *
273 * @see Iterator#hasNext()
274 */
275 public boolean hasNext()
276 {
277 if (returnProperty != null)
278 {
279 // hasNext has been called before
280 return true;
281 }
282
283 // find next node
284 if (state == ITERATE_NODE)
285 {
286 return reportNode();
287 }
288 else if (state == ITERATE_CHILDREN)
289 {
290 if (childrenIterator == null)
291 {
292 childrenIterator = visitedNode.iterateChildren();
293 }
294
295 boolean hasNext = iterateChildren(childrenIterator);
296
297 if (!hasNext && visitedNode.hasQualifier() && !getOptions().isOmitQualifiers())
298 {
299 state = ITERATE_QUALIFIER;
300 childrenIterator = null;
301 hasNext = hasNext();
302 }
303 return hasNext;
304 }
305 else
306 {
307 if (childrenIterator == null)
308 {
309 childrenIterator = visitedNode.iterateQualifier();
310 }
311
312 return iterateChildren(childrenIterator);
313 }
314 }
315
316
317 /**
318 * Sets the returnProperty as next item or recurses into <code>hasNext()</code>.
319 * @return Returns if there is a next item to return.
320 */
321 protected boolean reportNode()
322 {
323 state = ITERATE_CHILDREN;
324 if (visitedNode.getParent() != null &&
325 (!getOptions().isJustLeafnodes() || !visitedNode.hasChildren()))
326 {
327 returnProperty = createPropertyInfo(visitedNode, getBaseNS(), path);
328 return true;
329 }
330 else
331 {
332 return hasNext();
333 }
334 }
335
336
337 /**
338 * Handles the iteration of the children or qualfier
339 * @param iterator an iterator
340 * @return Returns if there are more elements available.
341 */
342 private boolean iterateChildren(Iterator iterator)
343 {
344 if (skipSiblings)
345 {
346 // setSkipSiblings(false);
347 skipSiblings = false;
348 subIterator = Collections.EMPTY_LIST.iterator();
349 }
350
351 // create sub iterator for every child,
352 // if its the first child visited or the former child is finished
353 if ((!subIterator.hasNext()) && iterator.hasNext())
354 {
355 XMPNode child = (XMPNode) iterator.next();
356 index++;
357 subIterator = new NodeIterator(child, path, index);
358 }
359
360 if (subIterator.hasNext())
361 {
362 returnProperty = (XMPPropertyInfo) subIterator.next();
363 return true;
364 }
365 else
366 {
367 return false;
368 }
369 }
370
371
372 /**
373 * Calls hasNext() and returnes the prepared node. Afterwards its set to null.
374 * The existance of returnProperty indicates if there is a next node, otherwise
375 * an exceptio is thrown.
376 *
377 * @see Iterator#next()
378 */
379 public Object next()
380 {
381 if (hasNext())
382 {
383 XMPPropertyInfo result = returnProperty;
384 returnProperty = null;
385 return result;
386 }
387 else
388 {
389 throw new NoSuchElementException("There are no more nodes to return");
390 }
391 }
392
393
394 /**
395 * Not supported.
396 * @see Iterator#remove()
397 */
398 public void remove()
399 {
400 throw new UnsupportedOperationException();
401 }
402
403
404 /**
405 * @param currNode the node that will be added to the path.
406 * @param parentPath the path up to this node.
407 * @param currentIndex the current array index if an arrey is traversed
408 * @return Returns the updated path.
409 */
410 protected String accumulatePath(XMPNode currNode, String parentPath, int currentIndex)
411 {
412 String separator;
413 String segmentName;
414 if (currNode.getParent() == null || currNode.getOptions().isSchemaNode())
415 {
416 return null;
417 }
418 else if (currNode.getParent().getOptions().isArray())
419 {
420 separator = "";
421 segmentName = "[" + String.valueOf(currentIndex) + "]";
422 }
423 else
424 {
425 separator = "/";
426 segmentName = currNode.getName();
427 }
428
429
430 if (parentPath == null || parentPath.length() == 0)
431 {
432 return segmentName;
433 }
434 else if (getOptions().isJustLeafname())
435 {
436 return !segmentName.startsWith("?") ?
437 segmentName :
438 segmentName.substring(1); // qualifier
439 }
440 else
441 {
442 return parentPath + separator + segmentName;
443 }
444 }
445
446
447 /**
448 * Creates a property info object from an <code>XMPNode</code>.
449 * @param node an <code>XMPNode</code>
450 * @param baseNS the base namespace to report
451 * @param path the full property path
452 * @return Returns a <code>XMPProperty</code>-object that serves representation of the node.
453 */
454 protected XMPPropertyInfo createPropertyInfo(final XMPNode node, final String baseNS,
455 final String path)
456 {
457 final String value = node.getOptions().isSchemaNode() ? null : node.getValue();
458
459 return new XMPPropertyInfo()
460 {
461 public String getNamespace()
462 {
463 if (!node.getOptions().isSchemaNode())
464 {
465 // determine namespace of leaf node
466 QName qname = new QName(node.getName());
467 return XMPMetaFactory.getSchemaRegistry().getNamespaceURI(qname.getPrefix());
468 }
469 else
470 {
471 return baseNS;
472 }
473 }
474
475 public String getPath()
476 {
477 return path;
478 }
479
480 public String getValue()
481 {
482 return value;
483 }
484
485 public PropertyOptions getOptions()
486 {
487 return node.getOptions();
488 }
489
490 public String getLanguage()
491 {
492 // the language is not reported
493 return null;
494 }
495 };
496 }
497
498
499 /**
500 * @return the childrenIterator
501 */
502 protected Iterator getChildrenIterator()
503 {
504 return childrenIterator;
505 }
506
507
508 /**
509 * @param childrenIterator the childrenIterator to set
510 */
511 protected void setChildrenIterator(Iterator childrenIterator)
512 {
513 this.childrenIterator = childrenIterator;
514 }
515
516
517 /**
518 * @return Returns the returnProperty.
519 */
520 protected XMPPropertyInfo getReturnProperty()
521 {
522 return returnProperty;
523 }
524
525
526 /**
527 * @param returnProperty the returnProperty to set
528 */
529 protected void setReturnProperty(XMPPropertyInfo returnProperty)
530 {
531 this.returnProperty = returnProperty;
532 }
533 }
534
535
536 /**
537 * This iterator is derived from the default <code>NodeIterator</code>,
538 * and is only used for the option {@link IteratorOptions#JUST_CHILDREN}.
539 *
540 * @author Stefan Makswit
541 * @version $Revision$
542 * @since 02.10.2006
543 */
544 private class NodeIteratorChildren extends NodeIterator
545 {
546 /** */
547 private String parentPath;
548 /** */
549 private Iterator childrenIterator;
550 /** */
551 private int index = 0;
552
553
554 /**
555 * Constructor
556 * @param parentNode the node which children shall be iterated.
557 * @param parentPath the full path of the former node without the leaf node.
558 */
559 public NodeIteratorChildren(XMPNode parentNode, String parentPath)
560 {
561 if (parentNode.getOptions().isSchemaNode())
562 {
563 setBaseNS(parentNode.getName());
564 }
565 this.parentPath = accumulatePath(parentNode, parentPath, 1);
566
567 childrenIterator = parentNode.iterateChildren();
568 }
569
570
571 /**
572 * Prepares the next node to return if not already done.
573 *
574 * @see Iterator#hasNext()
575 */
576 public boolean hasNext()
577 {
578 if (getReturnProperty() != null)
579 {
580 // hasNext has been called before
581 return true;
582 }
583 else if (skipSiblings)
584 {
585 return false;
586 }
587 else if (childrenIterator.hasNext())
588 {
589 XMPNode child = (XMPNode) childrenIterator.next();
590 index++;
591
592 String path = null;
593 if (child.getOptions().isSchemaNode())
594 {
595 setBaseNS(child.getName());
596 }
597 else if (child.getParent() != null)
598 {
599 // for all but the root node and schema nodes
600 path = accumulatePath(child, parentPath, index);
601 }
602
603 // report next property, skip not-leaf nodes in case options is set
604 if (!getOptions().isJustLeafnodes() || !child.hasChildren())
605 {
606 setReturnProperty(createPropertyInfo(child, getBaseNS(), path));
607 return true;
608 }
609 else
610 {
611 return hasNext();
612 }
613 }
614 else
615 {
616 return false;
617 }
618 }
619 }
620 }