| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 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 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public class XMPIteratorImpl implements XMPIterator |
| 40 |
{ |
| 41 |
|
| 42 |
private IteratorOptions options; |
| 43 |
|
| 44 |
private String baseNS = null; |
| 45 |
|
| 46 |
protected boolean skipSiblings = false; |
| 47 |
|
| 48 |
protected boolean skipSubtree = false; |
| 49 |
|
| 50 |
private Iterator nodeIterator = null; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
public XMPIteratorImpl(XMPMetaImpl xmp, String schemaNS, String propPath, |
| 63 |
IteratorOptions options) throws XMPException |
| 64 |
{ |
| 65 |
|
| 66 |
this.options = options != null ? options : new IteratorOptions(); |
| 67 |
|
| 68 |
|
| 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 |
|
| 77 |
startNode = xmp.getRoot(); |
| 78 |
} |
| 79 |
else if (baseSchema && baseProperty) |
| 80 |
{ |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
XMPPath path = XMPPathParser.expandXPath(schemaNS, propPath); |
| 87 |
|
| 88 |
|
| 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 |
|
| 102 |
startNode = XMPNodeUtils.findSchemaNode(xmp.getRoot(), schemaNS, false); |
| 103 |
} |
| 104 |
else |
| 105 |
{ |
| 106 |
|
| 107 |
throw new XMPException("Schema namespace URI is required", XMPError.BADSCHEMA); |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 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 |
|
| 126 |
nodeIterator = Collections.EMPTY_LIST.iterator(); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
public void skipSubtree() |
| 135 |
{ |
| 136 |
this.skipSubtree = true; |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
public void skipSiblings() |
| 144 |
{ |
| 145 |
skipSubtree(); |
| 146 |
this.skipSiblings = true; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
public boolean hasNext() |
| 154 |
{ |
| 155 |
return nodeIterator.hasNext(); |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
public Object next() |
| 163 |
{ |
| 164 |
return nodeIterator.next(); |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
public void remove() |
| 172 |
{ |
| 173 |
throw new UnsupportedOperationException("The XMPIterator does not support remove()."); |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
protected IteratorOptions getOptions() |
| 181 |
{ |
| 182 |
return options; |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
protected String getBaseNS() |
| 190 |
{ |
| 191 |
return baseNS; |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
protected void setBaseNS(String baseNS) |
| 199 |
{ |
| 200 |
this.baseNS = baseNS; |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
private class NodeIterator implements Iterator |
| 217 |
{ |
| 218 |
|
| 219 |
protected static final int ITERATE_NODE = 0; |
| 220 |
|
| 221 |
protected static final int ITERATE_CHILDREN = 1; |
| 222 |
|
| 223 |
protected static final int ITERATE_QUALIFIER = 2; |
| 224 |
|
| 225 |
|
| 226 |
private int state = ITERATE_NODE; |
| 227 |
|
| 228 |
private XMPNode visitedNode; |
| 229 |
|
| 230 |
private String path; |
| 231 |
|
| 232 |
private Iterator childrenIterator = null; |
| 233 |
|
| 234 |
private int index = 0; |
| 235 |
|
| 236 |
private Iterator subIterator = Collections.EMPTY_LIST.iterator(); |
| 237 |
|
| 238 |
private XMPPropertyInfo returnProperty = null; |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
public NodeIterator() |
| 245 |
{ |
| 246 |
|
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 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 |
|
| 266 |
path = accumulatePath(visitedNode, parentPath, index); |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
public boolean hasNext() |
| 276 |
{ |
| 277 |
if (returnProperty != null) |
| 278 |
{ |
| 279 |
|
| 280 |
return true; |
| 281 |
} |
| 282 |
|
| 283 |
|
| 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 |
|
| 319 |
|
| 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 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
private boolean iterateChildren(Iterator iterator) |
| 343 |
{ |
| 344 |
if (skipSiblings) |
| 345 |
{ |
| 346 |
|
| 347 |
skipSiblings = false; |
| 348 |
subIterator = Collections.EMPTY_LIST.iterator(); |
| 349 |
} |
| 350 |
|
| 351 |
|
| 352 |
|
| 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 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 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 |
|
| 396 |
|
| 397 |
|
| 398 |
public void remove() |
| 399 |
{ |
| 400 |
throw new UnsupportedOperationException(); |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 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); |
| 439 |
} |
| 440 |
else |
| 441 |
{ |
| 442 |
return parentPath + separator + segmentName; |
| 443 |
} |
| 444 |
} |
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 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 |
|
| 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 |
|
| 493 |
return null; |
| 494 |
} |
| 495 |
}; |
| 496 |
} |
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
protected Iterator getChildrenIterator() |
| 503 |
{ |
| 504 |
return childrenIterator; |
| 505 |
} |
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
protected void setChildrenIterator(Iterator childrenIterator) |
| 512 |
{ |
| 513 |
this.childrenIterator = childrenIterator; |
| 514 |
} |
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
protected XMPPropertyInfo getReturnProperty() |
| 521 |
{ |
| 522 |
return returnProperty; |
| 523 |
} |
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
protected void setReturnProperty(XMPPropertyInfo returnProperty) |
| 530 |
{ |
| 531 |
this.returnProperty = returnProperty; |
| 532 |
} |
| 533 |
} |
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 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 |
|
| 556 |
|
| 557 |
|
| 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 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
public boolean hasNext() |
| 577 |
{ |
| 578 |
if (getReturnProperty() != null) |
| 579 |
{ |
| 580 |
|
| 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 |
|
| 600 |
path = accumulatePath(child, parentPath, index); |
| 601 |
} |
| 602 |
|
| 603 |
|
| 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 |
} |