| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
package com.adobe.internal.xmp.impl.xpath; |
| 11 |
|
| 12 |
import com.adobe.internal.xmp.XMPError; |
| 13 |
import com.adobe.internal.xmp.XMPException; |
| 14 |
import com.adobe.internal.xmp.XMPMetaFactory; |
| 15 |
import com.adobe.internal.xmp.impl.Utils; |
| 16 |
import com.adobe.internal.xmp.properties.XMPAliasInfo; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
public final class XMPPathParser |
| 27 |
{ |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
private XMPPathParser() |
| 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 |
public static XMPPath expandXPath(String schemaNS, String path) throws XMPException |
| 88 |
{ |
| 89 |
if (schemaNS == null || path == null) |
| 90 |
{ |
| 91 |
throw new XMPException("Parameter must not be null", XMPError.BADPARAM); |
| 92 |
} |
| 93 |
|
| 94 |
XMPPath expandedXPath = new XMPPath(); |
| 95 |
PathPosition pos = new PathPosition(); |
| 96 |
pos.path = path; |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
parseRootNode(schemaNS, pos, expandedXPath); |
| 101 |
|
| 102 |
|
| 103 |
while (pos.stepEnd < path.length()) |
| 104 |
{ |
| 105 |
pos.stepBegin = pos.stepEnd; |
| 106 |
|
| 107 |
skipPathDelimiter(path, pos); |
| 108 |
|
| 109 |
pos.stepEnd = pos.stepBegin; |
| 110 |
|
| 111 |
|
| 112 |
XMPPathSegment segment; |
| 113 |
if (path.charAt(pos.stepBegin) != '[') |
| 114 |
{ |
| 115 |
|
| 116 |
segment = parseStructSegment(pos); |
| 117 |
} |
| 118 |
else |
| 119 |
{ |
| 120 |
|
| 121 |
segment = parseIndexSegment(pos); |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
if (segment.getKind() == XMPPath.STRUCT_FIELD_STEP) |
| 126 |
{ |
| 127 |
if (segment.getName().charAt(0) == '@') |
| 128 |
{ |
| 129 |
segment.setName("?" + segment.getName().substring(1)); |
| 130 |
if (!"?xml:lang".equals(segment.getName())) |
| 131 |
{ |
| 132 |
throw new XMPException("Only xml:lang allowed with '@'", |
| 133 |
XMPError.BADXPATH); |
| 134 |
} |
| 135 |
} |
| 136 |
if (segment.getName().charAt(0) == '?') |
| 137 |
{ |
| 138 |
pos.nameStart++; |
| 139 |
segment.setKind(XMPPath.QUALIFIER_STEP); |
| 140 |
} |
| 141 |
|
| 142 |
verifyQualName(pos.path.substring(pos.nameStart, pos.nameEnd)); |
| 143 |
} |
| 144 |
else if (segment.getKind() == XMPPath.FIELD_SELECTOR_STEP) |
| 145 |
{ |
| 146 |
if (segment.getName().charAt(1) == '@') |
| 147 |
{ |
| 148 |
segment.setName("[?" + segment.getName().substring(2)); |
| 149 |
if (!segment.getName().startsWith("[?xml:lang=")) |
| 150 |
{ |
| 151 |
throw new XMPException("Only xml:lang allowed with '@'", |
| 152 |
XMPError.BADXPATH); |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
if (segment.getName().charAt(1) == '?') |
| 157 |
{ |
| 158 |
pos.nameStart++; |
| 159 |
segment.setKind(XMPPath.QUAL_SELECTOR_STEP); |
| 160 |
verifyQualName(pos.path.substring(pos.nameStart, pos.nameEnd)); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
expandedXPath.add(segment); |
| 165 |
} |
| 166 |
return expandedXPath; |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
private static void skipPathDelimiter(String path, PathPosition pos) throws XMPException |
| 176 |
{ |
| 177 |
if (path.charAt(pos.stepBegin) == '/') |
| 178 |
{ |
| 179 |
|
| 180 |
|
| 181 |
pos.stepBegin++; |
| 182 |
|
| 183 |
|
| 184 |
if (pos.stepBegin >= path.length()) |
| 185 |
{ |
| 186 |
throw new XMPException("Empty XMPPath segment", XMPError.BADXPATH); |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
if (path.charAt(pos.stepBegin) == '*') |
| 191 |
{ |
| 192 |
|
| 193 |
|
| 194 |
pos.stepBegin++; |
| 195 |
if (pos.stepBegin >= path.length() || path.charAt(pos.stepBegin) != '[') |
| 196 |
{ |
| 197 |
throw new XMPException("Missing '[' after '*'", XMPError.BADXPATH); |
| 198 |
} |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
private static XMPPathSegment parseStructSegment(PathPosition pos) throws XMPException |
| 210 |
{ |
| 211 |
pos.nameStart = pos.stepBegin; |
| 212 |
while (pos.stepEnd < pos.path.length() && "/[*".indexOf(pos.path.charAt(pos.stepEnd)) < 0) |
| 213 |
{ |
| 214 |
pos.stepEnd++; |
| 215 |
} |
| 216 |
pos.nameEnd = pos.stepEnd; |
| 217 |
|
| 218 |
if (pos.stepEnd == pos.stepBegin) |
| 219 |
{ |
| 220 |
throw new XMPException("Empty XMPPath segment", XMPError.BADXPATH); |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
XMPPathSegment segment = new XMPPathSegment(pos.path.substring(pos.stepBegin, pos.stepEnd), |
| 225 |
XMPPath.STRUCT_FIELD_STEP); |
| 226 |
return segment; |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
private static XMPPathSegment parseIndexSegment(PathPosition pos) throws XMPException |
| 239 |
{ |
| 240 |
XMPPathSegment segment; |
| 241 |
pos.stepEnd++; |
| 242 |
|
| 243 |
if ('0' <= pos.path.charAt(pos.stepEnd) && pos.path.charAt(pos.stepEnd) <= '9') |
| 244 |
{ |
| 245 |
|
| 246 |
while (pos.stepEnd < pos.path.length() && '0' <= pos.path.charAt(pos.stepEnd) |
| 247 |
&& pos.path.charAt(pos.stepEnd) <= '9') |
| 248 |
{ |
| 249 |
pos.stepEnd++; |
| 250 |
} |
| 251 |
|
| 252 |
segment = new XMPPathSegment(null, XMPPath.ARRAY_INDEX_STEP); |
| 253 |
} |
| 254 |
else |
| 255 |
{ |
| 256 |
|
| 257 |
|
| 258 |
while (pos.stepEnd < pos.path.length() && pos.path.charAt(pos.stepEnd) != ']' |
| 259 |
&& pos.path.charAt(pos.stepEnd) != '=') |
| 260 |
{ |
| 261 |
pos.stepEnd++; |
| 262 |
} |
| 263 |
|
| 264 |
if (pos.stepEnd >= pos.path.length()) |
| 265 |
{ |
| 266 |
throw new XMPException("Missing ']' or '=' for array index", XMPError.BADXPATH); |
| 267 |
} |
| 268 |
|
| 269 |
if (pos.path.charAt(pos.stepEnd) == ']') |
| 270 |
{ |
| 271 |
if (!"[last()".equals(pos.path.substring(pos.stepBegin, pos.stepEnd))) |
| 272 |
{ |
| 273 |
throw new XMPException( |
| 274 |
"Invalid non-numeric array index", XMPError.BADXPATH); |
| 275 |
} |
| 276 |
segment = new XMPPathSegment(null, XMPPath.ARRAY_LAST_STEP); |
| 277 |
} |
| 278 |
else |
| 279 |
{ |
| 280 |
pos.nameStart = pos.stepBegin + 1; |
| 281 |
pos.nameEnd = pos.stepEnd; |
| 282 |
pos.stepEnd++; |
| 283 |
char quote = pos.path.charAt(pos.stepEnd); |
| 284 |
if (quote != '\'' && quote != '"') |
| 285 |
{ |
| 286 |
throw new XMPException( |
| 287 |
"Invalid quote in array selector", XMPError.BADXPATH); |
| 288 |
} |
| 289 |
|
| 290 |
pos.stepEnd++; |
| 291 |
while (pos.stepEnd < pos.path.length()) |
| 292 |
{ |
| 293 |
if (pos.path.charAt(pos.stepEnd) == quote) |
| 294 |
{ |
| 295 |
|
| 296 |
if (pos.stepEnd + 1 >= pos.path.length() |
| 297 |
|| pos.path.charAt(pos.stepEnd + 1) != quote) |
| 298 |
{ |
| 299 |
break; |
| 300 |
} |
| 301 |
pos.stepEnd++; |
| 302 |
} |
| 303 |
pos.stepEnd++; |
| 304 |
} |
| 305 |
|
| 306 |
if (pos.stepEnd >= pos.path.length()) |
| 307 |
{ |
| 308 |
throw new XMPException("No terminating quote for array selector", |
| 309 |
XMPError.BADXPATH); |
| 310 |
} |
| 311 |
pos.stepEnd++; |
| 312 |
|
| 313 |
|
| 314 |
segment = new XMPPathSegment(null, XMPPath.FIELD_SELECTOR_STEP); |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
|
| 319 |
if (pos.stepEnd >= pos.path.length() || pos.path.charAt(pos.stepEnd) != ']') |
| 320 |
{ |
| 321 |
throw new XMPException("Missing ']' for array index", XMPError.BADXPATH); |
| 322 |
} |
| 323 |
pos.stepEnd++; |
| 324 |
segment.setName(pos.path.substring(pos.stepBegin, pos.stepEnd)); |
| 325 |
|
| 326 |
return segment; |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
private static void parseRootNode(String schemaNS, PathPosition pos, XMPPath expandedXPath) |
| 339 |
throws XMPException |
| 340 |
{ |
| 341 |
while (pos.stepEnd < pos.path.length() && "/[*".indexOf(pos.path.charAt(pos.stepEnd)) < 0) |
| 342 |
{ |
| 343 |
pos.stepEnd++; |
| 344 |
} |
| 345 |
|
| 346 |
if (pos.stepEnd == pos.stepBegin) |
| 347 |
{ |
| 348 |
throw new XMPException("Empty initial XMPPath step", XMPError.BADXPATH); |
| 349 |
} |
| 350 |
|
| 351 |
String rootProp = verifyXPathRoot(schemaNS, pos.path.substring(pos.stepBegin, pos.stepEnd)); |
| 352 |
XMPAliasInfo aliasInfo = XMPMetaFactory.getSchemaRegistry().findAlias(rootProp); |
| 353 |
if (aliasInfo == null) |
| 354 |
{ |
| 355 |
|
| 356 |
expandedXPath.add(new XMPPathSegment(schemaNS, XMPPath.SCHEMA_NODE)); |
| 357 |
XMPPathSegment rootStep = new XMPPathSegment(rootProp, XMPPath.STRUCT_FIELD_STEP); |
| 358 |
expandedXPath.add(rootStep); |
| 359 |
} |
| 360 |
else |
| 361 |
{ |
| 362 |
|
| 363 |
expandedXPath.add(new XMPPathSegment(aliasInfo.getNamespace(), XMPPath.SCHEMA_NODE)); |
| 364 |
XMPPathSegment rootStep = new XMPPathSegment(verifyXPathRoot(aliasInfo.getNamespace(), |
| 365 |
aliasInfo.getPropName()), |
| 366 |
XMPPath.STRUCT_FIELD_STEP); |
| 367 |
rootStep.setAlias(true); |
| 368 |
rootStep.setAliasForm(aliasInfo.getAliasForm().getOptions()); |
| 369 |
expandedXPath.add(rootStep); |
| 370 |
|
| 371 |
if (aliasInfo.getAliasForm().isArrayAltText()) |
| 372 |
{ |
| 373 |
XMPPathSegment qualSelectorStep = new XMPPathSegment("[?xml:lang='x-default']", |
| 374 |
XMPPath.QUAL_SELECTOR_STEP); |
| 375 |
qualSelectorStep.setAlias(true); |
| 376 |
qualSelectorStep.setAliasForm(aliasInfo.getAliasForm().getOptions()); |
| 377 |
expandedXPath.add(qualSelectorStep); |
| 378 |
} |
| 379 |
else if (aliasInfo.getAliasForm().isArray()) |
| 380 |
{ |
| 381 |
XMPPathSegment indexStep = new XMPPathSegment("[1]", |
| 382 |
XMPPath.ARRAY_INDEX_STEP); |
| 383 |
indexStep.setAlias(true); |
| 384 |
indexStep.setAliasForm(aliasInfo.getAliasForm().getOptions()); |
| 385 |
expandedXPath.add(indexStep); |
| 386 |
} |
| 387 |
} |
| 388 |
} |
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
private static void verifyQualName(String qualName) throws XMPException |
| 401 |
{ |
| 402 |
int colonPos = qualName.indexOf(':'); |
| 403 |
if (colonPos > 0) |
| 404 |
{ |
| 405 |
String prefix = qualName.substring(0, colonPos); |
| 406 |
if (Utils.isXMLNameNS(prefix)) |
| 407 |
{ |
| 408 |
String regURI = XMPMetaFactory.getSchemaRegistry().getNamespaceURI( |
| 409 |
prefix); |
| 410 |
if (regURI != null) |
| 411 |
{ |
| 412 |
return; |
| 413 |
} |
| 414 |
|
| 415 |
throw new XMPException("Unknown namespace prefix for qualified name", |
| 416 |
XMPError.BADXPATH); |
| 417 |
} |
| 418 |
} |
| 419 |
|
| 420 |
throw new XMPException("Ill-formed qualified name", XMPError.BADXPATH); |
| 421 |
} |
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
private static void verifySimpleXMLName(String name) throws XMPException |
| 433 |
{ |
| 434 |
if (!Utils.isXMLName(name)) |
| 435 |
{ |
| 436 |
throw new XMPException("Bad XML name", XMPError.BADXPATH); |
| 437 |
} |
| 438 |
} |
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
private static String verifyXPathRoot(String schemaNS, String rootProp) |
| 456 |
throws XMPException |
| 457 |
{ |
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
if (schemaNS == null || schemaNS.length() == 0) |
| 462 |
{ |
| 463 |
throw new XMPException( |
| 464 |
"Schema namespace URI is required", XMPError.BADSCHEMA); |
| 465 |
} |
| 466 |
|
| 467 |
if ((rootProp.charAt(0) == '?') || (rootProp.charAt(0) == '@')) |
| 468 |
{ |
| 469 |
throw new XMPException("Top level name must not be a qualifier", XMPError.BADXPATH); |
| 470 |
} |
| 471 |
|
| 472 |
if (rootProp.indexOf('/') >= 0 || rootProp.indexOf('[') >= 0) |
| 473 |
{ |
| 474 |
throw new XMPException("Top level name must be simple", XMPError.BADXPATH); |
| 475 |
} |
| 476 |
|
| 477 |
String prefix = XMPMetaFactory.getSchemaRegistry().getNamespacePrefix(schemaNS); |
| 478 |
if (prefix == null) |
| 479 |
{ |
| 480 |
throw new XMPException("Unregistered schema namespace URI", XMPError.BADSCHEMA); |
| 481 |
} |
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
int colonPos = rootProp.indexOf(':'); |
| 486 |
if (colonPos < 0) |
| 487 |
{ |
| 488 |
|
| 489 |
|
| 490 |
verifySimpleXMLName(rootProp); |
| 491 |
return prefix + rootProp; |
| 492 |
} |
| 493 |
else |
| 494 |
{ |
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
verifySimpleXMLName(rootProp.substring(0, colonPos)); |
| 500 |
verifySimpleXMLName(rootProp.substring(colonPos)); |
| 501 |
|
| 502 |
prefix = rootProp.substring(0, colonPos + 1); |
| 503 |
|
| 504 |
String regPrefix = XMPMetaFactory.getSchemaRegistry().getNamespacePrefix(schemaNS); |
| 505 |
if (regPrefix == null) |
| 506 |
{ |
| 507 |
throw new XMPException("Unknown schema namespace prefix", XMPError.BADSCHEMA); |
| 508 |
} |
| 509 |
if (!prefix.equals(regPrefix)) |
| 510 |
{ |
| 511 |
throw new XMPException("Schema namespace URI and prefix mismatch", |
| 512 |
XMPError.BADSCHEMA); |
| 513 |
} |
| 514 |
|
| 515 |
return rootProp; |
| 516 |
} |
| 517 |
} |
| 518 |
} |
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
class PathPosition |
| 528 |
{ |
| 529 |
|
| 530 |
public String path = null; |
| 531 |
|
| 532 |
int nameStart = 0; |
| 533 |
|
| 534 |
int nameEnd = 0; |
| 535 |
|
| 536 |
int stepBegin = 0; |
| 537 |
|
| 538 |
int stepEnd = 0; |
| 539 |
} |
| 540 |
|