You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/11/07 09:47:25 UTC
[sling-org-apache-sling-jcr-contentloader] 22/36: SLING-1130 :
Content Loader does not parse dates. Apply patch from Josh Kennedy.
This is an automated email from the ASF dual-hosted git repository.
rombert pushed a commit to annotated tag org.apache.sling.jcr.contentloader-2.0.6
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-jcr-contentloader.git
commit dfba4118a2d9bc5f881fc818ccb0d57bb976ae14
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Fri Dec 11 16:10:01 2009 +0000
SLING-1130 : Content Loader does not parse dates. Apply patch from Josh Kennedy.
git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/jcr/contentloader@889675 13f79535-47bb-0310-9956-ffa450edef68
---
.../internal/DefaultContentCreator.java | 50 ++++++++++++++++++++++
.../contentloader/internal/readers/JsonReader.java | 3 ++
.../jcr/contentloader/internal/JsonReaderTest.java | 22 ++++++++++
3 files changed, 75 insertions(+)
diff --git a/src/main/java/org/apache/sling/jcr/contentloader/internal/DefaultContentCreator.java b/src/main/java/org/apache/sling/jcr/contentloader/internal/DefaultContentCreator.java
index c28e833..18a483c 100644
--- a/src/main/java/org/apache/sling/jcr/contentloader/internal/DefaultContentCreator.java
+++ b/src/main/java/org/apache/sling/jcr/contentloader/internal/DefaultContentCreator.java
@@ -22,6 +22,8 @@ import java.io.InputStream;
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.Set;
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 @@ public class DefaultContentCreator implements ContentCreator {
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 @@ public class DefaultContentCreator implements ContentCreator {
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 @@ public class DefaultContentCreator implements ContentCreator {
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 @@ public class DefaultContentCreator implements ContentCreator {
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)
diff --git a/src/main/java/org/apache/sling/jcr/contentloader/internal/readers/JsonReader.java b/src/main/java/org/apache/sling/jcr/contentloader/internal/readers/JsonReader.java
index 8a2ee2e..a357d42 100644
--- a/src/main/java/org/apache/sling/jcr/contentloader/internal/readers/JsonReader.java
+++ b/src/main/java/org/apache/sling/jcr/contentloader/internal/readers/JsonReader.java
@@ -26,6 +26,7 @@ import java.util.HashSet;
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 @@ import org.apache.sling.jcr.contentloader.internal.ImportProvider;
*/
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 @@ public class JsonReader implements ContentReader {
} 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
diff --git a/src/test/java/org/apache/sling/jcr/contentloader/internal/JsonReaderTest.java b/src/test/java/org/apache/sling/jcr/contentloader/internal/JsonReaderTest.java
index c2d2a69..e7916d1 100644
--- a/src/test/java/org/apache/sling/jcr/contentloader/internal/JsonReaderTest.java
+++ b/src/test/java/org/apache/sling/jcr/contentloader/internal/JsonReaderTest.java
@@ -144,6 +144,17 @@ public class JsonReaderTest {
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 @@ public class JsonReaderTest {
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\": []}";
--
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.