You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by gb...@apache.org on 2012/08/02 19:19:52 UTC
svn commit: r1368607 - in
/pdfbox/trunk/xmpbox/src/main/java/org/apache/padaf/xmpbox:
parser/StructuredPropertyParser.java parser/XMPDocumentBuilder.java
type/AbstractStructuredType.java type/TypeDescription.java
Author: gbailleul
Date: Thu Aug 2 17:19:52 2012
New Revision: 1368607
URL: http://svn.apache.org/viewvc?rev=1368607&view=rev
Log:
PDFBOX-1376: structured types containing arrays and structured types are now parsed by xmpbox
Modified:
pdfbox/trunk/xmpbox/src/main/java/org/apache/padaf/xmpbox/parser/StructuredPropertyParser.java
pdfbox/trunk/xmpbox/src/main/java/org/apache/padaf/xmpbox/parser/XMPDocumentBuilder.java
pdfbox/trunk/xmpbox/src/main/java/org/apache/padaf/xmpbox/type/AbstractStructuredType.java
pdfbox/trunk/xmpbox/src/main/java/org/apache/padaf/xmpbox/type/TypeDescription.java
Modified: pdfbox/trunk/xmpbox/src/main/java/org/apache/padaf/xmpbox/parser/StructuredPropertyParser.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/xmpbox/src/main/java/org/apache/padaf/xmpbox/parser/StructuredPropertyParser.java?rev=1368607&r1=1368606&r2=1368607&view=diff
==============================================================================
--- pdfbox/trunk/xmpbox/src/main/java/org/apache/padaf/xmpbox/parser/StructuredPropertyParser.java (original)
+++ pdfbox/trunk/xmpbox/src/main/java/org/apache/padaf/xmpbox/parser/StructuredPropertyParser.java Thu Aug 2 17:19:52 2012
@@ -29,6 +29,7 @@ import java.util.HashMap;
import java.util.Map;
import javax.xml.namespace.QName;
+import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
@@ -141,33 +142,100 @@ public class StructuredPropertyParser {
QName eltName;
String structuredEndName = skipDescription?"li":"Description";
-
while (!((elmtType == XMLStreamReader.END_ELEMENT) && reader.getName().getLocalPart().equals(structuredEndName))) {
// read element name, then text content
eltName = reader.getName();
- String eltContent = reader.getElementText();
- // check if property is expected
- String localPart = eltName.getLocalPart();
- if (propDesc.containsKey(localPart)) {
- PropertyDescription description = propDesc.get(localPart);
-
- AbstractField a = instanciateSimple(
- description.propertyType.propertyType(),
- metadata,
- eltName.getPrefix(),
- localPart,
- eltContent);
+ // prepare the text
+ elmtType = reader.next();
+ // TODO why not a space ??
+ if (elmtType == XMLStreamConstants.CHARACTERS && reader.getText().trim().length()>0) {
+ // text content
+ StringBuilder content = new StringBuilder();
+ while(elmtType != XMLStreamConstants.END_ELEMENT ) {
+ if(elmtType == XMLStreamConstants.CHARACTERS
+ || elmtType == XMLStreamConstants.CDATA
+ || elmtType == XMLStreamConstants.SPACE
+ || elmtType == XMLStreamConstants.ENTITY_REFERENCE) {
+ content.append(reader.getText());
+ } else if(elmtType == XMLStreamConstants.PROCESSING_INSTRUCTION
+ || elmtType == XMLStreamConstants.COMMENT) {
+ // skipping
+ } else if(elmtType == XMLStreamConstants.START_ELEMENT) {
+ throw new XMLStreamException(
+ "element text content may not contain START_ELEMENT", reader.getLocation());
+ } else {
+ throw new XMLStreamException(
+ "Unexpected event type "+elmtType, reader.getLocation());
+ }
+ elmtType = reader.next();
+ }
+
+
+ String eltContent = content.toString();
+ // check if property is expected
+ String localPart = eltName.getLocalPart();
+ if (propDesc.containsKey(localPart)) {
+ PropertyDescription description = propDesc.get(localPart);
+
+ AbstractField a = instanciateSimple(
+ description.propertyType.propertyType(),
+ metadata,
+ eltName.getPrefix(),
+ localPart,
+ eltContent);
+
+ property.addProperty(a);
+ } else {
+ // expect only defined properties are accepted
+ // XXX : really the good choice ?
+ // XXX : should we create text properties for unknown types ?
+ throw new XmpParsingException(
+ "Unknown property name for a job element : "
+ + eltName.getLocalPart());
+ }
+ elmtType = reader.nextTag();
- property.addProperty(a);
} else {
- // expect only defined properties are accepted
- // XXX : really the good choice ?
- // XXX : should we create text properties for unknown types ?
- throw new XmpParsingException(
- "Unknown property name for a job element : "
- + eltName.getLocalPart());
+ if (reader.getEventType()!=XMLStreamConstants.START_ELEMENT && reader.getEventType()!=XMLStreamConstants.END_ELEMENT) {
+ reader.nextTag();
+ }
+ if (reader.getEventType()==XMLStreamConstants.START_ELEMENT) {
+ TypeDescription td = TypeMapping.getStructuredTypeName(eltName.getNamespaceURI());
+ String ptype = td.getProperties().getPropertyType(eltName.getLocalPart());
+ if (TypeMapping.isStructuredType(ptype)) {
+ TypeDescription tclass = TypeMapping.getTypeDescription(ptype);
+ Class<? extends AbstractStructuredType> tcn = (Class<? extends AbstractStructuredType>)tclass.getTypeClass();
+ StructuredPropertyParser sp = new StructuredPropertyParser(builder, tcn);
+ sp.parse(metadata, reader.getName(), property.getContainer());
+ reader.nextTag();
+
+ } else if (TypeMapping.getArrayType(ptype)!=null) {
+ int pos = ptype.indexOf(' ');
+ String arrayType = TypeMapping.getArrayType(ptype);
+ String typeInArray = ptype.substring(pos+1);
+
+ TypeDescription tclass = TypeMapping.getTypeDescription(typeInArray);
+ Class<? extends AbstractStructuredType> tcn = (Class<? extends AbstractStructuredType>)tclass.getTypeClass();
+ // array element starting
+ builder.expectNextSpecificTag(XMLStreamConstants.START_ELEMENT, arrayType, "Should be starting a ",arrayType);
+ // array elements
+ while (reader.getEventType()==XMLStreamConstants.START_ELEMENT && reader.getName().getLocalPart().equals("li")) {
+ StructuredPropertyParser sp = new StructuredPropertyParser(builder, tcn);
+ sp.parse(metadata, reader.getName(), property.getContainer());
+ reader.nextTag();
+ }
+ // array element ending
+ builder.expectNextSpecificTag(XMLStreamConstants.END_ELEMENT, arrayType, "Should be ending a ",arrayType);
+ }
+
+
+ elmtType = reader.nextTag();
+ } else {
+ // end element
+// reader.nextTag();
+ elmtType = reader.getEventType();
+ }
}
- elmtType = reader.nextTag();
}
if (!skipDescription) {
// closing rdf:Description element
@@ -225,8 +293,6 @@ public class StructuredPropertyParser {
protected class PropertyDescription {
- // private String fieldName;
- //
private String propertyName;
private PropertyType propertyType;
Modified: pdfbox/trunk/xmpbox/src/main/java/org/apache/padaf/xmpbox/parser/XMPDocumentBuilder.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/xmpbox/src/main/java/org/apache/padaf/xmpbox/parser/XMPDocumentBuilder.java?rev=1368607&r1=1368606&r2=1368607&view=diff
==============================================================================
--- pdfbox/trunk/xmpbox/src/main/java/org/apache/padaf/xmpbox/parser/XMPDocumentBuilder.java (original)
+++ pdfbox/trunk/xmpbox/src/main/java/org/apache/padaf/xmpbox/parser/XMPDocumentBuilder.java Thu Aug 2 17:19:52 2012
@@ -606,7 +606,7 @@ public class XMPDocumentBuilder {
* @throws XMLStreamException
* When error during reading the rest of xmp stream
*/
- private void expectNextSpecificTag(int type, String localNameExpected,
+ protected void expectNextSpecificTag(int type, String localNameExpected,
String ... message) throws XmpUnexpectedTypeException,
XmpParsingException, XMLStreamException {
expectNextTag(type, message);
Modified: pdfbox/trunk/xmpbox/src/main/java/org/apache/padaf/xmpbox/type/AbstractStructuredType.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/xmpbox/src/main/java/org/apache/padaf/xmpbox/type/AbstractStructuredType.java?rev=1368607&r1=1368606&r2=1368607&view=diff
==============================================================================
--- pdfbox/trunk/xmpbox/src/main/java/org/apache/padaf/xmpbox/type/AbstractStructuredType.java (original)
+++ pdfbox/trunk/xmpbox/src/main/java/org/apache/padaf/xmpbox/type/AbstractStructuredType.java Thu Aug 2 17:19:52 2012
@@ -103,4 +103,8 @@ public abstract class AbstractStructured
}
}
+ public ComplexPropertyContainer getContainer() {
+ return container;
+ }
+
}
Modified: pdfbox/trunk/xmpbox/src/main/java/org/apache/padaf/xmpbox/type/TypeDescription.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/xmpbox/src/main/java/org/apache/padaf/xmpbox/type/TypeDescription.java?rev=1368607&r1=1368606&r2=1368607&view=diff
==============================================================================
--- pdfbox/trunk/xmpbox/src/main/java/org/apache/padaf/xmpbox/type/TypeDescription.java (original)
+++ pdfbox/trunk/xmpbox/src/main/java/org/apache/padaf/xmpbox/type/TypeDescription.java Thu Aug 2 17:19:52 2012
@@ -21,6 +21,8 @@
package org.apache.padaf.xmpbox.type;
+import org.apache.padaf.xmpbox.parser.PropMapping;
+
public class TypeDescription {
@@ -31,6 +33,9 @@ public class TypeDescription {
private BasicType basic;
private Class<? extends AbstractField> clz;
+
+ // TODO PropMapping should be in package Type
+ private PropMapping properties = null;
public TypeDescription(String type, BasicType basic,Class<? extends AbstractField> clz) {
super();
@@ -61,8 +66,12 @@ public class TypeDescription {
return basic;
}
-
-
-
+ public PropMapping getProperties() {
+ return properties;
+ }
+
+ protected void setProperties(PropMapping properties) {
+ this.properties = properties;
+ }
}