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

cziegeler pushed a commit to branch master
in repository 
https://gitbox.apache.org/repos/asf/sling-slingfeature-maven-plugin.git


The following commit(s) were added to refs/heads/master by this push:
     new d11f25a  SLING-9725 : Better support for text extensions
d11f25a is described below

commit d11f25a391e534e515362e56ddc1d49bc9fca54c
Author: Carsten Ziegeler <[email protected]>
AuthorDate: Thu Oct 1 10:07:43 2020 +0200

    SLING-9725 : Better support for text extensions
---
 .gitignore                                         |  1 +
 pom.xml                                            |  3 +-
 .../apache/sling/feature/maven/JSONFeatures.java   | 34 +++++++++++
 .../apache/sling/feature/maven/Preprocessor.java   |  4 ++
 .../feature/maven/mojos/AbstractFeatureMojo.java   |  4 ++
 .../sling/feature/maven/JSONFeaturesTest.java      | 68 ++++++++++++++++++++++
 src/test/resources/features/myfeature-txtext.txt   |  1 +
 src/test/resources/features/myfeature.json         |  5 ++
 src/test/resources/features/myfeature2-config.xml  |  1 +
 src/test/resources/features/myfeature2.json        |  5 ++
 10 files changed, 125 insertions(+), 1 deletion(-)

diff --git a/.gitignore b/.gitignore
index 0ddae23..165da3d 100644
--- a/.gitignore
+++ b/.gitignore
@@ -11,6 +11,7 @@ maven-eclipse.xml
 *.ipr
 *.iws
 *.bak
+.vscode/
 .vlt
 .DS_Store
 jcr.log
diff --git a/pom.xml b/pom.xml
index 9b43b1a..52d8e5d 100644
--- a/pom.xml
+++ b/pom.xml
@@ -99,7 +99,8 @@
                         <exclude>src/site/markdown/**</exclude>
                         <exclude>src/test/resources/**/*.txt</exclude>
                         <exclude>src/test/resources/**/*.feature</exclude>
-                        
<exclude>src/test/resources/META-INF/services/*</exclude>
+                       <exclude>src/test/resources/**/*.xml</exclude>
+                       
<exclude>src/test/resources/META-INF/services/*</exclude>
                         <exclude>src/it/**/*.json</exclude>
                     </excludes>
                 </configuration>
diff --git a/src/main/java/org/apache/sling/feature/maven/JSONFeatures.java 
b/src/main/java/org/apache/sling/feature/maven/JSONFeatures.java
index 8dba188..babe4b3 100644
--- a/src/main/java/org/apache/sling/feature/maven/JSONFeatures.java
+++ b/src/main/java/org/apache/sling/feature/maven/JSONFeatures.java
@@ -16,11 +16,13 @@
  */
 package org.apache.sling.feature.maven;
 
+import java.io.File;
 import java.io.IOException;
 import java.io.Reader;
 import java.io.StringReader;
 import java.io.StringWriter;
 import java.io.Writer;
+import java.nio.charset.StandardCharsets;
 import java.util.Collections;
 import java.util.Map;
 
@@ -34,8 +36,11 @@ import javax.json.JsonWriter;
 import javax.json.stream.JsonGenerator;
 import javax.json.stream.JsonGeneratorFactory;
 
+import org.apache.commons.io.FileUtils;
 import org.apache.felix.cm.json.Configurations;
 import org.apache.sling.feature.ArtifactId;
+import org.apache.sling.feature.Extension;
+import org.apache.sling.feature.ExtensionType;
 import org.apache.sling.feature.Feature;
 import org.apache.sling.feature.io.json.FeatureJSONWriter;
 
@@ -112,4 +117,33 @@ public class JSONFeatures {
             FeatureJSONWriter.write(writer, feature);
         }
     }
+
+    private static final String FILE_PREFIX = "@file";
+
+    /**
+     * Check for extensions of type text and if they reference a file
+     */
+       public static void handleExtensions(final Feature feature, final File 
file) throws IOException {
+        for(final Extension ext : feature.getExtensions()) {
+            if ( ext.getType() == ExtensionType.TEXT && 
ext.getText().startsWith(FILE_PREFIX)) {
+                final int pos = file.getName().lastIndexOf(".");
+                final String baseName = pos == -1 ? file.getName() : 
file.getName().substring(0, pos);
+                final String fileName;
+                if ( FILE_PREFIX.equals(ext.getText()) ) {
+                    fileName = 
baseName.concat("-").concat(ext.getName()).concat(".txt");
+                } else {
+                    if ( 
!ext.getText().substring(FILE_PREFIX.length()).startsWith(":") ) {
+                        throw new IOException("Invalid file reference: " + 
ext.getText());
+                    }
+                    fileName = 
baseName.concat("-").concat(ext.getText().substring(FILE_PREFIX.length() + 1));
+                }
+                final File txtFile = new File(file.getParentFile(), fileName);
+                if ( !txtFile.exists() || !txtFile.isFile()) {
+                    throw new IOException("Extension text file " + 
txtFile.getAbsolutePath() + " not found.");
+                }
+                final String contents = FileUtils.readFileToString(txtFile, 
StandardCharsets.UTF_8);
+                ext.setText(contents);
+            }
+        }
+       }
 }
diff --git a/src/main/java/org/apache/sling/feature/maven/Preprocessor.java 
b/src/main/java/org/apache/sling/feature/maven/Preprocessor.java
index 913b637..bc077ec 100644
--- a/src/main/java/org/apache/sling/feature/maven/Preprocessor.java
+++ b/src/main/java/org/apache/sling/feature/maven/Preprocessor.java
@@ -312,7 +312,11 @@ public class Preprocessor {
 
                     ProjectHelper.checkFeatureId(info.project, feature);
 
+                    // Extension handling
+                    JSONFeatures.handleExtensions(feature, file);
+
                     ProjectHelper.setFeatureInfo(info.project, feature);
+
                     this.postProcessReadFeature(feature);
                     (config.isTestConfig() ? info.testFeatures : 
info.features).put(file.toPath().normalize().toFile().getAbsolutePath(), 
feature);
 
diff --git 
a/src/main/java/org/apache/sling/feature/maven/mojos/AbstractFeatureMojo.java 
b/src/main/java/org/apache/sling/feature/maven/mojos/AbstractFeatureMojo.java
index 8abc426..7f8bb33 100644
--- 
a/src/main/java/org/apache/sling/feature/maven/mojos/AbstractFeatureMojo.java
+++ 
b/src/main/java/org/apache/sling/feature/maven/mojos/AbstractFeatureMojo.java
@@ -42,6 +42,7 @@ import org.apache.sling.feature.builder.FeatureProvider;
 import org.apache.sling.feature.io.json.FeatureJSONReader;
 import org.apache.sling.feature.maven.FeatureConstants;
 import org.apache.sling.feature.maven.FeatureProjectConfig;
+import org.apache.sling.feature.maven.JSONFeatures;
 import org.apache.sling.feature.maven.ProjectHelper;
 
 /**
@@ -276,6 +277,9 @@ public abstract class AbstractFeatureMojo extends 
AbstractMojo {
 
                         ProjectHelper.checkFeatureId(project, feature);
 
+                        // Extension handling
+                        JSONFeatures.handleExtensions(feature, file);
+
                         ProjectHelper.setFeatureInfo(project, feature);
 
                         // Add feature to map of features
diff --git a/src/test/java/org/apache/sling/feature/maven/JSONFeaturesTest.java 
b/src/test/java/org/apache/sling/feature/maven/JSONFeaturesTest.java
new file mode 100644
index 0000000..bb2d488
--- /dev/null
+++ b/src/test/java/org/apache/sling/feature/maven/JSONFeaturesTest.java
@@ -0,0 +1,68 @@
+/*
+ * 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.feature.maven;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+import java.net.URISyntaxException;
+import java.net.URL;
+
+import org.apache.sling.feature.Extension;
+import org.apache.sling.feature.Feature;
+import org.apache.sling.feature.io.json.FeatureJSONReader;
+import org.junit.Test;
+
+public class JSONFeaturesTest {
+    
+    @Test public void testFileReferenceInExtension() throws IOException, 
URISyntaxException {
+        final URL url = 
this.getClass().getResource("/features/myfeature.json");
+        final File file = new File(url.toURI());
+
+        try ( final FileReader r = new FileReader(file)) {
+            final Feature feature = FeatureJSONReader.read(r, 
file.getAbsolutePath());
+            final Extension ext = feature.getExtensions().getByName("txtext");
+            assertNotNull(ext);
+
+            assertEquals("@file", ext.getText());
+
+            JSONFeatures.handleExtensions(feature, file);
+
+            assertEquals("Hello World", ext.getText());
+        }
+    }
+
+    @Test public void testXMLFileReferenceInExtension() throws IOException, 
URISyntaxException {
+        final URL url = 
this.getClass().getResource("/features/myfeature2.json");
+        final File file = new File(url.toURI());
+
+        try ( final FileReader r = new FileReader(file)) {
+            final Feature feature = FeatureJSONReader.read(r, 
file.getAbsolutePath());
+            final Extension ext = feature.getExtensions().getByName("txtxml");
+            assertNotNull(ext);
+
+            assertEquals("@file:config.xml", ext.getText());
+
+            JSONFeatures.handleExtensions(feature, file);
+
+            assertEquals("<a>XML</a>", ext.getText());
+        }
+    }
+}
diff --git a/src/test/resources/features/myfeature-txtext.txt 
b/src/test/resources/features/myfeature-txtext.txt
new file mode 100644
index 0000000..5e1c309
--- /dev/null
+++ b/src/test/resources/features/myfeature-txtext.txt
@@ -0,0 +1 @@
+Hello World
\ No newline at end of file
diff --git a/src/test/resources/features/myfeature.json 
b/src/test/resources/features/myfeature.json
new file mode 100644
index 0000000..37df563
--- /dev/null
+++ b/src/test/resources/features/myfeature.json
@@ -0,0 +1,5 @@
+{
+    "id":"g:a:1.0.0",
+
+    "txtext:TEXT|false:": "@file"
+}
\ No newline at end of file
diff --git a/src/test/resources/features/myfeature2-config.xml 
b/src/test/resources/features/myfeature2-config.xml
new file mode 100644
index 0000000..eaade7e
--- /dev/null
+++ b/src/test/resources/features/myfeature2-config.xml
@@ -0,0 +1 @@
+<a>XML</a>
\ No newline at end of file
diff --git a/src/test/resources/features/myfeature2.json 
b/src/test/resources/features/myfeature2.json
new file mode 100644
index 0000000..6cc5775
--- /dev/null
+++ b/src/test/resources/features/myfeature2.json
@@ -0,0 +1,5 @@
+{
+    "id":"g:a:1.0.0",
+
+    "txtxml:TEXT|false:": "@file:config.xml"
+}
\ No newline at end of file

Reply via email to