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 22:24:33 UTC

svn commit: r1368691 - in /pdfbox/trunk/xmpbox/src: main/java/org/apache/padaf/xmpbox/parser/ main/java/org/apache/padaf/xmpbox/type/ test/java/org/apache/padaf/xmpbox/parser/ test/resources/org/apache/padaf/xmpbox/parser/

Author: gbailleul
Date: Thu Aug  2 20:24:33 2012
New Revision: 1368691

URL: http://svn.apache.org/viewvc?rev=1368691&view=rev
Log:
PDFBOX-1376: added parsing of sub structured as parseType=Resource
added a junit test with structured recursive

Added:
    pdfbox/trunk/xmpbox/src/test/resources/org/apache/padaf/xmpbox/parser/structured_recursive.xml
Modified:
    pdfbox/trunk/xmpbox/src/main/java/org/apache/padaf/xmpbox/parser/StructuredPropertyParser.java
    pdfbox/trunk/xmpbox/src/main/java/org/apache/padaf/xmpbox/type/TypeMapping.java
    pdfbox/trunk/xmpbox/src/test/java/org/apache/padaf/xmpbox/parser/DeserializationTest.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=1368691&r1=1368690&r2=1368691&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 20:24:33 2012
@@ -106,8 +106,8 @@ public class StructuredPropertyParser {
 
 
 
-	private boolean isParseTypeResource () {
-		XMLStreamReader reader = builder.getReader();
+	private boolean isParseTypeResource (XMLStreamReader reader) {
+//		XMLStreamReader reader = builder.getReader();
 		int count = reader.getAttributeCount();
 		for (int i=0; i < count ; i++) {
 			if ("parseType".equals(reader.getAttributeLocalName(i))) {
@@ -121,17 +121,32 @@ public class StructuredPropertyParser {
 		return false;
 	}
 
+	
 	public void parse(XMPMetadata metadata, QName altName,
 			ComplexPropertyContainer container)
 					throws XmpUnexpectedTypeException, XmpParsingException,
 					XMLStreamException, XmpUnknownPropertyTypeException,
 					XmpPropertyFormatException {
 		builder.expectCurrentLocalName("li");
-		XMLStreamReader reader = builder.getReader();
 		// check if parseType is defined
-		boolean skipDescription = isParseTypeResource();
+		boolean skipDescription = isParseTypeResource(builder.getReader());
+		
+		builder.getReader().nextTag();
+		parseSimple(metadata, altName, container, skipDescription,"li");
+		if (!skipDescription) {
+			builder.getReader().nextTag();
+		}
+	}
 
-		int elmtType = reader.nextTag();
+	
+	public void parseSimple(XMPMetadata metadata, QName altName,
+			ComplexPropertyContainer container, boolean skipDescription, String limiter)
+					throws XmpUnexpectedTypeException, XmpParsingException,
+					XMLStreamException, XmpUnknownPropertyTypeException,
+					XmpPropertyFormatException {
+
+		XMLStreamReader reader = builder.getReader();
+		int elmtType = reader.getEventType();
 
 		if (!skipDescription) {
 			// rdf:Description 
@@ -141,10 +156,17 @@ public class StructuredPropertyParser {
 		AbstractStructuredType property = instanciateProperty(metadata);
 
 		QName eltName;
-		String structuredEndName = skipDescription?"li":"Description";
+		String structuredEndName = skipDescription?limiter:"Description";
 		while (!((elmtType == XMLStreamReader.END_ELEMENT) && reader.getName().getLocalPart().equals(structuredEndName))) {
 			// read element name, then text content
 			eltName = reader.getName();
+
+			boolean isSubSkipDescription = false;
+			String subExpected = null;
+			if (reader.getEventType()==XMLStreamConstants.START_ELEMENT) {
+				isSubSkipDescription = isParseTypeResource(reader);
+				subExpected = reader.getName().getLocalPart();
+			}
 			// prepare the text
 			elmtType = reader.next();
 			// TODO why not a space ??
@@ -170,7 +192,6 @@ public class StructuredPropertyParser {
 					elmtType = reader.next();
 				}
 
-
 				String eltContent = content.toString();
 				// check if property is expected
 				String localPart = eltName.getLocalPart();
@@ -195,10 +216,11 @@ public class StructuredPropertyParser {
 				}
 				elmtType = reader.nextTag();
 
-			} else {
+			} else {	
 				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());
@@ -206,9 +228,7 @@ public class StructuredPropertyParser {
 						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();
-						
+						sp.parseSimple(metadata, reader.getName(), property.getContainer(),isSubSkipDescription,subExpected);// TODO
 					} else if (TypeMapping.getArrayType(ptype)!=null) {
 						int pos = ptype.indexOf(' ');
 						String arrayType = TypeMapping.getArrayType(ptype);
@@ -217,7 +237,8 @@ public class StructuredPropertyParser {
 						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);
+						builder.expectCurrentLocalName(arrayType);
+						builder.expectNextSpecificTag(XMLStreamConstants.START_ELEMENT, "li", "Should be starting a 'li'");
 						// array elements
 						while (reader.getEventType()==XMLStreamConstants.START_ELEMENT && reader.getName().getLocalPart().equals("li")) {
 							StructuredPropertyParser sp = new StructuredPropertyParser(builder, tcn);
@@ -225,7 +246,8 @@ public class StructuredPropertyParser {
 							reader.nextTag();
 						}
 						// array element ending
-						builder.expectNextSpecificTag(XMLStreamConstants.END_ELEMENT, arrayType, "Should be ending a ",arrayType);
+						builder.expectCurrentLocalName(arrayType);
+						reader.nextTag();
 					}
 					
 					
@@ -239,7 +261,8 @@ public class StructuredPropertyParser {
 		}
 		if (!skipDescription) {
 			// closing rdf:Description element
-			reader.nextTag();
+			builder.expectCurrentLocalName("Description");
+//			reader.nextTag();
 		}
 		container.addProperty(property);
 

Modified: pdfbox/trunk/xmpbox/src/main/java/org/apache/padaf/xmpbox/type/TypeMapping.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/xmpbox/src/main/java/org/apache/padaf/xmpbox/type/TypeMapping.java?rev=1368691&r1=1368690&r2=1368691&view=diff
==============================================================================
--- pdfbox/trunk/xmpbox/src/main/java/org/apache/padaf/xmpbox/type/TypeMapping.java (original)
+++ pdfbox/trunk/xmpbox/src/main/java/org/apache/padaf/xmpbox/type/TypeMapping.java Thu Aug  2 20:24:33 2012
@@ -24,10 +24,15 @@ package org.apache.padaf.xmpbox.type;
 import java.lang.reflect.Constructor;
 import java.lang.reflect.Field;
 import java.lang.reflect.InvocationTargetException;
+import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 
 import org.apache.padaf.xmpbox.XMPMetadata;
+import org.apache.padaf.xmpbox.parser.PropMapping;
+import org.apache.padaf.xmpbox.schema.PropertyAttributesAnnotation;
+import org.apache.padaf.xmpbox.schema.XMPSchema;
 import org.apache.padaf.xmpbox.type.TypeDescription.BasicType;
 
 public final class TypeMapping {
@@ -101,6 +106,11 @@ public final class TypeMapping {
         addToStructuredMaps(new TypeDescription("Job",null,JobType.class));
         addToStructuredMaps(new TypeDescription("ResourceRef",null,ResourceRefType.class));
         addToStructuredMaps(new TypeDescription("Version",null,VersionType.class));
+        // PDF/A structured types
+//        addToStructuredMaps(new TypeDescription("PDFAField",null,PDFAFieldType.class));
+//        addToStructuredMaps(new TypeDescription("PDFAProperty",null,PDFAPropertyType.class));
+//        addToStructuredMaps(new TypeDescription("PDFAType",null,PDFATypeType.class));
+//        addToStructuredMaps(new TypeDescription("PDFASchema",null,PDFASchemaType.class));
     }
 
     private static void addToBasicMaps (TypeDescription td) {
@@ -116,8 +126,12 @@ public final class TypeMapping {
     private static void addToStructuredMaps (TypeDescription td) {
         STRUCTURED_TYPES.put(td.getType(),td);
         STRUCTURED_CLASSES.put(td.getTypeClass(), td);
+        
         try {
-			STRUCTURED_NAMESPACES.put((String)td.getTypeClass().getField("ELEMENT_NS").get(null), td);
+        	String ns = (String)td.getTypeClass().getField("ELEMENT_NS").get(null);
+			STRUCTURED_NAMESPACES.put(ns, td);
+	        PropMapping pm = initializePropMapping(ns, (Class<? extends AbstractStructuredType>)td.getTypeClass());
+	        td.setProperties(pm);
 		} catch (IllegalArgumentException e) {
 			throw new IllegalArgumentException("Failed to init structured maps for "+td.getTypeClass(), e);
 		} catch (SecurityException e) {
@@ -258,7 +272,9 @@ public final class TypeMapping {
      * @return True if namespace URI is a reference for a complex basic type
      */
     public static boolean isStructuredTypeNamespace(String namespace) {
-        return STRUCTURED_TYPES.containsKey(namespace);
+//        return STRUCTURED_TYPES.containsKey(namespace);
+    	// TODO why was STRUCTURED_TYPE
+    	return STRUCTURED_NAMESPACES.containsKey(namespace);
     }
 
 
@@ -302,5 +318,44 @@ public final class TypeMapping {
     	return STRUCTURED_TYPES.containsKey(type);
     }
 
+	private static PropMapping initializePropMapping(String ns,
+			Class<? extends AbstractStructuredType> classSchem) {
+		PropertyType propType;
+		PropertyAttributesAnnotation propAtt;
+		Field[] fields;
+		PropMapping propMap = new PropMapping(ns);
+		fields = classSchem.getFields();
+		String propName = null;
+		for (Field field : fields) {
+			if (field.isAnnotationPresent(PropertyType.class)) {
+				try {
+					propName = (String) field.get(propName);
+				} catch (Exception e) {
+					throw new IllegalArgumentException(
+							"couldn't read one type declaration, please check accessibility and declaration of fields annoted in "
+									+ classSchem.getName(), e);
+				}
+				propType = field.getAnnotation(PropertyType.class);
+				if (!field.isAnnotationPresent(PropertyAttributesAnnotation.class)) {
+					propMap.addNewProperty(propName, propType.propertyType(),
+							null);
+				} else {
+					// XXX Case where a special annotation is used to specify
+					// attributes
+					// NOT IMPLEMENTED YET, JUST TO GIVE A CLUE TO MAKE THIS
+					propAtt = field
+							.getAnnotation(PropertyAttributesAnnotation.class);
+					List<String> attributes = new ArrayList<String>();
+					for (String att : propAtt.expectedAttributes()) {
+						attributes.add(att);
+					}
+					propMap.addNewProperty(propName, propType.propertyType(),
+							attributes);
+				}
+			}
+		}
+		return propMap;
+	}
+
     
 }

Modified: pdfbox/trunk/xmpbox/src/test/java/org/apache/padaf/xmpbox/parser/DeserializationTest.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/xmpbox/src/test/java/org/apache/padaf/xmpbox/parser/DeserializationTest.java?rev=1368691&r1=1368690&r2=1368691&view=diff
==============================================================================
--- pdfbox/trunk/xmpbox/src/test/java/org/apache/padaf/xmpbox/parser/DeserializationTest.java (original)
+++ pdfbox/trunk/xmpbox/src/test/java/org/apache/padaf/xmpbox/parser/DeserializationTest.java Thu Aug  2 20:24:33 2012
@@ -152,6 +152,18 @@ public class DeserializationTest {
 
 	}
 
+	
+	@Test
+	public void testStructuredRecursive () throws Exception {
+		InputStream fis = XMPDocumentBuilder.class
+				.getResourceAsStream("structured_recursive.xml");
+
+		XMPDocumentBuilder xdb = new XMPDocumentBuilder();
+
+		xdb.parse(fis);
+		
+	}
+	
 	@Test
 	public void testAltBagSeq() throws Exception {
 		InputStream fis = XMPDocumentBuilder.class

Added: pdfbox/trunk/xmpbox/src/test/resources/org/apache/padaf/xmpbox/parser/structured_recursive.xml
URL: http://svn.apache.org/viewvc/pdfbox/trunk/xmpbox/src/test/resources/org/apache/padaf/xmpbox/parser/structured_recursive.xml?rev=1368691&view=auto
==============================================================================
--- pdfbox/trunk/xmpbox/src/test/resources/org/apache/padaf/xmpbox/parser/structured_recursive.xml (added)
+++ pdfbox/trunk/xmpbox/src/test/resources/org/apache/padaf/xmpbox/parser/structured_recursive.xml Thu Aug  2 20:24:33 2012
@@ -0,0 +1,62 @@
+<!-- ! Licensed to the Apache Software Foundation (ASF) under one or more 
+	! contributor license agreements. See the NOTICE file distributed with ! 
+	this work for additional information regarding copyright ownership. ! The 
+	ASF licenses this file to You under the Apache License, Version 2.0 ! (the 
+	"License"); you may not use this file except in compliance with ! the License. 
+	You may obtain a copy of the License at ! ! http://www.apache.org/licenses/LICENSE-2.0 
+	! ! Unless required by applicable law or agreed to in writing, software ! 
+	distributed under the License is distributed on an "AS IS" BASIS, ! WITHOUT 
+	WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ! See the 
+	License for the specific language governing permissions and ! limitations 
+	under the License. ! -->
+<?xpacket begin='' id='W5M0MpCehiHzreSzNTczkc9d'?>
+<x:xmpmeta xmlns:x="adobe:ns:meta/">
+	<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
+		<rdf:Description rdf:about=""
+			xmlns:xmpMM="http://ns.adobe.com/xap/1.0/mm/">
+			<xmpMM:DocumentID>uuid:09C78666-2F91-3A9C-92AF-3691A6D594F7</xmpMM:DocumentID>
+			<xmpMM:Versions xmlns:stVer="http://ns.adobe.com/xap/1.0/sType/Version#" xmlns:stEvt="http://ns.adobe.com/xap/1.0/sType/ResourceEvent#">
+				<rdf:Seq>
+					<rdf:li>
+						<rdf:Description>
+							<stVer:comments>a text value</stVer:comments>
+							<stVer:modifier>g bailleul</stVer:modifier>
+							<stVer:event>
+								<rdf:Description>
+									<stEvt:action>copied</stEvt:action>
+									<stEvt:softwareAgent>a la mano</stEvt:softwareAgent>									
+								</rdf:Description>
+							</stVer:event>
+						</rdf:Description>
+					</rdf:li>
+					<rdf:li>
+						<rdf:Description>
+							<stVer:comments>a text value</stVer:comments>
+							<stVer:modifier>g bailleul</stVer:modifier>
+							<stVer:event rdf:parseType="Resource" >
+								<stEvt:action>copied</stEvt:action>
+								<stEvt:softwareAgent>a la mano</stEvt:softwareAgent>									
+							</stVer:event>
+						</rdf:Description>
+					</rdf:li>
+					<rdf:li rdf:parseType="Resource">
+							<stVer:comments>a text value</stVer:comments>
+							<stVer:modifier>g bailleul</stVer:modifier>
+					</rdf:li>
+				</rdf:Seq>
+			</xmpMM:Versions>
+		</rdf:Description>
+		<rdf:Description rdf:about=""
+			xmlns:pdfaid="http://www.aiim.org/pdfa/ns/id/">
+			<pdfaid:part>1</pdfaid:part>
+			<pdfaid:conformance>B</pdfaid:conformance>
+			<pdfaid:amd>1:2005</pdfaid:amd>
+		</rdf:Description>
+		<rdf:Description rdf:about=""
+			xmlns:pdf="http://ns.adobe.com/pdf/1.3/">
+			<pdf:Producer>PDFlib Personalization Server 7.0.2p5 (Win32)</pdf:Producer>
+		</rdf:Description>
+	</rdf:RDF>
+</x:xmpmeta>
+<?xpacket end='r'?>
+