chemistry-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fguilla...@apache.org
Subject svn commit: r796304 - in /incubator/chemistry/trunk/chemistry: chemistry-atompub/ chemistry-atompub/src/main/java/org/apache/chemistry/atompub/ chemistry-commons/src/main/java/org/apache/chemistry/util/ chemistry-commons/src/test/java/org/apache/chemis...
Date Tue, 21 Jul 2009 13:57:50 GMT
Author: fguillaume
Date: Tue Jul 21 13:57:49 2009
New Revision: 796304

URL: http://svn.apache.org/viewvc?rev=796304&view=rev
Log:
CMIS-43: Fixed AtomPub date I/O

Modified:
    incubator/chemistry/trunk/chemistry/chemistry-atompub/pom.xml
    incubator/chemistry/trunk/chemistry/chemistry-atompub/src/main/java/org/apache/chemistry/atompub/ValueAdapter.java
    incubator/chemistry/trunk/chemistry/chemistry-commons/src/main/java/org/apache/chemistry/util/GregorianCalendar.java
    incubator/chemistry/trunk/chemistry/chemistry-commons/src/test/java/org/apache/chemistry/util/TestGregorianCalendar.java
    incubator/chemistry/trunk/chemistry/chemistry-tests/src/main/java/org/apache/chemistry/test/BasicTestCase.java

Modified: incubator/chemistry/trunk/chemistry/chemistry-atompub/pom.xml
URL: http://svn.apache.org/viewvc/incubator/chemistry/trunk/chemistry/chemistry-atompub/pom.xml?rev=796304&r1=796303&r2=796304&view=diff
==============================================================================
--- incubator/chemistry/trunk/chemistry/chemistry-atompub/pom.xml (original)
+++ incubator/chemistry/trunk/chemistry/chemistry-atompub/pom.xml Tue Jul 21 13:57:49 2009
@@ -29,11 +29,14 @@
   <name>Chemistry AtomPub</name>
 
   <dependencies>
-
     <dependency>
       <groupId>org.apache.chemistry</groupId>
       <artifactId>chemistry-api</artifactId>
     </dependency>
+    <dependency>
+      <groupId>org.apache.chemistry</groupId>
+      <artifactId>chemistry-commons</artifactId>
+    </dependency>
 
     <dependency>
       <groupId>org.apache.abdera</groupId>

Modified: incubator/chemistry/trunk/chemistry/chemistry-atompub/src/main/java/org/apache/chemistry/atompub/ValueAdapter.java
URL: http://svn.apache.org/viewvc/incubator/chemistry/trunk/chemistry/chemistry-atompub/src/main/java/org/apache/chemistry/atompub/ValueAdapter.java?rev=796304&r1=796303&r2=796304&view=diff
==============================================================================
--- incubator/chemistry/trunk/chemistry/chemistry-atompub/src/main/java/org/apache/chemistry/atompub/ValueAdapter.java
(original)
+++ incubator/chemistry/trunk/chemistry/chemistry-atompub/src/main/java/org/apache/chemistry/atompub/ValueAdapter.java
Tue Jul 21 13:57:49 2009
@@ -28,8 +28,8 @@
 
 import javax.xml.namespace.QName;
 
-import org.apache.abdera.model.AtomDate;
 import org.apache.chemistry.PropertyType;
+import org.apache.chemistry.util.GregorianCalendar;
 
 /**
  * Adapter between a Java value and a XML String representation.
@@ -157,14 +157,14 @@
     protected static final class DateTimeValueAdapter extends ValueAdapter {
         @Override
         public Serializable readValue(String xml) {
-            return AtomDate.valueOf(xml).getCalendar();
+            return GregorianCalendar.fromAtomPub(xml);
         }
 
         // accepts both Calendar and Date
         @Override
         public String writeValue(Serializable val) {
-            return val.getClass() == Calendar.class ? AtomDate.format(((Calendar) val).getTime())
-                    : AtomDate.format((Date) val);
+            return val instanceof Calendar ? GregorianCalendar.toAtomPub((Calendar) val)
+                    : GregorianCalendar.toAtomPub((Date) val);
         }
 
         @Override

Modified: incubator/chemistry/trunk/chemistry/chemistry-commons/src/main/java/org/apache/chemistry/util/GregorianCalendar.java
URL: http://svn.apache.org/viewvc/incubator/chemistry/trunk/chemistry/chemistry-commons/src/main/java/org/apache/chemistry/util/GregorianCalendar.java?rev=796304&r1=796303&r2=796304&view=diff
==============================================================================
--- incubator/chemistry/trunk/chemistry/chemistry-commons/src/main/java/org/apache/chemistry/util/GregorianCalendar.java
(original)
+++ incubator/chemistry/trunk/chemistry/chemistry-commons/src/main/java/org/apache/chemistry/util/GregorianCalendar.java
Tue Jul 21 13:57:49 2009
@@ -17,27 +17,153 @@
 package org.apache.chemistry.util;
 
 import java.util.Calendar;
+import java.util.Date;
 import java.util.Locale;
 import java.util.TimeZone;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 /**
  * A GregorianCalendar with a better toString method.
  * <p>
- * This way you don't have to pull you hair when debugging dates, or maps
- * containing dates.
+ * It also provides a static {@link #fromAtomPub} method for construction.
  */
 public class GregorianCalendar extends java.util.GregorianCalendar {
 
     private static final long serialVersionUID = 1L;
 
-    public static Calendar getInstance() {
+    public static GregorianCalendar getInstance() {
         return new GregorianCalendar();
     }
 
-    public static Calendar getInstance(TimeZone zone) {
+    public static GregorianCalendar getInstance(TimeZone zone) {
         return new GregorianCalendar(zone);
     }
 
+    private static final Pattern ATOMPUB_PATTERN = Pattern.compile( //
+    "(\\d{4})-(\\d{2})-(\\d{2})[Tt]"
+            + "(\\d{2}):(\\d{2}):(\\d{2})(?:\\.(\\d{3}))?"
+            + "(?:[Zz]|([+-]\\d{2}:\\d{2}))");
+
+    /**
+     * Factory from an AtomPub date string representation.
+     * <p>
+     * Basically parses {@code YYYY-HH-MMThh:mm:ss.sss+hh:mm}, or a {@code Z}
+     * for the timezone, and with {@code .sss} being optional.
+     *
+     * @param date the string representation in AtomPub format
+     * @return the created instance
+     */
+    public static GregorianCalendar fromAtomPub(String date) {
+        Matcher m = ATOMPUB_PATTERN.matcher(date);
+        if (!m.matches()) {
+            throw new IllegalArgumentException("Invalid date format: " + date);
+        }
+        String tz = m.group(8);
+        GregorianCalendar cal = getInstance(TimeZone.getTimeZone("GMT"
+                + (tz == null ? "" : tz)));
+        cal.set(YEAR, Integer.parseInt(m.group(1)));
+        cal.set(MONTH, Integer.parseInt(m.group(2)) - 1);
+        cal.set(DATE, Integer.parseInt(m.group(3)));
+        cal.set(HOUR_OF_DAY, Integer.parseInt(m.group(4)));
+        cal.set(MINUTE, Integer.parseInt(m.group(5)));
+        cal.set(SECOND, Integer.parseInt(m.group(6)));
+        String ms = m.group(7);
+        cal.set(MILLISECOND, ms == null ? 0 : Integer.parseInt(ms));
+        return cal;
+    }
+
+    /**
+     * Serializer of a Calendar to the AtomPub representation.
+     *
+     * @param cal a {@link Calendar}
+     * @return the AtomPub string representation
+     */
+    public static String toAtomPub(Calendar cal) {
+        StringBuilder buf = new StringBuilder(28);
+        toAtomPub(cal, buf);
+        return buf.toString();
+    }
+
+    /**
+     * Serializer of a Date to the AtomPub representation.
+     *
+     * @param date a {@link Date}
+     * @return the AtomPub string representation
+     */
+    public static String toAtomPub(Date date) {
+        Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
+        cal.setTime(date);
+        return toAtomPub(cal);
+    }
+
+    protected static void toAtomPub(Calendar cal, StringBuilder buf) {
+        buf.append(cal.get(YEAR));
+        buf.append('-');
+        int f = cal.get(MONTH);
+        if (f < 9) {
+            buf.append('0');
+        }
+        buf.append(f + 1);
+        buf.append('-');
+        f = cal.get(DATE);
+        if (f < 10) {
+            buf.append('0');
+        }
+        buf.append(f);
+        buf.append('T');
+        f = cal.get(HOUR_OF_DAY);
+        if (f < 10) {
+            buf.append('0');
+        }
+        buf.append(f);
+        buf.append(':');
+        f = cal.get(MINUTE);
+        if (f < 10) {
+            buf.append('0');
+        }
+        buf.append(f);
+        buf.append(':');
+        f = cal.get(SECOND);
+        if (f < 10) {
+            buf.append('0');
+        }
+        buf.append(f);
+        buf.append('.');
+        f = cal.get(MILLISECOND);
+        if (f < 100) {
+            buf.append('0');
+        }
+        if (f < 10) {
+            buf.append('0');
+        }
+        buf.append(f);
+        int offset = cal.getTimeZone().getOffset(cal.getTimeInMillis()) / 60000;
+        if (offset == 0) {
+            buf.append('Z');
+        } else {
+            char sign;
+            if (offset < 0) {
+                offset = -offset;
+                sign = '-';
+            } else {
+                sign = '+';
+            }
+            buf.append(sign);
+            f = offset / 60;
+            if (f < 10) {
+                buf.append('0');
+            }
+            buf.append(f);
+            buf.append(':');
+            f = offset % 60;
+            if (f < 10) {
+                buf.append('0');
+            }
+            buf.append(f);
+        }
+    }
+
     /**
      * @see java.util.GregorianCalendar#GregorianCalendar()
      */
@@ -95,65 +221,7 @@
     public String toString() {
         StringBuilder buf = new StringBuilder(47);
         buf.append("GregorianCalendar(");
-        buf.append(get(YEAR));
-        buf.append('-');
-        int f = get(MONTH);
-        if (f < 9) {
-            buf.append('0');
-        }
-        buf.append(f + 1);
-        buf.append('-');
-        f = get(DATE);
-        if (f < 10) {
-            buf.append('0');
-        }
-        buf.append(f);
-        buf.append('T');
-        f = get(HOUR_OF_DAY);
-        if (f < 10) {
-            buf.append('0');
-        }
-        buf.append(f);
-        buf.append(':');
-        f = get(MINUTE);
-        if (f < 10) {
-            buf.append('0');
-        }
-        buf.append(f);
-        buf.append(':');
-        f = get(SECOND);
-        if (f < 10) {
-            buf.append('0');
-        }
-        buf.append(f);
-        buf.append('.');
-        f = get(MILLISECOND);
-        if (f < 100) {
-            buf.append('0');
-        }
-        if (f < 10) {
-            buf.append('0');
-        }
-        buf.append(f);
-        char sign;
-        int offset = getTimeZone().getOffset(getTimeInMillis()) / 60000;
-        if (offset < 0) {
-            offset = -offset;
-            sign = '-';
-        } else {
-            sign = '+';
-        }
-        buf.append(sign);
-        f = offset / 60;
-        if (f < 10) {
-            buf.append('0');
-        }
-        buf.append(f);
-        f = offset % 60;
-        if (f < 10) {
-            buf.append('0');
-        }
-        buf.append(f);
+        toAtomPub(this, buf);
         buf.append(')');
         return buf.toString();
     }

Modified: incubator/chemistry/trunk/chemistry/chemistry-commons/src/test/java/org/apache/chemistry/util/TestGregorianCalendar.java
URL: http://svn.apache.org/viewvc/incubator/chemistry/trunk/chemistry/chemistry-commons/src/test/java/org/apache/chemistry/util/TestGregorianCalendar.java?rev=796304&r1=796303&r2=796304&view=diff
==============================================================================
--- incubator/chemistry/trunk/chemistry/chemistry-commons/src/test/java/org/apache/chemistry/util/TestGregorianCalendar.java
(original)
+++ incubator/chemistry/trunk/chemistry/chemistry-commons/src/test/java/org/apache/chemistry/util/TestGregorianCalendar.java
Tue Jul 21 13:57:49 2009
@@ -32,7 +32,7 @@
         cal.set(Calendar.MINUTE, 4);
         cal.set(Calendar.SECOND, 5);
         cal.set(Calendar.MILLISECOND, 6);
-        assertEquals("GregorianCalendar(2009-01-02T03:04:05.006+0730)",
+        assertEquals("GregorianCalendar(2009-01-02T03:04:05.006+07:30)",
                 cal.toString());
     }
 
@@ -45,7 +45,32 @@
         cal.set(Calendar.MINUTE, 59);
         cal.set(Calendar.SECOND, 59);
         cal.set(Calendar.MILLISECOND, 999);
-        assertEquals("GregorianCalendar(2008-12-31T23:59:59.999-0600)",
+        assertEquals("GregorianCalendar(2008-12-31T23:59:59.999-06:00)",
+                cal.toString());
+    }
+
+    public void testToString3() {
+        Calendar cal = GregorianCalendar.getInstance(TimeZone.getTimeZone("GMT"));
+        cal.set(Calendar.YEAR, 2008);
+        cal.set(Calendar.MONTH, 0);
+        cal.set(Calendar.DATE, 1);
+        cal.set(Calendar.HOUR_OF_DAY, 1);
+        cal.set(Calendar.MINUTE, 1);
+        cal.set(Calendar.SECOND, 1);
+        cal.set(Calendar.MILLISECOND, 1);
+        assertEquals("GregorianCalendar(2008-01-01T01:01:01.001Z)",
+                cal.toString());
+    }
+
+    public void testFromAtomPub1() {
+        Calendar cal = GregorianCalendar.fromAtomPub("2009-07-14T12:00:00.123-06:30");
+        assertEquals("GregorianCalendar(2009-07-14T12:00:00.123-06:30)",
+                cal.toString());
+    }
+
+    public void testFromAtomPub2() {
+        Calendar cal = GregorianCalendar.fromAtomPub("2009-07-14T12:00:00Z");
+        assertEquals("GregorianCalendar(2009-07-14T12:00:00.000Z)",
                 cal.toString());
     }
 

Modified: incubator/chemistry/trunk/chemistry/chemistry-tests/src/main/java/org/apache/chemistry/test/BasicTestCase.java
URL: http://svn.apache.org/viewvc/incubator/chemistry/trunk/chemistry/chemistry-tests/src/main/java/org/apache/chemistry/test/BasicTestCase.java?rev=796304&r1=796303&r2=796304&view=diff
==============================================================================
--- incubator/chemistry/trunk/chemistry/chemistry-tests/src/main/java/org/apache/chemistry/test/BasicTestCase.java
(original)
+++ incubator/chemistry/trunk/chemistry/chemistry-tests/src/main/java/org/apache/chemistry/test/BasicTestCase.java
Tue Jul 21 13:57:49 2009
@@ -22,6 +22,7 @@
 import java.util.Collection;
 import java.util.List;
 import java.util.Map;
+import java.util.TimeZone;
 
 import junit.framework.TestCase;
 
@@ -37,6 +38,7 @@
 import org.apache.chemistry.Repository;
 import org.apache.chemistry.SPI;
 import org.apache.chemistry.Type;
+import org.apache.chemistry.util.GregorianCalendar;
 import org.apache.commons.io.IOUtils;
 
 /**
@@ -226,6 +228,14 @@
         Document doc = root.newDocument("doc");
         doc.setName("mydoc");
         doc.setValue("title", "mytitle");
+        GregorianCalendar cal = new GregorianCalendar(
+                TimeZone.getTimeZone("GMT+05:00"));
+        cal.clear();
+        cal.set(2009, 7 - 1, 14, 12, 00, 00);
+        cal.set(Calendar.MILLISECOND, 0);
+        assertEquals("GregorianCalendar(2009-07-14T12:00:00.000+05:00)",
+                cal.toString());
+        doc.setValue("date", cal);
         doc.save();
         // new connection
         closeConn();
@@ -236,6 +246,8 @@
         doc = (Document) children.get(0);
         assertEquals("mydoc", doc.getName());
         assertEquals("mytitle", doc.getString("title"));
+        Calendar cal2 = doc.getDateTime("date");
+        assertEquals(cal.toString(), cal2.toString());
     }
 
 }



Mime
View raw message