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-unsubscr...@camel.apache.org

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

Reply via email to