rdftemplate

Library for generating XML documents from RDF data using templates
git clone https://code.djc.id.au/git/rdftemplate/
commit 56573fa5767d23d5c3818ce0a9832884c628a29c
parent a6e061b9419469d7833ac557c7bd77831ecde971
Author: Dan Callaghan <djc@djc.id.au>
Date:   Thu,  1 Oct 2009 21:28:43 +1000

support years and year-months for date type

Diffstat:
Msrc/main/java/au/com/miskinhill/rdftemplate/datatype/DateDataType.java | 50+++++++++++++++++++++++++-------------------------
Asrc/main/java/au/com/miskinhill/rdftemplate/datatype/Year.java | 42++++++++++++++++++++++++++++++++++++++++++
Asrc/main/java/au/com/miskinhill/rdftemplate/datatype/YearMonth.java | 50++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/test/java/au/com/miskinhill/rdftemplate/datatype/DateDataTypeUnitTest.java | 35+++++++++++++++++++++++++++++++++++
Asrc/test/java/au/com/miskinhill/rdftemplate/datatype/YearMonthUnitTest.java | 24++++++++++++++++++++++++
Asrc/test/java/au/com/miskinhill/rdftemplate/datatype/YearUnitTest.java | 24++++++++++++++++++++++++
Msrc/test/resources/au/com/miskinhill/rdftemplate/test-template.out.xml | 4++--
7 files changed, 202 insertions(+), 27 deletions(-)
diff --git a/src/main/java/au/com/miskinhill/rdftemplate/datatype/DateDataType.java b/src/main/java/au/com/miskinhill/rdftemplate/datatype/DateDataType.java
@@ -1,8 +1,5 @@
 package au.com.miskinhill.rdftemplate.datatype;
 
-import java.util.Arrays;
-import java.util.List;
-
 import org.springframework.stereotype.Component;
 
 import com.hp.hpl.jena.datatypes.DatatypeFormatException;
@@ -24,10 +21,9 @@ public class DateDataType implements RDFDatatype {
         instance = new DateDataType();
     }
     
-    private final List<DateTimeFormatter> parsers = Arrays.asList(
-            DateTimeFormat.forPattern("yyyy"),
-            DateTimeFormat.forPattern("yyyy-mm"),
-            DateTimeFormat.forPattern("yyyy-mm-dd"));
+    private final DateTimeFormatter yearParser = DateTimeFormat.forPattern("yyyy");
+    private final DateTimeFormatter yearMonthParser = DateTimeFormat.forPattern("yyyy-MM");
+    private final DateTimeFormatter dateParser = DateTimeFormat.forPattern("yyyy-MM-dd");
 
     public DateDataType() {
         TypeMapper.getInstance().registerDatatype(this);
@@ -40,13 +36,12 @@ public class DateDataType implements RDFDatatype {
     
     @Override
     public Class<LocalDate> getJavaClass() {
-        return LocalDate.class;
+        return null;
     }
 
     @Override
     public String unparse(Object value) {
-        LocalDate date = (LocalDate) value;
-        return date.toString();
+        throw new UnsupportedOperationException();
     }
 
     @Override
@@ -70,28 +65,33 @@ public class DateDataType implements RDFDatatype {
     }
     
     @Override
-    public LocalDate parse(String lexicalForm) throws DatatypeFormatException {
-        for (DateTimeFormatter parser: parsers) {
-            try {
-                return parser.parseDateTime(lexicalForm).toLocalDate();
-            } catch (IllegalArgumentException e) {
-                // pass
-            }
+    public Object parse(String lexicalForm) throws DatatypeFormatException {
+        try {
+            return dateParser.parseDateTime(lexicalForm).toLocalDate();
+        } catch (IllegalArgumentException e) {
+            // pass
+        }
+        try {
+            return new YearMonth(yearMonthParser.parseDateTime(lexicalForm).toLocalDate());
+        } catch (IllegalArgumentException e) {
+            // pass
+        }
+        try {
+            return new Year(yearParser.parseDateTime(lexicalForm).toLocalDate());
+        } catch (IllegalArgumentException e) {
+            // pass
         }
         throw new DatatypeFormatException(lexicalForm, this, "No matching parsers found");
     }
 
     @Override
     public boolean isValid(String lexicalForm) {
-        for (DateTimeFormatter parser: parsers) {
-            try {
-                parser.parseDateTime(lexicalForm);
-                return true;
-            } catch (IllegalArgumentException e) {
-                // pass
-            }
+        try {
+            parse(lexicalForm);
+            return true;
+        } catch (DatatypeFormatException e) {
+            return false;
         }
-        return false;
     }
 
     @Override
diff --git a/src/main/java/au/com/miskinhill/rdftemplate/datatype/Year.java b/src/main/java/au/com/miskinhill/rdftemplate/datatype/Year.java
@@ -0,0 +1,42 @@
+package au.com.miskinhill.rdftemplate.datatype;
+
+import org.joda.time.LocalDate;
+
+public class Year {
+    
+    private final int year;
+    
+    public Year(int value) {
+        this.year = value;
+    }
+    
+    public Year(LocalDate date) {
+        this.year = date.getYear();
+    }
+    
+    @Override
+    public String toString() {
+        return Integer.toString(year);
+    }
+
+    @Override
+    public int hashCode() {
+        final int prime = 31;
+        int result = 1;
+        result = prime * result + year;
+        return result;
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj)
+            return true;
+        if (obj == null)
+            return false;
+        if (getClass() != obj.getClass())
+            return false;
+        Year other = (Year) obj;
+        return (year == other.year);
+    }
+
+}
diff --git a/src/main/java/au/com/miskinhill/rdftemplate/datatype/YearMonth.java b/src/main/java/au/com/miskinhill/rdftemplate/datatype/YearMonth.java
@@ -0,0 +1,50 @@
+package au.com.miskinhill.rdftemplate.datatype;
+
+import org.joda.time.LocalDate;
+
+public class YearMonth {
+    
+    private final int year;
+    private final int month;
+    
+    public YearMonth(int year, int month) {
+        this.year = year;
+        this.month = month;
+    }
+    
+    public YearMonth(LocalDate date) {
+        this.year = date.getYear();
+        this.month = date.getMonthOfYear();
+    }
+    
+    @Override
+    public String toString() {
+        return String.format("%04d-%02d", year, month);
+    }
+
+    @Override
+    public int hashCode() {
+        final int prime = 31;
+        int result = 1;
+        result = prime * result + month;
+        result = prime * result + year;
+        return result;
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj)
+            return true;
+        if (obj == null)
+            return false;
+        if (getClass() != obj.getClass())
+            return false;
+        YearMonth other = (YearMonth) obj;
+        if (month != other.month)
+            return false;
+        if (year != other.year)
+            return false;
+        return true;
+    }
+
+}
diff --git a/src/test/java/au/com/miskinhill/rdftemplate/datatype/DateDataTypeUnitTest.java b/src/test/java/au/com/miskinhill/rdftemplate/datatype/DateDataTypeUnitTest.java
@@ -0,0 +1,35 @@
+package au.com.miskinhill.rdftemplate.datatype;
+
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.junit.Assert.assertThat;
+
+import com.hp.hpl.jena.datatypes.RDFDatatype;
+import org.joda.time.LocalDate;
+import org.junit.Before;
+import org.junit.Test;
+
+public class DateDataTypeUnitTest {
+    
+    private RDFDatatype type;
+    
+    @Before
+    public void setUp() {
+        type = new DateDataType();
+    }
+    
+    @Test
+    public void shouldParseYear() {
+        assertThat((Year) type.parse("2003"), equalTo(new Year(2003)));
+    }
+    
+    @Test
+    public void shouldParseYearMonth() {
+        assertThat((YearMonth) type.parse("2003-05"), equalTo(new YearMonth(2003, 5)));
+    }
+    
+    @Test
+    public void shouldParseDate() {
+        assertThat((LocalDate) type.parse("2003-05-25"), equalTo(new LocalDate(2003, 5, 25)));
+    }
+
+}
diff --git a/src/test/java/au/com/miskinhill/rdftemplate/datatype/YearMonthUnitTest.java b/src/test/java/au/com/miskinhill/rdftemplate/datatype/YearMonthUnitTest.java
@@ -0,0 +1,24 @@
+package au.com.miskinhill.rdftemplate.datatype;
+
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.junit.Assert.assertThat;
+
+import org.joda.time.LocalDate;
+import org.junit.Test;
+
+public class YearMonthUnitTest {
+    
+    @Test
+    public void testToString() {
+        assertThat(new YearMonth(new LocalDate(2001, 5, 1)).toString(), equalTo("2001-05"));
+    }
+    
+    @Test
+    public void testEqualsHashCode() {
+        YearMonth yearMonth1 = new YearMonth(new LocalDate(2001, 5, 1));
+        YearMonth yearMonth2 = new YearMonth(new LocalDate(2001, 5, 1));
+        assertThat(yearMonth1, equalTo(yearMonth2));
+        assertThat(yearMonth1.hashCode(), equalTo(yearMonth2.hashCode()));
+    }
+
+}
diff --git a/src/test/java/au/com/miskinhill/rdftemplate/datatype/YearUnitTest.java b/src/test/java/au/com/miskinhill/rdftemplate/datatype/YearUnitTest.java
@@ -0,0 +1,24 @@
+package au.com.miskinhill.rdftemplate.datatype;
+
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.junit.Assert.assertThat;
+
+import org.joda.time.LocalDate;
+import org.junit.Test;
+
+public class YearUnitTest {
+    
+    @Test
+    public void testToString() {
+        assertThat(new Year(new LocalDate(2001, 1, 1)).toString(), equalTo("2001"));
+    }
+    
+    @Test
+    public void testEqualsHashCode() {
+        Year year1 = new Year(new LocalDate(2001, 1, 1));
+        Year year2 = new Year(new LocalDate(2001, 1, 1));
+        assertThat(year1, equalTo(year2));
+        assertThat(year1.hashCode(), equalTo(year2.hashCode()));
+    }
+
+}
diff --git a/src/test/resources/au/com/miskinhill/rdftemplate/test-template.out.xml b/src/test/resources/au/com/miskinhill/rdftemplate/test-template.out.xml
@@ -28,13 +28,13 @@
         <a href="http://miskinhill.com.au/journals/test/2:1/">
             Vol. 2,
             Issue 1
-            (2008-01-01)
+            (2008)
         </a>
     </li><li>
         <a href="http://miskinhill.com.au/journals/test/1:1/">
             Vol. 1,
             Issue 1
-            (2007-01-01)
+            (2007)
         </a>
     </li>
 </ul>