You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by je...@apache.org on 2018/04/20 19:19:08 UTC

[sling-org-apache-sling-jcr-contentloader] branch master updated: SLING-7590 Changed date parser to ISO8601 util

This is an automated email from the ASF dual-hosted git repository.

jeb pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-jcr-contentloader.git


The following commit(s) were added to refs/heads/master by this push:
     new dd1ff5f  SLING-7590 Changed date parser to ISO8601 util
dd1ff5f is described below

commit dd1ff5f20ec06325dea8e2f576f924cd2e11476c
Author: JE Bailey <je...@apache.org>
AuthorDate: Fri Apr 20 15:19:01 2018 -0400

    SLING-7590 Changed date parser to ISO8601 util
    
    streamlined the parsing and enabled the creation of Calendar objects
    with correct time zone offsets
---
 pom.xml                                            |  5 ++
 .../internal/DefaultContentCreator.java            | 56 +++++-----------------
 2 files changed, 16 insertions(+), 45 deletions(-)

diff --git a/pom.xml b/pom.xml
index 0e7c741..148c6a5 100644
--- a/pom.xml
+++ b/pom.xml
@@ -361,5 +361,10 @@
         	<type>bundle</type>
         	<scope>test</scope>
         </dependency>
+        <dependency>
+        	<groupId>org.apache.jackrabbit</groupId>
+        	<artifactId>jackrabbit-jcr-commons</artifactId>
+        	<version>2.2.9</version>
+        </dependency>
     </dependencies>
 </project>
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 8ed0d74..49c4a97 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
@@ -23,8 +23,6 @@ import java.io.UnsupportedEncodingException;
 import java.security.MessageDigest;
 import java.security.NoSuchAlgorithmException;
 import java.security.Principal;
-import java.text.ParseException;
-import java.text.SimpleDateFormat;
 import java.util.ArrayList;
 import java.util.Calendar;
 import java.util.Date;
@@ -36,7 +34,6 @@ import java.util.Map.Entry;
 import java.util.Set;
 import java.util.Stack;
 import java.util.StringTokenizer;
-import java.util.regex.Pattern;
 
 import javax.jcr.Item;
 import javax.jcr.ItemNotFoundException;
@@ -52,6 +49,7 @@ import org.apache.jackrabbit.api.security.user.Authorizable;
 import org.apache.jackrabbit.api.security.user.Group;
 import org.apache.jackrabbit.api.security.user.User;
 import org.apache.jackrabbit.api.security.user.UserManager;
+import org.apache.jackrabbit.util.ISO8601;
 import org.apache.sling.jcr.base.util.AccessControlUtil;
 import org.apache.sling.jcr.contentloader.ContentCreator;
 import org.apache.sling.jcr.contentloader.ContentImportListener;
@@ -71,10 +69,6 @@ public class DefaultContentCreator implements ContentCreator {
 
     private ImportOptions 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>();
 
     /**
@@ -312,12 +306,7 @@ public class DefaultContentCreator implements ContentCreator {
             }
         } else if (propertyType == PropertyType.DATE) {
             checkoutIfNecessary(node);
-            try {
-                node.setProperty(name, parseDateString(value));
-            } catch (ParseException e) {
-                // Fall back to default behaviour if this fails
-                node.setProperty(name, value, propertyType);
-            }
+            node.setProperty(name, ISO8601.parse(value));
             if (this.importListener != null) {
                 this.importListener.onCreate(node.getProperty(name).getPath());
             }
@@ -363,21 +352,17 @@ public class DefaultContentCreator implements ContentCreator {
             }
         } else if (propertyType == PropertyType.DATE) {
             checkoutIfNecessary(node);
-            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]));
-                }
+            
+            // This modification is to remove the colon in the JSON Timezone
+            ValueFactory valueFactory = node.getSession().getValueFactory();
+            Value[] jcrValues = new Value[values.length];
 
-                node.setProperty(name, jcrValues, propertyType);
-            } catch (ParseException e) {
-                // If this fails, fallback to the default
-                log.warn("Could not create dates for property, falling back to defaults", e);
-                node.setProperty(name, values, propertyType);
+            for (int i = 0; i < values.length; i++) {
+                jcrValues[i] = valueFactory.createValue(ISO8601.parse(values[i]));
             }
+
+            node.setProperty(name, jcrValues, propertyType);
+            
             if (this.importListener != null) {
                 this.importListener.onCreate(node.getProperty(name).getPath());
             }
@@ -577,25 +562,6 @@ 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;
-    }
-
     private void createProperty(String name, Object value, boolean overwriteExisting) throws RepositoryException {
         final Node node = this.parentNodeStack.peek();
         // check if the property already exists, don't overwrite it in this case

-- 
To stop receiving notification emails like this one, please contact
jeb@apache.org.