Debian Codegraph @

src/com/adobe/internal/xmp/impl/xpath/XMPPathParser.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.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 * Parser for XMP XPaths.
21 *
22 * @author Stefan Makswit
23 * @version $Revision$
24 * @since 01.03.2006
25 */
26 public final class XMPPathParser
27 {
28 /**
29 * Private constructor
30 */
31 private XMPPathParser()
32 {
33 // empty
34 }
35
36
37 /**
38 * Split an XMPPath expression apart at the conceptual steps, adding the
39 * root namespace prefix to the first property component. The schema URI is
40 * put in the first (0th) slot in the expanded XMPPath. Check if the top
41 * level component is an alias, but don't resolve it.
42 * <p>
43 * In the most verbose case steps are separated by '/', and each step can be
44 * of these forms:
45 * <dl>
46 * <dt>prefix:name
47 * <dd> A top level property or struct field.
48 * <dt>[index]
49 * <dd> An element of an array.
50 * <dt>[last()]
51 * <dd> The last element of an array.
52 * <dt>[fieldName=&quot;value&quot;]
53 * <dd> An element in an array of structs, chosen by a field value.
54 * <dt>[@xml:lang=&quot;value&quot;]
55 * <dd> An element in an alt-text array, chosen by the xml:lang qualifier.
56 * <dt>[?qualName=&quot;value&quot;]
57 * <dd> An element in an array, chosen by a qualifier value.
58 * <dt>@xml:lang
59 * <dd> An xml:lang qualifier.
60 * <dt>?qualName
61 * <dd> A general qualifier.
62 * </dl>
63 * <p>
64 * The logic is complicated though by shorthand for arrays, the separating
65 * '/' and leading '*' are optional. These are all equivalent: array/*[2]
66 * array/[2] array*[2] array[2] All of these are broken into the 2 steps
67 * "array" and "[2]".
68 * <p>
69 * The value portion in the array selector forms is a string quoted by '''
70 * or '"'. The value may contain any character including a doubled quoting
71 * character. The value may be empty.
72 * <p>
73 * The syntax isn't checked, but an XML name begins with a letter or '_',
74 * and contains letters, digits, '.', '-', '_', and a bunch of special
75 * non-ASCII Unicode characters. An XML qualified name is a pair of names
76 * separated by a colon.
77 * @param schemaNS
78 * schema namespace
79 * @param path
80 * property name
81 * @return Returns the expandet XMPPath.
82 * @throws XMPException
83 * Thrown if the format is not correct somehow.
84 *
85 * FfF: remove the schema step in the future
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 // Pull out the first component and do some special processing on it: add the schema
99 // namespace prefix and and see if it is an alias. The start must be a "qualName".
100 parseRootNode(schemaNS, pos, expandedXPath);
101
102 // Now continue to process the rest of the XMPPath string.
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 // A struct field or qualifier.
116 segment = parseStructSegment(pos);
117 }
118 else
119 {
120 // One of the array forms.
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 * @param path
172 * @param pos
173 * @throws XMPException
174 */
175 private static void skipPathDelimiter(String path, PathPosition pos) throws XMPException
176 {
177 if (path.charAt(pos.stepBegin) == '/')
178 {
179 // skip slash
180
181 pos.stepBegin++;
182
183 // added for Java
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 // skip asterisk
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 * Parses a struct segment
205 * @param pos the current position in the path
206 * @return Retusn the segment or an errror
207 * @throws XMPException If the sement is empty
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 // ! Touch up later, also changing '@' to '?'.
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 * Parses an array index segment.
232 *
233 * @param pos the xmp path
234 * @return Returns the segment or an error
235 * @throws XMPException thrown on xmp path errors
236 *
237 */
238 private static XMPPathSegment parseIndexSegment(PathPosition pos) throws XMPException
239 {
240 XMPPathSegment segment;
241 pos.stepEnd++; // Look at the character after the leading '['.
242
243 if ('0' <= pos.path.charAt(pos.stepEnd) && pos.path.charAt(pos.stepEnd) <= '9')
244 {
245 // A numeric (decimal integer) array index.
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 // Could be "[last()]" or one of the selector forms. Find the ']' or '='.
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++; // Absorb the '=', remember the quote.
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++; // Absorb the leading quote.
291 while (pos.stepEnd < pos.path.length())
292 {
293 if (pos.path.charAt(pos.stepEnd) == quote)
294 {
295 // check for escaped quote
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++; // Absorb the trailing quote.
312
313 // ! Touch up later, also changing '@' to '?'.
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 * Parses the root node of an XMP Path, checks if namespace and prefix fit together
332 * and resolve the property to the base property if it is an alias.
333 * @param schemaNS the root namespace
334 * @param pos the parsing position helper
335 * @param expandedXPath the path to contribute to
336 * @throws XMPException If the path is not valid.
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 // add schema xpath step
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 // add schema xpath step and base step of alias
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 * Verifies whether the qualifier name is not XML conformant or the
393 * namespace prefix has not been registered.
394 *
395 * @param qualName
396 * a qualifier name
397 * @throws XMPException
398 * If the name is not conformant
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 * Verify if an XML name is conformant.
426 *
427 * @param name
428 * an XML name
429 * @throws XMPException
430 * When the name is not XML conformant
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 * Set up the first 2 components of the expanded XMPPath. Normalizes the various cases of using
443 * the full schema URI and/or a qualified root property name. Returns true for normal
444 * processing. If allowUnknownSchemaNS is true and the schema namespace is not registered, false
445 * is returned. If allowUnknownSchemaNS is false and the schema namespace is not registered, an
446 * exception is thrown
447 * <P>
448 * (Should someday check the full syntax:)
449 *
450 * @param schemaNS schema namespace
451 * @param rootProp the root xpath segment
452 * @return Returns root QName.
453 * @throws XMPException Thrown if the format is not correct somehow.
454 */
455 private static String verifyXPathRoot(String schemaNS, String rootProp)
456 throws XMPException
457 {
458 // Do some basic checks on the URI and name. Try to lookup the URI. See if the name is
459 // qualified.
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 // Verify the various URI and prefix combinations. Initialize the
484 // expanded XMPPath.
485 int colonPos = rootProp.indexOf(':');
486 if (colonPos < 0)
487 {
488 // The propName is unqualified, use the schemaURI and associated
489 // prefix.
490 verifySimpleXMLName(rootProp); // Verify the part before any colon
491 return prefix + rootProp;
492 }
493 else
494 {
495 // The propName is qualified. Make sure the prefix is legit. Use the associated URI and
496 // qualified name.
497
498 // Verify the part before any colon
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 * This objects contains all needed char positions to parse.
526 */
527 class PathPosition
528 {
529 /** the complete path */
530 public String path = null;
531 /** the start of a segment name */
532 int nameStart = 0;
533 /** the end of a segment name */
534 int nameEnd = 0;
535 /** the begin of a step */
536 int stepBegin = 0;
537 /** the end of a step */
538 int stepEnd = 0;
539 }
540