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 10:26:41 UTC
[sling-org-apache-sling-testing-sling-mock] 20/48: SLING-6595
sling-mock: Use File System Content Parser for parsing JSON files
This is an automated email from the ASF dual-hosted git repository.
rombert pushed a commit to annotated tag org.apache.sling.testing.sling-mock-2.2.10
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-sling-mock.git
commit 9e5310bf7d18be766814af2ab03827323d00dff4
Author: Stefan Seifert <ss...@apache.org>
AuthorDate: Thu Mar 2 17:29:31 2017 +0000
SLING-6595 sling-mock: Use File System Content Parser for parsing JSON files
git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/testing/mocks/sling-mock@1785167 13f79535-47bb-0310-9956-ffa450edef68
---
pom.xml | 4 +-
.../testing/mock/sling/loader/ContentLoader.java | 176 +++------------------
2 files changed, 26 insertions(+), 154 deletions(-)
diff --git a/pom.xml b/pom.xml
index adf1e27..25c32c1 100644
--- a/pom.xml
+++ b/pom.xml
@@ -131,8 +131,8 @@
</dependency>
<dependency>
<groupId>org.apache.sling</groupId>
- <artifactId>org.apache.sling.commons.json</artifactId>
- <version>2.0.16</version>
+ <artifactId>org.apache.sling.fscontentparser</artifactId>
+ <version>1.0.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
diff --git a/src/main/java/org/apache/sling/testing/mock/sling/loader/ContentLoader.java b/src/main/java/org/apache/sling/testing/mock/sling/loader/ContentLoader.java
index 87df4b6..7ab801e 100644
--- a/src/main/java/org/apache/sling/testing/mock/sling/loader/ContentLoader.java
+++ b/src/main/java/org/apache/sling/testing/mock/sling/loader/ContentLoader.java
@@ -21,17 +21,10 @@ package org.apache.sling.testing.mock.sling.loader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
-import java.text.DateFormat;
-import java.text.ParseException;
-import java.text.SimpleDateFormat;
-import java.util.Calendar;
-import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
-import org.apache.commons.io.IOUtils;
-import org.apache.commons.lang3.CharEncoding;
import org.apache.commons.lang3.StringUtils;
import org.apache.jackrabbit.JcrConstants;
import org.apache.sling.api.resource.ModifiableValueMap;
@@ -39,11 +32,12 @@ import org.apache.sling.api.resource.PersistenceException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceUtil;
-import org.apache.sling.commons.json.JSONArray;
-import org.apache.sling.commons.json.JSONException;
-import org.apache.sling.commons.json.JSONObject;
-import org.apache.sling.commons.json.jcr.JsonItemWriter;
import org.apache.sling.commons.mime.MimeTypeService;
+import org.apache.sling.fscontentparser.ContentFileExtension;
+import org.apache.sling.fscontentparser.ContentFileParser;
+import org.apache.sling.fscontentparser.ContentFileParserFactory;
+import org.apache.sling.fscontentparser.ParseException;
+import org.apache.sling.fscontentparser.ParserOptions;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
@@ -56,13 +50,10 @@ import com.google.common.collect.ImmutableSet;
*/
public final class ContentLoader {
- private static final String REFERENCE = "jcr:reference:";
- private static final String PATH = "jcr:path:";
private static final String CONTENTTYPE_OCTET_STREAM = "application/octet-stream";
private static final String JCR_DATA_PLACEHOLDER = ":jcr:data";
private static final Set<String> IGNORED_NAMES = ImmutableSet.of(
- JcrConstants.JCR_PRIMARYTYPE,
JcrConstants.JCR_MIXINTYPES,
JcrConstants.JCR_UUID,
JcrConstants.JCR_BASEVERSION,
@@ -73,10 +64,14 @@ public final class ContentLoader {
"jcr:checkedOut",
"jcr:isCheckedOut",
"rep:policy");
+
+ private static ContentFileParser JSON_PARSER = ContentFileParserFactory.create(ContentFileExtension.JSON, new ParserOptions()
+ .detectCalendarValues(true)
+ .ignorePropertyNames(IGNORED_NAMES)
+ .ignoreResourceNames(IGNORED_NAMES));
private final ResourceResolver resourceResolver;
private final BundleContext bundleContext;
- private final DateFormat calendarFormat;
private final boolean autoCommit;
/**
@@ -102,7 +97,6 @@ public final class ContentLoader {
public ContentLoader(ResourceResolver resourceResolver, BundleContext bundleContext, boolean autoCommit) {
this.resourceResolver = resourceResolver;
this.bundleContext = bundleContext;
- this.calendarFormat = new SimpleDateFormat(JsonItemWriter.ECMA_DATE_FORMAT, JsonItemWriter.DATE_FORMAT_LOCALE);
this.autoCommit = autoCommit;
}
@@ -183,14 +177,13 @@ public final class ContentLoader {
throw new IllegalArgumentException("Resource does already exist: " + destPath);
}
- String jsonString = convertToJsonString(inputStream).trim();
- JSONObject json = new JSONObject(jsonString);
- Resource resource = this.createResource(parentResource, childName, json);
+ Map<String,Object> content = JSON_PARSER.parse(inputStream);
+ Resource resource = this.createResource(parentResource, childName, content);
if (autoCommit) {
resourceResolver.commit();
}
return resource;
- } catch (JSONException ex) {
+ } catch (ParseException ex) {
throw new RuntimeException(ex);
} catch (IOException ex) {
throw new RuntimeException(ex);
@@ -215,37 +208,22 @@ public final class ContentLoader {
}
}
- private Resource createResource(Resource parentResource, String childName, JSONObject jsonObject)
- throws IOException, JSONException {
-
+ @SuppressWarnings("unchecked")
+ private Resource createResource(Resource parentResource, String childName, Map<String,Object> content) throws IOException {
+
// collect all properties first
boolean hasJcrData = false;
Map<String, Object> props = new HashMap<String, Object>();
- JSONArray names = jsonObject.names();
- for (int i = 0; names != null && i < names.length(); i++) {
- final String name = names.getString(i);
+ for (Map.Entry<String,Object> entry : content.entrySet()) {
+ final String name = entry.getKey();
if (StringUtils.equals(name, JCR_DATA_PLACEHOLDER)) {
hasJcrData = true;
}
- else if (!IGNORED_NAMES.contains(name)) {
- Object obj = jsonObject.get(name);
- if (!(obj instanceof JSONObject)) {
- this.setProperty(props, name, obj);
- }
+ else if (!(entry.getValue() instanceof Map)) {
+ props.put(name, entry.getValue());
}
}
-
- // validate JCR primary type
- Object primaryTypeObj = jsonObject.opt(JcrConstants.JCR_PRIMARYTYPE);
- String primaryType = null;
- if (primaryTypeObj != null) {
- primaryType = String.valueOf(primaryTypeObj);
- }
- if (primaryType == null) {
- primaryType = JcrConstants.NT_UNSTRUCTURED;
- }
- props.put(JcrConstants.JCR_PRIMARYTYPE, primaryType);
-
+
// create resource
Resource resource = resourceResolver.create(parentResource, childName, props);
@@ -256,121 +234,15 @@ public final class ContentLoader {
}
// add child resources
- for (int i = 0; names != null && i < names.length(); i++) {
- final String name = names.getString(i);
- if (!IGNORED_NAMES.contains(name)) {
- Object obj = jsonObject.get(name);
- if (obj instanceof JSONObject) {
- createResource(resource, name, (JSONObject) obj);
- }
+ for (Map.Entry<String,Object> entry : content.entrySet()) {
+ if (entry.getValue() instanceof Map) {
+ createResource(resource, entry.getKey(), (Map<String,Object>)entry.getValue());
}
}
return resource;
}
- private void setProperty(Map<String, Object> props, String name, Object value) throws JSONException {
- if (value instanceof JSONArray) {
- // multivalue
- final JSONArray array = (JSONArray) value;
- if (array.length() > 0) {
- final Object[] values = new Object[array.length()];
- for (int i = 0; i < array.length(); i++) {
- values[i] = array.get(i);
- }
-
- if (values[0] instanceof Double || values[0] instanceof Float) {
- Double[] arrayValues = new Double[values.length];
- for (int i = 0; i < values.length; i++) {
- arrayValues[i] = (Double) values[i];
- }
- props.put(cleanupJsonName(name), arrayValues);
- } else if (values[0] instanceof Number) {
- Long[] arrayValues = new Long[values.length];
- for (int i = 0; i < values.length; i++) {
- arrayValues[i] = ((Number) values[i]).longValue();
- }
- props.put(cleanupJsonName(name), arrayValues);
- } else if (values[0] instanceof Boolean) {
- Boolean[] arrayValues = new Boolean[values.length];
- for (int i = 0; i < values.length; i++) {
- arrayValues[i] = (Boolean) values[i];
- }
- props.put(cleanupJsonName(name), arrayValues);
- } else {
- String[] arrayValues = new String[values.length];
- for (int i = 0; i < values.length; i++) {
- arrayValues[i] = values[i].toString();
- }
- props.put(cleanupJsonName(name), arrayValues);
- }
- } else {
- props.put(cleanupJsonName(name), new String[0]);
- }
-
- } else {
- // single value
- if (value instanceof Double || value instanceof Float) {
- props.put(cleanupJsonName(name), value);
- } else if (value instanceof Number) {
- props.put(cleanupJsonName(name), ((Number) value).longValue());
- } else if (value instanceof Boolean) {
- props.put(cleanupJsonName(name), value);
- } else {
- String stringValue = value.toString();
-
- // check if value is a Calendar object
- Calendar calendar = tryParseCalendarValue(stringValue);
- if (calendar != null) {
- props.put(cleanupJsonName(name), calendar);
- } else {
- props.put(cleanupJsonName(name), stringValue);
- }
-
- }
- }
- }
-
- private String cleanupJsonName(String name) {
- if (name.startsWith(REFERENCE)) {
- return name.substring(REFERENCE.length());
- }
- if (name.startsWith(PATH)) {
- return name.substring(PATH.length());
- }
- return name;
- }
-
- private String convertToJsonString(InputStream inputStream) {
- try {
- return IOUtils.toString(inputStream, CharEncoding.UTF_8);
- } catch (IOException ex) {
- throw new RuntimeException(ex);
- } finally {
- try {
- inputStream.close();
- } catch (IOException ex) {
- // ignore
- }
- }
- }
-
- private Calendar tryParseCalendarValue(String value) {
- if (StringUtils.isNotBlank(value)) {
- synchronized (calendarFormat) {
- try {
- Date date = calendarFormat.parse(value);
- Calendar calendar = Calendar.getInstance();
- calendar.setTime(date);
- return calendar;
- } catch (ParseException ex) {
- // ignore
- }
- }
- }
- return null;
- }
-
/**
* Import binary file as nt:file binary node into repository. Auto-creates
* parent hierarchies as nt:unstrucured nodes if missing. Mime type is
--
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.