You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2023/06/13 09:54:14 UTC

[camel] 01/01: tooling - camel-spring-xsd - Generate this in a more consistent way across different platforms, so we do not keep having large diffs on regen.

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

davsclaus pushed a commit to branch spring-xsd
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 304a708f7dd55589d640160412da3207ddcf2895
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Tue Jun 13 11:53:53 2023 +0200

    tooling - camel-spring-xsd - Generate this in a more consistent way across different platforms, so we do not keep having large diffs on regen.
---
 .../java/org/apache/camel/maven/Constants.java     | 10 +-----
 .../apache/camel/maven/DocumentationEnricher.java  |  4 +--
 .../camel/maven/EipDocumentationEnricherMojo.java  | 41 ++++++++++++++++++----
 .../maven/EipDocumentationEnricherMojoTest.java    | 12 +++++++
 .../src/test/resources/enriched-camel-spring.xsd   | 35 ++++++++++++++++++
 5 files changed, 85 insertions(+), 17 deletions(-)

diff --git a/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/Constants.java b/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/Constants.java
index fc3f0dd1956..90ce2a46d9c 100644
--- a/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/Constants.java
+++ b/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/Constants.java
@@ -23,7 +23,7 @@ public final class Constants {
 
     // Camel core constants.
     public static final String DEFAULT_XML_INTENTION = "  ";
-    public static final int WRAP_LENGTH = 80;
+    public static final int WRAP_LENGTH = 120;
 
     // XML constants.
     public static final String XML_SCHEMA_NAMESPACE_PREFIX = "xs";
@@ -33,14 +33,6 @@ public final class Constants {
     public static final String XS_ANNOTATION_ELEMENT_NAME = "xs:annotation";
     public static final String XS_DOCUMENTATION_ELEMENT_NAME = "xs:documentation";
 
-    // Json files constants.
-    public static final String PROPERTIES_ATTRIBUTE_NAME = "properties";
-    public static final String JSON_SUFIX = ".json";
-    public static final String DESCRIPTION_ATTRIBUTE_NAME = "description";
-    public static final String DEFAULT_VALUE_ATTRIBUTE_NAME = "defaultValue";
-    public static final String DEPRECATED_ATTRIBUTE_NAME = "deprecated";
-    public static final String MODEL_ATTRIBUTE_NAME = "model";
-
     private Constants() {
     }
 }
diff --git a/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/DocumentationEnricher.java b/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/DocumentationEnricher.java
index cd2d8d2ca9e..ceb5cdda31f 100644
--- a/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/DocumentationEnricher.java
+++ b/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/DocumentationEnricher.java
@@ -143,9 +143,9 @@ public class DocumentationEnricher {
 
     private String formatTextContent(Element item, String textContent) {
         StringBuilder stringBuilder = new StringBuilder();
-        stringBuilder.append(System.lineSeparator())
+        stringBuilder.append("\n")
                 .append(WordUtils.wrap(textContent, Constants.WRAP_LENGTH))
-                .append(System.lineSeparator());
+                .append("\n");
         // Fix closing tag intention.
         stringBuilder.append(Constants.DEFAULT_XML_INTENTION);
         for (Node parent = item.getParentNode(); parent != null; parent = parent.getParentNode()) {
diff --git a/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/EipDocumentationEnricherMojo.java b/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/EipDocumentationEnricherMojo.java
index b48691f20ad..5c3306fc537 100644
--- a/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/EipDocumentationEnricherMojo.java
+++ b/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/EipDocumentationEnricherMojo.java
@@ -19,12 +19,15 @@ package org.apache.camel.maven;
 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.StringWriter;
+import java.nio.charset.StandardCharsets;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.LinkedHashMap;
 import java.util.LinkedHashSet;
 import java.util.Map;
 import java.util.Set;
+import java.util.StringJoiner;
 
 import javax.xml.transform.Transformer;
 import javax.xml.transform.TransformerException;
@@ -198,7 +201,36 @@ public class EipDocumentationEnricherMojo extends AbstractMojo {
         }
         getLog().info("Enriched " + enriched + " models out of " + typeToNameMap.size() + " models");
 
-        saveToFile(document, outputCamelSchemaFile, XmlHelper.buildTransformer());
+        String xml = transformToXml(document, XmlHelper.buildTransformer());
+        xml = fixXmlOutput(xml);
+        xml = removeEmptyLines(xml);
+
+        saveToFile(document, outputCamelSchemaFile, xml);
+    }
+
+    public static String fixXmlOutput(String xml) {
+        xml = xml.replaceAll("><!\\[CDATA\\[", ">\n<![CDATA[");
+        xml = xml.replaceAll("\\h+<!\\[CDATA\\[", "<![CDATA[");
+        xml = xml.replaceAll("(\\h*)]]><", "]]>\n$1<");
+        return removeEmptyLines(xml);
+    }
+
+    public static String removeEmptyLines(String xml) {
+        StringJoiner sj = new StringJoiner("\n");
+        for (String l : xml.split("\n")) {
+            if (!l.isBlank()) {
+                sj.add(l);
+            }
+        }
+        return sj.toString();
+    }
+
+    private String transformToXml(Document document, Transformer transformer) throws TransformerException {
+        StringWriter sw = new StringWriter();
+        StreamResult result = new StreamResult(sw);
+        DOMSource source = new DOMSource(document);
+        transformer.transform(source, result);
+        return sw.toString();
     }
 
     private boolean jsonFileExistsForElement(
@@ -267,12 +299,9 @@ public class EipDocumentationEnricherMojo extends AbstractMojo {
         return baseType.replace("tns:", "");
     }
 
-    private void saveToFile(Document document, File outputFile, Transformer transformer)
-            throws IOException, TransformerException {
+    private void saveToFile(Document document, File outputFile, String xml) throws IOException {
         try (FileOutputStream os = new FileOutputStream(outputFile)) {
-            StreamResult result = new StreamResult(os);
-            DOMSource source = new DOMSource(document);
-            transformer.transform(source, result);
+            os.write(xml.getBytes(StandardCharsets.UTF_8));
         }
     }
 
diff --git a/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/test/java/org/apache/camel/maven/EipDocumentationEnricherMojoTest.java b/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/test/java/org/apache/camel/maven/EipDocumentationEnricherMojoTest.java
index f8762d1da25..704a332c244 100644
--- a/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/test/java/org/apache/camel/maven/EipDocumentationEnricherMojoTest.java
+++ b/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/test/java/org/apache/camel/maven/EipDocumentationEnricherMojoTest.java
@@ -17,8 +17,11 @@
 package org.apache.camel.maven;
 
 import java.io.File;
+import java.io.FileInputStream;
 
+import org.apache.camel.util.IOHelper;
 import org.apache.maven.plugin.MojoExecutionException;
+import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.ExtendWith;
@@ -85,4 +88,13 @@ public class EipDocumentationEnricherMojoTest {
             // Expected.
         }
     }
+
+    @Test
+    public void testFixXmlOutput() throws Exception {
+        String xml = IOHelper.loadText(new FileInputStream("src/test/resources/enriched-camel-spring.xsd"));
+        String out = EipDocumentationEnricherMojo.fixXmlOutput(xml);
+        Assertions.assertNotNull(out);
+        Assertions.assertNotEquals(xml, out);
+        // System.out.println(out);
+    }
 }
diff --git a/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/test/resources/enriched-camel-spring.xsd b/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/test/resources/enriched-camel-spring.xsd
new file mode 100644
index 00000000000..4217c6a4489
--- /dev/null
+++ b/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/test/resources/enriched-camel-spring.xsd
@@ -0,0 +1,35 @@
+<?xml version="1.0" encoding="UTF-8"?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://camel.apache.org/schema/spring" elementFormDefault="qualified" targetNamespace="http://camel.apache.org/schema/spring" version="1.0">
+
+    <xs:element name="aggregate" type="tns:aggregateDefinition">
+        <xs:annotation>
+            <xs:documentation xml:lang="en"><![CDATA[
+Aggregates many messages into a single message
+      ]]></xs:documentation>
+        </xs:annotation>
+    </xs:element>
+
+    <xs:element name="any23" type="tns:any23DataFormat">
+        <xs:annotation>
+            <xs:documentation xml:lang="en"><![CDATA[
+Extract RDF data from HTML documents.
+      ]]></xs:documentation>
+        </xs:annotation>
+    </xs:element>
+
+    <xs:simpleType name="springErrorHandlerType">
+
+        <xs:restriction base="xs:string">
+
+            <xs:enumeration value="DefaultErrorHandler"/>
+
+            <xs:enumeration value="DeadLetterChannel"/>
+
+            <xs:enumeration value="NoErrorHandler"/>
+
+            <xs:enumeration value="TransactionErrorHandler"/>
+
+        </xs:restriction>
+
+    </xs:simpleType>
+
+</xs:schema>
\ No newline at end of file