You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ra...@apache.org on 2019/07/22 09:43:57 UTC

[sling-org-apache-sling-contentparser-json] 03/08: SLING-8570 - Extract a generic Content Parser API from org.apache.sling.jcr.contentparser with pluggable implementations

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

radu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-contentparser-json.git

commit e6ac640b671ef2d660191b25dffff3f0bf442d05
Author: Radu Cotescu <ra...@apache.org>
AuthorDate: Fri Jul 12 16:33:23 2019 +0200

    SLING-8570 - Extract a generic Content Parser API from org.apache.sling.jcr.contentparser with pluggable implementations
    
    * extracted a test utils module, which can be used by all contentparser
    implementations
    * added a XML Content Parser implementation
---
 pom.xml                                            |  6 ++
 .../json/internal/JsonContentParserTest.java       |  3 +-
 .../contentparser/json/internal/TestUtils.java     | 60 -------------------
 .../json/internal/mapsupport/ContentElement.java   | 52 ----------------
 .../internal/mapsupport/ContentElementHandler.java | 69 ----------------------
 .../internal/mapsupport/ContentElementImpl.java    | 68 ---------------------
 6 files changed, 8 insertions(+), 250 deletions(-)

diff --git a/pom.xml b/pom.xml
index 3fc188b..7e6dbd9 100644
--- a/pom.xml
+++ b/pom.xml
@@ -88,6 +88,12 @@
             <scope>provided</scope>
         </dependency>
         <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.contentparser.testutils</artifactId>
+            <version>0.9.0-SNAPSHOT</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
             <groupId>junit</groupId>
             <artifactId>junit</artifactId>
             <scope>test</scope>
diff --git a/src/test/java/org/apache/sling/contentparser/json/internal/JsonContentParserTest.java b/src/test/java/org/apache/sling/contentparser/json/internal/JsonContentParserTest.java
index 3099ae8..b52e934 100644
--- a/src/test/java/org/apache/sling/contentparser/json/internal/JsonContentParserTest.java
+++ b/src/test/java/org/apache/sling/contentparser/json/internal/JsonContentParserTest.java
@@ -29,7 +29,8 @@ import org.apache.sling.contentparser.api.ContentParser;
 import org.apache.sling.contentparser.api.JsonParserFeature;
 import org.apache.sling.contentparser.api.ParseException;
 import org.apache.sling.contentparser.api.ParserOptions;
-import org.apache.sling.contentparser.json.internal.mapsupport.ContentElement;
+import org.apache.sling.contentparser.testutils.TestUtils;
+import org.apache.sling.contentparser.testutils.mapsupport.ContentElement;
 import org.junit.Before;
 import org.junit.Test;
 
diff --git a/src/test/java/org/apache/sling/contentparser/json/internal/TestUtils.java b/src/test/java/org/apache/sling/contentparser/json/internal/TestUtils.java
deleted file mode 100644
index 0d81e36..0000000
--- a/src/test/java/org/apache/sling/contentparser/json/internal/TestUtils.java
+++ /dev/null
@@ -1,60 +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.contentparser.json.internal;
-
-import java.io.BufferedInputStream;
-import java.io.ByteArrayInputStream;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.IOException;
-import java.nio.charset.StandardCharsets;
-
-import org.apache.sling.contentparser.api.ContentParser;
-import org.apache.sling.contentparser.api.ParserOptions;
-import org.apache.sling.contentparser.json.internal.mapsupport.ContentElement;
-import org.apache.sling.contentparser.json.internal.mapsupport.ContentElementHandler;
-
-public final class TestUtils {
-    
-    private TestUtils() {
-        // static methods only
-    }
-
-    static ContentElement parse(ContentParser contentParser, File file) throws IOException {
-        return parse(contentParser, new ParserOptions(), file);
-    }
-
-    static ContentElement parse(ContentParser contentParser, ParserOptions parserOptions, File file) throws IOException {
-        try (FileInputStream fis = new FileInputStream(file);
-             BufferedInputStream bis = new BufferedInputStream(fis)) {
-            ContentElementHandler handler = new ContentElementHandler();
-            contentParser.parse(handler, bis, parserOptions);
-            return handler.getRoot();
-        }
-    }
-    
-    static ContentElement parse(ContentParser contentParser, String jsonContent) throws IOException {
-        try (ByteArrayInputStream is = new ByteArrayInputStream(jsonContent.getBytes(StandardCharsets.UTF_8))) {
-            ContentElementHandler handler = new ContentElementHandler();
-            contentParser.parse(handler, is, new ParserOptions());
-            return handler.getRoot();
-        }
-    }
-    
-}
diff --git a/src/test/java/org/apache/sling/contentparser/json/internal/mapsupport/ContentElement.java b/src/test/java/org/apache/sling/contentparser/json/internal/mapsupport/ContentElement.java
deleted file mode 100644
index 0a4457e..0000000
--- a/src/test/java/org/apache/sling/contentparser/json/internal/mapsupport/ContentElement.java
+++ /dev/null
@@ -1,52 +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.contentparser.json.internal.mapsupport;
-
-import java.util.Map;
-
-/**
- * Represents a resource or node in the content hierarchy.
- */
-public interface ContentElement {
-
-    /**
-     * @return Resource name. The root resource has no name (null).
-     */
-    String getName();
-    
-    /**
-     * Properties of this resource.
-     * @return Properties (keys, values)
-     */
-    Map<String, Object> getProperties();
-    
-    /**
-     * Get children of current resource. The Map preserves the ordering of children.
-     * @return Children (child names, child objects)
-     */
-    Map<String, ContentElement> getChildren();
-    
-    /**
-     * Get child or descendant
-     * @param path Relative path to address child or one of it's descendants (use "/" as hierarchy separator).
-     * @return Child or null if no child found with this path
-     */
-    ContentElement getChild(String path);
-    
-}
diff --git a/src/test/java/org/apache/sling/contentparser/json/internal/mapsupport/ContentElementHandler.java b/src/test/java/org/apache/sling/contentparser/json/internal/mapsupport/ContentElementHandler.java
deleted file mode 100644
index bc21ec0..0000000
--- a/src/test/java/org/apache/sling/contentparser/json/internal/mapsupport/ContentElementHandler.java
+++ /dev/null
@@ -1,69 +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.contentparser.json.internal.mapsupport;
-
-import java.util.Map;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-
-import org.apache.commons.lang3.StringUtils;
-import org.apache.sling.contentparser.api.ContentHandler;
-
-/**
- * {@link ContentHandler} implementation that produces a tree of {@link ContentElement} items.
- */
-public class ContentElementHandler implements ContentHandler {
-    
-    private ContentElement root;
-    private Pattern PATH_PATTERN = Pattern.compile("^((/[^/]+)*)(/([^/]+))$"); 
-
-    @Override
-    public void resource(String path, Map<String, Object> properties) {
-        if (StringUtils.equals(path, "/")) {
-            root = new ContentElementImpl(null, properties);
-        }
-        else {
-            if (root == null) {
-                throw new RuntimeException("Root resource not set.");
-            }
-            Matcher matcher = PATH_PATTERN.matcher(path);
-            if (!matcher.matches()) {
-                throw new RuntimeException("Unexpected path:" + path);
-            }
-            String relativeParentPath = StringUtils.stripStart(matcher.group(1), "/");
-            String name = matcher.group(4);
-            ContentElement parent;
-            if (StringUtils.isEmpty(relativeParentPath)) {
-                parent = root;
-            }
-            else {
-                parent = root.getChild(relativeParentPath);
-            }
-            if (parent == null) {
-                throw new RuntimeException("Parent '" + relativeParentPath + "' does not exist.");
-            }
-            parent.getChildren().put(name, new ContentElementImpl(name, properties));
-        }
-    }
-    
-    public ContentElement getRoot() {
-        return root;
-    }
-
-}
diff --git a/src/test/java/org/apache/sling/contentparser/json/internal/mapsupport/ContentElementImpl.java b/src/test/java/org/apache/sling/contentparser/json/internal/mapsupport/ContentElementImpl.java
deleted file mode 100644
index ddbf9bd..0000000
--- a/src/test/java/org/apache/sling/contentparser/json/internal/mapsupport/ContentElementImpl.java
+++ /dev/null
@@ -1,68 +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.contentparser.json.internal.mapsupport;
-
-import java.util.LinkedHashMap;
-import java.util.Map;
-
-import org.apache.commons.lang3.StringUtils;
-
-final class ContentElementImpl implements ContentElement {
-    
-    private final String name;
-    private final Map<String, Object> properties;
-    private final Map<String, ContentElement> children = new LinkedHashMap<>();
-    
-    public ContentElementImpl(String name, Map<String, Object> properties) {
-        this.name = name;
-        this.properties = properties;
-    }
-
-    @Override
-    public String getName() {
-        return name;
-    }
-
-    @Override
-    public Map<String, Object> getProperties() {
-        return properties;
-    }
-
-    @Override
-    public Map<String, ContentElement> getChildren() {
-        return children;
-    }
-
-    @Override
-    public ContentElement getChild(String path) {
-        String name = StringUtils.substringBefore(path, "/");
-        ContentElement child = children.get(name);
-        if (child == null) {
-          return null;
-        }
-        String remainingPath = StringUtils.substringAfter(path, "/");
-        if (StringUtils.isEmpty(remainingPath)) {
-          return child;
-        }
-        else {
-          return child.getChild(remainingPath);
-        }
-    }
-
-}