Author: gbailleul
Date: Thu Aug 30 21:58:28 2012
New Revision: 1379181

URL: http://svn.apache.org/viewvc?rev=1379181&view=rev
Log:
PDFBOX-1343: reduced complexity in type handling helpers

Removed:
    
pdfbox/branches/xmpbox-refactoring/xmpbox/src/main/java/org/apache/padaf/xmpbox/schema/NSMapping.java
    
pdfbox/branches/xmpbox-refactoring/xmpbox/src/main/java/org/apache/padaf/xmpbox/schema/SchemaMapping.java
Modified:
    
pdfbox/branches/xmpbox-refactoring/xmpbox/src/main/java/org/apache/padaf/xmpbox/XMPMetadata.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/type/TypeMapping.java

Modified: 
pdfbox/branches/xmpbox-refactoring/xmpbox/src/main/java/org/apache/padaf/xmpbox/XMPMetadata.java
URL: 
http://svn.apache.org/viewvc/pdfbox/branches/xmpbox-refactoring/xmpbox/src/main/java/org/apache/padaf/xmpbox/XMPMetadata.java?rev=1379181&r1=1379180&r2=1379181&view=diff
==============================================================================
--- 
pdfbox/branches/xmpbox-refactoring/xmpbox/src/main/java/org/apache/padaf/xmpbox/XMPMetadata.java
 (original)
+++ 
pdfbox/branches/xmpbox-refactoring/xmpbox/src/main/java/org/apache/padaf/xmpbox/XMPMetadata.java
 Thu Aug 30 21:58:28 2012
@@ -28,11 +28,9 @@ import java.util.Map;
 import org.apache.padaf.xmpbox.parser.CreateXMPMetadataException;
 import org.apache.padaf.xmpbox.schema.AdobePDFSchema;
 import org.apache.padaf.xmpbox.schema.DublinCoreSchema;
-import org.apache.padaf.xmpbox.schema.NSMapping;
 import org.apache.padaf.xmpbox.schema.PDFAExtensionSchema;
 import org.apache.padaf.xmpbox.schema.PDFAIdentificationSchema;
 import org.apache.padaf.xmpbox.schema.PhotoshopSchema;
-import org.apache.padaf.xmpbox.schema.SchemaMapping;
 import org.apache.padaf.xmpbox.schema.XMPBasicJobTicketSchema;
 import org.apache.padaf.xmpbox.schema.XMPBasicSchema;
 import org.apache.padaf.xmpbox.schema.XMPMediaManagementSchema;
@@ -66,20 +64,14 @@ public class XMPMetadata {
 
        private List<XMPSchema> schemas;
 
-
        private TypeMapping typeMapping;
 
-       private NSMapping nsMapping;
-
-       private SchemaMapping schemaMapping;
-
        /**
         * Contructor of an empty default XMPMetaData
         * 
         * @throws CreateXMPMetadataException
         *             If DOM Document associated could not be created
         */
-       // TODO GBL GBA make protected
        protected XMPMetadata() {
                this (
                                XmpConstants.DEFAULT_XPACKET_BEGIN,
@@ -108,8 +100,6 @@ public class XMPMetadata {
                        String xpacketBytes, String xpacketEncoding) {
                this.schemas = new ArrayList<XMPSchema>();
                this.typeMapping = new TypeMapping(this);
-               this.nsMapping = new NSMapping(this);
-               this.schemaMapping = new SchemaMapping(this);
 
                this.xpacketBegin = xpacketBegin;
                this.xpacketId = xpacketId;
@@ -130,18 +120,6 @@ public class XMPMetadata {
                return this.typeMapping;
        }
 
-       public NSMapping getNsMapping () {
-               return this.nsMapping;
-       }
-
-       public SchemaMapping getSchemaMapping () {
-               return this.schemaMapping;
-       }
-
-       public void setSchemaMapping (SchemaMapping sm) {
-               this.schemaMapping = sm;
-       }
-
        /**
         * Get xpacketBytes
         * 

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=1379181&r1=1379180&r2=1379181&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
 Thu Aug 30 21:58:28 2012
@@ -40,7 +40,6 @@ import javax.xml.stream.XMLStreamReader;
 
 import org.apache.padaf.xmpbox.XMPMetadata;
 import org.apache.padaf.xmpbox.XmpConstants;
-import org.apache.padaf.xmpbox.schema.NSMapping;
 import org.apache.padaf.xmpbox.schema.PDFAExtensionSchema;
 import org.apache.padaf.xmpbox.schema.XMPSchema;
 import org.apache.padaf.xmpbox.schema.XMPSchemaFactory;
@@ -153,20 +152,6 @@ public class XMPDocumentBuilder {
                                        "Did not find initial rdf:RDF");
                        expectName(XmpConstants.RDF_NAMESPACE, "RDF");
 
-                       // TODO GBL GBA maybe not remove
-                       
metadata.getNsMapping().resetComplexBasicTypesDeclarationInEntireXMPLevel();
-                       // add all namespaces which could declare nsURI of a 
basicValueType
-                       // all others declarations are ignored
-                       int nsCount = reader.get().getNamespaceCount();
-                       // TODO MUTUALIZE namespace list loading
-                       for (int i = 0; i < nsCount; i++) {
-                               if 
(metadata.getTypeMapping().isStructuredTypeNamespace(reader.get().getNamespaceURI(i)))
 {
-                                       
metadata.getNsMapping().setComplexBasicTypesDeclarationForLevelXMP(
-                                                       
reader.get().getNamespaceURI(i), 
-                                                       
reader.get().getNamespacePrefix(i));
-                               }
-                       }
-
                        // now work on each rdf:Description
                        int type = reader.get().nextTag();
                        while (type == XMLStreamReader.START_ELEMENT) {
@@ -224,12 +209,12 @@ public class XMPDocumentBuilder {
                                                String prefix = st.getPrefix();
                                                ArrayProperty properties = 
st.getProperty();
                                                ArrayProperty valueTypes = 
st.getValueType();
-                                               XMPSchemaFactory xsf = 
meta.getSchemaMapping().getSchemaFactory(namespaceUri);
+                                               XMPSchemaFactory xsf = 
meta.getTypeMapping().getSchemaFactory(namespaceUri);
                                                // retrieve namespaces
                                                if (xsf==null) {
                                                        // create namespace 
with no field
-                                                       
meta.getSchemaMapping().addNewNameSpace(namespaceUri,prefix);
-                                                       xsf = 
meta.getSchemaMapping().getSchemaFactory(namespaceUri);
+                                                       
meta.getTypeMapping().addNewNameSpace(namespaceUri,prefix);
+                                                       xsf = 
meta.getTypeMapping().getSchemaFactory(namespaceUri);
                                                }
                                                // populate value type
                                                if (valueTypes!=null) {
@@ -598,8 +583,8 @@ public class XMPDocumentBuilder {
        private boolean addAttributeAsProperty(XMPMetadata metadata, XMPSchema 
schema, Attribute attr) {
                boolean added = false;
                String schemaNamespace = schema.getNamespace();
-               String prefix = /*attr.getPrefix() != null ? attr.getPrefix() 
:*/ schema.getPrefix();
-               String type = 
metadata.getNsMapping().getSpecifiedPropertyType(schemaNamespace, new 
QName(schemaNamespace, attr.getLocalName()));
+               String prefix = schema.getPrefix();
+               String type = 
metadata.getTypeMapping().getSpecifiedPropertyType(schemaNamespace, new 
QName(schemaNamespace, attr.getLocalName()));
 
                if (type != null) {
                        AbstractSimpleProperty prop = 
metadata.getTypeMapping().instanciateSimpleProperty(null, prefix, 
attr.getLocalName(), attr.getValue(), type);
@@ -634,30 +619,23 @@ public class XMPDocumentBuilder {
                        throws XmpParsingException, XMLStreamException, 
XmpSchemaException,
                        XmpUnknownValueTypeException, 
XmpExpectedRdfAboutAttribute,
                        BadFieldValueException {
-               NSMapping nsMap = metadata.getNsMapping();
-               nsMap.resetComplexBasicTypesDeclarationInSchemaLevel();
+               TypeMapping sm = metadata.getTypeMapping();
                int cptNS = reader.get().getNamespaceCount();
                HashMap<String, String> namespaces = new HashMap<String, 
String>();
-               // TODO MUTUALIZE namespace list loading
                for (int i = 0; i < cptNS; i++) {
                        namespaces.put(reader.get().getNamespacePrefix(i), 
reader.get()
                                        .getNamespaceURI(i));
-                       if 
(metadata.getTypeMapping().isStructuredTypeNamespace(reader.get().getNamespaceURI(i)))
 {
-                               
nsMap.setComplexBasicTypesDeclarationForLevelSchema(reader
-                                               .get().getNamespaceURI(i), 
reader.get()
-                                               .getNamespacePrefix(i));
-                       }
                }
                int c = 0;
                String namespaceUri = reader.get().getNamespaceURI(c);
                String namespacePrefix = reader.get().getNamespacePrefix(c);
                c++;
-               XMPSchema schema = nsMap.getAssociatedSchemaObject(metadata, 
namespaceUri, namespacePrefix);
+               XMPSchema schema = sm.getAssociatedSchemaObject(metadata, 
namespaceUri, namespacePrefix);
                while (c<reader.get().getNamespaceCount() && schema==null) {
                        // try next
                        namespaceUri = reader.get().getNamespaceURI(c);
                        namespacePrefix = reader.get().getNamespacePrefix(c);
-                       schema = nsMap.getAssociatedSchemaObject(metadata, 
namespaceUri, namespacePrefix);
+                       schema = sm.getAssociatedSchemaObject(metadata, 
namespaceUri, namespacePrefix);
                        c++;
                }
 
@@ -731,18 +709,18 @@ public class XMPDocumentBuilder {
         */
        private String getPropertyDeclarationInNamespaces(XMPSchema schema,
                        QName prop) throws XmpParsingException {
-               NSMapping nsMap = schema.getMetadata().getNsMapping();
+               TypeMapping sm = schema.getMetadata().getTypeMapping();
                Iterator<Entry<String, String>> it = 
schema.getAllNamespacesWithPrefix().entrySet().iterator();
                String type;
                StringBuilder unknownNS = new StringBuilder();
                while (it.hasNext()) {
                        Entry<String,String> entry = it.next();
                        String namespace = entry.getKey();
-                       if (!nsMap.isContainedNamespace(namespace)) {
+                       if (!sm.isContainedNamespace(namespace)) {
                                unknownNS.append(" 
'").append(namespace).append("' ");
                                continue;
                        }
-                       type = nsMap.getSpecifiedPropertyType(namespace, prop);
+                       type = sm.getSpecifiedPropertyType(namespace, prop);
                        if (type != null) {
                                return type;
                        }
@@ -871,7 +849,7 @@ public class XMPDocumentBuilder {
                        XmpUnexpectedTypeException, XMLStreamException,
                        XmpUnknownPropertyTypeException {
                XMPMetadata metadata = schema.getMetadata();
-               NSMapping nsMap = metadata.getNsMapping();
+//             NSMapping nsMap = metadata.getNsMapping();
                TypeMapping typeMapping = metadata.getTypeMapping();
                QName propertyName = reader.get().getName();
                if (parsingExtension) {
@@ -882,16 +860,6 @@ public class XMPDocumentBuilder {
                                return;
                        }
                }
-               nsMap.resetComplexBasicTypesDeclarationInPropertyLevel();
-               int cptNs = reader.get().getNamespaceCount();
-               // TODO MUTUALIZE namespace list loading
-               for (int i = 0; i < cptNs; i++) {
-                       if 
(typeMapping.isStructuredTypeNamespace(reader.get().getNamespaceURI(i))) {
-                               
nsMap.setComplexBasicTypesDeclarationForLevelSchema(reader
-                                               .get().getNamespaceURI(i), 
reader.get()
-                                               .getNamespacePrefix(i));
-                       }
-               }
                String type = getPropertyDeclarationInNamespaces(schema, 
propertyName);
                // found type, manage it
                if (type.equals("Lang Alt")) {
@@ -909,10 +877,6 @@ public class XMPDocumentBuilder {
                        int pos = type.indexOf(' ');
                        String arrayType = typeMapping.getArrayType(type);
                        String typeInArray = type.substring(pos+1);
-//                     if (typeMapping.is)
-//             TypeDescription<AbstractStructuredType> tclass = 
typeMapping.getTypeDescription(typeInArray);
-//                     Class<? extends AbstractField> tcn = 
tclass.getTypeClass();
-
                        if (typeMapping.isSimpleType(typeInArray)) {
                                // array of simple
                                parseSimplePropertyArray(

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=1379181&r1=1379180&r2=1379181&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
 Thu Aug 30 21:58:28 2012
@@ -27,7 +27,21 @@ import java.util.HashMap;
 import java.util.Map;
 import java.util.Map.Entry;
 
+import javax.xml.namespace.QName;
+
 import org.apache.padaf.xmpbox.XMPMetadata;
+import org.apache.padaf.xmpbox.schema.AdobePDFSchema;
+import org.apache.padaf.xmpbox.schema.DublinCoreSchema;
+import org.apache.padaf.xmpbox.schema.PDFAExtensionSchema;
+import org.apache.padaf.xmpbox.schema.PDFAIdentificationSchema;
+import org.apache.padaf.xmpbox.schema.PhotoshopSchema;
+import org.apache.padaf.xmpbox.schema.XMPBasicJobTicketSchema;
+import org.apache.padaf.xmpbox.schema.XMPBasicSchema;
+import org.apache.padaf.xmpbox.schema.XMPMediaManagementSchema;
+import org.apache.padaf.xmpbox.schema.XMPRightsManagementSchema;
+import org.apache.padaf.xmpbox.schema.XMPSchema;
+import org.apache.padaf.xmpbox.schema.XMPSchemaFactory;
+import org.apache.padaf.xmpbox.schema.XmpSchemaException;
 import org.apache.padaf.xmpbox.type.TypeDescription.BasicType;
 
 public final class TypeMapping {
@@ -48,8 +62,13 @@ public final class TypeMapping {
 
        private Map<String,TypeDescription<AbstractStructuredType>> 
STRUCTURED_NAMESPACES;
        
+       private Map<String, String> schemaUriToPrefered;
+
        private XMPMetadata metadata;
 
+       private Map<String, XMPSchemaFactory> schemaMap;
+
+
        public TypeMapping(XMPMetadata metadata) {
                this.metadata = metadata;
                initialize();
@@ -105,6 +124,20 @@ public final class TypeMapping {
                addToStructuredMaps(new 
TypeDescription<AbstractStructuredType>("PDFAProperty",null,PDFAPropertyType.class));
                addToStructuredMaps(new 
TypeDescription<AbstractStructuredType>("PDFAType",null,PDFATypeType.class));
                addToStructuredMaps(new 
TypeDescription<AbstractStructuredType>("PDFASchema",null,PDFASchemaType.class));
+               
+               // schema
+               schemaUriToPrefered = new HashMap<String, String>();
+               schemaMap = new HashMap<String, XMPSchemaFactory>();
+               addNameSpace("http://ns.adobe.com/xap/1.0/";, 
XMPBasicSchema.class);
+               addNameSpace(DublinCoreSchema.DCURI, DublinCoreSchema.class);
+               addNameSpace("http://www.aiim.org/pdfa/ns/extension/";, 
PDFAExtensionSchema.class);
+               addNameSpace("http://ns.adobe.com/xap/1.0/mm/";, 
XMPMediaManagementSchema.class);
+               addNameSpace("http://ns.adobe.com/pdf/1.3/";, 
AdobePDFSchema.class);
+               addNameSpace("http://www.aiim.org/pdfa/ns/id/";, 
PDFAIdentificationSchema.class);
+               addNameSpace("http://ns.adobe.com/xap/1.0/rights/";,     
XMPRightsManagementSchema.class);
+               addNameSpace(PhotoshopSchema.PHOTOSHOPURI,      
PhotoshopSchema.class);
+               
addNameSpace(XMPBasicJobTicketSchema.JOB_TICKET_URI,XMPBasicJobTicketSchema.class);
+
        }
 
        private void addToBasicMaps (TypeDescription<AbstractSimpleProperty> 
td) {
@@ -336,4 +369,88 @@ public final class TypeMapping {
                return propMap;
        }
 
+       private void addNameSpace(String ns, Class<? extends XMPSchema> 
classSchem) {
+               schemaMap.put(ns, new XMPSchemaFactory(ns, classSchem,  
ReflectHelper.initializePropMapping(ns, classSchem)));
+               try {
+                       schemaUriToPrefered.put(ns, 
classSchem.getField("PREFERED_PREFIX").get(null).toString());
+               } catch (IllegalArgumentException e) {
+                       throw new IllegalArgumentException("Failed to init 
'"+ns+"'", e);
+               } catch (SecurityException e) {
+                       throw new IllegalArgumentException("Failed to init 
'"+ns+"'", e);
+               } catch (IllegalAccessException e) {
+                       throw new IllegalArgumentException("Failed to init 
'"+ns+"'", e);
+               } catch (NoSuchFieldException e) {
+                       throw new IllegalArgumentException("Failed to init 
'"+ns+"'", e);
+               }
+       }
+
+       public void addNewNameSpace(String ns,String prefered) {
+               PropMapping mapping = new PropMapping(ns);
+               schemaMap.put(ns, new XMPSchemaFactory(ns, XMPSchema.class, 
mapping));
+               schemaUriToPrefered.put(ns, prefered);
+       }
+
+       /**
+        * Return the specialized schema class representation if it's known 
(create
+        * and add it to metadata). In other cases, return null
+        * 
+        * @param metadata
+        *            Metadata to link the new schema
+        * @param namespace
+        *            The namespace URI
+        * @return Schema representation
+        * @throws XmpSchemaException
+        *             When Instancing specified Object Schema failed
+        */
+       public XMPSchema getAssociatedSchemaObject(XMPMetadata metadata, String 
namespace, String prefix) throws XmpSchemaException {
+               if (schemaMap.containsKey(namespace)) {
+                       XMPSchemaFactory factory = schemaMap.get(namespace);
+                       return factory.createXMPSchema(metadata, prefix);
+               } else {
+                       XMPSchemaFactory factory = getSchemaFactory(namespace);
+                       return factory!=null?factory.createXMPSchema(metadata, 
prefix):null;
+               }
+       }
+
+       public XMPSchemaFactory getSchemaFactory(String namespace) {
+               return schemaMap.get(namespace);
+       }
+
+       /**
+        * Say if a specific namespace is known
+        * 
+        * @param namespace
+        *            The namespace URI checked
+        * @return True if namespace URI is known
+        */
+       public boolean isContainedNamespace(String namespace) {
+               boolean found = schemaMap.containsKey(namespace);
+               if (!found) {
+                       found = isStructuredTypeNamespace(namespace);
+               }
+               return found;
+       }
+
+       /**
+        * Give type of specified property in specified schema (given by its
+        * namespaceURI)
+        * 
+        * @param namespace
+        *            The namespaceURI to explore
+        * @param prop
+        *            the property Qualified Name
+        * @return Property type declared for namespace specified, null if 
unknown
+        */
+       public String getSpecifiedPropertyType(String namespace, QName prop) {
+               XMPSchemaFactory factory =getSchemaFactory(namespace);
+               if (factory!=null) {
+                       // found in schema
+                       return factory.getPropertyType(prop.getLocalPart());
+               } else {
+                       TypeDescription<AbstractStructuredType> td = 
getStructuredTypeName(prop.getPrefix());
+                       return td==null?null:td.getType();
+               }
+       }
+
+       
 }


Reply via email to