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


##########
core/camel-support/src/main/java/org/apache/camel/support/builder/ExpressionBuilder.java:
##########
@@ -2036,4 +2042,41 @@ public String toString() {
         };
     }
 
+    /**
+     * Returns the expression for the message body as pretty formatted string
+     */
+    public static Expression prettyBodyExpression() {
+        return new ExpressionAdapter() {
+            @Override
+            public Object evaluate(Exchange exchange) {
+                String body = exchange.getIn().getBody(String.class);
+
+                if (body == null) {
+                    return null;
+                } else if (body.startsWith("{") && body.endsWith("}") || 
body.startsWith("[") && body.endsWith("]")) {
+                    return Jsoner.prettyPrint(body); //json
+                } else if(body.startsWith("<") && body.endsWith(">")) {
+                    return ExpressionBuilder.prettyXml(body); //xml
+                }
+
+                return body;
+            }
+
+            @Override
+            public String toString() {
+                return "prettyBody()";
+            }
+        };
+    }
+
+    private static String prettyXml(String rawXml) {
+        try {
+            boolean includeDeclaration = rawXml.startsWith("<?xml");
+            return XmlPrettyPrinter.pettyPrint(rawXml, 2, includeDeclaration);
+        } catch (Exception e) {
+            LOG.warn("Something went wrong when trying to pretty format. 
Returning the raw value");

Review Comment:
   Remove this WARN



##########
core/camel-core-languages/src/main/docs/modules/languages/pages/simple-language.adoc:
##########
@@ -96,6 +96,8 @@ converted body can be null.
 
 |bodyOneLine | String | Converts the body to a String and removes all 
line-breaks so the string is in one line.
 
+|prettyBody | String | Converts the body to a String and pretty print if JSON 
or XML.

Review Comment:
   Converts the body to a String, and attempts to pretty print if JSon or XML, 
otherwise the body is returned as the String value.



##########
core/camel-xml-jaxp-util/src/main/java/org/apache/camel/util/xml/XmlPrettyPrinter.java:
##########
@@ -47,6 +46,23 @@ public interface ColorPrintElement {
         String color(int type, String value);
     }
 
+    /**
+     * Pad the string with leading spaces
+     *
+     * @param level  level
+     * @param blanks number of blanks per level
+     */
+    private static String padString(int level, int blanks) {

Review Comment:
   Can you do what @orpiske did with the repeat stuff



##########
core/camel-core-languages/src/main/java/org/apache/camel/language/csimple/CSimpleHelper.java:
##########
@@ -163,6 +169,30 @@ 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.trim()); //json
+        } else if (body.startsWith("<") && body.endsWith(">")) {
+            return CSimpleHelper.prettyXml(body.trim()); //xml
+        }
+
+        return body;
+    }
+
+    private static String prettyXml(String rawXml) {
+        try {
+            boolean includeDeclaration = rawXml.startsWith("<?xml");
+            return XmlPrettyPrinter.pettyPrint(rawXml, 2, includeDeclaration);
+        } catch (Exception e) {
+            LOG.warn("Something went wrong when trying to pretty format. 
Returning the raw value");

Review Comment:
   Remove this WARN



-- 
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