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/07/29 22:55:55 UTC

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

Author: gbailleul
Date: Sun Jul 29 20:55:54 2012
New Revision: 1366951

URL: http://svn.apache.org/viewvc?rev=1366951&view=rev
Log:
PDFBOX-1368: structured type can now be read as rdf:Description or parseType=Resource

Modified:
    pdfbox/trunk/xmpbox/src/main/java/org/apache/padaf/xmpbox/parser/StructuredPropertyParser.java
    pdfbox/trunk/xmpbox/src/test/java/org/apache/padaf/xmpbox/parser/DeserializationTest.java
    pdfbox/trunk/xmpbox/src/test/resources/org/apache/padaf/xmpbox/parser/ThumbisartorStyle.xml

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=1366951&r1=1366950&r2=1366951&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 Sun Jul 29 20:55:54 2012
@@ -44,14 +44,14 @@ import org.apache.padaf.xmpbox.type.Type
 public class StructuredPropertyParser {
 
 	private XMPDocumentBuilder builder = null;
-	
+
 	private Class<? extends AbstractStructuredType> typeClass = null;
 
 	private Constructor<? extends AbstractStructuredType> typeConstructor = null;
 
 	private Map<String,PropertyDescription> propDesc = null;
 
-//	private static Class<?> [] propertyConstructorParams = new Class [] {XMPMetadata.class,String.class};
+	//	private static Class<?> [] propertyConstructorParams = new Class [] {XMPMetadata.class,String.class};
 	private static Class<?> [] propertyConstructorParams = new Class [] {XMPMetadata.class};
 
 	public StructuredPropertyParser(XMPDocumentBuilder builder,Class<? extends AbstractStructuredType> propertyTypeClass) 
@@ -66,7 +66,7 @@ public class StructuredPropertyParser {
 			if (field.getAnnotation(PropertyType.class)!=null) {
 				PropertyDescription pd = new PropertyDescription();
 				pd.propertyType = field.getAnnotation(PropertyType.class);
-//				pd.fieldName = field.getName();
+				//				pd.fieldName = field.getName();
 				try {
 					pd.propertyName = field.get(null).toString();
 				} catch (IllegalArgumentException e1) {
@@ -90,7 +90,7 @@ public class StructuredPropertyParser {
 
 	private AbstractStructuredType instanciateProperty (XMPMetadata metadata) throws XmpParsingException {
 		try {
-//			return typeConstructor.newInstance(metadata,prefix);
+			//			return typeConstructor.newInstance(metadata,prefix);
 			return typeConstructor.newInstance(metadata);
 		} catch (IllegalArgumentException e) {
 			throw new XmpParsingException("Failed to instanciate structured type : "+typeClass.getName(),e);
@@ -103,49 +103,46 @@ public class StructuredPropertyParser {
 		}
 	}
 
-	
-//	private String retrieveNamespacePrefix (XMLStreamReader reader, String namespace) {
-//		int na = reader.getNamespaceCount();
-//		for (int i=0; i < na; i++) {
-//			if (reader.getNamespaceURI(i).equals(namespace)) {
-//				return reader.getNamespacePrefix(i);
-//			}
-//		}
-//		// no namespace for prefix
-//		return null;
-//	}
-//	
-//	private String getStructuredClassNamespace (Class<? extends AbstractStructuredType> clz) throws XmpUnexpectedTypeException {
-//		try {
-//			return (String)typeClass.getField("ELEMENT_NS").get(null);
-//		} catch (IllegalArgumentException e) {
-//			throw new XmpUnexpectedTypeException("Failed to find Structured type namespace ("+clz.getName()+")",e);
-//		} catch (SecurityException e) {
-//			throw new XmpUnexpectedTypeException("Failed to find Structured type namespace ("+clz.getName()+")",e);
-//		} catch (IllegalAccessException e) {
-//			throw new XmpUnexpectedTypeException("Failed to find Structured type namespace ("+clz.getName()+")",e);
-//		} catch (NoSuchFieldException e) {
-//			throw new XmpUnexpectedTypeException("Failed to find Structured type namespace ("+clz.getName()+")",e);
-//		}
-//		
-//	}
-	
-	
+
+
+	private boolean isParseTypeResource () {
+		XMLStreamReader reader = builder.getReader();
+		int count = reader.getAttributeCount();
+		for (int i=0; i < count ; i++) {
+			if ("parseType".equals(reader.getAttributeLocalName(i))) {
+				if ("Resource".equals(reader.getAttributeValue(i))) {
+					return true;
+				} else {
+					return false;
+				}
+			}
+		}
+		return false;
+	}
+
 	public void parse(XMPMetadata metadata, QName altName,
 			ComplexPropertyContainer container)
 					throws XmpUnexpectedTypeException, XmpParsingException,
 					XMLStreamException, XmpUnknownPropertyTypeException,
 					XmpPropertyFormatException {
 		builder.expectCurrentLocalName("li");
-		AbstractStructuredType property = instanciateProperty(metadata);
 		XMLStreamReader reader = builder.getReader();
+		// check if parseType is defined
+		boolean skipDescription = isParseTypeResource();
+
 		int elmtType = reader.nextTag();
-		// rdf:Description is mandatory
-		builder.expectCurrentLocalName("Description");
-		elmtType = reader.nextTag();
-		
+
+		if (!skipDescription) {
+			// rdf:Description 
+			builder.expectCurrentLocalName("Description");
+			elmtType = reader.nextTag();
+		}
+		AbstractStructuredType property = instanciateProperty(metadata);
+
 		QName eltName;
-		while (!((elmtType == XMLStreamReader.END_ELEMENT) && reader.getName().getLocalPart().equals("Description"))) {
+		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();
@@ -172,8 +169,10 @@ public class StructuredPropertyParser {
 			}
 			elmtType = reader.nextTag();
 		}
-		// closing rdf:Description element
-		reader.nextTag();
+		if (!skipDescription) {
+			// closing rdf:Description element
+			reader.nextTag();
+		}
 		container.addProperty(property);
 
 	}
@@ -226,8 +225,8 @@ public class StructuredPropertyParser {
 
 	protected class PropertyDescription {
 
-//		private String fieldName;
-//
+		//		private String fieldName;
+		//
 		private String propertyName;
 
 		private PropertyType propertyType;

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=1366951&r1=1366950&r2=1366951&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 Sun Jul 29 20:55:54 2012
@@ -195,14 +195,15 @@ public class DeserializationTest {
 		List<ThumbnailType> thumbs = metadata.getXMPBasicSchema()
 				.getThumbnailsProperty();
 		Assert.assertNotNull(thumbs);
-		Assert.assertEquals(1, thumbs.size());
+		Assert.assertEquals(2, thumbs.size());
+
 		ThumbnailType thumb = thumbs.get(0);
-		/*
-		 * <xapGImg:height>162</xapGImg:height>
-		 * <xapGImg:width>216</xapGImg:width>
-		 * <xapGImg:format>JPEG</xapGImg:format>
-		 * <xapGImg:image>/9j/4AAQSkZJRgABAgEASABIAAD</xapGImg:image>
-		 */
+		Assert.assertEquals(new Integer(162), thumb.getHeight());
+		Assert.assertEquals(new Integer(216), thumb.getWidth());
+		Assert.assertEquals("JPEG", thumb.getFormat());
+		Assert.assertEquals("/9j/4AAQSkZJRgABAgEASABIAAD", thumb.getImage());
+
+		thumb = thumbs.get(1);
 		Assert.assertEquals(new Integer(162), thumb.getHeight());
 		Assert.assertEquals(new Integer(216), thumb.getWidth());
 		Assert.assertEquals("JPEG", thumb.getFormat());

Modified: pdfbox/trunk/xmpbox/src/test/resources/org/apache/padaf/xmpbox/parser/ThumbisartorStyle.xml
URL: http://svn.apache.org/viewvc/pdfbox/trunk/xmpbox/src/test/resources/org/apache/padaf/xmpbox/parser/ThumbisartorStyle.xml?rev=1366951&r1=1366950&r2=1366951&view=diff
==============================================================================
--- pdfbox/trunk/xmpbox/src/test/resources/org/apache/padaf/xmpbox/parser/ThumbisartorStyle.xml (original)
+++ pdfbox/trunk/xmpbox/src/test/resources/org/apache/padaf/xmpbox/parser/ThumbisartorStyle.xml Sun Jul 29 20:55:54 2012
@@ -24,6 +24,12 @@
 			<xmp:Thumbnails xmlns:xapGImg="http://ns.adobe.com/xap/1.0/g/img/">
 				<rdf:Alt>
 					<rdf:li rdf:parseType="Resource">
+							<xapGImg:height>162</xapGImg:height>
+							<xapGImg:width>216</xapGImg:width>
+							<xapGImg:format>JPEG</xapGImg:format>
+							<xapGImg:image>/9j/4AAQSkZJRgABAgEASABIAAD</xapGImg:image>
+					</rdf:li>
+					<rdf:li>
 						<rdf:Description>
 							<xapGImg:height>162</xapGImg:height>
 							<xapGImg:width>216</xapGImg:width>