You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by "essobedo (via GitHub)" <gi...@apache.org> on 2023/06/15 06:59:04 UTC

[GitHub] [camel] essobedo commented on a diff in pull request #10388: CAMEL-18793: add prettyBody to simple language

essobedo commented on code in PR #10388:
URL: https://github.com/apache/camel/pull/10388#discussion_r1230510086


##########
core/camel-core-languages/src/main/java/org/apache/camel/language/csimple/CSimpleHelper.java:
##########
@@ -163,6 +178,40 @@ public static String bodyOneLine(Exchange exchange) {
         return body;
     }
 
+    public static String prettyBody(Exchange exchange) {
+        String body = exchange.getIn().getBody(String.class);
+
+        if (body == null) {
+            return null;
+        } else if (body.startsWith("{") && body.endsWith("}") || body.startsWith("[") && body.endsWith("]")) {
+            body = Jsoner.prettyPrint(body); //json
+        } else if (body.startsWith("<") && body.endsWith(">")) {
+            return CSimpleHelper.prettyXml(body); //xml
+        }

Review Comment:
   You should rather rely on the content type (aka the header `Exchange.CONTENT_TYPE`)



##########
core/camel-core-languages/src/main/java/org/apache/camel/language/csimple/CSimpleHelper.java:
##########
@@ -163,6 +178,40 @@ public static String bodyOneLine(Exchange exchange) {
         return body;
     }
 
+    public static String prettyBody(Exchange exchange) {
+        String body = exchange.getIn().getBody(String.class);
+
+        if (body == null) {
+            return null;
+        } else if (body.startsWith("{") && body.endsWith("}") || body.startsWith("[") && body.endsWith("]")) {
+            body = Jsoner.prettyPrint(body); //json
+        } else if (body.startsWith("<") && body.endsWith(">")) {
+            return CSimpleHelper.prettyXml(body); //xml
+        }
+
+        return body;
+    }
+
+    private static String prettyXml(String rawXml) {
+        try {
+            InputSource src = new InputSource(new StringReader(rawXml));
+            Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(src);
+
+            TransformerFactory transformerFactory = TransformerFactory.newInstance();
+            transformerFactory.setAttribute("indent-number", 2);
+            Transformer transformer = transformerFactory.newTransformer();
+            transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
+            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");

Review Comment:
   Why remove the XML declaration? without it, you cannot easily know the encoding to use to parse the content



##########
core/camel-core-languages/src/main/java/org/apache/camel/language/csimple/CSimpleHelper.java:
##########
@@ -163,6 +178,40 @@ public static String bodyOneLine(Exchange exchange) {
         return body;
     }
 
+    public static String prettyBody(Exchange exchange) {
+        String body = exchange.getIn().getBody(String.class);
+
+        if (body == null) {
+            return null;
+        } else if (body.startsWith("{") && body.endsWith("}") || body.startsWith("[") && body.endsWith("]")) {
+            body = Jsoner.prettyPrint(body); //json
+        } else if (body.startsWith("<") && body.endsWith(">")) {
+            return CSimpleHelper.prettyXml(body); //xml
+        }
+
+        return body;
+    }
+
+    private static String prettyXml(String rawXml) {
+        try {
+            InputSource src = new InputSource(new StringReader(rawXml));
+            Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(src);

Review Comment:
   If you get the content of the body as `InputStream`, you could use `StreamSource` instead which would prevent to parse it first



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org