SLING-9177 : Support same feature file format as slingfeature maven plugin
diff --git a/src/main/java/org/apache/sling/maven/slingstart/FeatureModelConverter.java b/src/main/java/org/apache/sling/maven/slingstart/FeatureModelConverter.java
index fc92783..5296c66 100644
--- a/src/main/java/org/apache/sling/maven/slingstart/FeatureModelConverter.java
+++ b/src/main/java/org/apache/sling/maven/slingstart/FeatureModelConverter.java
@@ -18,15 +18,25 @@
 
 import java.io.File;
 import java.io.FileInputStream;
+import java.io.FileReader;
 import java.io.FileWriter;
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.io.Reader;
-import java.nio.file.Files;
+import java.io.StringReader;
+import java.io.StringWriter;
+import java.io.Writer;
 import java.util.ArrayList;
 import java.util.List;
-import java.util.Properties;
+import java.util.Map;
 
+import javax.json.Json;
+import javax.json.JsonObject;
+import javax.json.JsonReader;
+import javax.json.JsonValue;
+import javax.json.stream.JsonGenerator;
+
+import org.apache.felix.configurator.impl.json.JSMin;
 import org.apache.maven.MavenExecutionException;
 import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
 import org.apache.maven.artifact.resolver.ArtifactResolver;
@@ -105,11 +115,26 @@
 
         List<File> substedFiles = new ArrayList<>();
         for (File f : files) {
-            try {
-                substedFiles.add(substituteVars(project, f, processedFeaturesDir));
-            } catch (IOException e) {
-                throw new MavenExecutionException("Problem processing feature file " + f.getAbsolutePath(), e);
+            File outFile = new File(processedFeaturesDir, f.getName());
+            if (!outFile.exists() || outFile.lastModified() <= f.lastModified()) {
+                try {
+                    final String suggestedClassifier;
+                    if (!f.getName().equals("feature.json")) {
+                        final int lastDot = f.getName().lastIndexOf('.');
+                        suggestedClassifier = f.getName().substring(0, lastDot);
+                    } else {
+                        suggestedClassifier = null;
+                    }
+                    final String json = readFeatureFile(project, f, suggestedClassifier);
+
+                    try (final Writer fileWriter = new FileWriter(outFile)) {
+                        fileWriter.write(json);
+                    }
+                } catch (IOException e) {
+                    throw new MavenExecutionException("Problem processing feature file " + f.getAbsolutePath(), e);
+                }
             }
+            substedFiles.add(outFile);
         }
 
         File targetDir = new File(project.getBuild().getDirectory(), BUILD_DIR);
@@ -125,47 +150,74 @@
         }
     }
 
-    private static File substituteVars(MavenProject project, File f, File processedFeaturesDir) throws IOException {
-        File file = new File(processedFeaturesDir, f.getName());
+    /**
+     * Read the json file, minify it, add id if missing and replace variables
+     *
+     * @param file The json file
+     * @return The read and minified JSON
+     */
+    public static String readFeatureFile(final MavenProject project, final File file,
+            final String suggestedClassifier) {
+        final StringBuilder sb = new StringBuilder();
+        try (final Reader reader = new FileReader(file)) {
+            final char[] buf = new char[4096];
+            int l = 0;
 
-        if (file.exists() && file.lastModified() > f.lastModified()) {
-            // The file already exists, so we don't need to write it again
-            return file;
+            while ((l = reader.read(buf)) > 0) {
+                sb.append(buf, 0, l);
+            }
+        } catch (final IOException io) {
+            throw new RuntimeException("Unable to read feature " + file.getAbsolutePath(), io);
+        }
+        final String readJson = sb.toString();
+
+        // minify JSON (remove comments)
+        String json;
+        try (final Writer out = new StringWriter(); final Reader in = new StringReader(readJson)) {
+            final JSMin min = new JSMin(in, out);
+            min.jsmin();
+            json = out.toString();
+        } catch (final IOException e) {
+            throw new RuntimeException("Unable to read feature file " + file.getAbsolutePath(), e);
         }
 
-        try (FileWriter fw = new FileWriter(file)) {
-            for (String s : Files.readAllLines(f.toPath())) {
-                fw.write(replaceVars(project, s));
-                fw.write(System.getProperty("line.separator"));
+        // check if "id" is set
+        try (final JsonReader reader = Json.createReader(new StringReader(json))) {
+            final JsonObject obj = reader.readObject();
+            if (!obj.containsKey("id")) {
+                final StringBuilder isb = new StringBuilder();
+                isb.append(project.getGroupId());
+                isb.append(':');
+                isb.append(project.getArtifactId());
+                isb.append(':');
+                isb.append("slingosgifeature");
+
+                if (suggestedClassifier != null) {
+                    isb.append(':');
+                    isb.append(suggestedClassifier);
+                }
+                isb.append(':');
+                isb.append(project.getVersion());
+
+                final StringWriter writer = new StringWriter();
+
+                try (final JsonGenerator generator = Json.createGenerator(writer)) {
+                    generator.writeStartObject();
+
+                    generator.write("id", isb.toString());
+
+                    for (final Map.Entry<String, JsonValue> entry : obj.entrySet()) {
+                        generator.write(entry.getKey(), entry.getValue());
+                    }
+                    generator.writeEnd();
+                }
+
+                json = writer.toString();
             }
         }
-        return file;
-    }
 
-    static String replaceVars(MavenProject project, String s) {
-        // There must be a better way than enumerating all these?
-        s = replaceAll(s, "project.groupId", project.getGroupId());
-        s = replaceAll(s, "project.artifactId", project.getArtifactId());
-        s = replaceAll(s, "project.version", project.getVersion());
-        s = replaceAll(s, "project.osgiVersion", getOSGiVersion(project.getVersion()));
-
-        s = replaceProperties(System.getProperties(), s);
-        s = replaceProperties(project.getProperties(), s);
-
-        return s;
-    }
-
-    private static String replaceProperties(Properties props, String s) {
-        if (props != null) {
-            for (String key : props.stringPropertyNames()) {
-                s = replaceAll(s, key, props.getProperty(key));
-            }
-        }
-        return s;
-    }
-
-    private static String replaceAll(String s, String key, String value) {
-        return s.replaceAll("\\Q${" + key + "}\\E", value);
+        // replace variables
+        return Substitution.replaceMavenVars(project, json);
     }
 
     /**
diff --git a/src/main/java/org/apache/sling/maven/slingstart/Substitution.java b/src/main/java/org/apache/sling/maven/slingstart/Substitution.java
new file mode 100644
index 0000000..a2d5f8c
--- /dev/null
+++ b/src/main/java/org/apache/sling/maven/slingstart/Substitution.java
@@ -0,0 +1,101 @@
+/*
+ * 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.maven.slingstart;
+
+import java.util.Collections;
+import java.util.List;
+
+import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
+import org.apache.maven.project.MavenProject;
+import org.codehaus.plexus.interpolation.InterpolationException;
+import org.codehaus.plexus.interpolation.ObjectBasedValueSource;
+import org.codehaus.plexus.interpolation.PrefixAwareRecursionInterceptor;
+import org.codehaus.plexus.interpolation.PrefixedValueSourceWrapper;
+import org.codehaus.plexus.interpolation.PropertiesBasedValueSource;
+import org.codehaus.plexus.interpolation.RecursionInterceptor;
+import org.codehaus.plexus.interpolation.RegexBasedInterpolator;
+
+import aQute.bnd.version.MavenVersion;
+
+public class Substitution {
+    public static String replaceMavenVars(MavenProject project, String s) {
+        RegexBasedInterpolator interpolator = new RegexBasedInterpolator();
+        project.getProperties().setProperty("project.osgiVersion", getOSGiVersion(project.getVersion()));
+        interpolator.addValueSource(new PropertiesBasedValueSource(System.getProperties()));
+        interpolator.addValueSource(new PropertiesBasedValueSource(project.getProperties()));
+
+        List<String> synonymPrefixes = Collections.singletonList("project.");
+
+        PrefixedValueSourceWrapper modelWrapper = new PrefixedValueSourceWrapper(
+                new ObjectBasedValueSource(project),
+                synonymPrefixes,
+                true);
+        interpolator.addValueSource( modelWrapper );
+
+        RecursionInterceptor recursionInterceptor = new PrefixAwareRecursionInterceptor(synonymPrefixes, true);
+
+        try {
+            return interpolator.interpolate(s, recursionInterceptor);
+        } catch (InterpolationException e) {
+            throw new RuntimeException("An error occurred while interpolating variables to JSON:\n" + s, e);
+        }
+    }
+
+    /**
+     * Remove leading zeros for a version part
+     */
+    private static String cleanVersionString(final String version) {
+        final StringBuilder sb = new StringBuilder();
+        boolean afterDot = false;
+        for(int i=0;i<version.length(); i++) {
+            final char c = version.charAt(i);
+            if ( c == '.' ) {
+                if (afterDot == true ) {
+                    sb.append('0');
+                }
+                afterDot = true;
+                sb.append(c);
+            } else if ( afterDot && c == '0' ) {
+                // skip
+            } else if ( afterDot && c == '-' ) {
+                sb.append('0');
+                sb.append(c);
+                afterDot = false;
+            } else {
+                afterDot = false;
+                sb.append(c);
+            }
+        }
+        return sb.toString();
+    }
+
+    public static String getOSGiVersion(final String version) {
+        final DefaultArtifactVersion dav = new DefaultArtifactVersion(cleanVersionString(version));
+        final StringBuilder sb = new StringBuilder();
+        sb.append(dav.getMajorVersion());
+        sb.append('.');
+        sb.append(dav.getMinorVersion());
+        sb.append('.');
+        sb.append(dav.getIncrementalVersion());
+        if ( dav.getQualifier() != null ) {
+            sb.append('.');
+            sb.append(dav.getQualifier());
+        }
+        final MavenVersion mavenVersion = new MavenVersion(sb.toString());
+        return mavenVersion.getOSGiVersion().toString();
+    }
+}
diff --git a/src/test/java/org/apache/sling/maven/slingstart/FeatureModelConverterTest.java b/src/test/java/org/apache/sling/maven/slingstart/FeatureModelConverterTest.java
index 7a51f7d..563ca62 100644
--- a/src/test/java/org/apache/sling/maven/slingstart/FeatureModelConverterTest.java
+++ b/src/test/java/org/apache/sling/maven/slingstart/FeatureModelConverterTest.java
@@ -16,7 +16,6 @@
  */
 package org.apache.sling.maven.slingstart;
 
-import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
 
@@ -83,6 +82,8 @@
         Mockito.when(proj.getBasedir()).thenReturn(projBaseDir);
         Mockito.when(proj.getBuild()).thenReturn(build);
         Mockito.when(proj.getVersion()).thenReturn("1.0");
+        final Properties projProps = new Properties();
+        Mockito.when(proj.getProperties()).thenReturn(projProps);
 
         ProjectInfo pi = new ProjectInfo();
         pi.project = proj;
@@ -119,6 +120,8 @@
         Mockito.when(proj.getBuild()).thenReturn(build);
         Mockito.when(proj.getGroupId()).thenReturn("generated");
         Mockito.when(proj.getVersion()).thenReturn("0.0.1-SNAPSHOT");
+        final Properties projProps = new Properties();
+        Mockito.when(proj.getProperties()).thenReturn(projProps);
 
         ProjectInfo pi = new ProjectInfo();
         pi.project = proj;
@@ -141,43 +144,4 @@
         assertTrue(inheritsProv.contains("org.apache.aries/org.apache.aries.util/1.1.3"));
         assertTrue(inheritsProv.contains("org.apache.sling/org.apache.sling.commons.log/5.1.0"));
     }
-
-    @Test
-    public void testReplaceVars() {
-        MavenProject mp = Mockito.mock(MavenProject.class);
-
-        Properties props = new Properties();
-        props.put("foo", "bar");
-
-        Mockito.when(mp.getGroupId()).thenReturn("abc");
-        Mockito.when(mp.getArtifactId()).thenReturn("a.b.c");
-        Mockito.when(mp.getVersion()).thenReturn("1.2.3-SNAPSHOT");
-        Mockito.when(mp.getProperties()).thenReturn(props);
-
-        assertEquals("xxxabcyyy", FeatureModelConverter.replaceVars(mp,
-                "xxx${project.groupId}yyy"));
-        assertEquals("xxxabcyyya.b.c1.2.3-SNAPSHOT", FeatureModelConverter.replaceVars(mp,
-                "xxx${project.groupId}yyy${project.artifactId}${project.version}"));
-        assertEquals("xxxabcyyya.b.c1.2.3.SNAPSHOT", FeatureModelConverter.replaceVars(mp,
-                "xxx${project.groupId}yyy${project.artifactId}${project.osgiVersion}"));
-        assertEquals("xxxbaryyy", FeatureModelConverter.replaceVars(mp, "xxx${foo}yyy"));
-    }
-
-    @Test
-    public void testReplaceVarsFromSystemProperties() {
-        Properties storedProps = new Properties();
-        storedProps.putAll(System.getProperties());
-
-        try {
-            System.setProperty("blah", "hello");
-
-            MavenProject mp = Mockito.mock(MavenProject.class);
-            Mockito.when(mp.getVersion()).thenReturn("1");
-            assertEquals("hello hello ${blaaah}", FeatureModelConverter.replaceVars(mp,
-                    "${blah} ${blah} ${blaaah}"));
-        } finally {
-            // Restore the system properties
-            System.setProperties(storedProps);
-        }
-    }
 }
diff --git a/src/test/java/org/apache/sling/maven/slingstart/GenerateResourcesMojoTest.java b/src/test/java/org/apache/sling/maven/slingstart/GenerateResourcesMojoTest.java
index 2c03dfc..c708e45 100644
--- a/src/test/java/org/apache/sling/maven/slingstart/GenerateResourcesMojoTest.java
+++ b/src/test/java/org/apache/sling/maven/slingstart/GenerateResourcesMojoTest.java
@@ -23,6 +23,7 @@
 import java.io.IOException;
 import java.lang.reflect.Field;
 import java.net.URL;
+import java.util.Properties;
 
 import org.apache.maven.artifact.repository.ArtifactRepository;
 import org.apache.maven.execution.MavenSession;
@@ -71,7 +72,8 @@
         MavenProject proj = Mockito.mock(MavenProject.class);
         Mockito.when(proj.getBuild()).thenReturn(build);
         Mockito.when(proj.getVersion()).thenReturn("1");
-
+        final Properties projProps = new Properties();
+        Mockito.when(proj.getProperties()).thenReturn(projProps);
         File f = new File(System.getProperty("user.home") + File.separatorChar + ".m2");
         ArtifactRepository localRepo = Mockito.mock(ArtifactRepository.class);
         Mockito.when(localRepo.getUrl()).thenReturn(f.toURI().toURL().toString());
diff --git a/src/test/java/org/apache/sling/maven/slingstart/SubstitutionTest.java b/src/test/java/org/apache/sling/maven/slingstart/SubstitutionTest.java
new file mode 100644
index 0000000..7def6e8
--- /dev/null
+++ b/src/test/java/org/apache/sling/maven/slingstart/SubstitutionTest.java
@@ -0,0 +1,61 @@
+/*
+ * 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.maven.slingstart;
+
+import org.apache.maven.project.MavenProject;
+import org.junit.Test;
+
+import java.util.Properties;
+
+import static org.junit.Assert.assertEquals;
+
+public class SubstitutionTest {
+    @Test
+    public void testReplaceMavenVars() {
+        MavenProject proj = new MavenProject();
+        Properties p = proj.getProperties();
+        p.put("test", "foo");
+        assertEquals("hellofoogoodbyefoo", Substitution.replaceMavenVars(proj, "hello${test}goodbye${test}"));
+    }
+
+    @Test
+    public void testReplaceMavenVarsWithSystemProperties() {
+        Properties storedProps = new Properties();
+        storedProps.putAll(System.getProperties());
+
+        try {
+            MavenProject proj = new MavenProject();
+            Properties p = proj.getProperties();
+            p.put("test", "foo");
+
+            System.setProperty("test", "bar");
+
+            assertEquals("hellobargoodbyebar", Substitution.replaceMavenVars(proj, "hello${test}goodbye${test}"));
+        } finally {
+            // Restore the system properties
+            System.setProperties(storedProps);
+        }
+    }
+
+    @Test
+    public void testOSGiVersion() {
+    	assertEquals("1.2.3", Substitution.getOSGiVersion("1.2.3"));
+    	assertEquals("1.2.0.SNAPSHOT", Substitution.getOSGiVersion("1.2-SNAPSHOT"));
+    	assertEquals("4.5.6.SNAPSHOT", Substitution.getOSGiVersion("4.5.6-SNAPSHOT"));
+    	assertEquals("6.5.0.some", Substitution.getOSGiVersion("6.5.0-some"));
+    }
+}