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

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


The following commit(s) were added to refs/heads/master by this push:
     new c2c9f87  Convert repoinit sections to use JSON array representation
c2c9f87 is described below

commit c2c9f87c73ae295b465da316e1c945b2a8638d72
Author: David Bosschaert <david.bosscha...@gmail.com>
AuthorDate: Wed Apr 4 14:57:26 2018 +0100

    Convert repoinit sections to use JSON array representation
---
 .../modelconverter/impl/FeatureToProvisioning.java | 23 ++++++++++++++++-
 .../modelconverter/impl/ProvisioningToFeature.java | 29 +++++++++++++++++++---
 .../modelconverter/impl/ModelConverterTest.java    | 13 +++++++---
 .../src/test/resources/repoinit.json               |  2 +-
 4 files changed, 59 insertions(+), 8 deletions(-)

diff --git 
a/featuremodel/feature-modelconverter/src/main/java/org/apache/sling/feature/modelconverter/impl/FeatureToProvisioning.java
 
b/featuremodel/feature-modelconverter/src/main/java/org/apache/sling/feature/modelconverter/impl/FeatureToProvisioning.java
index 3c526c2..04b8428 100644
--- 
a/featuremodel/feature-modelconverter/src/main/java/org/apache/sling/feature/modelconverter/impl/FeatureToProvisioning.java
+++ 
b/featuremodel/feature-modelconverter/src/main/java/org/apache/sling/feature/modelconverter/impl/FeatureToProvisioning.java
@@ -21,6 +21,7 @@ import org.apache.sling.feature.ArtifactId;
 import org.apache.sling.feature.Bundles;
 import org.apache.sling.feature.Configurations;
 import org.apache.sling.feature.Extension;
+import org.apache.sling.feature.ExtensionType;
 import org.apache.sling.feature.Extensions;
 import org.apache.sling.feature.KeyValueMap;
 import org.apache.sling.feature.process.FeatureResolver;
@@ -41,11 +42,18 @@ import java.io.File;
 import java.io.FileReader;
 import java.io.FileWriter;
 import java.io.IOException;
+import java.io.StringReader;
 import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
+import javax.json.Json;
+import javax.json.JsonArray;
+import javax.json.JsonReader;
+import javax.json.JsonString;
+import javax.json.JsonValue;
+
 /** Converter that converts the feature model to the provisioning model.
  */
 public class FeatureToProvisioning {
@@ -214,7 +222,20 @@ public class FeatureToProvisioning {
 
             } else if ( Extension.NAME_REPOINIT.equals(ext.getName()) ) {
                 final Section section = new Section("repoinit");
-                section.setContents(ext.getText());
+                if (ext.getType() == ExtensionType.TEXT) {
+                    section.setContents(ext.getText());
+                } else if (ext.getType() == ExtensionType.JSON) {
+                    JsonReader reader = Json.createReader(new 
StringReader(ext.getJSON()));
+                    JsonArray arr = reader.readArray();
+                    StringBuilder sb = new StringBuilder();
+                    for (JsonValue v : arr) {
+                        if (v instanceof JsonString) {
+                            sb.append(((JsonString) v).getString());
+                            sb.append('\n');
+                        }
+                    }
+                    section.setContents(sb.toString());
+                }
                 f.getAdditionalSections().add(section);
             } else if ( ext.isRequired() ) {
                 LOGGER.error("Unable to convert required extension {}", 
ext.getName());
diff --git 
a/featuremodel/feature-modelconverter/src/main/java/org/apache/sling/feature/modelconverter/impl/ProvisioningToFeature.java
 
b/featuremodel/feature-modelconverter/src/main/java/org/apache/sling/feature/modelconverter/impl/ProvisioningToFeature.java
index 70a6d76..cf1a973 100644
--- 
a/featuremodel/feature-modelconverter/src/main/java/org/apache/sling/feature/modelconverter/impl/ProvisioningToFeature.java
+++ 
b/featuremodel/feature-modelconverter/src/main/java/org/apache/sling/feature/modelconverter/impl/ProvisioningToFeature.java
@@ -405,11 +405,34 @@ public class ProvisioningToFeature {
         }
         Extension repoExtension = 
extensions.getByName(Extension.NAME_REPOINIT);
         for(final Section sect : feature.getAdditionalSections("repoinit")) {
-            final String text = sect.getContents();
+            String text = sect.getContents();
             if ( repoExtension == null ) {
-                repoExtension = new Extension(ExtensionType.TEXT, 
Extension.NAME_REPOINIT, true);
+//                repoExtension = new Extension(ExtensionType.TEXT, 
Extension.NAME_REPOINIT, true);
+//                extensions.add(repoExtension);
+//                repoExtension.setJSON(text);
+
+                repoExtension = new Extension(ExtensionType.JSON, 
Extension.NAME_REPOINIT, true);
                 extensions.add(repoExtension);
-                repoExtension.setText(text);
+                text = text.replace('\t', ' ');
+                String[] lines = text.split("[\n]");
+
+                StringBuilder sb = new StringBuilder();
+                sb.append('[');
+
+                boolean first = true;
+                for (String t : lines) {
+                    if (first)
+                        first = false;
+                    else
+                        sb.append(',');
+
+                    sb.append('"');
+                    sb.append(t);
+                    sb.append('"');
+                }
+                sb.append(']');
+
+                repoExtension.setJSON(sb.toString());
             } else {
                 repoExtension.setText(repoExtension.getText() + "\n\n" + text);
             }
diff --git 
a/featuremodel/feature-modelconverter/src/test/java/org/apache/sling/feature/modelconverter/impl/ModelConverterTest.java
 
b/featuremodel/feature-modelconverter/src/test/java/org/apache/sling/feature/modelconverter/impl/ModelConverterTest.java
index 4231489..df0eacf 100644
--- 
a/featuremodel/feature-modelconverter/src/test/java/org/apache/sling/feature/modelconverter/impl/ModelConverterTest.java
+++ 
b/featuremodel/feature-modelconverter/src/test/java/org/apache/sling/feature/modelconverter/impl/ModelConverterTest.java
@@ -19,6 +19,7 @@ package org.apache.sling.feature.modelconverter.impl;
 import org.apache.sling.feature.Bundles;
 import org.apache.sling.feature.Configurations;
 import org.apache.sling.feature.Extension;
+import org.apache.sling.feature.ExtensionType;
 import org.apache.sling.feature.Extensions;
 import org.apache.sling.feature.support.ArtifactManager;
 import org.apache.sling.feature.support.ArtifactManagerConfig;
@@ -505,9 +506,15 @@ public class ModelConverterTest {
             assertEquals(ex.getName(), ac.getName());
             assertEquals(ex.isRequired(), ac.isRequired());
 
-            String exTxt = ex.getText().replaceAll("\\s+", "");
-            String acTxt = ac.getText().replaceAll("\\s+", "");
-            assertEquals(exTxt, acTxt);
+            if (ex.getType() == ExtensionType.TEXT) {
+                String exTxt = ex.getText().replaceAll("\\s+", "");
+                String acTxt = ac.getText().replaceAll("\\s+", "");
+                assertEquals(exTxt, acTxt);
+            } else if (ex.getType() == ExtensionType.JSON) {
+                String exJson = ex.getJSON().replaceAll("\\s+", 
"").replaceAll("\"\",", "");
+                String acJson = ac.getJSON().replaceAll("\\s+", 
"").replaceAll("\"\",", "");
+                assertEquals(exJson, acJson);
+            }
 
             /* TODO reinstantiate for Artifacts extentions
             assertEquals(ex.getArtifacts().size(), ac.getArtifacts().size());
diff --git 
a/featuremodel/feature-modelconverter/src/test/resources/repoinit.json 
b/featuremodel/feature-modelconverter/src/test/resources/repoinit.json
index 01d3b16..1fe41f3 100644
--- a/featuremodel/feature-modelconverter/src/test/resources/repoinit.json
+++ b/featuremodel/feature-modelconverter/src/test/resources/repoinit.json
@@ -21,7 +21,7 @@
             
"references":"model@repoinit:context:/resources/provisioning/model.txt"
         }
     },
-    "repoinit:TEXT|false": [
+    "repoinit:JSON|false": [
         "# general",
         "create path (sling:OrderedFolder) /content",
         "set ACL for everyone",

-- 
To stop receiving notification emails like this one, please contact
dav...@apache.org.

Reply via email to