You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xmlgraphics.apache.org by je...@apache.org on 2008/08/11 10:55:58 UTC

svn commit: r684677 - in /xmlgraphics/commons/trunk: ./ src/java/org/apache/xmlgraphics/xmp/ test/java/org/apache/xmlgraphics/xmp/

Author: jeremias
Date: Mon Aug 11 01:55:56 2008
New Revision: 684677

URL: http://svn.apache.org/viewvc?rev=684677&view=rev
Log:
XMP framework: Added support for the structure property shorthand form and for specifying simple property values as attributes on rdf:Description elements.

Added:
    xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/xmp/test-attribute-values.xmp
Modified:
    xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/xmp/XMPHandler.java
    xmlgraphics/commons/trunk/status.xml
    xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/xmp/XMPParserTest.java
    xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/xmp/test-structures.xmp

Modified: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/xmp/XMPHandler.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/xmp/XMPHandler.java?rev=684677&r1=684676&r2=684677&view=diff
==============================================================================
--- xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/xmp/XMPHandler.java (original)
+++ xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/xmp/XMPHandler.java Mon Aug 11 01:55:56 2008
@@ -35,18 +35,12 @@
 public class XMPHandler extends DefaultHandler {
 
     private Metadata meta;
-    
+
     private StringBuffer content = new StringBuffer();
-    //private Attributes lastAttributes;
     private Stack attributesStack = new Stack();
     private Stack nestingInfoStack = new Stack();
     private Stack contextStack = new Stack();
-    
-    //private QName currentPropertyName;
-    //private XMPProperty currentProperty;
-    //private XMPComplexValue currentComplexValue;
-    //private PropertyAccess currentProperties;
-    
+
     /** @return the parsed metadata, available after the parsing. */
     public Metadata getMetadata() {
         return this.meta;
@@ -60,7 +54,7 @@
             return true;
         }
     }
-    
+
     private PropertyAccess getCurrentProperties() {
         Object obj = this.contextStack.peek();
         if (obj instanceof PropertyAccess) {
@@ -69,7 +63,7 @@
             return null;
         }
     }
-    
+
     private QName getCurrentPropName() {
         Object obj = this.contextStack.peek();
         if (obj instanceof QName) {
@@ -78,7 +72,7 @@
             return null;
         }
     }
-    
+
     private QName popCurrentPropName() throws SAXException {
         Object obj = this.contextStack.pop();
         this.nestingInfoStack.pop();
@@ -88,7 +82,7 @@
             throw new SAXException("Invalid XMP structure. Property name expected");
         }
     }
-    
+
     private XMPComplexValue getCurrentComplexValue() {
         Object obj = this.contextStack.peek();
         if (obj instanceof XMPComplexValue) {
@@ -97,7 +91,7 @@
             return null;
         }
     }
-    
+
     private XMPStructure getCurrentStructure() {
         Object obj = this.contextStack.peek();
         if (obj instanceof XMPStructure) {
@@ -106,7 +100,7 @@
             return null;
         }
     }
-    
+
     private XMPArray getCurrentArray(boolean required) throws SAXException {
         Object obj = this.contextStack.peek();
         if (obj instanceof XMPArray) {
@@ -121,17 +115,14 @@
     }
 
     // --- Overrides ---
-    
-    /**
-     * @see org.xml.sax.helpers.DefaultHandler#startElement(
-     *      java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
-     */
-    public void startElement(String uri, String localName, String qName, Attributes attributes) 
+
+    /** {@inheritDoc} */
+    public void startElement(String uri, String localName, String qName, Attributes attributes)
                 throws SAXException {
         super.startElement(uri, localName, qName, attributes);
         content.setLength(0); //Reset text buffer (see characters())
         attributesStack.push(new AttributesImpl(attributes));
-        
+
         if (XMPConstants.XMP_NAMESPACE.equals(uri)) {
             if (!"xmpmeta".equals(localName)) {
                 throw new SAXException("Expected x:xmpmeta element, not " + qName);
@@ -140,19 +131,30 @@
                 throw new SAXException("Invalid XMP document. Root already received earlier.");
             }
             this.meta = new Metadata();
-            //this.currentProperties = this.meta;
             this.contextStack.push(this.meta);
             this.nestingInfoStack.push("metadata");
         } else if (XMPConstants.RDF_NAMESPACE.equals(uri)) {
             if ("RDF".equals(localName)) {
                 if (this.meta == null) {
                     this.meta = new Metadata();
-                    //this.currentProperties = this.meta;
                     this.contextStack.push(this.meta);
                     this.nestingInfoStack.push("metadata");
                 }
             } else if ("Description".equals(localName)) {
                 String about = attributes.getValue(XMPConstants.RDF_NAMESPACE, "about");
+                for (int i = 0, c = attributes.getLength(); i < c; i++) {
+                    String ns = attributes.getURI(i);
+                    if (XMPConstants.RDF_NAMESPACE.equals(ns)) {
+                        //ignore
+                    } else if ("".equals(ns)) {
+                        //ignore
+                    } else {
+                        String qn = attributes.getQName(i);
+                        String v = attributes.getValue(i);
+                        XMPProperty prop = new XMPProperty(new QName(ns, qn), v);
+                        getCurrentProperties().setProperty(prop);
+                    }
+                }
                 if (this.contextStack.peek().equals(this.meta)) {
                     //rdf:RDF is the parent
                 } else {
@@ -160,26 +162,18 @@
                         throw new SAXException(
                                 "Nested rdf:Description elements may not have an about property");
                     }
-                    //a structured property is the parent
-                    XMPStructure struct = new XMPStructure();
-                    //this.currentComplexValue = struct;
-                    this.contextStack.push(struct);
-                    //this.currentProperties = struct;
-                    this.nestingInfoStack.push("struct");
+                    startStructure();
                 }
             } else if ("Seq".equals(localName)) {
                 XMPArray array = new XMPArray(XMPArrayType.SEQ);
-                //this.currentComplexValue = array;
                 this.contextStack.push(array);
                 this.nestingInfoStack.push("Seq");
             } else if ("Bag".equals(localName)) {
                 XMPArray array = new XMPArray(XMPArrayType.BAG);
-                //this.currentComplexValue = array;
                 this.contextStack.push(array);
                 this.nestingInfoStack.push("Bag");
             } else if ("Alt".equals(localName)) {
                 XMPArray array = new XMPArray(XMPArrayType.ALT);
-                //this.currentComplexValue = array;
                 this.contextStack.push(array);
                 this.nestingInfoStack.push("Alt");
             } else if ("li".equals(localName)) {
@@ -192,16 +186,24 @@
                 throw new SAXException("Unexpected element in the RDF namespace: " + localName);
             }
         } else {
+            if (getCurrentPropName() != null) {
+                //Structure (shorthand form)
+                startStructure();
+            }
             QName name = new QName(uri, qName);
             this.contextStack.push(name);
             this.nestingInfoStack.push("prop:" + name);
         }
     }
-    
-    /**
-     * @see org.xml.sax.helpers.DefaultHandler#endElement(
-     *      java.lang.String, java.lang.String, java.lang.String)
-     */
+
+    private void startStructure() {
+        //a structured property is the parent
+        XMPStructure struct = new XMPStructure();
+        this.contextStack.push(struct);
+        this.nestingInfoStack.push("struct");
+    }
+
+    /** {@inheritDoc} */
     public void endElement(String uri, String localName, String qName) throws SAXException {
         Attributes atts = (Attributes)attributesStack.pop();
         if (XMPConstants.XMP_NAMESPACE.equals(uri)) {
@@ -211,7 +213,7 @@
                 XMPStructure struct = getCurrentStructure();
                 if (struct != null) {
                     //Pop the structure
-                    Object obj = this.contextStack.pop();
+                    this.contextStack.pop();
                     this.nestingInfoStack.pop();
                     getCurrentArray(true).add(struct);
                 } else {
@@ -234,8 +236,6 @@
                 }*/
             } else {
                 //nop, don't pop stack so the parent element has access
-                //this.contextStack.pop();
-                //this.nestingInfoStack.pop();
             }
         } else {
             XMPProperty prop;
@@ -244,9 +244,9 @@
                 //Pop content of property
                 Object obj = this.contextStack.pop();
                 this.nestingInfoStack.pop();
-                
+
                 name = popCurrentPropName();
-                
+
                 if (obj instanceof XMPComplexValue) {
                     XMPComplexValue complexValue = (XMPComplexValue)obj;
                     prop = new XMPProperty(name, complexValue);
@@ -266,10 +266,8 @@
             if (prop.getName() == null) {
                 throw new IllegalStateException("No content in XMP property");
             }
+            assert getCurrentProperties() != null : "no current property";
             getCurrentProperties().setProperty(prop);
-            //this.currentProperties.setProperty(this.currentProperty);
-            //this.currentProperty = null;
-            //this.currentPropertyName = null;
         }
 
         content.setLength(0); //Reset text buffer (see characters())
@@ -282,7 +280,7 @@
     }
     */
 
-    /** @see org.xml.sax.ContentHandler#characters(char[], int, int) */
+    /** {@inheritDoc} */
     public void characters(char[] ch, int start, int length) throws SAXException {
         content.append(ch, start, length);
     }

Modified: xmlgraphics/commons/trunk/status.xml
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/status.xml?rev=684677&r1=684676&r2=684677&view=diff
==============================================================================
--- xmlgraphics/commons/trunk/status.xml (original)
+++ xmlgraphics/commons/trunk/status.xml Mon Aug 11 01:55:56 2008
@@ -41,6 +41,10 @@
   <changes>
     <release version="Trunk" date="n/a">
       <action context="Code" dev="JM" type="add">
+        XMP framework: Added support for the structure property shorthand form and for
+        specifying simple property values as attributes on rdf:Description elements.
+      </action>
+      <action context="Code" dev="JM" type="add">
         Added RefinedImageFlavor to the image loading framework for better refinement
         of image flavors.
       </action>

Modified: xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/xmp/XMPParserTest.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/xmp/XMPParserTest.java?rev=684677&r1=684676&r2=684677&view=diff
==============================================================================
--- xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/xmp/XMPParserTest.java (original)
+++ xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/xmp/XMPParserTest.java Mon Aug 11 01:55:56 2008
@@ -38,11 +38,11 @@
     public void testParseBasics() throws Exception {
         URL url = getClass().getResource("test-basics.xmp");
         Metadata meta = XMPParser.parseXMP(url);
-        
+
         DublinCoreAdapter dcAdapter = DublinCoreSchema.getAdapter(meta);
         XMPBasicAdapter basicAdapter = XMPBasicSchema.getAdapter(meta);
         AdobePDFAdapter pdfAdapter = AdobePDFSchema.getAdapter(meta);
-        
+
         XMPProperty prop;
         prop = meta.getProperty(XMPConstants.DUBLIN_CORE_NAMESPACE, "creator");
         XMPArray array;
@@ -50,7 +50,7 @@
         assertEquals(1, array.getSize());
         assertEquals("John Doe", array.getValue(0).toString());
         assertEquals("John Doe", dcAdapter.getCreators()[0]);
-               
+
         prop = meta.getProperty(XMPConstants.DUBLIN_CORE_NAMESPACE, "title");
         assertEquals("Example document", prop.getValue().toString());
         assertEquals("Example document", dcAdapter.getTitle());
@@ -66,34 +66,34 @@
         assertEquals("1.4", prop.getValue().toString());
         assertEquals("1.4", pdfAdapter.getPDFVersion());
     }
-    
+
     public void testParse1() throws Exception {
         URL url = getClass().getResource("unknown-schema.xmp");
         Metadata meta = XMPParser.parseXMP(url);
-        
+
         DublinCoreAdapter dcAdapter = DublinCoreSchema.getAdapter(meta);
-        
+
         XMPProperty prop;
         //Access through the known schema as reference
         prop = meta.getProperty(XMPConstants.DUBLIN_CORE_NAMESPACE, "title");
         assertEquals("Unknown Schema", prop.getValue().toString());
         assertEquals("Unknown Schema", dcAdapter.getTitle());
-        
+
         //Access through a schema unknown to the XMP framework
         prop = meta.getProperty("http://unknown.org/something", "dummy");
         assertEquals("Dummy!", prop.getValue().toString());
     }
-    
+
     public void testParseStructures() throws Exception {
         URL url = getClass().getResource("test-structures.xmp");
         Metadata meta = XMPParser.parseXMP(url);
-        
+
         XMPProperty prop;
-        
+
         String testns = "http://foo.bar/test/";
         prop = meta.getProperty(testns, "something");
         assertEquals("blablah", prop.getValue().toString());
-        
+
         prop = meta.getProperty(testns, "ingredients");
         XMPArray array = prop.getArrayValue();
         assertEquals(3, array.getSize());
@@ -103,14 +103,30 @@
         assertEquals("Apples", prop.getValue());
         prop = struct.getProperty(testns, "amount");
         assertEquals("4", prop.getValue());
-        
+
         prop = meta.getProperty(testns, "villain");
         XMPProperty prop1;
         prop1 = prop.getStructureValue().getProperty(testns, "name");
         assertEquals("Darth Sidious", prop1.getValue());
         prop1 = prop.getStructureValue().getProperty(testns, "other-name");
         assertEquals("Palpatine", prop1.getValue());
-        
+
+        //Test shorthand form
+        prop = meta.getProperty(testns, "project");
+        prop1 = prop.getStructureValue().getProperty(testns, "name");
+        assertEquals("Apache XML Graphics", prop1.getValue());
+        prop1 = prop.getStructureValue().getProperty(testns, "url");
+        assertEquals("http://xmlgraphics.apache.org/", prop1.getValue());
+
+    }
+
+    public void testAttributeValues() throws Exception {
+        URL url = getClass().getResource("test-attribute-values.xmp");
+        Metadata meta = XMPParser.parseXMP(url);
+
+        DublinCoreAdapter dcAdapter = DublinCoreSchema.getAdapter(meta);
+        assertEquals("Ender's Game", dcAdapter.getTitle());
+        assertEquals("Orson Scott Card", dcAdapter.getCreators()[0]);
     }
-    
+
 }

Added: xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/xmp/test-attribute-values.xmp
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/xmp/test-attribute-values.xmp?rev=684677&view=auto
==============================================================================
--- xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/xmp/test-attribute-values.xmp (added)
+++ xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/xmp/test-attribute-values.xmp Mon Aug 11 01:55:56 2008
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<x:xmpmeta xmlns:x="adobe:ns:meta/">
+  <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
+    <rdf:Description xmlns:dc="http://purl.org/dc/elements/1.1/"
+          xmlns:test="http://foo.bar/test" rdf:about=""
+          dc:title="Ender's Game" dc:creator="Orson Scott Card"/>
+  </rdf:RDF>
+</x:xmpmeta>

Modified: xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/xmp/test-structures.xmp
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/xmp/test-structures.xmp?rev=684677&r1=684676&r2=684677&view=diff
==============================================================================
--- xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/xmp/test-structures.xmp (original)
+++ xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/xmp/test-structures.xmp Mon Aug 11 01:55:56 2008
@@ -35,6 +35,10 @@
           <test:other-name>Palpatine</test:other-name>
         </rdf:Description>
       </test:villain>
+      <test:project> <!-- shorthand form for structure -->
+        <test:name>Apache XML Graphics</test:name>
+        <test:url>http://xmlgraphics.apache.org/</test:url>
+      </test:project>
     </rdf:Description>
   </rdf:RDF>
 </x:xmpmeta>



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: commits-help@xmlgraphics.apache.org