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 2006/06/20 16:31:41 UTC

svn commit: r415656 - in /xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/xmp: XMPSchemaAdapter.java schemas/DublinCoreAdapter.java schemas/DublinCoreSchema.java schemas/XMPBasicAdapter.java

Author: jeremias
Date: Tue Jun 20 07:31:40 2006
New Revision: 415656

URL: http://svn.apache.org/viewvc?rev=415656&view=rev
Log:
Read access for date values. How many times have I missed proper ISO 8601 date/time support in my life as a developer....
Some nits.

Modified:
    xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/xmp/XMPSchemaAdapter.java
    xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/xmp/schemas/DublinCoreAdapter.java
    xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/xmp/schemas/DublinCoreSchema.java
    xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/xmp/schemas/XMPBasicAdapter.java

Modified: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/xmp/XMPSchemaAdapter.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/xmp/XMPSchemaAdapter.java?rev=415656&r1=415655&r2=415656&view=diff
==============================================================================
--- xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/xmp/XMPSchemaAdapter.java (original)
+++ xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/xmp/XMPSchemaAdapter.java Tue Jun 20 07:31:40 2006
@@ -19,9 +19,11 @@
 package org.apache.xmlgraphics.xmp;
 
 import java.text.DateFormat;
+import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.Calendar;
 import java.util.Date;
+import java.util.TimeZone;
 
 import org.apache.xmlgraphics.util.QName;
 
@@ -33,6 +35,10 @@
     private static DateFormat pseudoISO8601DateFormat = new SimpleDateFormat(
                                                             "yyyy'-'MM'-'dd'T'HH':'mm':'ss");
 
+    static {
+        pseudoISO8601DateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
+    }
+    
     /** the Metadata object this schema instance operates on */
     protected Metadata meta;
     private XMPSchema schema;
@@ -113,11 +119,15 @@
      */
     public static String formatISO8601Date(Date dt) {
         //ISO 8601 cannot be expressed directly using SimpleDateFormat
-        StringBuffer sb = new StringBuffer(pseudoISO8601DateFormat.format(dt));
         Calendar cal = Calendar.getInstance();
         cal.setTime(dt);
         int offset = cal.get(Calendar.ZONE_OFFSET);
         offset += cal.get(Calendar.DST_OFFSET);
+        
+        //DateFormat is operating on GMT so adjust for time zone offset
+        Date dt1 = new Date(dt.getTime() + offset);
+        StringBuffer sb = new StringBuffer(pseudoISO8601DateFormat.format(dt1));
+
         offset /= (1000 * 60); //Convert to minutes
         
         if (offset == 0) {
@@ -145,6 +155,45 @@
     }
     
     /**
+     * Parses an ISO 8601 date and time value.
+     * @param dt the date and time value as an ISO 8601 string
+     * @return the parsed date/time
+     * @todo Parse formats other than yyyy-mm-ddThh:mm:ssZ
+     */
+    public static Date parseISO8601Date(final String dt) {
+        int offset = 0;
+        String parsablePart;
+        if (dt.endsWith("Z")) {
+            parsablePart = dt.substring(0, dt.length() - 1);
+        } else {
+            int pos;
+            int neg = 1;
+            pos = dt.lastIndexOf('+');
+            if (pos < 0) {
+                pos = dt.lastIndexOf('-');
+                neg = -1;
+            }
+            if (pos >= 0) {
+                String timeZonePart = dt.substring(pos);
+                parsablePart = dt.substring(0, pos);
+                offset = Integer.parseInt(timeZonePart.substring(1, 3)) * 60;
+                offset += Integer.parseInt(timeZonePart.substring(4, 6));
+                offset *= neg;
+            } else {
+                parsablePart = dt;
+            }
+        }
+        Date d;
+        try {
+            d = pseudoISO8601DateFormat.parse(parsablePart);
+        } catch (ParseException e) {
+            throw new IllegalArgumentException("Invalid ISO 8601 date format: " + dt);
+        }
+        d.setTime(d.getTime() - offset * 60 * 1000);
+        return d;
+    }
+
+    /**
      * Adds a date value to an ordered array.
      * @param propName the property name
      * @param value the date value
@@ -165,6 +214,20 @@
     }
 
     /**
+     * Returns a date value.
+     * @param propName the property name
+     * @return the date value or null if the value is not set
+     */
+    protected Date getDateValue(String propName) {
+        String dt = getValue(propName);
+        if (dt == null) {
+            return null;
+        } else {
+            return parseISO8601Date(dt);
+        }
+    }
+    
+    /**
      * Sets a language-dependent value.
      * @param propName the property name
      * @param lang the language ("x-default" or null for the default language)
@@ -266,10 +329,13 @@
     /**
      * Returns an object array representation of the property's values.
      * @param propName the property name
-     * @return the object array
+     * @return the object array or null if the property isn't set
      */
     protected Object[] getObjectArray(String propName) {
         XMPProperty prop = meta.getProperty(getQName(propName));
+        if (prop == null) {
+            return null;
+        }
         XMPArray array = prop.getArrayValue();
         if (array != null) {
             return array.toObjectArray();
@@ -282,10 +348,13 @@
      * Returns a String array representation of the property's values. Complex values to converted
      * to Strings using the toString() method.
      * @param propName the property name
-     * @return the String array
+     * @return the String array or null if the property isn't set
      */
     protected String[] getStringArray(String propName) {
         Object[] arr = getObjectArray(propName);
+        if (arr == null) {
+            return null;
+        }
         String[] res = new String[arr.length];
         for (int i = 0, c = res.length; i < c; i++) {
             res[i] = arr[i].toString();

Modified: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/xmp/schemas/DublinCoreAdapter.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/xmp/schemas/DublinCoreAdapter.java?rev=415656&r1=415655&r2=415656&view=diff
==============================================================================
--- xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/xmp/schemas/DublinCoreAdapter.java (original)
+++ xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/xmp/schemas/DublinCoreAdapter.java Tue Jun 20 07:31:40 2006
@@ -25,7 +25,10 @@
 import org.apache.xmlgraphics.xmp.XMPSchemaRegistry;
 
 /**
- * Schema adapter implementation for the Dublin Core schema. 
+ * Schema adapter implementation for the Dublin Core schema.
+ * <p>
+ * Note: In Adobe's XMP specification dc:subject is defined as "bag Text", but in PDF/A-1 it is
+ * defined as "Text". Here it is implemented as "bag Text". 
  */
 public class DublinCoreAdapter extends XMPSchemaAdapter {
 

Modified: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/xmp/schemas/DublinCoreSchema.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/xmp/schemas/DublinCoreSchema.java?rev=415656&r1=415655&r2=415656&view=diff
==============================================================================
--- xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/xmp/schemas/DublinCoreSchema.java (original)
+++ xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/xmp/schemas/DublinCoreSchema.java Tue Jun 20 07:31:40 2006
@@ -51,7 +51,7 @@
      * @param meta the metadata object
      * @return the newly instantiated adapter
      */
-    public DublinCoreAdapter getAdapter(Metadata meta) {
+    public static DublinCoreAdapter getAdapter(Metadata meta) {
         return new DublinCoreAdapter(meta);
     }
 

Modified: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/xmp/schemas/XMPBasicAdapter.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/xmp/schemas/XMPBasicAdapter.java?rev=415656&r1=415655&r2=415656&view=diff
==============================================================================
--- xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/xmp/schemas/XMPBasicAdapter.java (original)
+++ xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/xmp/schemas/XMPBasicAdapter.java Tue Jun 20 07:31:40 2006
@@ -58,6 +58,11 @@
         setDateValue("CreateDate", creationDate);
     }
 
+    /** @return the date and time the resource was originally created */
+    public Date getCreateDate() {
+        return getDateValue("CreateDate");
+    }
+    
     /**
      * Sets the date and time the resource was last modified.
      * @param modifyDate the modification date
@@ -66,6 +71,11 @@
         setDateValue("ModifyDate", modifyDate);
     }
 
+    /** @return the date and time the resource was last modified */
+    public Date getModifyDate() {
+        return getDateValue("ModifyDate");
+    }
+    
     /**
      * Sets the date and time any metadata for this resource was last changed.
      * @param metadataDate the modification date for the metadata
@@ -73,4 +83,10 @@
     public void setMetadataDate(Date metadataDate) {
         setDateValue("MetadataDate", metadataDate);
     }
+    
+    /** @return the date and time the resource was originally created */
+    public Date getMetadataDate() {
+        return getDateValue("MetadataDate");
+    }
+
 }



---------------------------------------------------------------------
Apache XML Graphics Project URL: http://xmlgraphics.apache.org/
To unsubscribe, e-mail: commits-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: commits-help@xmlgraphics.apache.org