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/24 16:32:48 UTC
svn commit: r1376944 - in /pdfbox/branches/xmpbox-refactoring/xmpbox/src:
main/java/org/apache/padaf/xmpbox/parser/
main/java/org/apache/padaf/xmpbox/schema/
main/java/org/apache/padaf/xmpbox/type/
test/java/org/apache/padaf/xmpbox/parser/
Author: gbailleul
Date: Fri Aug 24 14:32:47 2012
New Revision: 1376944
URL: http://svn.apache.org/viewvc?rev=1376944&view=rev
Log:
PDFBOX-1343: remove class introspection where not necessary, reduced complexity on property init
Added:
pdfbox/branches/xmpbox-refactoring/xmpbox/src/main/java/org/apache/padaf/xmpbox/type/PropMapping.java
- copied, changed from r1370327, pdfbox/branches/xmpbox-refactoring/xmpbox/src/main/java/org/apache/padaf/xmpbox/parser/PropMapping.java
pdfbox/branches/xmpbox-refactoring/xmpbox/src/main/java/org/apache/padaf/xmpbox/type/ReflectHelper.java
Removed:
pdfbox/branches/xmpbox-refactoring/xmpbox/src/main/java/org/apache/padaf/xmpbox/parser/PropMapping.java
pdfbox/branches/xmpbox-refactoring/xmpbox/src/main/java/org/apache/padaf/xmpbox/schema/PropertyAttributesAnnotation.java
Modified:
pdfbox/branches/xmpbox-refactoring/xmpbox/src/main/java/org/apache/padaf/xmpbox/parser/StructuredPropertyParser.java
pdfbox/branches/xmpbox-refactoring/xmpbox/src/main/java/org/apache/padaf/xmpbox/parser/XMPDocumentBuilder.java
pdfbox/branches/xmpbox-refactoring/xmpbox/src/main/java/org/apache/padaf/xmpbox/parser/XMPSchemaFactory.java
pdfbox/branches/xmpbox-refactoring/xmpbox/src/main/java/org/apache/padaf/xmpbox/schema/SchemaMapping.java
pdfbox/branches/xmpbox-refactoring/xmpbox/src/main/java/org/apache/padaf/xmpbox/type/AbstractStructuredType.java
pdfbox/branches/xmpbox-refactoring/xmpbox/src/main/java/org/apache/padaf/xmpbox/type/TypeDescription.java
pdfbox/branches/xmpbox-refactoring/xmpbox/src/main/java/org/apache/padaf/xmpbox/type/TypeMapping.java
pdfbox/branches/xmpbox-refactoring/xmpbox/src/test/java/org/apache/padaf/xmpbox/parser/PropMappingTest.java
Modified: pdfbox/branches/xmpbox-refactoring/xmpbox/src/main/java/org/apache/padaf/xmpbox/parser/StructuredPropertyParser.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/xmpbox-refactoring/xmpbox/src/main/java/org/apache/padaf/xmpbox/parser/StructuredPropertyParser.java?rev=1376944&r1=1376943&r2=1376944&view=diff
==============================================================================
--- pdfbox/branches/xmpbox-refactoring/xmpbox/src/main/java/org/apache/padaf/xmpbox/parser/StructuredPropertyParser.java (original)
+++ pdfbox/branches/xmpbox-refactoring/xmpbox/src/main/java/org/apache/padaf/xmpbox/parser/StructuredPropertyParser.java Fri Aug 24 14:32:47 2012
@@ -22,11 +22,6 @@
package org.apache.padaf.xmpbox.parser;
import java.io.IOException;
-import java.lang.reflect.Constructor;
-import java.lang.reflect.Field;
-import java.lang.reflect.InvocationTargetException;
-import java.util.HashMap;
-import java.util.Map;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLStreamConstants;
@@ -40,7 +35,8 @@ import org.apache.padaf.xmpbox.type.Abst
import org.apache.padaf.xmpbox.type.ArrayProperty;
import org.apache.padaf.xmpbox.type.ComplexPropertyContainer;
import org.apache.padaf.xmpbox.type.DefinedStructuredType;
-import org.apache.padaf.xmpbox.type.PropertyType;
+import org.apache.padaf.xmpbox.type.PropMapping;
+import org.apache.padaf.xmpbox.type.ReflectHelper;
import org.apache.padaf.xmpbox.type.TypeDescription;
public class StructuredPropertyParser {
@@ -49,64 +45,21 @@ public class StructuredPropertyParser {
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};
+ private PropMapping propDesc = null;
+
+ private boolean isDefinedStructureType = false;
public StructuredPropertyParser(XMPDocumentBuilder builder,Class<? extends AbstractStructuredType> propertyTypeClass)
throws XmpPropertyFormatException {
this.builder = builder;
this.typeClass = propertyTypeClass;
- this.propDesc = new HashMap<String, PropertyDescription>();
// retrieve xmp properties
- Field [] fields = typeClass.getFields();
- for (Field field : fields) {
- if (field.getAnnotation(PropertyType.class)!=null) {
- PropertyDescription pd = new PropertyDescription();
- pd.propertyType = field.getAnnotation(PropertyType.class);
- // pd.fieldName = field.getName();
- try {
- pd.propertyName = field.get(null).toString();
- } catch (IllegalArgumentException e1) {
- throw new XmpPropertyFormatException("Failed to parse structured type : "+typeClass.getName(),e1);
- } catch (IllegalAccessException e1) {
- throw new XmpPropertyFormatException("Failed to parse structured type : "+typeClass.getName(),e1);
- }
- propDesc.put(pd.propertyName, pd);
- }
- }
- // retrieve constructor
- if (DefinedStructuredType.class.isAssignableFrom(typeClass)) {
- // specific case from DefinedStructured type
- typeConstructor = null;
- } else {
- // reflection to find constructor
- try {
- typeConstructor = typeClass.getConstructor(propertyConstructorParams);
- } catch (SecurityException e) {
- throw new XmpPropertyFormatException("Failed to initialize structured type parser : "+typeClass.getName(),e);
- } catch (NoSuchMethodException e) {
- throw new XmpPropertyFormatException("Failed to initialize structured type parser : "+typeClass.getName(),e);
- }
- }
-
+ this.propDesc = ReflectHelper.initializePropMapping(null, propertyTypeClass); // TODO GBL GBA
+ this.isDefinedStructureType = DefinedStructuredType.class.isAssignableFrom(typeClass);
}
private AbstractStructuredType instanciateProperty (XMPMetadata metadata) throws XmpParsingException {
- try {
- // return typeConstructor.newInstance(metadata,prefix);
- return typeConstructor.newInstance(metadata);
- } catch (IllegalArgumentException e) {
- throw new XmpParsingException("Failed to instanciate structured type : "+typeClass.getName(),e);
- } catch (InstantiationException e) {
- throw new XmpParsingException("Failed to instanciate structured type : "+typeClass.getName(),e);
- } catch (IllegalAccessException e) {
- throw new XmpParsingException("Failed to instanciate structured type : "+typeClass.getName(),e);
- } catch (InvocationTargetException e) {
- throw new XmpParsingException("Failed to instanciate structured type : "+typeClass.getName(),e);
- }
+ return builder.getTypeMapping().instanciateStructuredType(metadata, typeClass);
}
@@ -151,7 +104,7 @@ public class StructuredPropertyParser {
elmtType = reader.nextTag();
}
AbstractStructuredType property = null;
- if (typeConstructor==null) {
+ if (isDefinedStructureType) {
// defined type
property = new DefinedStructuredType(metadata, null, null);// TODO
} else {
@@ -198,10 +151,10 @@ public class StructuredPropertyParser {
// check if property is expected
String localPart = eltName.getLocalPart();
if (propDesc.containsKey(localPart)) {
- PropertyDescription description = propDesc.get(localPart);
+ String ptype = propDesc.getPropertyType(localPart);
AbstractField a = instanciateSimple(
- description.propertyType.propertyType(),
+ ptype,
metadata,
eltName.getPrefix(),
localPart,
@@ -326,16 +279,4 @@ public class StructuredPropertyParser {
return builder.getTypeMapping().instanciateSimpleProperty(metadata, null, prefix, propertyName, value, type);
}
-
-
- protected class PropertyDescription {
-
- private String propertyName;
-
- private PropertyType propertyType;
-
- }
-
-
-
}
Modified: pdfbox/branches/xmpbox-refactoring/xmpbox/src/main/java/org/apache/padaf/xmpbox/parser/XMPDocumentBuilder.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/xmpbox-refactoring/xmpbox/src/main/java/org/apache/padaf/xmpbox/parser/XMPDocumentBuilder.java?rev=1376944&r1=1376943&r2=1376944&view=diff
==============================================================================
--- pdfbox/branches/xmpbox-refactoring/xmpbox/src/main/java/org/apache/padaf/xmpbox/parser/XMPDocumentBuilder.java (original)
+++ pdfbox/branches/xmpbox-refactoring/xmpbox/src/main/java/org/apache/padaf/xmpbox/parser/XMPDocumentBuilder.java Fri Aug 24 14:32:47 2012
@@ -25,8 +25,6 @@ import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
-import java.lang.reflect.Constructor;
-import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
@@ -154,7 +152,7 @@ public class XMPDocumentBuilder {
XMPDocumentBuilder preproc = new XMPDocumentBuilder();
XMPMetadata xmpPreproc = preproc.doParsingParsing(xmp,true);
- populateSchemaMapping(schemaMapping, xmpPreproc);
+ populateSchemaMapping(xmpPreproc);
return doParsingParsing(xmp,false);
@@ -251,7 +249,7 @@ public class XMPDocumentBuilder {
}
}
- private void populateSchemaMapping (SchemaMapping sm, XMPMetadata meta)
+ private void populateSchemaMapping (XMPMetadata meta)
throws XmpRequiredPropertyException,XmpUnknownValueTypeException,XmpUnexpectedNamespacePrefixException {
List<XMPSchema> schems = meta.getAllSchemas();
for (XMPSchema xmpSchema : schems) {
@@ -344,7 +342,7 @@ public class XMPDocumentBuilder {
throw new XmpUnknownValueTypeException("Type not defined : "+ptype);
}
// load the property
- xsf.getPropertyDefinition().addNewProperty(pname, ptype, null);
+ xsf.getPropertyDefinition().addNewProperty(pname, ptype);
} // TODO unmanaged ?
}
} // TODO unmanaged ?
@@ -807,12 +805,8 @@ public class XMPDocumentBuilder {
.getAttributeLocalName(i), reader.get()
.getAttributeValue(i)));
}
- Class<?> [] constParams = new Class<?> [] {XMPMetadata.class,String.class,String.class,String.class,Object.class};
- Constructor<? extends AbstractSimpleProperty> constructor = tclass.getConstructor(constParams);
-
- prop = constructor.newInstance(metadata, null,propertyName.getPrefix(),
- propertyName.getLocalPart(), reader.get()
- .getElementText());
+ prop = typeMapping.instanciateSimpleProperty(metadata, null, propertyName.getPrefix(),
+ propertyName.getLocalPart(), reader.get().getElementText(),typeMapping.getType(tclass));
if (prop != null) {
container.addProperty(prop);
// ADD ATTRIBUTES
@@ -829,14 +823,6 @@ public class XMPDocumentBuilder {
+ propertyName.getLocalPart() + "'", e);
} catch (SecurityException e) {
throw new XmpPropertyFormatException("Failed to create property",e);
- } catch (NoSuchMethodException e) {
- throw new XmpPropertyFormatException("Failed to create property",e);
- } catch (InstantiationException e) {
- throw new XmpPropertyFormatException("Failed to create property",e);
- } catch (IllegalAccessException e) {
- throw new XmpPropertyFormatException("Failed to create property",e);
- } catch (InvocationTargetException e) {
- throw new XmpPropertyFormatException("Failed to create property",e);
}
}
@@ -938,55 +924,49 @@ public class XMPDocumentBuilder {
.getNamespacePrefix(i));
}
}
- try {
- String type = getPropertyDeclarationInNamespaces(schema, propertyName);
- // found type, manage it
- if (type.equals("Lang Alt")) {
- parseSimplePropertyArray(metadata, propertyName, ArrayProperty.ALTERNATIVE_ARRAY, TextType.class, schema.getContent());
- } else if (typeMapping.isSimpleType(type)) {
- TypeDescription tclass = typeMapping.getTypeDescription(type);
- Class<? extends AbstractSimpleProperty> tcn = (Class<? extends AbstractSimpleProperty>)tclass.getTypeClass();
- parseSimpleProperty(metadata, propertyName, tcn, schema.getContent());
- } else if (typeMapping.isStructuredType(type)) {
- TypeDescription tclass = typeMapping.getTypeDescription(type);
+ String type = getPropertyDeclarationInNamespaces(schema, propertyName);
+ // found type, manage it
+ if (type.equals("Lang Alt")) {
+ parseSimplePropertyArray(metadata, propertyName, ArrayProperty.ALTERNATIVE_ARRAY, TextType.class, schema.getContent());
+ } else if (typeMapping.isSimpleType(type)) {
+ TypeDescription tclass = typeMapping.getTypeDescription(type);
+ Class<? extends AbstractSimpleProperty> tcn = (Class<? extends AbstractSimpleProperty>)tclass.getTypeClass();
+ parseSimpleProperty(metadata, propertyName, tcn, schema.getContent());
+ } else if (typeMapping.isStructuredType(type)) {
+ TypeDescription tclass = typeMapping.getTypeDescription(type);
+ StructuredPropertyParser parser = new StructuredPropertyParser(
+ this, (Class<? extends AbstractStructuredType>)tclass.getTypeClass());
+ parseStructuredProperty(metadata, parser, schema.getContent());
+ } else if (typeMapping.getArrayType(type)!=null) {
+ // retrieve array type and content type
+ int pos = type.indexOf(' ');
+ String arrayType = typeMapping.getArrayType(type);
+ String typeInArray = type.substring(pos+1);
+ TypeDescription tclass = typeMapping.getTypeDescription(typeInArray);
+ Class<? extends AbstractField> tcn = tclass.getTypeClass();
+
+ if (AbstractSimpleProperty.class.isAssignableFrom(tcn)) {
+ // array of simple
+ parseSimplePropertyArray(
+ metadata,
+ propertyName,
+ arrayType,
+ (Class<? extends AbstractSimpleProperty>)tcn,
+ schema.getContent());
+ } else if (AbstractStructuredType.class.isAssignableFrom(tcn)) {
+ // array of structured
StructuredPropertyParser parser = new StructuredPropertyParser(
- this, (Class<? extends AbstractStructuredType>)tclass.getTypeClass());
- parseStructuredProperty(metadata, parser, schema.getContent());
- } else if (typeMapping.getArrayType(type)!=null) {
- // retrieve array type and content type
- int pos = type.indexOf(' ');
- String arrayType = typeMapping.getArrayType(type);
- String typeInArray = type.substring(pos+1);
- TypeDescription tclass = typeMapping.getTypeDescription(typeInArray);
- Class<? extends AbstractField> tcn = tclass.getTypeClass();
-
- if (AbstractSimpleProperty.class.isAssignableFrom(tcn)) {
- // array of simple
- parseSimplePropertyArray(
- metadata,
- propertyName,
- arrayType,
- (Class<? extends AbstractSimpleProperty>)tcn,
- schema.getContent());
- } else if (AbstractStructuredType.class.isAssignableFrom(tcn)) {
- // array of structured
- StructuredPropertyParser parser = new StructuredPropertyParser(
- this, (Class<? extends AbstractStructuredType>)tcn);
- parseStructuredPropertyArray(metadata, propertyName, arrayType, parser, schema.getContent());
- } else {
- // invalid case
- throw new XmpUnknownPropertyTypeException("Unknown type : "+type);
- }
- // } else if (type.equals("Field")) {
- // parseFieldProperty(metadata, propertyName, schema);
+ this, (Class<? extends AbstractStructuredType>)tcn);
+ parseStructuredPropertyArray(metadata, propertyName, arrayType, parser, schema.getContent());
} else {
- throw new XmpUnknownPropertyTypeException("Unknown type : " + type);
+ // invalid case
+ throw new XmpUnknownPropertyTypeException("Unknown type : "+type);
}
-
- } catch (XmpUnknownPropertyException e) {
- throw e;
+ // } else if (type.equals("Field")) {
+ // parseFieldProperty(metadata, propertyName, schema);
+ } else {
+ throw new XmpUnknownPropertyTypeException("Unknown type : " + type);
}
-
}
Modified: pdfbox/branches/xmpbox-refactoring/xmpbox/src/main/java/org/apache/padaf/xmpbox/parser/XMPSchemaFactory.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/xmpbox-refactoring/xmpbox/src/main/java/org/apache/padaf/xmpbox/parser/XMPSchemaFactory.java?rev=1376944&r1=1376943&r2=1376944&view=diff
==============================================================================
--- pdfbox/branches/xmpbox-refactoring/xmpbox/src/main/java/org/apache/padaf/xmpbox/parser/XMPSchemaFactory.java (original)
+++ pdfbox/branches/xmpbox-refactoring/xmpbox/src/main/java/org/apache/padaf/xmpbox/parser/XMPSchemaFactory.java Fri Aug 24 14:32:47 2012
@@ -27,6 +27,7 @@ import java.util.List;
import org.apache.padaf.xmpbox.XMPMetadata;
import org.apache.padaf.xmpbox.schema.XMPSchema;
+import org.apache.padaf.xmpbox.type.PropMapping;
/**
@@ -103,26 +104,6 @@ public class XMPSchemaFactory {
}
/**
- * Get attributes declared for a property (NOT USED YET)
- *
- * @param name
- * The property Name
- * @return List of all attributes defined for this property
- */
- public List<String> getPropertyAttributes(String name) {
- List<String> result = propDef.getPropertyAttributes(name);
- if (result == null) {
- for (PropMapping mapping : importedPropertyMapping) {
- result = mapping.getPropertyAttributes(name);
- if (result != null) {
- break;
- }
- }
- }
- return result;
- }
-
- /**
* Create a schema that corresponding to this factory and add it to metadata
*
* @param metadata
Modified: pdfbox/branches/xmpbox-refactoring/xmpbox/src/main/java/org/apache/padaf/xmpbox/schema/SchemaMapping.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/xmpbox-refactoring/xmpbox/src/main/java/org/apache/padaf/xmpbox/schema/SchemaMapping.java?rev=1376944&r1=1376943&r2=1376944&view=diff
==============================================================================
--- pdfbox/branches/xmpbox-refactoring/xmpbox/src/main/java/org/apache/padaf/xmpbox/schema/SchemaMapping.java (original)
+++ pdfbox/branches/xmpbox-refactoring/xmpbox/src/main/java/org/apache/padaf/xmpbox/schema/SchemaMapping.java Fri Aug 24 14:32:47 2012
@@ -21,17 +21,14 @@
package org.apache.padaf.xmpbox.schema;
-import java.lang.reflect.Field;
-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.parser.XMPSchemaFactory;
import org.apache.padaf.xmpbox.parser.XmpSchemaException;
-import org.apache.padaf.xmpbox.type.PropertyType;
+import org.apache.padaf.xmpbox.type.PropMapping;
+import org.apache.padaf.xmpbox.type.ReflectHelper;
public final class SchemaMapping {
@@ -66,7 +63,7 @@ public final class SchemaMapping {
* When could not read property name in Schema Class given
*/
private static void addNameSpace(String ns, Class<? extends XMPSchema> classSchem) {
- nsMaps.put(ns, new XMPSchemaFactory(ns, classSchem, initializePropMapping(ns, classSchem)));
+ nsMaps.put(ns, new XMPSchemaFactory(ns, classSchem, ReflectHelper.initializePropMapping(ns, classSchem)));
}
public void addNewNameSpace(String ns) {
@@ -75,61 +72,6 @@ public final class SchemaMapping {
}
-
-
- /**
- * Initialize the Property Mapping for a given schema
- *
- * @param ns
- * Namespace URI
- * @param classSchem
- * The class representation of the schema linked to the namespace
- * @return Construct expected properties types representation
- * @throws XmpSchemaException
- * When could not read property name in field with properties
- * annotations
- */
- private static PropMapping initializePropMapping(String ns,
- Class<? extends XMPSchema> 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;
- }
-
-
-
/**
* Return the specialized schema class representation if it's known (create
* and add it to metadata). In other cases, return null
@@ -160,4 +102,5 @@ public final class SchemaMapping {
}
+
}
Modified: pdfbox/branches/xmpbox-refactoring/xmpbox/src/main/java/org/apache/padaf/xmpbox/type/AbstractStructuredType.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/xmpbox-refactoring/xmpbox/src/main/java/org/apache/padaf/xmpbox/type/AbstractStructuredType.java?rev=1376944&r1=1376943&r2=1376944&view=diff
==============================================================================
--- pdfbox/branches/xmpbox-refactoring/xmpbox/src/main/java/org/apache/padaf/xmpbox/type/AbstractStructuredType.java (original)
+++ pdfbox/branches/xmpbox-refactoring/xmpbox/src/main/java/org/apache/padaf/xmpbox/type/AbstractStructuredType.java Fri Aug 24 14:32:47 2012
@@ -22,7 +22,6 @@
package org.apache.padaf.xmpbox.type;
import java.util.Calendar;
-import java.util.List;
import org.apache.padaf.xmpbox.XMPMetadata;
Copied: pdfbox/branches/xmpbox-refactoring/xmpbox/src/main/java/org/apache/padaf/xmpbox/type/PropMapping.java (from r1370327, pdfbox/branches/xmpbox-refactoring/xmpbox/src/main/java/org/apache/padaf/xmpbox/parser/PropMapping.java)
URL: http://svn.apache.org/viewvc/pdfbox/branches/xmpbox-refactoring/xmpbox/src/main/java/org/apache/padaf/xmpbox/type/PropMapping.java?p2=pdfbox/branches/xmpbox-refactoring/xmpbox/src/main/java/org/apache/padaf/xmpbox/type/PropMapping.java&p1=pdfbox/branches/xmpbox-refactoring/xmpbox/src/main/java/org/apache/padaf/xmpbox/parser/PropMapping.java&r1=1370327&r2=1376944&rev=1376944&view=diff
==============================================================================
--- pdfbox/branches/xmpbox-refactoring/xmpbox/src/main/java/org/apache/padaf/xmpbox/parser/PropMapping.java (original)
+++ pdfbox/branches/xmpbox-refactoring/xmpbox/src/main/java/org/apache/padaf/xmpbox/type/PropMapping.java Fri Aug 24 14:32:47 2012
@@ -19,7 +19,7 @@
*
****************************************************************************/
-package org.apache.padaf.xmpbox.parser;
+package org.apache.padaf.xmpbox.type;
import java.util.ArrayList;
import java.util.HashMap;
@@ -39,8 +39,9 @@ import java.util.Map;
public class PropMapping {
private String namespace;
+
private Map<String, String> types;
- private Map<String, List<String>> attributes;
+
/**
* Build PropMapping for specified namespace
@@ -51,8 +52,6 @@ public class PropMapping {
public PropMapping(String namespace) {
this.namespace = namespace;
types = new HashMap<String, String>();
- attributes = new HashMap<String, List<String>>();
-
}
/**
@@ -84,11 +83,8 @@ public class PropMapping {
* A list of attribute (put null while attribute management is
* not implemented)
*/
- public void addNewProperty(String name, String type, List<String> attr) {
+ public void addNewProperty(String name, String type) {
types.put(name, type);
- if (attr != null) {
- attributes.put(name, attr);
- }
}
/**
@@ -102,15 +98,8 @@ public class PropMapping {
return types.get(name);
}
- /**
- * Return an unmodifiable list of property attributes from its qualifiedName
- *
- * @param name
- * LocalName of the property
- * @return List of all attributes declared for this property
- */
- public List<String> getPropertyAttributes(String name) {
- return attributes.get(name);
+ public boolean containsKey (String name) {
+ return types.containsKey(name);
}
-
+
}
Added: pdfbox/branches/xmpbox-refactoring/xmpbox/src/main/java/org/apache/padaf/xmpbox/type/ReflectHelper.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/xmpbox-refactoring/xmpbox/src/main/java/org/apache/padaf/xmpbox/type/ReflectHelper.java?rev=1376944&view=auto
==============================================================================
--- pdfbox/branches/xmpbox-refactoring/xmpbox/src/main/java/org/apache/padaf/xmpbox/type/ReflectHelper.java (added)
+++ pdfbox/branches/xmpbox-refactoring/xmpbox/src/main/java/org/apache/padaf/xmpbox/type/ReflectHelper.java Fri Aug 24 14:32:47 2012
@@ -0,0 +1,68 @@
+/*****************************************************************************
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+package org.apache.padaf.xmpbox.type;
+
+import java.lang.reflect.Field;
+
+
+public final class ReflectHelper {
+
+ private ReflectHelper () {
+ }
+
+ /**
+ * Initialize the Property Mapping for a given schema
+ *
+ * @param ns
+ * Namespace URI
+ * @param classSchem
+ * The class representation of the schema linked to the namespace
+ * @return Construct expected properties types representation
+ * @throws XmpSchemaException
+ * When could not read property name in field with properties
+ * annotations
+ */
+ public final static PropMapping initializePropMapping(String ns,
+ Class<?> classSchem) {
+ PropMapping propMap = new PropMapping(ns);
+ Field [] 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);
+ }
+ PropertyType propType = field.getAnnotation(PropertyType.class);
+ propMap.addNewProperty(propName, propType.propertyType());
+ }
+ }
+ return propMap;
+ }
+
+
+
+
+}
Modified: pdfbox/branches/xmpbox-refactoring/xmpbox/src/main/java/org/apache/padaf/xmpbox/type/TypeDescription.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/xmpbox-refactoring/xmpbox/src/main/java/org/apache/padaf/xmpbox/type/TypeDescription.java?rev=1376944&r1=1376943&r2=1376944&view=diff
==============================================================================
--- pdfbox/branches/xmpbox-refactoring/xmpbox/src/main/java/org/apache/padaf/xmpbox/type/TypeDescription.java (original)
+++ pdfbox/branches/xmpbox-refactoring/xmpbox/src/main/java/org/apache/padaf/xmpbox/type/TypeDescription.java Fri Aug 24 14:32:47 2012
@@ -21,7 +21,6 @@
package org.apache.padaf.xmpbox.type;
-import org.apache.padaf.xmpbox.parser.PropMapping;
public class TypeDescription {
@@ -36,7 +35,6 @@ public class TypeDescription {
private DefinedStructuredType definedStructuredType;
- // TODO PropMapping should be in package Type
private PropMapping properties = null;
public TypeDescription(String type, BasicType basic,Class<? extends AbstractField> clz) {
Modified: pdfbox/branches/xmpbox-refactoring/xmpbox/src/main/java/org/apache/padaf/xmpbox/type/TypeMapping.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/xmpbox-refactoring/xmpbox/src/main/java/org/apache/padaf/xmpbox/type/TypeMapping.java?rev=1376944&r1=1376943&r2=1376944&view=diff
==============================================================================
--- pdfbox/branches/xmpbox-refactoring/xmpbox/src/main/java/org/apache/padaf/xmpbox/type/TypeMapping.java (original)
+++ pdfbox/branches/xmpbox-refactoring/xmpbox/src/main/java/org/apache/padaf/xmpbox/type/TypeMapping.java Fri Aug 24 14:32:47 2012
@@ -22,121 +22,111 @@
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 java.util.Map.Entry;
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.parser.XmpParsingException;
import org.apache.padaf.xmpbox.type.TypeDescription.BasicType;
public final class TypeMapping {
-
- private static final Map<String,TypeDescription> BASIC_TYPES;
- private static final Map<Class<? extends AbstractField>,TypeDescription> BASIC_CLASSES;
+ private Map<String,TypeDescription> BASIC_TYPES;
- private static final Map<String,TypeDescription> DERIVED_TYPES;
-
- private static final Map<Class<? extends AbstractField>,TypeDescription> DERIVED_CLASSES;
+ private Map<Class<? extends AbstractField>,TypeDescription> BASIC_CLASSES;
- private static final Map<String, TypeDescription> STRUCTURED_TYPES;
+ private Map<String,TypeDescription> DERIVED_TYPES;
- private static final Map<Class<? extends AbstractField>,TypeDescription> STRUCTURED_CLASSES;
+ private Map<Class<? extends AbstractField>,TypeDescription> DERIVED_CLASSES;
- private static final Map<String,TypeDescription> STRUCTURED_NAMESPACES;
-
- private static Map<Class<?>, Map<String,String>> structuredTypes = new HashMap<Class<?>, Map<String,String>>();
+ private Map<String, TypeDescription> STRUCTURED_TYPES;
+
+ private Map<Class<? extends AbstractField>,TypeDescription> STRUCTURED_CLASSES;
+
+ private Map<String,TypeDescription> STRUCTURED_NAMESPACES;
-
public TypeMapping() {
-
+ initialize();
}
-
- private static Class<?> [] simplePropertyConstParams = new Class<?> [] {
- XMPMetadata.class,
- String.class,
+
+ private static Class<?> [] simplePropertyConstParams = new Class<?> [] {
+ XMPMetadata.class,
+ String.class,
String.class,
String.class,
Object.class
- };
+ };
+
+ private void initialize () {
+ // basic
+ BASIC_TYPES = new HashMap<String,TypeDescription>();
+ BASIC_CLASSES = new HashMap<Class<? extends AbstractField>, TypeDescription>();
+ addToBasicMaps(new TypeDescription("Text",BasicType.Text,TextType.class));
+ addToBasicMaps(new TypeDescription("Date",BasicType.Date,DateType.class));
+ addToBasicMaps(new TypeDescription("Boolean",BasicType.Boolean,BooleanType.class));
+ addToBasicMaps(new TypeDescription("Integer",BasicType.Integer,IntegerType.class));
+ addToBasicMaps(new TypeDescription("Real",BasicType.Real,RealType.class));
+
+ // derived
+ DERIVED_TYPES = new HashMap<String,TypeDescription>();
+ DERIVED_CLASSES = new HashMap<Class<? extends AbstractField>, TypeDescription>();
+ addToDerivedMaps(new TypeDescription("AgentName",BasicType.Text,AgentNameType.class));
+ addToDerivedMaps(new TypeDescription("Choice",BasicType.Text,ChoiceType.class));
+ addToDerivedMaps(new TypeDescription("GUID",BasicType.Text,GUIDType.class));
+ addToDerivedMaps(new TypeDescription("Lang Alt",BasicType.Text,TextType.class));
+ addToDerivedMaps(new TypeDescription("Locale",BasicType.Text,LocaleType.class));
+ addToDerivedMaps(new TypeDescription("MIMEType",BasicType.Text,MIMEType.class));
+ addToDerivedMaps(new TypeDescription("ProperName",BasicType.Text,ProperNameType.class));
+ addToDerivedMaps(new TypeDescription("RenditionClass",BasicType.Text,RenditionClassType.class));
+ addToDerivedMaps(new TypeDescription("URL",BasicType.Text,URLType.class));
+ addToDerivedMaps(new TypeDescription("URI",BasicType.Text,URIType.class));
+ addToDerivedMaps(new TypeDescription("XPath",BasicType.Text,XPathType.class));
+ addToDerivedMaps(new TypeDescription("Part",BasicType.Text,PartType.class));
+
+ // structured types
+ STRUCTURED_TYPES = new HashMap<String, TypeDescription>();
+ STRUCTURED_CLASSES = new HashMap<Class<? extends AbstractField>, TypeDescription>();
+ STRUCTURED_NAMESPACES = new HashMap<String, TypeDescription>();
+ addToStructuredMaps(new TypeDescription("Thumbnail",null,ThumbnailType.class));
+ addToStructuredMaps(new TypeDescription("Layer",null,LayerType.class));
+ addToStructuredMaps(new TypeDescription("ResourceEvent",null,ResourceEventType.class));
+ 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 void addToBasicMaps (TypeDescription td) {
+ BASIC_TYPES.put(td.getType(),td);
+ BASIC_CLASSES.put(td.getTypeClass(), td);
+ }
- static {
- // basic
- BASIC_TYPES = new HashMap<String,TypeDescription>();
- BASIC_CLASSES = new HashMap<Class<? extends AbstractField>, TypeDescription>();
- addToBasicMaps(new TypeDescription("Text",BasicType.Text,TextType.class));
- addToBasicMaps(new TypeDescription("Date",BasicType.Date,DateType.class));
- addToBasicMaps(new TypeDescription("Boolean",BasicType.Boolean,BooleanType.class));
- addToBasicMaps(new TypeDescription("Integer",BasicType.Integer,IntegerType.class));
- addToBasicMaps(new TypeDescription("Real",BasicType.Real,RealType.class));
-
- // derived
- DERIVED_TYPES = new HashMap<String,TypeDescription>();
- DERIVED_CLASSES = new HashMap<Class<? extends AbstractField>, TypeDescription>();
- addToDerivedMaps(new TypeDescription("AgentName",BasicType.Text,AgentNameType.class));
- addToDerivedMaps(new TypeDescription("Choice",BasicType.Text,ChoiceType.class));
- addToDerivedMaps(new TypeDescription("GUID",BasicType.Text,GUIDType.class));
- addToDerivedMaps(new TypeDescription("Lang Alt",BasicType.Text,TextType.class));
- addToDerivedMaps(new TypeDescription("Locale",BasicType.Text,LocaleType.class));
- addToDerivedMaps(new TypeDescription("MIMEType",BasicType.Text,MIMEType.class));
- addToDerivedMaps(new TypeDescription("ProperName",BasicType.Text,ProperNameType.class));
- addToDerivedMaps(new TypeDescription("RenditionClass",BasicType.Text,RenditionClassType.class));
- addToDerivedMaps(new TypeDescription("URL",BasicType.Text,URLType.class));
- addToDerivedMaps(new TypeDescription("URI",BasicType.Text,URIType.class));
- addToDerivedMaps(new TypeDescription("XPath",BasicType.Text,XPathType.class));
- addToDerivedMaps(new TypeDescription("Part",BasicType.Text,PartType.class));
-
- // structured types
- STRUCTURED_TYPES = new HashMap<String, TypeDescription>();
- STRUCTURED_CLASSES = new HashMap<Class<? extends AbstractField>, TypeDescription>();
- STRUCTURED_NAMESPACES = new HashMap<String, TypeDescription>();
- addToStructuredMaps(new TypeDescription("Thumbnail",null,ThumbnailType.class));
- addToStructuredMaps(new TypeDescription("Layer",null,LayerType.class));
- addToStructuredMaps(new TypeDescription("ResourceEvent",null,ResourceEventType.class));
- 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) {
- BASIC_TYPES.put(td.getType(),td);
- BASIC_CLASSES.put(td.getTypeClass(), td);
- }
-
- public static void addToDerivedMaps (TypeDescription td) {
- DERIVED_TYPES.put(td.getType(),td);
- DERIVED_CLASSES.put(td.getTypeClass(), td);
- }
-
- public static void addToStructuredMaps (TypeDescription td) {
- STRUCTURED_TYPES.put(td.getType(),td);
- STRUCTURED_CLASSES.put(td.getTypeClass(), td);
-
- try {
- String ns = (String)td.getTypeClass().getField("ELEMENT_NS").get(null);
- STRUCTURED_NAMESPACES.put(ns, td);
+ public void addToDerivedMaps (TypeDescription td) {
+ DERIVED_TYPES.put(td.getType(),td);
+ DERIVED_CLASSES.put(td.getTypeClass(), td);
+ }
+
+ public void addToStructuredMaps (TypeDescription td) {
+ try {
+ String ns = (String)td.getTypeClass().getField("ELEMENT_NS").get(null);
Class<? extends AbstractStructuredType> clz = (Class<? extends AbstractStructuredType>)td.getTypeClass();
if (clz!=null) {
- PropMapping pm = initializePropMapping(ns, clz);
- td.setProperties(pm);
+ PropMapping pm = ReflectHelper.initializePropMapping(ns, clz);
+ td.setProperties(pm);
} else {
- PropMapping pm = initializePropMapping(ns, td.getDefinedStructure());
- td.setProperties(pm);
+ PropMapping pm = initializePropMapping(ns, td.getDefinedStructure());
+ td.setProperties(pm);
}
+ addToStructuredMaps(td, ns);
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException("Failed to init structured maps for "+td.getTypeClass(), e);
} catch (SecurityException e) {
@@ -145,235 +135,178 @@ public final class TypeMapping {
throw new IllegalArgumentException("Failed to init structured maps for "+td.getTypeClass(), e);
} catch (NoSuchFieldException e) {
throw new IllegalArgumentException("Failed to init structured maps for "+td.getTypeClass(), e);
- }
- }
+ }
+ }
- public void addToStructuredMaps (TypeDescription td, String ns) {
- STRUCTURED_TYPES.put(td.getType(),td);
- STRUCTURED_CLASSES.put(td.getTypeClass(), td);
+ public void addToStructuredMaps (TypeDescription td, String ns) {
+ STRUCTURED_TYPES.put(td.getType(),td);
+ STRUCTURED_CLASSES.put(td.getTypeClass(), td);
STRUCTURED_NAMESPACES.put(ns, td);
- }
+ }
+
-
- public String getType (Class<?> clz) {
- // search in basic
- TypeDescription td = BASIC_CLASSES.get(clz);
- // search in derived
- if (td==null) {
- td = DERIVED_CLASSES.get(clz);
- }
- // search in structured
- if (td==null) {
- td = STRUCTURED_CLASSES.get(clz);
- }
- // return type if exists
- return (td!=null)?td.getType():null;
- }
-
- /**
- * Return the type description linked the specified paramater. If the type
- * parameter is an array, the TypeDescription of the elements of the array
- * will be returned
- *
- * @param type
- * @return
- */
- public TypeDescription getTypeDescription (String type) {
- if (BASIC_TYPES.containsKey(type)) {
- return BASIC_TYPES.get(type);
- } else if (DERIVED_TYPES.containsKey(type)) {
- return DERIVED_TYPES.get(type);
- } else if (STRUCTURED_TYPES.containsKey(type)) {
- return STRUCTURED_TYPES.get(type);
- } else {
- int pos = type.indexOf(' ');
- if (pos>0) {
- return getTypeDescription(type.substring(pos+1));
- } else {
- // unknown type
- return null;
- }
- }
- }
-
- public AbstractSimpleProperty instanciateSimpleProperty (XMPMetadata xmp,String nsuri, String prefix, String name, Object value, String type) {
- // constructor parameters
- Object [] params = new Object [] {
- xmp,
- nsuri,
- prefix,
- name,
- value
- };
- // type
- try {
+ public String getType (Class<?> clz) {
+ // search in basic
+ TypeDescription td = BASIC_CLASSES.get(clz);
+ // search in derived
+ if (td==null) {
+ td = DERIVED_CLASSES.get(clz);
+ }
+ // search in structured
+ if (td==null) {
+ td = STRUCTURED_CLASSES.get(clz);
+ }
+ // return type if exists
+ return (td!=null)?td.getType():null;
+ }
+
+ /**
+ * Return the type description linked the specified paramater. If the type
+ * parameter is an array, the TypeDescription of the elements of the array
+ * will be returned
+ *
+ * @param type
+ * @return
+ */
+ public TypeDescription getTypeDescription (String type) {
+ if (BASIC_TYPES.containsKey(type)) {
+ return BASIC_TYPES.get(type);
+ } else if (DERIVED_TYPES.containsKey(type)) {
+ return DERIVED_TYPES.get(type);
+ } else if (STRUCTURED_TYPES.containsKey(type)) {
+ return STRUCTURED_TYPES.get(type);
+ } else {
+ int pos = type.indexOf(' ');
+ if (pos>0) {
+ return getTypeDescription(type.substring(pos+1));
+ } else {
+ // unknown type
+ return null;
+ }
+ }
+ }
+
+ public AbstractStructuredType instanciateStructuredType (XMPMetadata metadata, Class<? extends AbstractStructuredType> propertyTypeClass) throws XmpParsingException {
+ try {
+ Constructor<? extends AbstractStructuredType> construct = propertyTypeClass.getConstructor(new Class<?> [] {XMPMetadata.class});
+ return construct.newInstance(metadata);
+ } catch (InvocationTargetException e) {
+ throw new XmpParsingException("Failed to instanciate structured type : "+propertyTypeClass,e);
+ } catch (IllegalArgumentException e) {
+ throw new XmpParsingException("Failed to instanciate structured type : "+propertyTypeClass,e);
+ } catch (InstantiationException e) {
+ throw new XmpParsingException("Failed to instanciate structured type : "+propertyTypeClass,e);
+ } catch (IllegalAccessException e) {
+ throw new XmpParsingException("Failed to instanciate structured type : "+propertyTypeClass,e);
+ } catch (SecurityException e) {
+ throw new XmpParsingException("Failed to instanciate structured type : "+propertyTypeClass,e);
+ } catch (NoSuchMethodException e) {
+ throw new XmpParsingException("Failed to instanciate structured type : "+propertyTypeClass,e);
+ }
+ }
+
+ public AbstractSimpleProperty instanciateSimpleProperty (XMPMetadata xmp,String nsuri, String prefix, String name, Object value, String type) {
+ // constructor parameters
+ Object [] params = new Object [] {
+ xmp,
+ nsuri,
+ prefix,
+ name,
+ value
+ };
+ // type
+ try {
TypeDescription description = getTypeDescription(type);
Class<? extends AbstractSimpleProperty> clz = (Class<? extends AbstractSimpleProperty>)description.getTypeClass();
Constructor<? extends AbstractSimpleProperty> cons = clz.getConstructor(simplePropertyConstParams);
return cons.newInstance(params);
- } catch (NoSuchMethodError e) {
- throw new IllegalArgumentException("Failed to instanciate property", e);
- } catch (IllegalArgumentException e) {
- throw new IllegalArgumentException("Failed to instanciate property", e);
+ } catch (NoSuchMethodError e) {
+ throw new IllegalArgumentException("Failed to instanciate property", e);
+ } catch (IllegalArgumentException e) {
+ throw new IllegalArgumentException("Failed to instanciate property", e);
} catch (InstantiationException e) {
- throw new IllegalArgumentException("Failed to instanciate property", e);
+ throw new IllegalArgumentException("Failed to instanciate property", e);
} catch (IllegalAccessException e) {
- throw new IllegalArgumentException("Failed to instanciate property", e);
+ throw new IllegalArgumentException("Failed to instanciate property", e);
} catch (InvocationTargetException e) {
- throw new IllegalArgumentException("Failed to instanciate property", e);
+ throw new IllegalArgumentException("Failed to instanciate property", e);
} catch (SecurityException e) {
- throw new IllegalArgumentException("Failed to instanciate property", e);
+ throw new IllegalArgumentException("Failed to instanciate property", e);
} catch (NoSuchMethodException e) {
- throw new IllegalArgumentException("Failed to instanciate property", e);
+ throw new IllegalArgumentException("Failed to instanciate property", e);
}
- }
-
+ }
+
public AbstractSimpleProperty instanciateSimpleField (Class<?> clz, XMPMetadata xmp, String nsuri, String prefix,String propertyName, Object value) {
- Map<String, String> fields = getStructuredTypeFields(clz);
- String fieldName = fields.get(propertyName);
- try {
- Field f= clz.getField(fieldName);
- PropertyType pt = f.getAnnotation(PropertyType.class);
- String simpleType = pt.propertyType();
- return instanciateSimpleProperty(xmp, nsuri, prefix, propertyName, value, simpleType);
- } catch (SecurityException e) {
- throw new IllegalArgumentException("Failed to instanciate",e);
- } catch (NoSuchFieldException e) {
- throw new IllegalArgumentException("Failed to instanciate",e);
- }
+ PropMapping pm = ReflectHelper.initializePropMapping(null, clz);
+ String simpleType = pm.getPropertyType(propertyName);
+ return instanciateSimpleProperty(xmp, nsuri, prefix, propertyName, value, simpleType);
}
-
-
- private static Map<String,String> getStructuredTypeFields (Class<?> clz) throws IllegalArgumentException {
- Map<String,String> result = structuredTypes.get(clz);
- if (result==null) {
- result = new HashMap<String, String>();
- Field [] fields = clz.getFields();
- for (Field field : fields) {
- PropertyType pt = field.getAnnotation(PropertyType.class);
- if (pt!=null) {
- String name = field.getName();
- String value;
- try {
- value = field.get(null).toString();
- result.put(value, name);
- } catch (IllegalAccessException e) {
- throw new IllegalArgumentException("Cannot parse this class", e);
- }
- }
- }
- structuredTypes.put(clz, result);
- }
- return result;
+ public TypeDescription getStructuredTypeName (String namespace) {
+ return STRUCTURED_NAMESPACES.get(namespace);
}
-
+ /**
+ * Check if a namespace used reference a complex basic types (like
+ * Thumbnails)
+ *
+ * @param namespace
+ * The namespace URI to check
+ * @return True if namespace URI is a reference for a complex basic type
+ */
+ public boolean isStructuredTypeNamespace(String namespace) {
+ return STRUCTURED_NAMESPACES.containsKey(namespace);
+ }
-
- public TypeDescription getStructuredTypeName (String namespace) {
- return STRUCTURED_NAMESPACES.get(namespace);
- }
-
- /**
- * Check if a namespace used reference a complex basic types (like
- * Thumbnails)
- *
- * @param namespace
- * The namespace URI to check
- * @return True if namespace URI is a reference for a complex basic type
- */
- public boolean isStructuredTypeNamespace(String namespace) {
- return STRUCTURED_NAMESPACES.containsKey(namespace);
- }
-
-
- public boolean isArrayOfSimpleType (String type) {
- int pos = type.indexOf(' ');
- if (pos<0) {
- // not array
- return false;
- } else {
- String second = type.substring(pos+1);
- return isSimpleType(second);
- }
-
- }
-
- public String getArrayType (String type) {
- int pos = type.indexOf(' ');
- if (pos<0) {
- // not array
- return null;
- } else {
- String first = type.substring(0,pos);
- if (first.equalsIgnoreCase(ArrayProperty.UNORDERED_ARRAY)) {
- return ArrayProperty.UNORDERED_ARRAY;
- } else if (first.equalsIgnoreCase(ArrayProperty.ORDERED_ARRAY)) {
- return ArrayProperty.ORDERED_ARRAY;
- } else if (first.equalsIgnoreCase(ArrayProperty.ALTERNATIVE_ARRAY)) {
- return ArrayProperty.ALTERNATIVE_ARRAY;
- } else {
- // else not an array
- return null;
- }
- }
- }
-
- public boolean isSimpleType(String type) {
- return (BASIC_TYPES.containsKey(type) || DERIVED_TYPES.containsKey(type));
- }
-
- public boolean isStructuredType(String type) {
- 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);
- }
+ public boolean isArrayOfSimpleType (String type) {
+ int pos = type.indexOf(' ');
+ if (pos<0) {
+ // not array
+ return false;
+ } else {
+ String second = type.substring(pos+1);
+ return isSimpleType(second);
+ }
+
+ }
+
+ public String getArrayType (String type) {
+ int pos = type.indexOf(' ');
+ if (pos<0) {
+ // not array
+ return null;
+ } else {
+ String first = type.substring(0,pos);
+ if (first.equalsIgnoreCase(ArrayProperty.UNORDERED_ARRAY)) {
+ return ArrayProperty.UNORDERED_ARRAY;
+ } else if (first.equalsIgnoreCase(ArrayProperty.ORDERED_ARRAY)) {
+ return ArrayProperty.ORDERED_ARRAY;
+ } else if (first.equalsIgnoreCase(ArrayProperty.ALTERNATIVE_ARRAY)) {
+ return ArrayProperty.ALTERNATIVE_ARRAY;
+ } else {
+ // else not an array
+ return null;
}
}
- return propMap;
+ }
+
+ public boolean isSimpleType(String type) {
+ return (BASIC_TYPES.containsKey(type) || DERIVED_TYPES.containsKey(type));
+ }
+
+ public boolean isStructuredType(String type) {
+ return STRUCTURED_TYPES.containsKey(type);
}
private static PropMapping initializePropMapping(String ns,
DefinedStructuredType dst) {
PropMapping propMap = new PropMapping(ns);
for (Entry<String, String> entry: dst.getDefinedProperties().entrySet()) {
- propMap.addNewProperty(entry.getKey(), entry.getValue(), null);
+ propMap.addNewProperty(entry.getKey(), entry.getValue());
}
return propMap;
}
-
+
}
Modified: pdfbox/branches/xmpbox-refactoring/xmpbox/src/test/java/org/apache/padaf/xmpbox/parser/PropMappingTest.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/xmpbox-refactoring/xmpbox/src/test/java/org/apache/padaf/xmpbox/parser/PropMappingTest.java?rev=1376944&r1=1376943&r2=1376944&view=diff
==============================================================================
--- pdfbox/branches/xmpbox-refactoring/xmpbox/src/test/java/org/apache/padaf/xmpbox/parser/PropMappingTest.java (original)
+++ pdfbox/branches/xmpbox-refactoring/xmpbox/src/test/java/org/apache/padaf/xmpbox/parser/PropMappingTest.java Fri Aug 24 14:32:47 2012
@@ -26,7 +26,7 @@ import java.util.List;
import junit.framework.Assert;
-import org.apache.padaf.xmpbox.parser.PropMapping;
+import org.apache.padaf.xmpbox.type.PropMapping;
import org.junit.Before;
import org.junit.Test;
@@ -50,10 +50,10 @@ public class PropMappingTest {
String name = "propName";
String type = "PropType";
- propMap.addNewProperty(name, type, null);
+ propMap.addNewProperty(name, type);
Assert.assertEquals(1, propMap.getPropertiesName().size());
Assert.assertEquals(name, propMap.getPropertiesName().get(0));
- Assert.assertNull(propMap.getPropertyAttributes(name));
+// Assert.assertNull(propMap.getPropertyAttributes(name));
Assert.assertEquals(type, propMap.getPropertyType(name));
}
@@ -62,13 +62,8 @@ public class PropMappingTest {
public void testPropMapAttr() {
String name = "propName";
String type = "PropType";
- List<String> attr = new ArrayList<String>();
- String att1 = "attr1";
- String att2 = "attr2";
- attr.add(att1);
- attr.add(att2);
- propMap.addNewProperty(name, type, attr);
- Assert.assertEquals(attr, propMap.getPropertyAttributes(name));
+ propMap.addNewProperty(name, type);
+// Assert.assertEquals(attr, propMap.getPropertyAttributes(name));
}
}