| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
package com.adobe.internal.xmp.impl; |
| 11 |
|
| 12 |
import java.util.GregorianCalendar; |
| 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.XMPDateTimeFactory; |
| 18 |
import com.adobe.internal.xmp.XMPError; |
| 19 |
import com.adobe.internal.xmp.XMPException; |
| 20 |
import com.adobe.internal.xmp.XMPMetaFactory; |
| 21 |
import com.adobe.internal.xmp.XMPUtils; |
| 22 |
import com.adobe.internal.xmp.impl.xpath.XMPPath; |
| 23 |
import com.adobe.internal.xmp.impl.xpath.XMPPathSegment; |
| 24 |
import com.adobe.internal.xmp.options.AliasOptions; |
| 25 |
import com.adobe.internal.xmp.options.PropertyOptions; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public class XMPNodeUtils implements XMPConst |
| 36 |
{ |
| 37 |
|
| 38 |
static final int CLT_NO_VALUES = 0; |
| 39 |
|
| 40 |
static final int CLT_SPECIFIC_MATCH = 1; |
| 41 |
|
| 42 |
static final int CLT_SINGLE_GENERIC = 2; |
| 43 |
|
| 44 |
static final int CLT_MULTIPLE_GENERIC = 3; |
| 45 |
|
| 46 |
static final int CLT_XDEFAULT = 4; |
| 47 |
|
| 48 |
static final int CLT_FIRST_ITEM = 5; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
private XMPNodeUtils() |
| 55 |
{ |
| 56 |
|
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
static XMPNode findSchemaNode(XMPNode tree, String namespaceURI, |
| 75 |
boolean createNodes) |
| 76 |
throws XMPException |
| 77 |
{ |
| 78 |
return findSchemaNode(tree, namespaceURI, null, createNodes); |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
static XMPNode findSchemaNode(XMPNode tree, String namespaceURI, String suggestedPrefix, |
| 98 |
boolean createNodes) |
| 99 |
throws XMPException |
| 100 |
{ |
| 101 |
assert tree.getParent() == null; |
| 102 |
XMPNode schemaNode = tree.findChildByName(namespaceURI); |
| 103 |
|
| 104 |
if (schemaNode == null && createNodes) |
| 105 |
{ |
| 106 |
schemaNode = new XMPNode(namespaceURI, |
| 107 |
new PropertyOptions() |
| 108 |
.setSchemaNode(true)); |
| 109 |
schemaNode.setImplicit(true); |
| 110 |
|
| 111 |
|
| 112 |
String prefix = XMPMetaFactory.getSchemaRegistry().getNamespacePrefix(namespaceURI); |
| 113 |
if (prefix == null) |
| 114 |
{ |
| 115 |
if (suggestedPrefix != null && suggestedPrefix.length() != 0) |
| 116 |
{ |
| 117 |
prefix = XMPMetaFactory.getSchemaRegistry().registerNamespace(namespaceURI, |
| 118 |
suggestedPrefix); |
| 119 |
} |
| 120 |
else |
| 121 |
{ |
| 122 |
throw new XMPException("Unregistered schema namespace URI", |
| 123 |
XMPError.BADSCHEMA); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
schemaNode.setValue(prefix); |
| 128 |
|
| 129 |
tree.addChild(schemaNode); |
| 130 |
} |
| 131 |
|
| 132 |
return schemaNode; |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
static XMPNode findChildNode(XMPNode parent, String childName, boolean createNodes) |
| 150 |
throws XMPException |
| 151 |
{ |
| 152 |
if (!parent.getOptions().isSchemaNode() && !parent.getOptions().isStruct()) |
| 153 |
{ |
| 154 |
if (!parent.isImplicit()) |
| 155 |
{ |
| 156 |
throw new XMPException("Named children only allowed for schemas and structs", |
| 157 |
XMPError.BADXPATH); |
| 158 |
} |
| 159 |
else if (parent.getOptions().isArray()) |
| 160 |
{ |
| 161 |
throw new XMPException("Named children not allowed for arrays", |
| 162 |
XMPError.BADXPATH); |
| 163 |
} |
| 164 |
else if (createNodes) |
| 165 |
{ |
| 166 |
parent.getOptions().setStruct(true); |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
XMPNode childNode = parent.findChildByName(childName); |
| 171 |
|
| 172 |
if (childNode == null && createNodes) |
| 173 |
{ |
| 174 |
PropertyOptions options = new PropertyOptions(); |
| 175 |
childNode = new XMPNode(childName, options); |
| 176 |
childNode.setImplicit(true); |
| 177 |
parent.addChild(childNode); |
| 178 |
} |
| 179 |
|
| 180 |
assert childNode != null || !createNodes; |
| 181 |
|
| 182 |
return childNode; |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
static XMPNode findNode(XMPNode xmpTree, XMPPath xpath, boolean createNodes, |
| 200 |
PropertyOptions leafOptions) throws XMPException |
| 201 |
{ |
| 202 |
|
| 203 |
if (xpath == null || xpath.size() == 0) |
| 204 |
{ |
| 205 |
throw new XMPException("Empty XMPPath", XMPError.BADXPATH); |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
XMPNode rootImplicitNode = null; |
| 211 |
XMPNode currNode = null; |
| 212 |
|
| 213 |
|
| 214 |
currNode = findSchemaNode(xmpTree, |
| 215 |
xpath.getSegment(XMPPath.STEP_SCHEMA).getName(), createNodes); |
| 216 |
if (currNode == null) |
| 217 |
{ |
| 218 |
return null; |
| 219 |
} |
| 220 |
else if (currNode.isImplicit()) |
| 221 |
{ |
| 222 |
currNode.setImplicit(false); |
| 223 |
rootImplicitNode = currNode; |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
try |
| 229 |
{ |
| 230 |
for (int i = 1; i < xpath.size(); i++) |
| 231 |
{ |
| 232 |
currNode = followXPathStep(currNode, xpath.getSegment(i), createNodes); |
| 233 |
if (currNode == null) |
| 234 |
{ |
| 235 |
if (createNodes) |
| 236 |
{ |
| 237 |
|
| 238 |
deleteNode(rootImplicitNode); |
| 239 |
} |
| 240 |
return null; |
| 241 |
} |
| 242 |
else if (currNode.isImplicit()) |
| 243 |
{ |
| 244 |
|
| 245 |
currNode.setImplicit(false); |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
if (i == 1 && |
| 250 |
xpath.getSegment(i).isAlias() && |
| 251 |
xpath.getSegment(i).getAliasForm() != 0) |
| 252 |
{ |
| 253 |
currNode.getOptions().setOption(xpath.getSegment(i).getAliasForm(), true); |
| 254 |
} |
| 255 |
|
| 256 |
else if (i < xpath.size() - 1 && |
| 257 |
xpath.getSegment(i).getKind() == XMPPath.STRUCT_FIELD_STEP && |
| 258 |
!currNode.getOptions().isCompositeProperty()) |
| 259 |
{ |
| 260 |
currNode.getOptions().setStruct(true); |
| 261 |
} |
| 262 |
|
| 263 |
if (rootImplicitNode == null) |
| 264 |
{ |
| 265 |
rootImplicitNode = currNode; |
| 266 |
} |
| 267 |
} |
| 268 |
} |
| 269 |
} |
| 270 |
catch (XMPException e) |
| 271 |
{ |
| 272 |
|
| 273 |
if (rootImplicitNode != null) |
| 274 |
{ |
| 275 |
deleteNode(rootImplicitNode); |
| 276 |
} |
| 277 |
throw e; |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
if (rootImplicitNode != null) |
| 282 |
{ |
| 283 |
|
| 284 |
currNode.getOptions().mergeWith(leafOptions); |
| 285 |
currNode.setOptions(currNode.getOptions()); |
| 286 |
} |
| 287 |
|
| 288 |
return currNode; |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
static void deleteNode(XMPNode node) |
| 300 |
{ |
| 301 |
XMPNode parent = node.getParent(); |
| 302 |
|
| 303 |
if (node.getOptions().isQualifier()) |
| 304 |
{ |
| 305 |
|
| 306 |
parent.removeQualifier(node); |
| 307 |
} |
| 308 |
else |
| 309 |
{ |
| 310 |
|
| 311 |
parent.removeChild(node); |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
if (!parent.hasChildren() && parent.getOptions().isSchemaNode()) |
| 316 |
{ |
| 317 |
parent.getParent().removeChild(parent); |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
static void setNodeValue(XMPNode node, Object value) |
| 329 |
{ |
| 330 |
String strValue = serializeNodeValue(value); |
| 331 |
if (!(node.getOptions().isQualifier() && XML_LANG.equals(node.getName()))) |
| 332 |
{ |
| 333 |
node.setValue(strValue); |
| 334 |
} |
| 335 |
else |
| 336 |
{ |
| 337 |
node.setValue(Utils.normalizeLangValue(strValue)); |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
static PropertyOptions verifySetOptions(PropertyOptions options, Object itemValue) |
| 353 |
throws XMPException |
| 354 |
{ |
| 355 |
|
| 356 |
if (options == null) |
| 357 |
{ |
| 358 |
|
| 359 |
options = new PropertyOptions(); |
| 360 |
} |
| 361 |
|
| 362 |
if (options.isArrayAltText()) |
| 363 |
{ |
| 364 |
options.setArrayAlternate(true); |
| 365 |
} |
| 366 |
|
| 367 |
if (options.isArrayAlternate()) |
| 368 |
{ |
| 369 |
options.setArrayOrdered(true); |
| 370 |
} |
| 371 |
|
| 372 |
if (options.isArrayOrdered()) |
| 373 |
{ |
| 374 |
options.setArray(true); |
| 375 |
} |
| 376 |
|
| 377 |
if (options.isCompositeProperty() && itemValue != null && itemValue.toString().length() > 0) |
| 378 |
{ |
| 379 |
throw new XMPException("Structs and arrays can't have values", |
| 380 |
XMPError.BADOPTIONS); |
| 381 |
} |
| 382 |
|
| 383 |
options.assertConsistency(options.getOptions()); |
| 384 |
|
| 385 |
return options; |
| 386 |
} |
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
static String serializeNodeValue(Object value) |
| 398 |
{ |
| 399 |
String strValue; |
| 400 |
if (value == null) |
| 401 |
{ |
| 402 |
strValue = null; |
| 403 |
} |
| 404 |
else if (value instanceof Boolean) |
| 405 |
{ |
| 406 |
strValue = XMPUtils.convertFromBoolean(((Boolean) value).booleanValue()); |
| 407 |
} |
| 408 |
else if (value instanceof Integer) |
| 409 |
{ |
| 410 |
strValue = XMPUtils.convertFromInteger(((Integer) value).intValue()); |
| 411 |
} |
| 412 |
else if (value instanceof Long) |
| 413 |
{ |
| 414 |
strValue = XMPUtils.convertFromLong(((Long) value).longValue()); |
| 415 |
} |
| 416 |
else if (value instanceof Double) |
| 417 |
{ |
| 418 |
strValue = XMPUtils.convertFromDouble(((Double) value).doubleValue()); |
| 419 |
} |
| 420 |
else if (value instanceof XMPDateTime) |
| 421 |
{ |
| 422 |
strValue = XMPUtils.convertFromDate((XMPDateTime) value); |
| 423 |
} |
| 424 |
else if (value instanceof GregorianCalendar) |
| 425 |
{ |
| 426 |
XMPDateTime dt = XMPDateTimeFactory.createFromCalendar((GregorianCalendar) value); |
| 427 |
strValue = XMPUtils.convertFromDate(dt); |
| 428 |
} |
| 429 |
else if (value instanceof byte[]) |
| 430 |
{ |
| 431 |
strValue = XMPUtils.encodeBase64((byte[]) value); |
| 432 |
} |
| 433 |
else |
| 434 |
{ |
| 435 |
strValue = value.toString(); |
| 436 |
} |
| 437 |
|
| 438 |
return strValue != null ? Utils.removeControlChars(strValue) : null; |
| 439 |
} |
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
private static XMPNode followXPathStep( |
| 461 |
XMPNode parentNode, |
| 462 |
XMPPathSegment nextStep, |
| 463 |
boolean createNodes) throws XMPException |
| 464 |
{ |
| 465 |
XMPNode nextNode = null; |
| 466 |
int index = 0; |
| 467 |
int stepKind = nextStep.getKind(); |
| 468 |
|
| 469 |
if (stepKind == XMPPath.STRUCT_FIELD_STEP) |
| 470 |
{ |
| 471 |
nextNode = findChildNode(parentNode, nextStep.getName(), createNodes); |
| 472 |
} |
| 473 |
else if (stepKind == XMPPath.QUALIFIER_STEP) |
| 474 |
{ |
| 475 |
nextNode = findQualifierNode( |
| 476 |
parentNode, nextStep.getName().substring(1), createNodes); |
| 477 |
} |
| 478 |
else |
| 479 |
{ |
| 480 |
|
| 481 |
|
| 482 |
if (!parentNode.getOptions().isArray()) |
| 483 |
{ |
| 484 |
throw new XMPException("Indexing applied to non-array", XMPError.BADXPATH); |
| 485 |
} |
| 486 |
|
| 487 |
if (stepKind == XMPPath.ARRAY_INDEX_STEP) |
| 488 |
{ |
| 489 |
index = findIndexedItem(parentNode, nextStep.getName(), createNodes); |
| 490 |
} |
| 491 |
else if (stepKind == XMPPath.ARRAY_LAST_STEP) |
| 492 |
{ |
| 493 |
index = parentNode.getChildrenLength(); |
| 494 |
} |
| 495 |
else if (stepKind == XMPPath.FIELD_SELECTOR_STEP) |
| 496 |
{ |
| 497 |
String[] result = Utils.splitNameAndValue(nextStep.getName()); |
| 498 |
String fieldName = result[0]; |
| 499 |
String fieldValue = result[1]; |
| 500 |
index = lookupFieldSelector(parentNode, fieldName, fieldValue); |
| 501 |
} |
| 502 |
else if (stepKind == XMPPath.QUAL_SELECTOR_STEP) |
| 503 |
{ |
| 504 |
String[] result = Utils.splitNameAndValue(nextStep.getName()); |
| 505 |
String qualName = result[0]; |
| 506 |
String qualValue = result[1]; |
| 507 |
index = lookupQualSelector( |
| 508 |
parentNode, qualName, qualValue, nextStep.getAliasForm()); |
| 509 |
} |
| 510 |
else |
| 511 |
{ |
| 512 |
throw new XMPException("Unknown array indexing step in FollowXPathStep", |
| 513 |
XMPError.INTERNALFAILURE); |
| 514 |
} |
| 515 |
|
| 516 |
if (1 <= index && index <= parentNode.getChildrenLength()) |
| 517 |
{ |
| 518 |
nextNode = parentNode.getChild(index); |
| 519 |
} |
| 520 |
} |
| 521 |
|
| 522 |
return nextNode; |
| 523 |
} |
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
private static XMPNode findQualifierNode(XMPNode parent, String qualName, boolean createNodes) |
| 541 |
throws XMPException |
| 542 |
{ |
| 543 |
assert !qualName.startsWith("?"); |
| 544 |
|
| 545 |
XMPNode qualNode = parent.findQualifierByName(qualName); |
| 546 |
|
| 547 |
if (qualNode == null && createNodes) |
| 548 |
{ |
| 549 |
qualNode = new XMPNode(qualName, null); |
| 550 |
qualNode.setImplicit(true); |
| 551 |
|
| 552 |
parent.addQualifier(qualNode); |
| 553 |
} |
| 554 |
|
| 555 |
return qualNode; |
| 556 |
} |
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
private static int findIndexedItem(XMPNode arrayNode, String segment, boolean createNodes) |
| 567 |
throws XMPException |
| 568 |
{ |
| 569 |
int index = 0; |
| 570 |
|
| 571 |
try |
| 572 |
{ |
| 573 |
segment = segment.substring(1, segment.length() - 1); |
| 574 |
index = Integer.parseInt(segment); |
| 575 |
if (index < 1) |
| 576 |
{ |
| 577 |
throw new XMPException("Array index must be larger than zero", |
| 578 |
XMPError.BADXPATH); |
| 579 |
} |
| 580 |
} |
| 581 |
catch (NumberFormatException e) |
| 582 |
{ |
| 583 |
throw new XMPException("Array index not digits.", XMPError.BADXPATH); |
| 584 |
} |
| 585 |
|
| 586 |
if (createNodes && index == arrayNode.getChildrenLength() + 1) |
| 587 |
{ |
| 588 |
|
| 589 |
XMPNode newItem = new XMPNode(ARRAY_ITEM_NAME, null); |
| 590 |
newItem.setImplicit(true); |
| 591 |
arrayNode.addChild(newItem); |
| 592 |
} |
| 593 |
|
| 594 |
return index; |
| 595 |
} |
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
private static int lookupFieldSelector(XMPNode arrayNode, String fieldName, String fieldValue) |
| 610 |
throws XMPException |
| 611 |
{ |
| 612 |
int result = -1; |
| 613 |
|
| 614 |
for (int index = 1; index <= arrayNode.getChildrenLength() && result < 0; index++) |
| 615 |
{ |
| 616 |
XMPNode currItem = arrayNode.getChild(index); |
| 617 |
|
| 618 |
if (!currItem.getOptions().isStruct()) |
| 619 |
{ |
| 620 |
throw new XMPException("Field selector must be used on array of struct", |
| 621 |
XMPError.BADXPATH); |
| 622 |
} |
| 623 |
|
| 624 |
for (int f = 1; f <= currItem.getChildrenLength(); f++) |
| 625 |
{ |
| 626 |
XMPNode currField = currItem.getChild(f); |
| 627 |
if (!fieldName.equals(currField.getName())) |
| 628 |
{ |
| 629 |
continue; |
| 630 |
} |
| 631 |
if (fieldValue.equals(currField.getValue())) |
| 632 |
{ |
| 633 |
result = index; |
| 634 |
break; |
| 635 |
} |
| 636 |
} |
| 637 |
} |
| 638 |
|
| 639 |
return result; |
| 640 |
} |
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
private static int lookupQualSelector(XMPNode arrayNode, String qualName, |
| 658 |
String qualValue, int aliasForm) throws XMPException |
| 659 |
{ |
| 660 |
if (XML_LANG.equals(qualName)) |
| 661 |
{ |
| 662 |
qualValue = Utils.normalizeLangValue(qualValue); |
| 663 |
int index = XMPNodeUtils.lookupLanguageItem(arrayNode, qualValue); |
| 664 |
if (index < 0 && (aliasForm & AliasOptions.PROP_ARRAY_ALT_TEXT) > 0) |
| 665 |
{ |
| 666 |
XMPNode langNode = new XMPNode(ARRAY_ITEM_NAME, null); |
| 667 |
XMPNode xdefault = new XMPNode(XML_LANG, X_DEFAULT, null); |
| 668 |
langNode.addQualifier(xdefault); |
| 669 |
arrayNode.addChild(1, langNode); |
| 670 |
return 1; |
| 671 |
} |
| 672 |
else |
| 673 |
{ |
| 674 |
return index; |
| 675 |
} |
| 676 |
} |
| 677 |
else |
| 678 |
{ |
| 679 |
for (int index = 1; index < arrayNode.getChildrenLength(); index++) |
| 680 |
{ |
| 681 |
XMPNode currItem = arrayNode.getChild(index); |
| 682 |
|
| 683 |
for (Iterator it = currItem.iterateQualifier(); it.hasNext();) |
| 684 |
{ |
| 685 |
XMPNode qualifier = (XMPNode) it.next(); |
| 686 |
if (qualName.equals(qualifier.getName()) && |
| 687 |
qualValue.equals(qualifier.getValue())) |
| 688 |
{ |
| 689 |
return index; |
| 690 |
} |
| 691 |
} |
| 692 |
} |
| 693 |
return -1; |
| 694 |
} |
| 695 |
} |
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
static void normalizeLangArray(XMPNode arrayNode) |
| 709 |
{ |
| 710 |
if (!arrayNode.getOptions().isArrayAltText()) |
| 711 |
{ |
| 712 |
return; |
| 713 |
} |
| 714 |
|
| 715 |
|
| 716 |
for (int i = 2; i <= arrayNode.getChildrenLength(); i++) |
| 717 |
{ |
| 718 |
XMPNode child = arrayNode.getChild(i); |
| 719 |
if (child.hasQualifier() && X_DEFAULT.equals(child.getQualifier(1).getValue())) |
| 720 |
{ |
| 721 |
|
| 722 |
try |
| 723 |
{ |
| 724 |
arrayNode.removeChild(i); |
| 725 |
arrayNode.addChild(1, child); |
| 726 |
} |
| 727 |
catch (XMPException e) |
| 728 |
{ |
| 729 |
|
| 730 |
assert false; |
| 731 |
} |
| 732 |
|
| 733 |
if (i == 2) |
| 734 |
{ |
| 735 |
arrayNode.getChild(2).setValue(child.getValue()); |
| 736 |
} |
| 737 |
break; |
| 738 |
} |
| 739 |
} |
| 740 |
} |
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
static void detectAltText(XMPNode arrayNode) |
| 751 |
{ |
| 752 |
if (arrayNode.getOptions().isArrayAlternate() && arrayNode.hasChildren()) |
| 753 |
{ |
| 754 |
boolean isAltText = false; |
| 755 |
for (Iterator it = arrayNode.iterateChildren(); it.hasNext();) |
| 756 |
{ |
| 757 |
XMPNode child = (XMPNode) it.next(); |
| 758 |
if (child.getOptions().getHasLanguage()) |
| 759 |
{ |
| 760 |
isAltText = true; |
| 761 |
break; |
| 762 |
} |
| 763 |
} |
| 764 |
|
| 765 |
if (isAltText) |
| 766 |
{ |
| 767 |
arrayNode.getOptions().setArrayAltText(true); |
| 768 |
normalizeLangArray(arrayNode); |
| 769 |
} |
| 770 |
} |
| 771 |
} |
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
static void appendLangItem(XMPNode arrayNode, String itemLang, String itemValue) |
| 783 |
throws XMPException |
| 784 |
{ |
| 785 |
XMPNode newItem = new XMPNode(ARRAY_ITEM_NAME, itemValue, null); |
| 786 |
XMPNode langQual = new XMPNode(XML_LANG, itemLang, null); |
| 787 |
newItem.addQualifier(langQual); |
| 788 |
|
| 789 |
if (!X_DEFAULT.equals(langQual.getValue())) |
| 790 |
{ |
| 791 |
arrayNode.addChild(newItem); |
| 792 |
} |
| 793 |
else |
| 794 |
{ |
| 795 |
arrayNode.addChild(1, newItem); |
| 796 |
} |
| 797 |
} |
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
static Object[] chooseLocalizedText(XMPNode arrayNode, String genericLang, String specificLang) |
| 820 |
throws XMPException |
| 821 |
{ |
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
if (!arrayNode.getOptions().isArrayAltText()) |
| 826 |
{ |
| 827 |
throw new XMPException("Localized text array is not alt-text", XMPError.BADXPATH); |
| 828 |
} |
| 829 |
else if (!arrayNode.hasChildren()) |
| 830 |
{ |
| 831 |
return new Object[] { new Integer(XMPNodeUtils.CLT_NO_VALUES), null }; |
| 832 |
} |
| 833 |
|
| 834 |
int foundGenericMatches = 0; |
| 835 |
XMPNode resultNode = null; |
| 836 |
XMPNode xDefault = null; |
| 837 |
|
| 838 |
|
| 839 |
for (Iterator it = arrayNode.iterateChildren(); it.hasNext();) |
| 840 |
{ |
| 841 |
XMPNode currItem = (XMPNode) it.next(); |
| 842 |
|
| 843 |
|
| 844 |
if (currItem.getOptions().isCompositeProperty()) |
| 845 |
{ |
| 846 |
throw new XMPException("Alt-text array item is not simple", XMPError.BADXPATH); |
| 847 |
} |
| 848 |
else if (!currItem.hasQualifier() |
| 849 |
|| !XML_LANG.equals(currItem.getQualifier(1).getName())) |
| 850 |
{ |
| 851 |
throw new XMPException("Alt-text array item has no language qualifier", |
| 852 |
XMPError.BADXPATH); |
| 853 |
} |
| 854 |
|
| 855 |
String currLang = currItem.getQualifier(1).getValue(); |
| 856 |
|
| 857 |
|
| 858 |
if (specificLang.equals(currLang)) |
| 859 |
{ |
| 860 |
return new Object[] { new Integer(XMPNodeUtils.CLT_SPECIFIC_MATCH), currItem }; |
| 861 |
} |
| 862 |
else if (genericLang != null && currLang.startsWith(genericLang)) |
| 863 |
{ |
| 864 |
if (resultNode == null) |
| 865 |
{ |
| 866 |
resultNode = currItem; |
| 867 |
} |
| 868 |
|
| 869 |
foundGenericMatches++; |
| 870 |
} |
| 871 |
else if (X_DEFAULT.equals(currLang)) |
| 872 |
{ |
| 873 |
xDefault = currItem; |
| 874 |
} |
| 875 |
} |
| 876 |
|
| 877 |
|
| 878 |
if (foundGenericMatches == 1) |
| 879 |
{ |
| 880 |
return new Object[] { new Integer(XMPNodeUtils.CLT_SINGLE_GENERIC), resultNode }; |
| 881 |
} |
| 882 |
else if (foundGenericMatches > 1) |
| 883 |
{ |
| 884 |
return new Object[] { new Integer(XMPNodeUtils.CLT_MULTIPLE_GENERIC), resultNode }; |
| 885 |
} |
| 886 |
else if (xDefault != null) |
| 887 |
{ |
| 888 |
return new Object[] { new Integer(XMPNodeUtils.CLT_XDEFAULT), xDefault }; |
| 889 |
} |
| 890 |
else |
| 891 |
{ |
| 892 |
|
| 893 |
return new Object[] { new Integer(XMPNodeUtils.CLT_FIRST_ITEM), arrayNode.getChild(1) }; |
| 894 |
} |
| 895 |
} |
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
static int lookupLanguageItem(XMPNode arrayNode, String language) throws XMPException |
| 909 |
{ |
| 910 |
if (!arrayNode.getOptions().isArray()) |
| 911 |
{ |
| 912 |
|
| 913 |
throw new XMPException("Language item must be used on array", XMPError.BADXPATH); |
| 914 |
} |
| 915 |
|
| 916 |
for (int index = 1; index <= arrayNode.getChildrenLength(); index++) |
| 917 |
{ |
| 918 |
XMPNode child = arrayNode.getChild(index); |
| 919 |
if (!child.hasQualifier() || !XML_LANG.equals(child.getQualifier(1).getName())) |
| 920 |
{ |
| 921 |
continue; |
| 922 |
} |
| 923 |
else if (language.equals(child.getQualifier(1).getValue())) |
| 924 |
{ |
| 925 |
return index; |
| 926 |
} |
| 927 |
} |
| 928 |
|
| 929 |
return -1; |
| 930 |
} |
| 931 |
} |