Author: cziegeler
Date: Fri Dec 11 16:10:01 2009
New Revision: 889675
URL: http://svn.apache.org/viewvc?rev=889675&view=rev
Log:
SLING-1130 : Content Loader does not parse dates. Apply patch from Josh Kennedy.
Modified:
sling/trunk/bundles/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/DefaultContentCreator.java
sling/trunk/bundles/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/readers/JsonReader.java
sling/trunk/bundles/jcr/contentloader/src/test/java/org/apache/sling/jcr/contentloader/internal/JsonReaderTest.java
Modified: sling/trunk/bundles/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/DefaultContentCreator.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/DefaultContentCreator.java?rev=889675&r1=889674&r2=889675&view=diff
==============================================================================
--- sling/trunk/bundles/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/DefaultContentCreator.java
(original)
+++ sling/trunk/bundles/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/DefaultContentCreator.java
Fri Dec 11 16:10:01 2009
@@ -22,6 +22,8 @@
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.security.Principal;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
@@ -35,6 +37,7 @@
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.Map.Entry;
+import java.util.regex.Pattern;
import javax.jcr.Item;
import javax.jcr.Node;
@@ -65,6 +68,9 @@
private PathEntry configuration;
+ private final Pattern jsonDatePattern = Pattern.compile("^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\\.[0-9]{3}[-+]{1}[0-9]{2}[:]{0,1}[0-9]{2}$");
+ private final SimpleDateFormat jsonDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
+
private final Stack<Node> parentNodeStack = new Stack<Node>();
/** The list of versionables. */
@@ -318,6 +324,14 @@
this.versionables.add(node);
}
}
+ } else if ( propertyType == PropertyType.DATE ) {
+ try {
+ node.setProperty(name, parseDateString(value) );
+ }
+ catch (ParseException e) {
+ // Fall back to default behaviour if this fails
+ node.setProperty(name, value, propertyType);
+ }
} else {
node.setProperty(name, value, propertyType);
}
@@ -351,6 +365,24 @@
if (!hasAll) {
delayedMultipleReferences.put(propPath, uuidOrPaths);
}
+ } else if ( propertyType == PropertyType.DATE ) {
+ try {
+ // This modification is to remove the colon in the JSON Timezone
+ ValueFactory valueFactory = node.getSession().getValueFactory();
+ Value[] jcrValues = new Value[values.length];
+
+ for (int i = 0; i < values.length; i++) {
+ jcrValues[i] = valueFactory.createValue( parseDateString( values[i] ) );
+ }
+
+ node.setProperty(name, jcrValues, propertyType);
+ }
+ catch (ParseException e) {
+ // If this failes, fallback to the default
+ jcrContentHelper.log.warn("Could not create dates for property, fallingback
to defaults", e);
+ node.setProperty(name, values, propertyType);
+ }
+
} else {
node.setProperty(name, values, propertyType);
}
@@ -561,6 +593,24 @@
return (item.isNode()) ? (Node) item : null;
}
+ private Calendar parseDateString(String value) throws ParseException {
+ if (jsonDatePattern.matcher(value).matches()) {
+ String modifiedJsonDate = value;
+
+ // This modification is to remove the colon in the JSON Timezone
+ // to match the Java Version
+ if (value.lastIndexOf(":") == 26) {
+ modifiedJsonDate = value.substring(0, 26) + value.substring(27);
+ }
+
+ Calendar cal = Calendar.getInstance();
+ cal.setTime( jsonDateFormat.parse( modifiedJsonDate ) );
+
+ return cal;
+ }
+
+ return null;
+ }
/**
* @see org.apache.sling.jcr.contentloader.internal.ContentCreator#createFileAndResourceNode(java.lang.String,
java.io.InputStream, java.lang.String, long)
Modified: sling/trunk/bundles/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/readers/JsonReader.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/readers/JsonReader.java?rev=889675&r1=889674&r2=889675&view=diff
==============================================================================
--- sling/trunk/bundles/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/readers/JsonReader.java
(original)
+++ sling/trunk/bundles/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/readers/JsonReader.java
Fri Dec 11 16:10:01 2009
@@ -26,6 +26,7 @@
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
+import java.util.regex.Pattern;
import javax.jcr.PropertyType;
import javax.jcr.RepositoryException;
@@ -88,6 +89,7 @@
*/
public class JsonReader implements ContentReader {
+ private static final Pattern jsonDate = Pattern.compile("^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\\.[0-9]{3}[-+]{1}[0-9]{2}[:]{0,1}[0-9]{2}$");
private static final String REFERENCE = "jcr:reference:";
private static final String PATH = "jcr:path:";
@@ -233,6 +235,7 @@
} else if (object instanceof String) {
if (name.startsWith(REFERENCE)) return PropertyType.REFERENCE;
if (name.startsWith(PATH)) return PropertyType.PATH;
+ if (jsonDate.matcher((String) object).matches()) return PropertyType.DATE;
}
// fall back to default
Modified: sling/trunk/bundles/jcr/contentloader/src/test/java/org/apache/sling/jcr/contentloader/internal/JsonReaderTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/contentloader/src/test/java/org/apache/sling/jcr/contentloader/internal/JsonReaderTest.java?rev=889675&r1=889674&r2=889675&view=diff
==============================================================================
--- sling/trunk/bundles/jcr/contentloader/src/test/java/org/apache/sling/jcr/contentloader/internal/JsonReaderTest.java
(original)
+++ sling/trunk/bundles/jcr/contentloader/src/test/java/org/apache/sling/jcr/contentloader/internal/JsonReaderTest.java
Fri Dec 11 16:10:01 2009
@@ -144,6 +144,17 @@
this.parse(json);
}
+ @org.junit.Test public void testPropertiesSingleDateValue() throws Exception {
+ String json = "{ \"p1\": \"2009-09-24T16:32:57.948-07:00\"}";
+
+ this.mockery.checking(new Expectations() {{
+ allowing(creator).createNode(null, null, null); inSequence(mySequence);
+ allowing(creator).createProperty("p1", PropertyType.DATE, "2009-09-24T16:32:57.948-07:00");
inSequence(mySequence);
+ allowing(creator).finishNode(); inSequence(mySequence);
+ }});
+ this.parse(json);
+ }
+
@org.junit.Test public void testPropertiesTwoSingleValue() throws Exception {
String json = "{ \"p1\": \"v1\", \"p2\": \"v2\"}";
@@ -167,6 +178,17 @@
this.parse(json);
}
+ @org.junit.Test public void testPropertiesMultiDateValue() throws Exception {
+ String json = "{ \"p1\": [\"2009-09-24T16:32:57.948-07:00\"]}";
+
+ this.mockery.checking(new Expectations() {{
+ allowing(creator).createNode(null, null, null); inSequence(mySequence);
+ allowing(creator).createProperty("p1", PropertyType.DATE, new String[] {"2009-09-24T16:32:57.948-07:00"});
inSequence(mySequence);
+ allowing(creator).finishNode(); inSequence(mySequence);
+ }});
+ this.parse(json);
+ }
+
@org.junit.Test public void testPropertiesMultiValueEmpty() throws Exception {
String json = "{ \"p1\": []}";
|