Debian Codegraph @

src/com/adobe/internal/xmp/impl/ParseRDF.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.List;
13 import java.util.ArrayList;
14 import java.util.Iterator;
15 import org.w3c.dom.Attr;
16 import org.w3c.dom.NamedNodeMap;
17 import org.w3c.dom.Node;
18
19 import com.adobe.internal.xmp.XMPConst;
20 import com.adobe.internal.xmp.XMPError;
21 import com.adobe.internal.xmp.XMPException;
22 import com.adobe.internal.xmp.XMPMetaFactory;
23 import com.adobe.internal.xmp.XMPSchemaRegistry;
24 import com.adobe.internal.xmp.options.ParseOptions;
25 import com.adobe.internal.xmp.options.PropertyOptions;
26
27
28 /**
29 * Parser for "normal" XML serialisation of RDF.
30 *
31 * @author Stefan Makswit
32 * @version $Revision$
33 * @since 14.07.2006
34 */
35 public class ParseRDF implements XMPError, XMPConst
36 {
37 /** */
38 public static final int RDFTERM_OTHER = 0;
39 /** Start of coreSyntaxTerms. */
40 public static final int RDFTERM_RDF = 1;
41 /** */
42 public static final int RDFTERM_ID = 2;
43 /** */
44 public static final int RDFTERM_ABOUT = 3;
45 /** */
46 public static final int RDFTERM_PARSE_TYPE = 4;
47 /** */
48 public static final int RDFTERM_RESOURCE = 5;
49 /** */
50 public static final int RDFTERM_NODE_ID = 6;
51 /** End of coreSyntaxTerms */
52 public static final int RDFTERM_DATATYPE = 7;
53 /** Start of additions for syntax Terms. */
54 public static final int RDFTERM_DESCRIPTION = 8;
55 /** End of of additions for syntaxTerms. */
56 public static final int RDFTERM_LI = 9;
57 /** Start of oldTerms. */
58 public static final int RDFTERM_ABOUT_EACH = 10;
59 /** */
60 public static final int RDFTERM_ABOUT_EACH_PREFIX = 11;
61 /** End of oldTerms. */
62 public static final int RDFTERM_BAG_ID = 12;
63 /** */
64 public static final int RDFTERM_FIRST_CORE = RDFTERM_RDF;
65 /** */
66 public static final int RDFTERM_LAST_CORE = RDFTERM_DATATYPE;
67 /** ! Yes, the syntax terms include the core terms. */
68 public static final int RDFTERM_FIRST_SYNTAX = RDFTERM_FIRST_CORE;
69 /** */
70 public static final int RDFTERM_LAST_SYNTAX = RDFTERM_LI;
71 /** */
72 public static final int RDFTERM_FIRST_OLD = RDFTERM_ABOUT_EACH;
73 /** */
74 public static final int RDFTERM_LAST_OLD = RDFTERM_BAG_ID;
75 /** this prefix is used for default namespaces */
76 public static final String DEFAULT_PREFIX = "_dflt";
77
78
79
80 /**
81 * The main parsing method. The XML tree is walked through from the root node and and XMP tree
82 * is created. This is a raw parse, the normalisation of the XMP tree happens outside.
83 *
84 * @param xmlRoot the XML root node
85 * @param options ParseOptions to indicate the parse options provided by the client
86 * @return Returns an XMP metadata object (not normalized)
87 * @throws XMPException Occurs if the parsing fails for any reason.
88 */
89 static XMPMetaImpl parse(Node xmlRoot, ParseOptions options) throws XMPException
90 {
91 XMPMetaImpl xmp = new XMPMetaImpl();
92 rdf_RDF(xmp, xmlRoot, options);
93 return xmp;
94 }
95
96
97 /**
98 * Each of these parsing methods is responsible for recognizing an RDF
99 * syntax production and adding the appropriate structure to the XMP tree.
100 * They simply return for success, failures will throw an exception.
101 *
102 * @param xmp the xmp metadata object that is generated
103 * @param rdfRdfNode the top-level xml node
104 * @param options ParseOptions to indicate the parse options provided by the client
105 * @throws XMPException thown on parsing errors
106 */
107 static void rdf_RDF(XMPMetaImpl xmp, Node rdfRdfNode, ParseOptions options) throws XMPException
108 {
109 if (rdfRdfNode.hasAttributes())
110 {
111 rdf_NodeElementList (xmp, xmp.getRoot(), rdfRdfNode, options);
112 }
113 else
114 {
115 throw new XMPException("Invalid attributes of rdf:RDF element", BADRDF);
116 }
117 }
118
119
120 /**
121 * 7.2.10 nodeElementList<br>
122 * ws* ( nodeElement ws* )*
123 *
124 * Note: this method is only called from the rdf:RDF-node (top level)
125 * @param xmp the xmp metadata object that is generated
126 * @param xmpParent the parent xmp node
127 * @param rdfRdfNode the top-level xml node
128 * @param options ParseOptions to indicate the parse options provided by the client
129 * @throws XMPException thown on parsing errors
130 */
131 private static void rdf_NodeElementList(XMPMetaImpl xmp, XMPNode xmpParent, Node rdfRdfNode, ParseOptions options)
132 throws XMPException
133 {
134 for (int i = 0; i < rdfRdfNode.getChildNodes().getLength(); i++)
135 {
136 Node child = rdfRdfNode.getChildNodes().item(i);
137 // filter whitespaces (and all text nodes)
138 if (!isWhitespaceNode(child))
139 {
140 rdf_NodeElement (xmp, xmpParent, child, true, options);
141 }
142 }
143 }
144
145
146 /**
147 * 7.2.5 nodeElementURIs
148 * anyURI - ( coreSyntaxTerms | rdf:li | oldTerms )
149 *
150 * 7.2.11 nodeElement
151 * start-element ( URI == nodeElementURIs,
152 * attributes == set ( ( idAttr | nodeIdAttr | aboutAttr )?, propertyAttr* ) )
153 * propertyEltList
154 * end-element()
155 *
156 * A node element URI is rdf:Description or anything else that is not an RDF
157 * term.
158 *
159 * @param xmp the xmp metadata object that is generated
160 * @param xmpParent the parent xmp node
161 * @param xmlNode the currently processed XML node
162 * @param isTopLevel Flag if the node is a top-level node
163 * @param options ParseOptions to indicate the parse options provided by the client
164 * @throws XMPException thown on parsing errors
165 */
166 private static void rdf_NodeElement(XMPMetaImpl xmp, XMPNode xmpParent, Node xmlNode,
167 boolean isTopLevel, ParseOptions options) throws XMPException
168 {
169 int nodeTerm = getRDFTermKind (xmlNode);
170 if (nodeTerm != RDFTERM_DESCRIPTION && nodeTerm != RDFTERM_OTHER)
171 {
172 throw new XMPException("Node element must be rdf:Description or typed node",
173 BADRDF);
174 }
175 else if (isTopLevel && nodeTerm == RDFTERM_OTHER)
176 {
177 throw new XMPException("Top level typed node not allowed", BADXMP);
178 }
179 else
180 {
181 rdf_NodeElementAttrs (xmp, xmpParent, xmlNode, isTopLevel, options);
182 rdf_PropertyElementList (xmp, xmpParent, xmlNode, isTopLevel, options);
183 }
184
185 }
186
187
188 /**
189 *
190 * 7.2.7 propertyAttributeURIs
191 * anyURI - ( coreSyntaxTerms | rdf:Description | rdf:li | oldTerms )
192 *
193 * 7.2.11 nodeElement
194 * start-element ( URI == nodeElementURIs,
195 * attributes == set ( ( idAttr | nodeIdAttr | aboutAttr )?, propertyAttr* ) )
196 * propertyEltList
197 * end-element()
198 *
199 * Process the attribute list for an RDF node element. A property attribute URI is
200 * anything other than an RDF term. The rdf:ID and rdf:nodeID attributes are simply ignored,
201 * as are rdf:about attributes on inner nodes.
202 *
203 * @param xmp the xmp metadata object that is generated
204 * @param xmpParent the parent xmp node
205 * @param xmlNode the currently processed XML node
206 * @param isTopLevel Flag if the node is a top-level node
207 * @param options ParseOptions to indicate the parse options provided by the client
208 * @throws XMPException thown on parsing errors
209 */
210 private static void rdf_NodeElementAttrs(XMPMetaImpl xmp, XMPNode xmpParent, Node xmlNode,
211 boolean isTopLevel, ParseOptions options) throws XMPException
212 {
213 // Used to detect attributes that are mutually exclusive.
214 int exclusiveAttrs = 0;
215
216 for (int i = 0; i < xmlNode.getAttributes().getLength(); i++)
217 {
218 Node attribute = xmlNode.getAttributes().item(i);
219
220 // quick hack, ns declarations do not appear in C++
221 // ignore "ID" without namespace
222 if ("xmlns".equals(attribute.getPrefix()) ||
223 (attribute.getPrefix() == null && "xmlns".equals(attribute.getNodeName())))
224 {
225 continue;
226 }
227
228 int attrTerm = getRDFTermKind(attribute);
229
230 switch (attrTerm)
231 {
232 case RDFTERM_ID:
233 case RDFTERM_NODE_ID:
234 case RDFTERM_ABOUT:
235 if (exclusiveAttrs > 0)
236 {
237 throw new XMPException("Mutally exclusive about, ID, nodeID attributes",
238 BADRDF);
239 }
240
241 exclusiveAttrs++;
242
243 if (isTopLevel && (attrTerm == RDFTERM_ABOUT))
244 {
245 // This is the rdf:about attribute on a top level node. Set
246 // the XMP tree name if
247 // it doesn't have a name yet. Make sure this name matches
248 // the XMP tree name.
249 if (xmpParent.getName() != null && xmpParent.getName().length() > 0)
250 {
251 if (!xmpParent.getName().equals(attribute.getNodeValue()))
252 {
253 throw new XMPException("Mismatched top level rdf:about values",
254 BADXMP);
255 }
256 }
257 else
258 {
259 xmpParent.setName(attribute.getNodeValue());
260 }
261 }
262 break;
263
264 case RDFTERM_OTHER:
265 addChildNode(xmp, xmpParent, attribute, attribute.getNodeValue(), isTopLevel);
266 break;
267
268 default:
269 throw new XMPException("Invalid nodeElement attribute", BADRDF);
270 }
271
272 }
273 }
274
275
276 /**
277 * 7.2.13 propertyEltList
278 * ws* ( propertyElt ws* )*
279 *
280 * @param xmp the xmp metadata object that is generated
281 * @param xmpParent the parent xmp node
282 * @param xmlParent the currently processed XML node
283 * @param isTopLevel Flag if the node is a top-level node
284 * @param options ParseOptions to indicate the parse options provided by the client
285 * @throws XMPException thown on parsing errors
286 */
287 private static void rdf_PropertyElementList(XMPMetaImpl xmp, XMPNode xmpParent, Node xmlParent,
288 boolean isTopLevel, ParseOptions options) throws XMPException
289 {
290 for (int i = 0; i < xmlParent.getChildNodes().getLength(); i++)
291 {
292 Node currChild = xmlParent.getChildNodes().item(i);
293 if (isWhitespaceNode(currChild))
294 {
295 continue;
296 }
297 else if (currChild.getNodeType() != Node.ELEMENT_NODE)
298 {
299 throw new XMPException("Expected property element node not found", BADRDF);
300 }
301 else if ((xmpParent.getOptions().isArrayLimited() && (i > xmpParent.getOptions().getArrayElementsLimit())))
302 {
303 break;
304 }
305 else
306 {
307 rdf_PropertyElement(xmp, xmpParent, currChild, isTopLevel, options);
308 }
309 }
310 }
311
312
313 /**
314 * 7.2.14 propertyElt
315 *
316 * resourcePropertyElt | literalPropertyElt | parseTypeLiteralPropertyElt |
317 * parseTypeResourcePropertyElt | parseTypeCollectionPropertyElt |
318 * parseTypeOtherPropertyElt | emptyPropertyElt
319 *
320 * 7.2.15 resourcePropertyElt
321 * start-element ( URI == propertyElementURIs, attributes == set ( idAttr? ) )
322 * ws* nodeElement ws*
323 * end-element()
324 *
325 * 7.2.16 literalPropertyElt
326 * start-element (
327 * URI == propertyElementURIs, attributes == set ( idAttr?, datatypeAttr?) )
328 * text()
329 * end-element()
330 *
331 * 7.2.17 parseTypeLiteralPropertyElt
332 * start-element (
333 * URI == propertyElementURIs, attributes == set ( idAttr?, parseLiteral ) )
334 * literal
335 * end-element()
336 *
337 * 7.2.18 parseTypeResourcePropertyElt
338 * start-element (
339 * URI == propertyElementURIs, attributes == set ( idAttr?, parseResource ) )
340 * propertyEltList
341 * end-element()
342 *
343 * 7.2.19 parseTypeCollectionPropertyElt
344 * start-element (
345 * URI == propertyElementURIs, attributes == set ( idAttr?, parseCollection ) )
346 * nodeElementList
347 * end-element()
348 *
349 * 7.2.20 parseTypeOtherPropertyElt
350 * start-element ( URI == propertyElementURIs, attributes == set ( idAttr?, parseOther ) )
351 * propertyEltList
352 * end-element()
353 *
354 * 7.2.21 emptyPropertyElt
355 * start-element ( URI == propertyElementURIs,
356 * attributes == set ( idAttr?, ( resourceAttr | nodeIdAttr )?, propertyAttr* ) )
357 * end-element()
358 *
359 * The various property element forms are not distinguished by the XML element name,
360 * but by their attributes for the most part. The exceptions are resourcePropertyElt and
361 * literalPropertyElt. They are distinguished by their XML element content.
362 *
363 * NOTE: The RDF syntax does not explicitly include the xml:lang attribute although it can
364 * appear in many of these. We have to allow for it in the attibute counts below.
365 *
366 * @param xmp the xmp metadata object that is generated
367 * @param xmpParent the parent xmp node
368 * @param xmlNode the currently processed XML node
369 * @param isTopLevel Flag if the node is a top-level node
370 * @param options ParseOptions to indicate the parse options provided by the client
371 * @throws XMPException thown on parsing errors
372 */
373 private static void rdf_PropertyElement(XMPMetaImpl xmp, XMPNode xmpParent, Node xmlNode,
374 boolean isTopLevel, ParseOptions options) throws XMPException
375 {
376 int nodeTerm = getRDFTermKind (xmlNode);
377 if (!isPropertyElementName(nodeTerm))
378 {
379 throw new XMPException("Invalid property element name", BADRDF);
380 }
381
382 // remove the namespace-definitions from the list
383 NamedNodeMap attributes = xmlNode.getAttributes();
384 List nsAttrs = null;
385 for (int i = 0; i < attributes.getLength(); i++)
386 {
387 Node attribute = attributes.item(i);
388 if ("xmlns".equals(attribute.getPrefix()) ||
389 (attribute.getPrefix() == null && "xmlns".equals(attribute.getNodeName())))
390 {
391 if (nsAttrs == null)
392 {
393 nsAttrs = new ArrayList();
394 }
395 nsAttrs.add(attribute.getNodeName());
396 }
397 }
398 if (nsAttrs != null)
399 {
400 for (Iterator it = nsAttrs.iterator(); it.hasNext();)
401 {
402 String ns = (String) it.next();
403 attributes.removeNamedItem(ns);
404 }
405 }
406
407
408 if (attributes.getLength() > 3)
409 {
410 // Only an emptyPropertyElt can have more than 3 attributes.
411 rdf_EmptyPropertyElement(xmp, xmpParent, xmlNode, isTopLevel);
412 }
413 else
414 {
415 // Look through the attributes for one that isn't rdf:ID or xml:lang,
416 // it will usually tell what we should be dealing with.
417 // The called routines must verify their specific syntax!
418
419 for (int i = 0; i < attributes.getLength(); i++)
420 {
421 Node attribute = attributes.item(i);
422 String attrLocal = attribute.getLocalName();
423 String attrNS = attribute.getNamespaceURI();
424 String attrValue = attribute.getNodeValue();
425 if (!(XML_LANG.equals(attribute.getNodeName()) &&
426 !("ID".equals(attrLocal) && NS_RDF.equals(attrNS))))
427 {
428 if ("datatype".equals(attrLocal) && NS_RDF.equals(attrNS))
429 {
430 rdf_LiteralPropertyElement (xmp, xmpParent, xmlNode, isTopLevel);
431 }
432 else if (!("parseType".equals(attrLocal) && NS_RDF.equals(attrNS)))
433 {
434 rdf_EmptyPropertyElement (xmp, xmpParent, xmlNode, isTopLevel);
435 }
436 else if ("Literal".equals(attrValue))
437 {
438 rdf_ParseTypeLiteralPropertyElement();
439 }
440 else if ("Resource".equals(attrValue))
441 {
442 rdf_ParseTypeResourcePropertyElement(xmp, xmpParent, xmlNode, isTopLevel, options);
443 }
444 else if ("Collection".equals(attrValue))
445 {
446 rdf_ParseTypeCollectionPropertyElement();
447 }
448 else
449 {
450 rdf_ParseTypeOtherPropertyElement();
451 }
452
453 return;
454 }
455 }
456
457 // Only rdf:ID and xml:lang, could be a resourcePropertyElt, a literalPropertyElt,
458 // or an emptyPropertyElt. Look at the child XML nodes to decide which.
459
460 if (xmlNode.hasChildNodes())
461 {
462 for (int i = 0; i < xmlNode.getChildNodes().getLength(); i++)
463 {
464 Node currChild = xmlNode.getChildNodes().item(i);
465 if (currChild.getNodeType() != Node.TEXT_NODE)
466 {
467 rdf_ResourcePropertyElement (xmp, xmpParent, xmlNode, isTopLevel, options);
468 return;
469 }
470 }
471
472 rdf_LiteralPropertyElement (xmp, xmpParent, xmlNode, isTopLevel);
473 }
474 else
475 {
476 rdf_EmptyPropertyElement (xmp, xmpParent, xmlNode, isTopLevel);
477 }
478 }
479 }
480
481
482 /**
483 * 7.2.15 resourcePropertyElt
484 * start-element ( URI == propertyElementURIs, attributes == set ( idAttr? ) )
485 * ws* nodeElement ws*
486 * end-element()
487 *
488 * This handles structs using an rdf:Description node,
489 * arrays using rdf:Bag/Seq/Alt, and typedNodes. It also catches and cleans up qualified
490 * properties written with rdf:Description and rdf:value.
491 *
492 * @param xmp the xmp metadata object that is generated
493 * @param xmpParent the parent xmp node
494 * @param xmlNode the currently processed XML node
495 * @param isTopLevel Flag if the node is a top-level node
496 * @param options ParseOptions to indicate the parse options provided by the client
497 * @throws XMPException thown on parsing errors
498 */
499 private static void rdf_ResourcePropertyElement(XMPMetaImpl xmp, XMPNode xmpParent,
500 Node xmlNode, boolean isTopLevel, ParseOptions options) throws XMPException
501 {
502 if (isTopLevel && "iX:changes".equals(xmlNode.getNodeName()))
503 {
504 // Strip old "punchcard" chaff which has on the prefix "iX:".
505 return;
506 }
507
508 XMPNode newCompound = addChildNode(xmp, xmpParent, xmlNode, "", isTopLevel);
509
510 // walk through the attributes
511 for (int i = 0; i < xmlNode.getAttributes().getLength(); i++)
512 {
513 Node attribute = xmlNode.getAttributes().item(i);
514 if ("xmlns".equals(attribute.getPrefix()) ||
515 (attribute.getPrefix() == null && "xmlns".equals(attribute.getNodeName())))
516 {
517 continue;
518 }
519
520 String attrLocal = attribute.getLocalName();
521 String attrNS = attribute.getNamespaceURI();
522 if (XML_LANG.equals(attribute.getNodeName()))
523 {
524 addQualifierNode (newCompound, XML_LANG, attribute.getNodeValue());
525 }
526 else if ("ID".equals(attrLocal) && NS_RDF.equals(attrNS))
527 {
528 continue; // Ignore all rdf:ID attributes.
529 }
530 else
531 {
532 throw new XMPException(
533 "Invalid attribute for resource property element", BADRDF);
534 }
535 }
536
537 // walk through the children
538
539 Node currChild = null;
540 boolean found = false;
541 int i;
542 for (i = 0; i < xmlNode.getChildNodes().getLength(); i++)
543 {
544 currChild = xmlNode.getChildNodes().item(i);
545 if (!isWhitespaceNode(currChild))
546 {
547 if (currChild.getNodeType() == Node.ELEMENT_NODE && !found)
548 {
549 boolean isRDF = NS_RDF.equals(currChild.getNamespaceURI());
550 String childLocal = currChild.getLocalName();
551
552 if (isRDF && "Bag".equals(childLocal))
553 {
554 newCompound.getOptions().setArray(true);
555 }
556 else if (isRDF && "Seq".equals(childLocal))
557 {
558 newCompound.getOptions().setArray(true).setArrayOrdered(true);
559 }
560 else if (isRDF && "Alt".equals(childLocal))
561 {
562 newCompound.getOptions().setArray(true).setArrayOrdered(true)
563 .setArrayAlternate(true);
564 }
565 else
566 {
567 newCompound.getOptions().setStruct(true);
568 if (!isRDF && !"Description".equals(childLocal))
569 {
570 String typeName = currChild.getNamespaceURI();
571 if (typeName == null)
572 {
573 throw new XMPException(
574 "All XML elements must be in a namespace", BADXMP);
575 }
576 typeName += ':' + childLocal;
577 addQualifierNode (newCompound, "rdf:type", typeName);
578 }
579 }
580
581 Integer listLimit;
582 //listLimit will hold the limit value for the array is the array name is found in the mXMPNodesToLimit map, otherwise will be null
583 if(newCompound.getOptions().isArray() && ((listLimit = options.getXMPNodesToLimit().get(newCompound.getName())) != null))
584 {
585 newCompound.getOptions().setArrayElementLimit(listLimit);
586 }
587 rdf_NodeElement (xmp, newCompound, currChild, false, options);
588
589 if (newCompound.getHasValueChild())
590 {
591 fixupQualifiedNode (newCompound);
592 }
593 else if (newCompound.getOptions().isArrayAlternate())
594 {
595 XMPNodeUtils.detectAltText(newCompound);
596 }
597
598 found = true;
599 }
600 else if (found)
601 {
602 // found second child element
603 throw new XMPException(
604 "Invalid child of resource property element", BADRDF);
605 }
606 else
607 {
608 throw new XMPException(
609 "Children of resource property element must be XML elements", BADRDF);
610 }
611 }
612 }
613
614 if (!found)
615 {
616 // didn't found any child elements
617 throw new XMPException("Missing child of resource property element", BADRDF);
618 }
619 }
620
621
622 /**
623 * 7.2.16 literalPropertyElt
624 * start-element ( URI == propertyElementURIs,
625 * attributes == set ( idAttr?, datatypeAttr?) )
626 * text()
627 * end-element()
628 *
629 * Add a leaf node with the text value and qualifiers for the attributes.
630 * @param xmp the xmp metadata object that is generated
631 * @param xmpParent the parent xmp node
632 * @param xmlNode the currently processed XML node
633 * @param isTopLevel Flag if the node is a top-level node
634 * @throws XMPException thown on parsing errors
635 */
636 private static void rdf_LiteralPropertyElement(XMPMetaImpl xmp, XMPNode xmpParent,
637 Node xmlNode, boolean isTopLevel) throws XMPException
638 {
639 XMPNode newChild = addChildNode (xmp, xmpParent, xmlNode, null, isTopLevel);
640
641 for (int i = 0; i < xmlNode.getAttributes().getLength(); i++)
642 {
643 Node attribute = xmlNode.getAttributes().item(i);
644 if ("xmlns".equals(attribute.getPrefix()) ||
645 (attribute.getPrefix() == null && "xmlns".equals(attribute.getNodeName())))
646 {
647 continue;
648 }
649
650 String attrNS = attribute.getNamespaceURI();
651 String attrLocal = attribute.getLocalName();
652 if (XML_LANG.equals(attribute.getNodeName()))
653 {
654 addQualifierNode(newChild, XML_LANG, attribute.getNodeValue());
655 }
656 else if (NS_RDF.equals(attrNS) &&
657 ("ID".equals(attrLocal) || "datatype".equals(attrLocal)))
658 {
659 continue; // Ignore all rdf:ID and rdf:datatype attributes.
660 }
661 else
662 {
663 throw new XMPException(
664 "Invalid attribute for literal property element", BADRDF);
665 }
666 }
667
668 // FfF: Can there be more than one text node in a row?
669 String textValue = "";
670 for (int i = 0; i < xmlNode.getChildNodes().getLength(); i++)
671 {
672 Node child = xmlNode.getChildNodes().item(i);
673 if (child.getNodeType() == Node.TEXT_NODE)
674 {
675 textValue += child.getNodeValue();
676 }
677 else
678 {
679 throw new XMPException("Invalid child of literal property element", BADRDF);
680 }
681 }
682 newChild.setValue(textValue);
683 }
684
685
686 /**
687 * 7.2.17 parseTypeLiteralPropertyElt
688 * start-element ( URI == propertyElementURIs,
689 * attributes == set ( idAttr?, parseLiteral ) )
690 * literal
691 * end-element()
692 *
693 * @throws XMPException thown on parsing errors
694 */
695 private static void rdf_ParseTypeLiteralPropertyElement() throws XMPException
696 {
697 throw new XMPException("ParseTypeLiteral property element not allowed", BADXMP);
698 }
699
700
701 /**
702 * 7.2.18 parseTypeResourcePropertyElt
703 * start-element ( URI == propertyElementURIs,
704 * attributes == set ( idAttr?, parseResource ) )
705 * propertyEltList
706 * end-element()
707 *
708 * Add a new struct node with a qualifier for the possible rdf:ID attribute.
709 * Then process the XML child nodes to get the struct fields.
710 *
711 * @param xmp the xmp metadata object that is generated
712 * @param xmpParent the parent xmp node
713 * @param xmlNode the currently processed XML node
714 * @param isTopLevel Flag if the node is a top-level node
715 * @param options ParseOptions to indicate the parse options provided by the client
716 * @throws XMPException thown on parsing errors
717 */
718 private static void rdf_ParseTypeResourcePropertyElement(XMPMetaImpl xmp, XMPNode xmpParent,
719 Node xmlNode, boolean isTopLevel, ParseOptions options) throws XMPException
720 {
721 XMPNode newStruct = addChildNode (xmp, xmpParent, xmlNode, "", isTopLevel);
722
723 newStruct.getOptions().setStruct(true);
724
725 for (int i = 0; i < xmlNode.getAttributes().getLength(); i++)
726 {
727 Node attribute = xmlNode.getAttributes().item(i);
728 if ("xmlns".equals(attribute.getPrefix()) ||
729 (attribute.getPrefix() == null && "xmlns".equals(attribute.getNodeName())))
730 {
731 continue;
732 }
733
734 String attrLocal = attribute.getLocalName();
735 String attrNS = attribute.getNamespaceURI();
736 if (XML_LANG.equals(attribute.getNodeName()))
737 {
738 addQualifierNode (newStruct, XML_LANG, attribute.getNodeValue());
739 }
740 else if (NS_RDF.equals(attrNS) &&
741 ("ID".equals(attrLocal) || "parseType".equals(attrLocal)))
742 {
743 continue; // The caller ensured the value is "Resource".
744 // Ignore all rdf:ID attributes.
745 }
746 else
747 {
748 throw new XMPException("Invalid attribute for ParseTypeResource property element",
749 BADRDF);
750 }
751 }
752
753 rdf_PropertyElementList (xmp, newStruct, xmlNode, false, options);
754
755 if (newStruct.getHasValueChild())
756 {
757 fixupQualifiedNode (newStruct);
758 }
759
760 // *** Need to look for arrays using rdf:Description and rdf:type.
761 }
762
763
764 /**
765 * 7.2.19 parseTypeCollectionPropertyElt
766 * start-element ( URI == propertyElementURIs,
767 * attributes == set ( idAttr?, parseCollection ) )
768 * nodeElementList
769 * end-element()
770 *
771 * @throws XMPException thown on parsing errors
772 */
773 private static void rdf_ParseTypeCollectionPropertyElement() throws XMPException
774 {
775 throw new XMPException("ParseTypeCollection property element not allowed", BADXMP);
776 }
777
778
779 /**
780 * 7.2.20 parseTypeOtherPropertyElt
781 * start-element ( URI == propertyElementURIs, attributes == set ( idAttr?, parseOther ) )
782 * propertyEltList
783 * end-element()
784 *
785 * @throws XMPException thown on parsing errors
786 */
787 private static void rdf_ParseTypeOtherPropertyElement() throws XMPException
788 {
789 throw new XMPException("ParseTypeOther property element not allowed", BADXMP);
790 }
791
792
793 /**
794 * 7.2.21 emptyPropertyElt
795 * start-element ( URI == propertyElementURIs,
796 * attributes == set (
797 * idAttr?, ( resourceAttr | nodeIdAttr )?, propertyAttr* ) )
798 * end-element()
799 *
800 * <ns:Prop1/> <!-- a simple property with an empty value -->
801 * <ns:Prop2 rdf:resource="http: *www.adobe.com/"/> <!-- a URI value -->
802 * <ns:Prop3 rdf:value="..." ns:Qual="..."/> <!-- a simple qualified property -->
803 * <ns:Prop4 ns:Field1="..." ns:Field2="..."/> <!-- a struct with simple fields -->
804 *
805 * An emptyPropertyElt is an element with no contained content, just a possibly empty set of
806 * attributes. An emptyPropertyElt can represent three special cases of simple XMP properties: a
807 * simple property with an empty value (ns:Prop1), a simple property whose value is a URI
808 * (ns:Prop2), or a simple property with simple qualifiers (ns:Prop3).
809 * An emptyPropertyElt can also represent an XMP struct whose fields are all simple and
810 * unqualified (ns:Prop4).
811 *
812 * It is an error to use both rdf:value and rdf:resource - that can lead to invalid RDF in the
813 * verbose form written using a literalPropertyElt.
814 *
815 * The XMP mapping for an emptyPropertyElt is a bit different from generic RDF, partly for
816 * design reasons and partly for historical reasons. The XMP mapping rules are:
817 * <ol>
818 * <li> If there is an rdf:value attribute then this is a simple property
819 * with a text value.
820 * All other attributes are qualifiers.
821 * <li> If there is an rdf:resource attribute then this is a simple property
822 * with a URI value.
823 * All other attributes are qualifiers.
824 * <li> If there are no attributes other than xml:lang, rdf:ID, or rdf:nodeID
825 * then this is a simple
826 * property with an empty value.
827 * <li> Otherwise this is a struct, the attributes other than xml:lang, rdf:ID,
828 * or rdf:nodeID are fields.
829 * </ol>
830 *
831 * @param xmp the xmp metadata object that is generated
832 * @param xmpParent the parent xmp node
833 * @param xmlNode the currently processed XML node
834 * @param isTopLevel Flag if the node is a top-level node
835 * @throws XMPException thown on parsing errors
836 */
837 private static void rdf_EmptyPropertyElement(XMPMetaImpl xmp, XMPNode xmpParent, Node xmlNode,
838 boolean isTopLevel) throws XMPException
839 {
840 boolean hasPropertyAttrs = false;
841 boolean hasResourceAttr = false;
842 boolean hasNodeIDAttr = false;
843 boolean hasValueAttr = false;
844
845 Node valueNode = null; // ! Can come from rdf:value or rdf:resource.
846
847 if (xmlNode.hasChildNodes())
848 {
849 throw new XMPException(
850 "Nested content not allowed with rdf:resource or property attributes",
851 BADRDF);
852 }
853
854 // First figure out what XMP this maps to and remember the XML node for a simple value.
855 for (int i = 0; i < xmlNode.getAttributes().getLength(); i++)
856 {
857 Node attribute = xmlNode.getAttributes().item(i);
858 if ("xmlns".equals(attribute.getPrefix()) ||
859 (attribute.getPrefix() == null && "xmlns".equals(attribute.getNodeName())))
860 {
861 continue;
862 }
863
864 int attrTerm = getRDFTermKind (attribute);
865
866 switch (attrTerm)
867 {
868 case RDFTERM_ID :
869 // Nothing to do.
870 break;
871
872 case RDFTERM_RESOURCE :
873 if (hasNodeIDAttr)
874 {
875 throw new XMPException(
876 "Empty property element can't have both rdf:resource and rdf:nodeID",
877 BADRDF);
878 }
879 else if (hasValueAttr)
880 {
881 throw new XMPException(
882 "Empty property element can't have both rdf:value and rdf:resource",
883 BADXMP);
884 }
885
886 hasResourceAttr = true;
887 if (!hasValueAttr)
888 {
889 valueNode = attribute;
890 }
891 break;
892
893 case RDFTERM_NODE_ID:
894 if (hasResourceAttr)
895 {
896 throw new XMPException(
897 "Empty property element can't have both rdf:resource and rdf:nodeID",
898 BADRDF);
899 }
900 hasNodeIDAttr = true;
901 break;
902
903 case RDFTERM_OTHER:
904 if ("value".equals(attribute.getLocalName())
905 && NS_RDF.equals(attribute.getNamespaceURI()))
906 {
907 if (hasResourceAttr)
908 {
909 throw new XMPException(
910 "Empty property element can't have both rdf:value and rdf:resource",
911 BADXMP);
912 }
913 hasValueAttr = true;
914 valueNode = attribute;
915 }
916 else if (!XML_LANG.equals(attribute.getNodeName()))
917 {
918 hasPropertyAttrs = true;
919 }
920 break;
921
922 default:
923 throw new XMPException("Unrecognized attribute of empty property element",
924 BADRDF);
925 }
926 }
927
928 // Create the right kind of child node and visit the attributes again
929 // to add the fields or qualifiers.
930 // ! Because of implementation vagaries,
931 // the xmpParent is the tree root for top level properties.
932 // ! The schema is found, created if necessary, by addChildNode.
933
934 XMPNode childNode = addChildNode(xmp, xmpParent, xmlNode, "", isTopLevel);
935 boolean childIsStruct = false;
936
937 if (hasValueAttr || hasResourceAttr)
938 {
939 childNode.setValue(valueNode != null ? valueNode.getNodeValue() : "");
940 if (!hasValueAttr)
941 {
942 // ! Might have both rdf:value and rdf:resource.
943 childNode.getOptions().setURI(true);
944 }
945 }
946 else if (hasPropertyAttrs)
947 {
948 childNode.getOptions().setStruct(true);
949 childIsStruct = true;
950 }
951
952 for (int i = 0; i < xmlNode.getAttributes().getLength(); i++)
953 {
954 Node attribute = xmlNode.getAttributes().item(i);
955 if (attribute == valueNode ||
956 "xmlns".equals(attribute.getPrefix()) ||
957 (attribute.getPrefix() == null && "xmlns".equals(attribute.getNodeName())))
958 {
959 continue; // Skip the rdf:value or rdf:resource attribute holding the value.
960 }
961
962 int attrTerm = getRDFTermKind (attribute);
963
964 switch (attrTerm)
965 {
966 case RDFTERM_ID :
967 case RDFTERM_NODE_ID :
968 break; // Ignore all rdf:ID and rdf:nodeID attributes.
969
970 case RDFTERM_RESOURCE :
971 addQualifierNode(childNode, "rdf:resource", attribute.getNodeValue());
972 break;
973
974 case RDFTERM_OTHER :
975 if (!childIsStruct)
976 {
977 addQualifierNode(
978 childNode, attribute.getNodeName(), attribute.getNodeValue());
979 }
980 else if (XML_LANG.equals(attribute.getNodeName()))
981 {
982 addQualifierNode (childNode, XML_LANG, attribute.getNodeValue());
983 }
984 else
985 {
986 addChildNode (xmp, childNode, attribute, attribute.getNodeValue(), false);
987 }
988 break;
989
990 default :
991 throw new XMPException("Unrecognized attribute of empty property element",
992 BADRDF);
993 }
994
995 }
996 }
997
998
999 /**
1000 * Adds a child node.
1001 *
1002 * @param xmp the xmp metadata object that is generated
1003 * @param xmpParent the parent xmp node
1004 * @param xmlNode the currently processed XML node
1005 * @param value Node value
1006 * @param isTopLevel Flag if the node is a top-level node
1007 * @return Returns the newly created child node.
1008 * @throws XMPException thown on parsing errors
1009 */
1010 private static XMPNode addChildNode(XMPMetaImpl xmp, XMPNode xmpParent, Node xmlNode,
1011 String value, boolean isTopLevel) throws XMPException
1012 {
1013 XMPSchemaRegistry registry = XMPMetaFactory.getSchemaRegistry();
1014 String namespace = xmlNode.getNamespaceURI();
1015 String childName;
1016 if (namespace != null)
1017 {
1018 if (NS_DC_DEPRECATED.equals(namespace))
1019 {
1020 // Fix a legacy DC namespace
1021 // FfF: remove it after CS3
1022 namespace = NS_DC;
1023 }
1024
1025 String prefix = registry.getNamespacePrefix(namespace);
1026 if (prefix == null)
1027 {
1028 prefix = xmlNode.getPrefix() != null ? xmlNode.getPrefix() : DEFAULT_PREFIX;
1029 prefix = registry.registerNamespace(namespace, prefix);
1030 }
1031 childName = prefix + xmlNode.getLocalName();
1032 }
1033 else
1034 {
1035 throw new XMPException(
1036 "XML namespace required for all elements and attributes", BADRDF);
1037 }
1038
1039
1040 // create schema node if not already there
1041 PropertyOptions childOptions = new PropertyOptions();
1042 boolean isAlias = false;
1043 if (isTopLevel)
1044 {
1045 // Lookup the schema node, adjust the XMP parent pointer.
1046 // Incoming parent must be the tree root.
1047 XMPNode schemaNode = XMPNodeUtils.findSchemaNode(xmp.getRoot(), namespace,
1048 DEFAULT_PREFIX, true);
1049 schemaNode.setImplicit(false); // Clear the implicit node bit.
1050
1051 // *** Should use "opt &= ~flag" (no conditional),
1052 // need runtime check for proper 32 bit code.
1053 xmpParent = schemaNode;
1054
1055 // If this is an alias set the alias flag in the node
1056 // and the hasAliases flag in the tree.
1057 if (registry.findAlias(childName) != null)
1058 {
1059 isAlias = true;
1060 xmp.getRoot().setHasAliases(true);
1061 schemaNode.setHasAliases(true);
1062 }
1063 }
1064
1065
1066 // Make sure that this is not a duplicate of a named node.
1067 boolean isArrayItem = isNumberedArrayItemName(childName);
1068
1069 boolean isValueNode = "rdf:value".equals(childName);
1070
1071 // Create XMP node and so some checks
1072 XMPNode newChild = new XMPNode(
1073 childName, value, childOptions);
1074 newChild.setAlias(isAlias);
1075
1076 // Add the new child to the XMP parent node, a value node first.
1077 if (!isValueNode)
1078 {
1079 xmpParent.addChild(newChild);
1080 }
1081 else
1082 {
1083 xmpParent.addChild(1, newChild);
1084 }
1085
1086
1087 if (isValueNode)
1088 {
1089 if (isTopLevel || !xmpParent.getOptions().isStruct())
1090 {
1091 throw new XMPException("Misplaced rdf:value element", BADRDF);
1092 }
1093 xmpParent.setHasValueChild(true);
1094 }
1095
1096 boolean isParentArray = xmpParent.getOptions().isArray();
1097
1098 if (isParentArray && isArrayItem)
1099 {
1100 newChild.setName(ARRAY_ITEM_NAME);
1101 }
1102 else if (!isParentArray && isArrayItem)
1103 {
1104 throw new XMPException("Misplaced rdf:li element", BADRDF);
1105 }
1106 else if (isParentArray && !isArrayItem)
1107 {
1108 throw new XMPException("Arrays cannot have arbitrary child names", BADRDF);
1109 }
1110
1111 return newChild;
1112 }
1113
1114
1115 /**
1116 * Adds a qualifier node.
1117 *
1118 * @param xmpParent the parent xmp node
1119 * @param name the name of the qualifier which has to be
1120 * QName including the <b>default prefix</b>
1121 * @param value the value of the qualifier
1122 * @return Returns the newly created child node.
1123 * @throws XMPException thown on parsing errors
1124 */
1125 private static XMPNode addQualifierNode(XMPNode xmpParent, String name, String value)
1126 throws XMPException
1127 {
1128 boolean isLang = XML_LANG.equals(name);
1129
1130 XMPNode newQual = null;
1131
1132 // normalize value of language qualifiers
1133 newQual = new XMPNode(name, isLang ? Utils.normalizeLangValue(value) : value, null);
1134 xmpParent.addQualifier(newQual);
1135
1136 return newQual;
1137 }
1138
1139
1140 /**
1141 * The parent is an RDF pseudo-struct containing an rdf:value field. Fix the
1142 * XMP data model. The rdf:value node must be the first child, the other
1143 * children are qualifiers. The form, value, and children of the rdf:value
1144 * node are the real ones. The rdf:value node's qualifiers must be added to
1145 * the others.
1146 *
1147 * @param xmpParent the parent xmp node
1148 * @throws XMPException thown on parsing errors
1149 */
1150 private static void fixupQualifiedNode(XMPNode xmpParent) throws XMPException
1151 {
1152 assert xmpParent.getOptions().isStruct() && xmpParent.hasChildren();
1153
1154 XMPNode valueNode = xmpParent.getChild(1);
1155 assert "rdf:value".equals(valueNode.getName());
1156
1157 // Move the qualifiers on the value node to the parent.
1158 // Make sure an xml:lang qualifier stays at the front.
1159 // Check for duplicate names between the value node's qualifiers and the parent's children.
1160 // The parent's children are about to become qualifiers. Check here, between the groups.
1161 // Intra-group duplicates are caught by XMPNode#addChild(...).
1162
1163 // FfF: integrate this into the loop below
1164 if (valueNode.getOptions().getHasLanguage())
1165 {
1166 if (xmpParent.getOptions().getHasLanguage())
1167 {
1168 throw new XMPException("Redundant xml:lang for rdf:value element",
1169 BADXMP);
1170 }
1171 XMPNode langQual = valueNode.getQualifier(1);
1172 valueNode.removeQualifier(langQual);
1173 xmpParent.addQualifier(langQual);
1174 }
1175
1176 // Start the remaining copy after the xml:lang qualifier.
1177 for (int i = 1; i <= valueNode.getQualifierLength(); i++)
1178 {
1179 XMPNode qualifier = valueNode.getQualifier(i);
1180 xmpParent.addQualifier(qualifier);
1181 }
1182
1183
1184 // Change the parent's other children into qualifiers.
1185 // This loop starts at 1, child 0 is the rdf:value node.
1186 for (int i = 2; i <= xmpParent.getChildrenLength(); i++)
1187 {
1188 XMPNode qualifier = xmpParent.getChild(i);
1189 xmpParent.addQualifier(qualifier);
1190 }
1191
1192 // Move the options and value last, other checks need the parent's original options.
1193 // Move the value node's children to be the parent's children.
1194 assert xmpParent.getOptions().isStruct() || xmpParent.getHasValueChild();
1195
1196 xmpParent.setHasValueChild(false);
1197 xmpParent.getOptions().setStruct(false);
1198 xmpParent.getOptions().mergeWith(valueNode.getOptions());
1199 xmpParent.setValue(valueNode.getValue());
1200
1201 xmpParent.removeChildren();
1202 for (Iterator it = valueNode.iterateChildren(); it.hasNext();)
1203 {
1204 XMPNode child = (XMPNode) it.next();
1205 xmpParent.addChild(child);
1206 }
1207 }
1208
1209
1210 /**
1211 * Checks if the node is a white space.
1212 * @param node an XML-node
1213 * @return Returns whether the node is a whitespace node,
1214 * i.e. a text node that contains only whitespaces.
1215 */
1216 private static boolean isWhitespaceNode(Node node)
1217 {
1218 if (node.getNodeType() != Node.TEXT_NODE)
1219 {
1220 return false;
1221 }
1222
1223 String value = node.getNodeValue();
1224 for (int i = 0; i < value.length(); i++)
1225 {
1226 if (!Character.isWhitespace(value.charAt(i)))
1227 {
1228 return false;
1229 }
1230 }
1231
1232 return true;
1233 }
1234
1235
1236 /**
1237 * 7.2.6 propertyElementURIs
1238 * anyURI - ( coreSyntaxTerms | rdf:Description | oldTerms )
1239 *
1240 * @param term the term id
1241 * @return Return true if the term is a property element name.
1242 */
1243 private static boolean isPropertyElementName(int term)
1244 {
1245 if (term == RDFTERM_DESCRIPTION || isOldTerm(term))
1246 {
1247 return false;
1248 }
1249 else
1250 {
1251 return (!isCoreSyntaxTerm(term));
1252 }
1253 }
1254
1255
1256 /**
1257 * 7.2.4 oldTerms<br>
1258 * rdf:aboutEach | rdf:aboutEachPrefix | rdf:bagID
1259 *
1260 * @param term the term id
1261 * @return Returns true if the term is an old term.
1262 */
1263 private static boolean isOldTerm(int term)
1264 {
1265 return RDFTERM_FIRST_OLD <= term && term <= RDFTERM_LAST_OLD;
1266 }
1267
1268
1269 /**
1270 * 7.2.2 coreSyntaxTerms<br>
1271 * rdf:RDF | rdf:ID | rdf:about | rdf:parseType | rdf:resource | rdf:nodeID |
1272 * rdf:datatype
1273 *
1274 * @param term the term id
1275 * @return Return true if the term is a core syntax term
1276 */
1277 private static boolean isCoreSyntaxTerm(int term)
1278 {
1279 return RDFTERM_FIRST_CORE <= term && term <= RDFTERM_LAST_CORE;
1280 }
1281
1282
1283 /**
1284 * Determines the ID for a certain RDF Term.
1285 * Arranged to hopefully minimize the parse time for large XMP.
1286 *
1287 * @param node an XML node
1288 * @return Returns the term ID.
1289 */
1290 private static int getRDFTermKind(Node node)
1291 {
1292 String localName = node.getLocalName();
1293 String namespace = node.getNamespaceURI();
1294
1295 if (
1296 namespace == null &&
1297 ("about".equals(localName) || "ID".equals(localName)) &&
1298 (node instanceof Attr) &&
1299 NS_RDF.equals(((Attr) node).getOwnerElement().getNamespaceURI())
1300 )
1301 {
1302 namespace = NS_RDF;
1303 }
1304
1305 if (NS_RDF.equals(namespace))
1306 {
1307 if ("li".equals(localName))
1308 {
1309 return RDFTERM_LI;
1310 }
1311 else if ("parseType".equals(localName))
1312 {
1313 return RDFTERM_PARSE_TYPE;
1314 }
1315 else if ("Description".equals(localName))
1316 {
1317 return RDFTERM_DESCRIPTION;
1318 }
1319 else if ("about".equals(localName))
1320 {
1321 return RDFTERM_ABOUT;
1322 }
1323 else if ("resource".equals(localName))
1324 {
1325 return RDFTERM_RESOURCE;
1326 }
1327 else if ("RDF".equals(localName))
1328 {
1329 return RDFTERM_RDF;
1330 }
1331 else if ("ID".equals(localName))
1332 {
1333 return RDFTERM_ID;
1334 }
1335 else if ("nodeID".equals(localName))
1336 {
1337 return RDFTERM_NODE_ID;
1338 }
1339 else if ("datatype".equals(localName))
1340 {
1341 return RDFTERM_DATATYPE;
1342 }
1343 else if ("aboutEach".equals(localName))
1344 {
1345 return RDFTERM_ABOUT_EACH;
1346 }
1347 else if ("aboutEachPrefix".equals(localName))
1348 {
1349 return RDFTERM_ABOUT_EACH_PREFIX;
1350 }
1351 else if ("bagID".equals(localName))
1352 {
1353 return RDFTERM_BAG_ID;
1354 }
1355 }
1356
1357 return RDFTERM_OTHER;
1358 }
1359
1360
1361 /**
1362 * Check if the child name
1363 *
1364 * @param nodeName
1365 * @return Returns
1366 */
1367 private static boolean isNumberedArrayItemName(String nodeName)
1368 {
1369 boolean result = "rdf:li".equals(nodeName);
1370
1371 if (nodeName.startsWith("rdf:_"))
1372 {
1373 result = true;
1374 for (int i = 5; i < nodeName.length(); i++)
1375 {
1376 result = result && nodeName.charAt(i) >= '0' && nodeName.charAt(i) <= '9';
1377 }
1378 }
1379
1380 return result;
1381 }
1382 }