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:38:44 UTC
[sling-org-apache-sling-fsresource] 11/29: SLING-6440 switch to
fscontentparser rename "Filesystem" to "File System" update to latest
parent
This is an automated email from the ASF dual-hosted git repository.
rombert pushed a commit to annotated tag org.apache.sling.fsresource-2.0.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-fsresource.git
commit c1a74d392a17a5801d2f9848449b9d22d42b0b6f
Author: Stefan Seifert <ss...@apache.org>
AuthorDate: Thu Mar 2 17:34:31 2017 +0000
SLING-6440 switch to fscontentparser
rename "Filesystem" to "File System"
update to latest parent
git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/extensions/fsresource@1785169 13f79535-47bb-0310-9956-ffa450edef68
---
pom.xml | 44 ++----
.../fsprovider/internal/FsResourceProvider.java | 8 +-
.../fsprovider/internal/mapper/FileResource.java | 2 +-
.../fsprovider/internal/mapper/ValueMapUtil.java | 7 -
.../internal/parser/ContentFileCache.java | 2 +-
...tFileParser.java => ContentFileParserUtil.java} | 27 +++-
.../internal/parser/ContentFileTypes.java | 6 +-
.../internal/parser/JcrXmlFileParser.java | 147 ------------------
.../internal/parser/JcrXmlValueConverter.java | 168 ---------------------
.../fsprovider/internal/parser/JsonFileParser.java | 125 ---------------
.../fsprovider/internal/JcrXmlContentTest.java | 10 +-
.../sling/fsprovider/internal/JsonContentTest.java | 15 +-
...serTest.java => ContentFileParserUtilTest.java} | 10 +-
.../internal/parser/JcrXmlFileParserTest.java | 34 -----
.../internal/parser/JcrXmlValueConverterTest.java | 105 -------------
15 files changed, 62 insertions(+), 648 deletions(-)
diff --git a/pom.xml b/pom.xml
index ea0200f..1ab9b94 100644
--- a/pom.xml
+++ b/pom.xml
@@ -22,7 +22,7 @@
<parent>
<groupId>org.apache.sling</groupId>
<artifactId>sling</artifactId>
- <version>29</version>
+ <version>30-SNAPSHOT</version>
<relativePath />
</parent>
@@ -30,10 +30,9 @@
<packaging>bundle</packaging>
<version>1.2.3-SNAPSHOT</version>
- <name>Apache Sling Filesystem Resource Provider</name>
+ <name>Apache Sling File System Resource Provider</name>
<description>
- Provides a ResourceProvider implementation supporting filesystem
- based resources.
+ Provides a ResourceProvider implementation supporting file system based resources.
</description>
<scm>
@@ -61,33 +60,14 @@
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
- <executions>
- <!-- Configure extra execution of 'manifest' in process-classes phase to make sure SCR metadata is generated before unit test runs -->
- <execution>
- <id>scr-metadata</id>
- <goals>
- <goal>manifest</goal>
- </goals>
- <configuration>
- <supportIncrementalBuild>true</supportIncrementalBuild>
- </configuration>
- </execution>
- </executions>
<configuration>
- <!-- Export SCR metadata to classpath to have them available in unit tests -->
- <exportScr>true</exportScr>
<instructions>
- <!-- Embed Apache Johnzon -->
+ <!-- Embed Apache Johnzon and fscontentparser -->
<Embed-Dependency>
johnzon-core;scope=compile;inline=false,
- geronimo-json_1.0_spec;scope=compile;inline=false
+ geronimo-json_1.0_spec;scope=compile;inline=false,
+ org.apache.sling.fscontentparser;scope=compile;inline=false
</Embed-Dependency>
- <!-- Embed the nessecary parts of the jackrabbit-jcr-commons bundle as described in http://njbartlett.name/2014/05/26/static-linking.html -->
- <Conditional-Package>org.apache.jackrabbit.util</Conditional-Package>
- <Import-Package>
- !org.apache.jackrabbit.*,
- *
- </Import-Package>
</instructions>
</configuration>
</plugin>
@@ -152,18 +132,18 @@
<scope>compile</scope>
</dependency>
<dependency>
- <groupId>org.apache.jackrabbit</groupId>
- <artifactId>jackrabbit-jcr-commons</artifactId>
- <version>2.8.0</version>
- <scope>compile</scope>
- </dependency>
- <dependency>
<groupId>org.apache.sling</groupId>
<artifactId>adapter-annotations</artifactId>
<version>1.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>org.apache.sling.fscontentparser</artifactId>
+ <version>1.0.0-SNAPSHOT</version>
+ <scope>compile</scope>
+ </dependency>
+ <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
diff --git a/src/main/java/org/apache/sling/fsprovider/internal/FsResourceProvider.java b/src/main/java/org/apache/sling/fsprovider/internal/FsResourceProvider.java
index 65246c4..d214987 100644
--- a/src/main/java/org/apache/sling/fsprovider/internal/FsResourceProvider.java
+++ b/src/main/java/org/apache/sling/fsprovider/internal/FsResourceProvider.java
@@ -63,7 +63,7 @@ import org.osgi.service.metatype.annotations.ObjectClassDefinition;
service=ResourceProvider.class,
configurationPolicy=ConfigurationPolicy.REQUIRE,
property={
- Constants.SERVICE_DESCRIPTION + "=Sling Filesystem Resource Provider",
+ Constants.SERVICE_DESCRIPTION + "=Sling File System Resource Provider",
Constants.SERVICE_VENDOR + "=The Apache Software Foundation"
})
@Designate(ocd=FsResourceProvider.Config.class, factory=true)
@@ -74,7 +74,7 @@ public final class FsResourceProvider extends ResourceProvider<Object> {
*/
public static final String RESOURCE_METADATA_FILE_DIRECTORY = ":org.apache.sling.fsprovider.file.directory";
- @ObjectClassDefinition(name = "Apache Sling Filesystem Resource Provider",
+ @ObjectClassDefinition(name = "Apache Sling File System Resource Provider",
description = "Configure an instance of the filesystem " +
"resource provider in terms of provider root and filesystem location")
public @interface Config {
@@ -83,8 +83,8 @@ public final class FsResourceProvider extends ResourceProvider<Object> {
* files and folders mapped into the resource tree (value is
* "provider.file").
*/
- @AttributeDefinition(name = "Filesystem Root",
- description = "Filesystem directory mapped to the virtual " +
+ @AttributeDefinition(name = "File System Root",
+ description = "File system directory mapped to the virtual " +
"resource tree. This property must not be an empty string. If the path is " +
"relative it is resolved against sling.home or the current working directory. " +
"The path may be a file or folder. If the path does not address an existing " +
diff --git a/src/main/java/org/apache/sling/fsprovider/internal/mapper/FileResource.java b/src/main/java/org/apache/sling/fsprovider/internal/mapper/FileResource.java
index 7472dc6..83f345e 100644
--- a/src/main/java/org/apache/sling/fsprovider/internal/mapper/FileResource.java
+++ b/src/main/java/org/apache/sling/fsprovider/internal/mapper/FileResource.java
@@ -82,7 +82,7 @@ public final class FileResource extends AbstractResource {
private static final Logger log = LoggerFactory.getLogger(FileResource.class);
/**
- * Creates an instance of this Filesystem resource.
+ * Creates an instance of this File system resource.
*
* @param resolver The owning resource resolver
* @param resourcePath The resource path in the resource tree
diff --git a/src/main/java/org/apache/sling/fsprovider/internal/mapper/ValueMapUtil.java b/src/main/java/org/apache/sling/fsprovider/internal/mapper/ValueMapUtil.java
index 8ddbda7..0d12ee1 100644
--- a/src/main/java/org/apache/sling/fsprovider/internal/mapper/ValueMapUtil.java
+++ b/src/main/java/org/apache/sling/fsprovider/internal/mapper/ValueMapUtil.java
@@ -22,8 +22,6 @@ import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
-import javax.jcr.nodetype.NodeType;
-
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.wrappers.ValueMapDecorator;
@@ -55,11 +53,6 @@ final class ValueMapUtil {
}
}
- // fallback to default jcr:primaryType is none is set
- if (!props.containsKey("jcr:primaryType")) {
- props.put("jcr:primaryType", NodeType.NT_UNSTRUCTURED);
- }
-
return new ValueMapDecorator(props);
}
diff --git a/src/main/java/org/apache/sling/fsprovider/internal/parser/ContentFileCache.java b/src/main/java/org/apache/sling/fsprovider/internal/parser/ContentFileCache.java
index 289fb67..745ae60 100644
--- a/src/main/java/org/apache/sling/fsprovider/internal/parser/ContentFileCache.java
+++ b/src/main/java/org/apache/sling/fsprovider/internal/parser/ContentFileCache.java
@@ -57,7 +57,7 @@ public final class ContentFileCache {
content = contentCache.get(path);
}
if (content == null) {
- content = ContentFileParser.parse(file);
+ content = ContentFileParserUtil.parse(file);
if (content == null) {
content = NULL_MAP;
}
diff --git a/src/main/java/org/apache/sling/fsprovider/internal/parser/ContentFileParser.java b/src/main/java/org/apache/sling/fsprovider/internal/parser/ContentFileParserUtil.java
similarity index 63%
rename from src/main/java/org/apache/sling/fsprovider/internal/parser/ContentFileParser.java
rename to src/main/java/org/apache/sling/fsprovider/internal/parser/ContentFileParserUtil.java
index 81a69a3..ec9af59 100644
--- a/src/main/java/org/apache/sling/fsprovider/internal/parser/ContentFileParser.java
+++ b/src/main/java/org/apache/sling/fsprovider/internal/parser/ContentFileParserUtil.java
@@ -25,17 +25,34 @@ import java.io.File;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
+import org.apache.sling.fscontentparser.ContentFileExtension;
+import org.apache.sling.fscontentparser.ContentFileParser;
+import org.apache.sling.fscontentparser.ContentFileParserFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Parses files that contains content fragments (e.g. JSON, JCR XML).
*/
-class ContentFileParser {
+class ContentFileParserUtil {
- private static final Logger log = LoggerFactory.getLogger(ContentFileParser.class);
+ private static final Logger log = LoggerFactory.getLogger(ContentFileParserUtil.class);
- private ContentFileParser() {
+ private static final ContentFileParser JSON_PARSER;
+ static {
+ // workaround for JsonProvider classloader issue until https://issues.apache.org/jira/browse/GERONIMO-6560 is fixed
+ ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
+ try {
+ Thread.currentThread().setContextClassLoader(ContentFileParserUtil.class.getClassLoader());
+ JSON_PARSER = ContentFileParserFactory.create(ContentFileExtension.JSON);
+ }
+ finally {
+ Thread.currentThread().setContextClassLoader(oldClassLoader);
+ }
+ }
+ private static final ContentFileParser JCR_XML_PARSER = ContentFileParserFactory.create(ContentFileExtension.JCR_XML);
+
+ private ContentFileParserUtil() {
// static methods only
}
@@ -47,10 +64,10 @@ class ContentFileParser {
public static Map<String,Object> parse(File file) {
try {
if (StringUtils.endsWith(file.getName(), JSON_SUFFIX)) {
- return JsonFileParser.parse(file);
+ return JSON_PARSER.parse(file);
}
else if (StringUtils.endsWith(file.getName(), JCR_XML_SUFFIX)) {
- return JcrXmlFileParser.parse(file);
+ return JCR_XML_PARSER.parse(file);
}
}
catch (Throwable ex) {
diff --git a/src/main/java/org/apache/sling/fsprovider/internal/parser/ContentFileTypes.java b/src/main/java/org/apache/sling/fsprovider/internal/parser/ContentFileTypes.java
index 85e6445..689654d 100644
--- a/src/main/java/org/apache/sling/fsprovider/internal/parser/ContentFileTypes.java
+++ b/src/main/java/org/apache/sling/fsprovider/internal/parser/ContentFileTypes.java
@@ -18,6 +18,8 @@
*/
package org.apache.sling.fsprovider.internal.parser;
+import org.apache.sling.fscontentparser.ContentFileExtension;
+
/**
* Content file types.
*/
@@ -26,12 +28,12 @@ public final class ContentFileTypes {
/**
* JSON content files.
*/
- public static final String JSON_SUFFIX = ".json";
+ public static final String JSON_SUFFIX = "." + ContentFileExtension.JSON;
/**
* JCR XML content files.
*/
- public static final String JCR_XML_SUFFIX = ".jcr.xml";
+ public static final String JCR_XML_SUFFIX = "." + ContentFileExtension.JCR_XML;
private ContentFileTypes() {
// static methods only
diff --git a/src/main/java/org/apache/sling/fsprovider/internal/parser/JcrXmlFileParser.java b/src/main/java/org/apache/sling/fsprovider/internal/parser/JcrXmlFileParser.java
deleted file mode 100644
index 6174bf9..0000000
--- a/src/main/java/org/apache/sling/fsprovider/internal/parser/JcrXmlFileParser.java
+++ /dev/null
@@ -1,147 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.sling.fsprovider.internal.parser;
-
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.IOException;
-import java.util.HashMap;
-import java.util.LinkedHashMap;
-import java.util.Map;
-import java.util.Stack;
-
-import javax.xml.parsers.ParserConfigurationException;
-import javax.xml.parsers.SAXParser;
-import javax.xml.parsers.SAXParserFactory;
-
-import org.apache.jackrabbit.util.ISO9075;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.xml.sax.Attributes;
-import org.xml.sax.SAXException;
-import org.xml.sax.SAXParseException;
-import org.xml.sax.helpers.DefaultHandler;
-
-/**
- * Parses JCR XML files that contains content fragments.
- */
-class JcrXmlFileParser {
-
- private static final Logger log = LoggerFactory.getLogger(JcrXmlFileParser.class);
-
- private static final SAXParserFactory SAX_PARSER_FACTORY;
- static {
- SAX_PARSER_FACTORY = SAXParserFactory.newInstance();
- SAX_PARSER_FACTORY.setNamespaceAware(true);
- }
-
- private JcrXmlFileParser() {
- // static methods only
- }
-
- /**
- * Parse JSON file.
- * @param file File
- * @return Content
- */
- public static Map<String,Object> parse(File file) {
- log.debug("Parse JCR XML content from {}", file.getPath());
- try (FileInputStream fis = new FileInputStream(file)) {
- XmlHandler xmlHandler = new XmlHandler();
- SAXParser parser = SAX_PARSER_FACTORY.newSAXParser();
- parser.parse(fis, xmlHandler);
- if (xmlHandler.hasError()) {
- throw xmlHandler.getError();
- }
- return xmlHandler.getContent();
- }
- catch (IOException | ParserConfigurationException | SAXException ex) {
- log.warn("Error parsing JCR XML content from " + file.getPath(), ex);
- return null;
- }
- }
-
- /**
- * Decodes element or attribute names.
- * @param qname qname
- * @return Decoded name
- */
- static String decodeName(String qname) {
- return ISO9075.decode(qname);
- }
-
- /**
- * Parses XML stream to Map.
- */
- static class XmlHandler extends DefaultHandler {
- private final Map<String,Object> content = new LinkedHashMap<>();
- private final Stack<Map<String,Object>> elements = new Stack<>();
- private SAXParseException error;
-
- public Map<String,Object> getContent() {
- return content;
- }
-
- public boolean hasError() {
- return error != null;
- }
-
- public SAXParseException getError() {
- return error;
- }
-
- @Override
- public void startElement(String uri, String localName, String qName, Attributes attributes)
- throws SAXException {
-
- // prepare map for element
- Map<String,Object> element;
- if (elements.isEmpty()) {
- element = content;
- }
- else {
- element = new HashMap<>();
- elements.peek().put(decodeName(qName), element);
- }
- elements.push(element);
-
- // get attributes
- for (int i=0; i<attributes.getLength(); i++) {
- element.put(decodeName(attributes.getQName(i)), JcrXmlValueConverter.parseValue(attributes.getValue(i)));
- }
- }
-
- @Override
- public void endElement(String uri, String localName, String qName) throws SAXException {
- elements.pop();
- }
-
- @Override
- public void error(SAXParseException ex) throws SAXException {
- this.error = ex;
- }
-
- @Override
- public void fatalError(SAXParseException ex) throws SAXException {
- this.error = ex;
- }
-
- }
-
-}
diff --git a/src/main/java/org/apache/sling/fsprovider/internal/parser/JcrXmlValueConverter.java b/src/main/java/org/apache/sling/fsprovider/internal/parser/JcrXmlValueConverter.java
deleted file mode 100644
index 8a0d0db..0000000
--- a/src/main/java/org/apache/sling/fsprovider/internal/parser/JcrXmlValueConverter.java
+++ /dev/null
@@ -1,168 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.sling.fsprovider.internal.parser;
-
-import java.util.ArrayList;
-import java.util.List;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-
-import org.apache.commons.lang3.ArrayUtils;
-import org.apache.commons.lang3.StringUtils;
-import org.apache.jackrabbit.util.ISO8601;
-
-/**
- * Parses JCR XML files that contains content fragments.
- */
-class JcrXmlValueConverter {
-
- private static final Pattern TYPE_PREFIX = Pattern.compile("^\\{([^\\{\\}]+)\\}(.+)$");
- private static final Pattern VALUE_ARRAY = Pattern.compile("^\\[(.*)\\]$");
-
- private JcrXmlValueConverter() {
- // static methods only
- }
-
- /**
- * Parse JSON value from XML Attribute.
- * @param value XML attribute value
- * @return Value object
- */
- public static Object parseValue(final String rawValue) {
- String value = rawValue;
- String[] valueArray = null;
-
- if (rawValue == null) {
- return null;
- }
-
- // detect type prefix
- String typePrefix = null;
- Matcher typePrefixMatcher = TYPE_PREFIX.matcher(value);
- if (typePrefixMatcher.matches()) {
- typePrefix = typePrefixMatcher.group(1);
- value = typePrefixMatcher.group(2);
- }
-
- // check for array
- Matcher arrayMatcher = VALUE_ARRAY.matcher(value);
- if (arrayMatcher.matches()) {
- value = null;
- valueArray = splitPreserveAllTokens(arrayMatcher.group(1), ',');
- }
-
- // convert values
- if (valueArray != null) {
- Object[] result = new Object[valueArray.length];
- for (int i=0; i<valueArray.length; i++) {
- result[i] = convertValue(valueArray[i], typePrefix, true);
- }
- return result;
- }
- else {
- return convertValue(value, typePrefix, false);
- }
- }
-
- /**
- * Split string preserving all tokens - but ignore separators that are escaped with \.
- * @param str Combined string
- * @param sep Separator
- * @return Tokens
- */
- private static String[] splitPreserveAllTokens(String str, char sep) {
- final int len = str.length();
- if (len == 0) {
- return ArrayUtils.EMPTY_STRING_ARRAY;
- }
- final List<String> list = new ArrayList<String>();
- int i = 0, start = 0;
- boolean match = false;
- boolean lastMatch = false;
- boolean escaped = false;
- while (i < len) {
- if (str.charAt(i) == '\\' && !escaped) {
- escaped = true;
- }
- else {
- if (str.charAt(i) == sep && !escaped) {
- lastMatch = true;
- list.add(str.substring(start, i));
- match = false;
- start = ++i;
- continue;
- }
- lastMatch = false;
- match = true;
- escaped = false;
- }
- i++;
- }
- if (match || lastMatch) {
- list.add(str.substring(start, i));
- }
- return list.toArray(new String[list.size()]);
- }
-
- /**
- * Parse value depending on type prefix.
- * @param value Value
- * @param typePrefix Type prefix
- * @param inArray Value is in array
- * @return Value object
- */
- private static Object convertValue(final String value, final String typePrefix, final boolean inArray) {
- if (typePrefix == null || StringUtils.equals(typePrefix, "Name")) {
- return deescapeStringValue(value, inArray);
- }
- else if (StringUtils.equals(typePrefix, "Boolean")) {
- return Boolean.valueOf(value);
- }
- else if (StringUtils.equals(typePrefix, "Long")) {
- return Long.valueOf(value);
- }
- else if (StringUtils.equals(typePrefix, "Decimal")) {
- return Double.valueOf(value);
- }
- else if (StringUtils.equals(typePrefix, "Date")) {
- return ISO8601.parse(value);
- }
- else {
- throw new IllegalArgumentException("Unexpected type prefix: " + typePrefix);
- }
- }
-
- /**
- * De-escape string value.
- * @param value Escaped string value
- * @param inArray In array
- * @return De-escaped string value
- */
- private static String deescapeStringValue(final String value, final boolean inArray) {
- String descapedValue = value;
- if (inArray) {
- descapedValue = StringUtils.replace(descapedValue, "\\,", ",");
- }
- else if (StringUtils.startsWith(descapedValue, "\\{") || StringUtils.startsWith(descapedValue, "\\[")) {
- descapedValue = descapedValue.substring(1);
- }
- return StringUtils.replace(descapedValue, "\\\\", "\\");
- }
-
-}
diff --git a/src/main/java/org/apache/sling/fsprovider/internal/parser/JsonFileParser.java b/src/main/java/org/apache/sling/fsprovider/internal/parser/JsonFileParser.java
deleted file mode 100644
index 8070e0d..0000000
--- a/src/main/java/org/apache/sling/fsprovider/internal/parser/JsonFileParser.java
+++ /dev/null
@@ -1,125 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.sling.fsprovider.internal.parser;
-
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.IOException;
-import java.util.HashMap;
-import java.util.LinkedHashMap;
-import java.util.Map;
-
-import javax.json.Json;
-import javax.json.JsonArray;
-import javax.json.JsonNumber;
-import javax.json.JsonObject;
-import javax.json.JsonReader;
-import javax.json.JsonReaderFactory;
-import javax.json.JsonString;
-import javax.json.JsonValue;
-import javax.json.stream.JsonParsingException;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-/**
- * Parses JSON files that contains content fragments.
- */
-class JsonFileParser {
-
- private static final Logger log = LoggerFactory.getLogger(JsonFileParser.class);
-
- private static final JsonReaderFactory JSON_READER_FACTORY;
- static {
- // allow comments in JSON files
- Map<String,Object> jsonReaderFactoryConfig = new HashMap<>();
- jsonReaderFactoryConfig.put("org.apache.johnzon.supports-comments", true);
- // workaround for JsonProvider classloader issue until https://issues.apache.org/jira/browse/GERONIMO-6560 is fixed
- ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
- try {
- Thread.currentThread().setContextClassLoader(JsonFileParser.class.getClassLoader());
- JSON_READER_FACTORY = Json.createReaderFactory(jsonReaderFactoryConfig);
- }
- finally {
- Thread.currentThread().setContextClassLoader(oldClassLoader);
- }
- }
-
- private JsonFileParser() {
- // static methods only
- }
-
- /**
- * Parse JSON file.
- * @param file File
- * @return Content
- */
- public static Map<String,Object> parse(File file) {
- log.debug("Parse JSON content from {}", file.getPath());
- try (FileInputStream fis = new FileInputStream(file);
- JsonReader reader = JSON_READER_FACTORY.createReader(fis)) {
- return toMap(reader.readObject());
- }
- catch (IOException | JsonParsingException ex) {
- log.warn("Error parsing JSON content from " + file.getPath(), ex);
- return null;
- }
- }
-
- private static Map<String,Object> toMap(JsonObject object) {
- Map<String,Object> map = new LinkedHashMap<>();
- for (Map.Entry<String, JsonValue> entry : object.entrySet()) {
- map.put(entry.getKey(), convertValue(entry.getValue()));
- }
- return map;
- }
-
- private static Object convertValue(JsonValue value) {
- switch (value.getValueType()) {
- case STRING:
- return ((JsonString)value).getString();
- case NUMBER:
- JsonNumber numberValue = (JsonNumber)value;
- if (numberValue.isIntegral()) {
- return numberValue.longValue();
- }
- else {
- return numberValue.doubleValue();
- }
- case TRUE:
- return true;
- case FALSE:
- return false;
- case NULL:
- return null;
- case ARRAY:
- JsonArray arrayValue = (JsonArray)value;
- Object[] values = new Object[arrayValue.size()];
- for (int i=0; i<values.length; i++) {
- values[i] = convertValue(arrayValue.get(i));
- }
- return values;
- case OBJECT:
- return toMap((JsonObject)value);
- default:
- throw new IllegalArgumentException("Unexpected JSON value type: " + value.getValueType());
- }
- }
-
-}
diff --git a/src/test/java/org/apache/sling/fsprovider/internal/JcrXmlContentTest.java b/src/test/java/org/apache/sling/fsprovider/internal/JcrXmlContentTest.java
index 39f18d4..ded7f29 100644
--- a/src/test/java/org/apache/sling/fsprovider/internal/JcrXmlContentTest.java
+++ b/src/test/java/org/apache/sling/fsprovider/internal/JcrXmlContentTest.java
@@ -90,7 +90,7 @@ public class JcrXmlContentTest {
}
@Test
- public void testJsonContent_Root() {
+ public void testContent_Root() {
Resource underTest = fsroot.getChild("folder3/content");
assertNotNull(underTest);
assertEquals("app:Page", underTest.getValueMap().get("jcr:primaryType", String.class));
@@ -99,7 +99,7 @@ public class JcrXmlContentTest {
}
@Test
- public void testJsonContent_Level1() {
+ public void testContent_Level1() {
Resource underTest = fsroot.getChild("folder3/content/jcr:content");
assertNotNull(underTest);
assertEquals("app:PageContent", underTest.getValueMap().get("jcr:primaryType", String.class));
@@ -108,7 +108,7 @@ public class JcrXmlContentTest {
}
@Test
- public void testJsonContent_Level3() {
+ public void testContent_Level3() {
Resource underTest = fsroot.getChild("folder3/content/jcr:content/content/contentheadline");
assertNotNull(underTest);
assertEquals("nt:unstructured", underTest.getValueMap().get("jcr:primaryType", String.class));
@@ -117,7 +117,7 @@ public class JcrXmlContentTest {
}
@Test
- public void testJsonContent_Datatypes() {
+ public void testContent_Datatypes() {
Resource underTest = fsroot.getChild("folder3/content/jcr:content");
ValueMap props = underTest.getValueMap();
@@ -131,7 +131,7 @@ public class JcrXmlContentTest {
}
@Test
- public void testJsonContent_InvalidPath() {
+ public void testContent_InvalidPath() {
Resource underTest = fsroot.getChild("folder2/content/jcr:content/xyz");
assertNull(underTest);
}
diff --git a/src/test/java/org/apache/sling/fsprovider/internal/JsonContentTest.java b/src/test/java/org/apache/sling/fsprovider/internal/JsonContentTest.java
index 797575b..031812f 100644
--- a/src/test/java/org/apache/sling/fsprovider/internal/JsonContentTest.java
+++ b/src/test/java/org/apache/sling/fsprovider/internal/JsonContentTest.java
@@ -42,6 +42,7 @@ import javax.jcr.nodetype.NodeType;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ValueMap;
+import org.apache.sling.fscontentparser.ParserOptions;
import org.apache.sling.fsprovider.internal.TestUtils.RegisterFsResourcePlugin;
import org.apache.sling.hamcrest.ResourceMatchers;
import org.apache.sling.testing.mock.sling.ResourceResolverType;
@@ -99,7 +100,7 @@ public class JsonContentTest {
}
@Test
- public void testJsonContent_Root() {
+ public void testContent_Root() {
Resource underTest = fsroot.getChild("folder2/content");
assertNotNull(underTest);
assertEquals("app:Page", underTest.getValueMap().get("jcr:primaryType", String.class));
@@ -108,7 +109,7 @@ public class JsonContentTest {
}
@Test
- public void testJsonContent_Level1() {
+ public void testContent_Level1() {
Resource underTest = fsroot.getChild("folder2/content/jcr:content");
assertNotNull(underTest);
assertEquals("app:PageContent", underTest.getValueMap().get("jcr:primaryType", String.class));
@@ -118,7 +119,7 @@ public class JsonContentTest {
}
@Test
- public void testJsonContent_Level5() {
+ public void testContent_Level5() {
Resource underTest = fsroot.getChild("folder2/content/jcr:content/par/image/file/jcr:content");
assertNotNull(underTest);
assertEquals("nt:resource", underTest.getValueMap().get("jcr:primaryType", String.class));
@@ -126,7 +127,7 @@ public class JsonContentTest {
}
@Test
- public void testJsonContent_Datatypes() {
+ public void testContent_Datatypes() {
Resource underTest = fsroot.getChild("folder2/content/toolbar/profiles/jcr:content");
ValueMap props = underTest.getValueMap();
@@ -140,7 +141,7 @@ public class JsonContentTest {
}
@Test
- public void testJsonContent_Datatypes_JCR() throws RepositoryException {
+ public void testContent_Datatypes_JCR() throws RepositoryException {
Resource underTest = fsroot.getChild("folder2/content/toolbar/profiles/jcr:content");
ValueMap props = underTest.getValueMap();
Node node = underTest.adaptTo(Node.class);
@@ -218,11 +219,11 @@ public class JsonContentTest {
@Test
public void testFallbackNodeType() throws RepositoryException {
Resource underTest = fsroot.getChild("folder2/content/jcr:content/par/title_2");
- assertEquals(NodeType.NT_UNSTRUCTURED, underTest.adaptTo(Node.class).getPrimaryNodeType().getName());
+ assertEquals(ParserOptions.DEFAULT_PRIMARY_TYPE, underTest.adaptTo(Node.class).getPrimaryNodeType().getName());
}
@Test
- public void testJsonContent_InvalidPath() {
+ public void testContent_InvalidPath() {
Resource underTest = fsroot.getChild("folder2/content/jcr:content/xyz");
assertNull(underTest);
}
diff --git a/src/test/java/org/apache/sling/fsprovider/internal/parser/ContentFileParserTest.java b/src/test/java/org/apache/sling/fsprovider/internal/parser/ContentFileParserUtilTest.java
similarity index 86%
rename from src/test/java/org/apache/sling/fsprovider/internal/parser/ContentFileParserTest.java
rename to src/test/java/org/apache/sling/fsprovider/internal/parser/ContentFileParserUtilTest.java
index 290ea21..9bdaa51 100644
--- a/src/test/java/org/apache/sling/fsprovider/internal/parser/ContentFileParserTest.java
+++ b/src/test/java/org/apache/sling/fsprovider/internal/parser/ContentFileParserUtilTest.java
@@ -27,13 +27,13 @@ import java.util.Map;
import org.junit.Test;
-public class ContentFileParserTest {
+public class ContentFileParserUtilTest {
@SuppressWarnings("unchecked")
@Test
public void testParseJson() {
File file = new File("src/test/resources/fs-test/folder2/content.json");
- Map<String,Object> content = ContentFileParser.parse(file);
+ Map<String,Object> content = ContentFileParserUtil.parse(file);
assertNotNull(content);
assertEquals("app:Page", content.get("jcr:primaryType"));
assertEquals("app:PageContent", ((Map<String,Object>)content.get("jcr:content")).get("jcr:primaryType"));
@@ -42,7 +42,7 @@ public class ContentFileParserTest {
@Test
public void testParseInvalidJson() {
File file = new File("src/test/resources/invalid-test/invalid.json");
- Map<String,Object> content = ContentFileParser.parse(file);
+ Map<String,Object> content = ContentFileParserUtil.parse(file);
assertNull(content);
}
@@ -50,7 +50,7 @@ public class ContentFileParserTest {
@Test
public void testParseJcrXml() {
File file = new File("src/test/resources/fs-test/folder3/content.jcr.xml");
- Map<String,Object> content = ContentFileParser.parse(file);
+ Map<String,Object> content = ContentFileParserUtil.parse(file);
assertNotNull(content);
assertEquals("app:Page", content.get("jcr:primaryType"));
assertEquals("app:PageContent", ((Map<String,Object>)content.get("jcr:content")).get("jcr:primaryType"));
@@ -59,7 +59,7 @@ public class ContentFileParserTest {
@Test
public void testParseInvalidJcrXml() {
File file = new File("src/test/resources/invalid-test/invalid.jcr.xml");
- Map<String,Object> content = ContentFileParser.parse(file);
+ Map<String,Object> content = ContentFileParserUtil.parse(file);
assertNull(content);
}
diff --git a/src/test/java/org/apache/sling/fsprovider/internal/parser/JcrXmlFileParserTest.java b/src/test/java/org/apache/sling/fsprovider/internal/parser/JcrXmlFileParserTest.java
deleted file mode 100644
index 7cccbcf..0000000
--- a/src/test/java/org/apache/sling/fsprovider/internal/parser/JcrXmlFileParserTest.java
+++ /dev/null
@@ -1,34 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.sling.fsprovider.internal.parser;
-
-import static org.junit.Assert.assertEquals;
-
-import org.apache.jackrabbit.util.ISO9075;
-import org.junit.Test;
-
-public class JcrXmlFileParserTest {
-
- @Test
- public void testDecodeName() {
- assertEquals("jcr:title", JcrXmlFileParser.decodeName("jcr:" + ISO9075.encode("title")));
- assertEquals("sling:123", JcrXmlFileParser.decodeName("sling:" + ISO9075.encode("123")));
- }
-
-}
diff --git a/src/test/java/org/apache/sling/fsprovider/internal/parser/JcrXmlValueConverterTest.java b/src/test/java/org/apache/sling/fsprovider/internal/parser/JcrXmlValueConverterTest.java
deleted file mode 100644
index 15fe989..0000000
--- a/src/test/java/org/apache/sling/fsprovider/internal/parser/JcrXmlValueConverterTest.java
+++ /dev/null
@@ -1,105 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.sling.fsprovider.internal.parser;
-
-import static org.apache.sling.fsprovider.internal.parser.JcrXmlValueConverter.parseValue;
-import static org.junit.Assert.assertArrayEquals;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNull;
-
-import java.util.Calendar;
-
-import org.junit.Test;
-
-public class JcrXmlValueConverterTest {
-
- @Test
- public void testNull() {
- assertNull(parseValue(null));
- }
-
- @Test(expected = IllegalArgumentException.class)
- public void testInvalid() {
- parseValue("{InvalidType}xyz");
- }
-
- @Test
- public void testString() {
- assertEquals("myString", parseValue("myString"));
- assertEquals("prop", "myString [ ] { } \\ ,", parseValue("myString [ ] { } \\\\ ,"));
- assertEquals("{myString}", parseValue("\\{myString}"));
- assertEquals("aaa{myString}", parseValue("aaa{myString}"));
- assertEquals("[myString]", parseValue("\\[myString]"));
- assertEquals("aaa[myString]", parseValue("aaa[myString]"));
- }
-
- @Test
- public void testStringArray() {
- assertArrayEquals(new Object[] { "myString1", "myString2" }, (Object[]) parseValue("[myString1,myString2]"));
- assertArrayEquals(new Object[] { "myString1,[]\\äöü߀", "myString2", "myString3 [ ] { } \\ ,", "", "[myString5]", "{myString6}" },
- (Object[]) parseValue("[myString1\\,[]\\\\äöü߀,myString2,myString3 [ ] { } \\\\ \\,,,[myString5],{myString6}]"));
- }
-
- @Test
- public void testBoolean() {
- assertEquals(true, parseValue("{Boolean}true"));
- assertEquals(false, parseValue("{Boolean}false"));
- }
-
- @Test
- public void testBooleanArray() {
- assertArrayEquals(new Object[] { true, false }, (Object[]) parseValue("{Boolean}[true,false]"));
- }
-
- @Test
- public void testLong() {
- assertEquals(1L, parseValue("{Long}1"));
- assertEquals(10000000000L, parseValue("{Long}10000000000"));
- }
-
- @Test
- public void testLongArray() {
- assertArrayEquals(new Object[] { 1L, 2L }, (Object[]) parseValue("{Long}[1,2]"));
- assertArrayEquals(new Object[] { 10000000000L, 20000000000L }, (Object[]) parseValue("{Long}[10000000000,20000000000]"));
- }
-
- @Test
- public void testDouble() {
- assertEquals(1.234d, parseValue("{Decimal}1.234"));
- }
-
- @Test
- public void testDoubleArray() {
- assertArrayEquals(new Object[] { 1.234d, 2.345d }, (Object[]) parseValue("{Decimal}[1.234,2.345]"));
- }
-
- @Test
- public void testCalendar() {
- Calendar value = (Calendar)parseValue("{Date}2010-09-05T15:10:20.000Z");
- assertEquals(2010, value.get(Calendar.YEAR));
- assertEquals(8, value.get(Calendar.MONTH));
- assertEquals(5, value.get(Calendar.DAY_OF_MONTH));
- }
-
- @Test
- public void testStringArrayRepPrivileges() {
- assertArrayEquals(new Object[] { "rep:write", "crx:replicate", "jcr:read" }, (Object[]) parseValue("{Name}[rep:write,crx:replicate,jcr:read]"));
- }
-
-}
--
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.