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

gnodet pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/maven-hocon-extension.git


The following commit(s) were added to refs/heads/main by this push:
     new 9111351  Support short gav syntax for dependencies and parent, parse 
xml config
9111351 is described below

commit 91113512f906240458bc26e7ca4ab95a8bc53e40
Author: Guillaume Nodet <gno...@gmail.com>
AuthorDate: Fri Dec 6 14:59:50 2024 +0100

    Support short gav syntax for dependencies and parent, parse xml config
---
 src/mdo/hocon-reader.vm                            | 87 ++++++++++++++++++----
 .../java/org/apache/maven/hocon/ParsingTest.java   | 26 +++++++
 src/test/resources/pom2.hocon                      | 43 +++++++++++
 3 files changed, 140 insertions(+), 16 deletions(-)

diff --git a/src/mdo/hocon-reader.vm b/src/mdo/hocon-reader.vm
index b21925f..7971d2b 100644
--- a/src/mdo/hocon-reader.vm
+++ b/src/mdo/hocon-reader.vm
@@ -51,6 +51,8 @@ import org.apache.maven.api.annotations.Generated;
 #foreach ( $class in $model.allClasses )
 import ${packageModelV4}.${class.name};
 #end
+import org.apache.maven.api.xml.XmlNode;
+import org.apache.maven.internal.xml.XmlNodeImpl;
 import com.typesafe.config.ConfigList;
 import com.typesafe.config.ConfigObject;
 import com.typesafe.config.ConfigValue;
@@ -73,6 +75,25 @@ public class ${className} {
             ${classUcapName}.Builder ${classLcapName} = 
${classUcapName}.newBuilder(true);
             ((ConfigObject) value).forEach((k, v) -> {
                 switch (k) {
+  #if($class.name=="Plugin")
+                    case "gav": {
+                        String[] tokens = getStringValue(v).split(":");
+                        if (tokens.length < 1 || tokens.length > 3) {
+                            throw new RuntimeException("Invalid artifact, you 
must specify "
+                                    + "groupId:artifactId[:version] " + value);
+                        }
+                        if (tokens.length == 1) {
+                            ${classLcapName}.artifactId(tokens[0]);
+                        } else {
+                            ${classLcapName}.groupId(tokens[0]);
+                            ${classLcapName}.artifactId(tokens[1]);
+                            if (tokens.length >= 3) {
+                                ${classLcapName}.version(tokens[2]);
+                            }
+                        }
+                        break;
+                    }
+  #end
   #foreach ( $field in $allFields )
     #if ( ! $Helper.xmlFieldMetadata( $field ).transient && $field.name != 
"root" )
       #set ( $fieldTagName = $Helper.xmlFieldMetadata( $field ).tagName )
@@ -94,7 +115,7 @@ public class ${className} {
                         ${classLcapName}.${field.name}(getIntegerValue(v));
                         break;
       #elseif ( $field.type == "DOM" )
-//                        
${classLcapName}.${field.name}(XmlNodeBuilder.build(parser, true));
+                        ${classLcapName}.${field.name}(parseXmlNode(k, v));
                         break;
       #elseif ( $field.type == "java.util.List" && $field.to == "String" && 
$field.multiplicity == "*" )
                         ${classLcapName}.${field.name}(getStringList(v));
@@ -130,25 +151,49 @@ public class ${className} {
     #end
   #end
             return ${classLcapName}.build();
-  #if($class.name=="Dependency")
+  #if($class.name=="Parent")
         } else if (value != null && value.valueType() == 
ConfigValueType.STRING) {
-            Dependency.Builder dependency = Dependency.newBuilder(true);
-            String[] tokens = ((String) value.unwrapped()).split(":");
-            if (tokens.length < 3 || tokens.length > 5) {
+            Parent.Builder parent = Parent.newBuilder(true);
+            String[] tokens = getStringValue(value).split(":");
+            if (tokens.length < 1 || tokens.length > 3) {
                 throw new RuntimeException("Invalid artifact, you must specify 
"
-                        + "groupId:artifactId:version[:packaging[:classifier]] 
" + value);
+                        + "groupId:artifactId[:version] " + value);
             }
-            dependency.groupId(tokens[0]);
-            dependency.artifactId(tokens[1]);
-            dependency.version(tokens[2]);
-            if (tokens.length >= 4) {
-                dependency.type(tokens[3]);
+            if (tokens.length == 1) {
+                parent.artifactId(tokens[0]);
+            } else {
+                parent.groupId(tokens[0]);
+                parent.artifactId(tokens[1]);
+                if (tokens.length >= 3) {
+                    parent.version(tokens[2]);
+                }
             }
-            if (tokens.length == 5) {
-                dependency.classifier(tokens[4]);
+            return parent.build();
+  #elseif($class.name=="Dependency")
+        } else if (value != null && value.valueType() == 
ConfigValueType.STRING) {
+            Dependency.Builder dependency = Dependency.newBuilder(true);
+            String[] tokens = getStringValue(value).split(":");
+            if (tokens.length < 1 || tokens.length > 5) {
+                throw new RuntimeException("Invalid artifact, you must specify 
"
+                        + 
"groupId:artifactId[:version[:packaging[:classifier]]] " + value);
+            }
+            if (tokens.length == 1) {
+                dependency.artifactId(tokens[0]);
+            } else {
+                dependency.groupId(tokens[0]);
+                dependency.artifactId(tokens[1]);
+                if (tokens.length >= 3) {
+                    dependency.version(tokens[2]);
+                }
+                if (tokens.length >= 4) {
+                    dependency.type(tokens[3]);
+                }
+                if (tokens.length == 5) {
+                    dependency.classifier(tokens[4]);
+                }
             }
             return dependency.build();
-    #end
+  #end
         } else if (value != null) {
             throw new IllegalArgumentException("Invalid syntax: cannot parse: 
" + value);
         }
@@ -158,6 +203,16 @@ public class ${className} {
  #end
 #end
 
+    protected XmlNode parseXmlNode(String name, ConfigValue value) {
+        if (value instanceof ConfigObject obj) {
+            List<XmlNode> children = obj.entrySet().stream().map(e -> 
parseXmlNode(e.getKey(), e.getValue())).toList();
+            return new XmlNodeImpl(name, null, null, children, null);
+        } else if (value != null) {
+            return new XmlNodeImpl(name, getStringValue(value));
+        }
+        return null;
+    }
+
     protected String getStringValue(ConfigValue v) {
         return v.unwrapped().toString();
     }
@@ -171,11 +226,11 @@ public class ${className} {
     }
 
     protected boolean getBooleanValue(ConfigValue v) {
-        return Boolean.parseBoolean(v.unwrapped().toString());
+        return Boolean.parseBoolean(getStringValue(v));
     }
 
     protected int getIntegerValue(ConfigValue v) {
-        return Integer.parseInt(v.unwrapped().toString());
+        return Integer.parseInt(getStringValue(v));
     }
 
     protected ConfigList getList(ConfigValue v) {
diff --git a/src/test/java/org/apache/maven/hocon/ParsingTest.java 
b/src/test/java/org/apache/maven/hocon/ParsingTest.java
index 6f611af..46e2448 100644
--- a/src/test/java/org/apache/maven/hocon/ParsingTest.java
+++ b/src/test/java/org/apache/maven/hocon/ParsingTest.java
@@ -22,10 +22,14 @@ import java.io.File;
 
 import com.typesafe.config.Config;
 import com.typesafe.config.ConfigFactory;
+import org.apache.maven.api.model.Dependency;
 import org.apache.maven.api.model.Model;
+import org.apache.maven.api.model.Plugin;
+import org.apache.maven.api.xml.XmlNode;
 import org.junit.jupiter.api.Test;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
 
 class ParsingTest {
 
@@ -39,4 +43,26 @@ class ParsingTest {
         assertEquals(1, model.getDependencies().size());
         assertEquals(2, model.getProperties().size());
     }
+
+    @Test
+    void testParse2() throws Exception {
+        Config config = ConfigFactory.parseFile(new 
File("src/test/resources/pom2.hocon"));
+
+        Model model = new HoconReader().parseModel(config.root());
+
+        assertEquals("40", model.getParent().getVersion());
+        assertEquals(2, model.getDependencies().size());
+        Dependency dependency = model.getDependencies().get(0);
+        assertEquals("org.apache.maven", dependency.getGroupId());
+        assertEquals("maven-api-core", dependency.getArtifactId());
+        assertEquals("${maven.version}", dependency.getVersion());
+        assertEquals(1, model.getBuild().getPlugins().size());
+        Plugin plugin = model.getBuild().getPlugins().get(0);
+        assertEquals("org.apache.maven", plugin.getGroupId());
+        assertEquals("maven-compiler-plugin", plugin.getArtifactId());
+        assertEquals("${maven.compiler.version}", plugin.getVersion());
+        XmlNode node = plugin.getConfiguration();
+        assertNotNull(node);
+        assertEquals("17", node.getChild("release").getValue());
+    }
 }
diff --git a/src/test/resources/pom2.hocon b/src/test/resources/pom2.hocon
new file mode 100644
index 0000000..0039b25
--- /dev/null
+++ b/src/test/resources/pom2.hocon
@@ -0,0 +1,43 @@
+#
+# 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.
+#
+modelVersion = 4.1.0
+parent = "org.apache.maven.extensions:maven-extensions:40"
+artifactId = maven-hocon-extension
+version = 1.0.0-SNAPSHOT
+
+properties = {
+  maven.version = 4.0.0-beta-5
+  maven.compiler.version = 3.5.0
+}
+
+dependencies = [
+    "org.apache.maven:maven-api-core:${maven.version}",
+    "org.junit.jupiter:junit-jupiter:5.11.2:test"
+]
+
+build = {
+    plugins = [
+        {
+            gav = 
"org.apache.maven:maven-compiler-plugin:${maven.compiler.version}"
+            configuration = {
+                release = 17
+            }
+        }
+    ]
+}
\ No newline at end of file

Reply via email to