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


##########
core/camel-util/src/test/java/org/apache/camel/util/EscapeHelperTest.java:
##########
@@ -0,0 +1,108 @@
+/*
+ * 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.
+ */
+package org.apache.camel.util;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertSame;
+
+public class EscapeHelperTest {
+
+    @Test
+    public void testHtml() {
+        assertEquals("Monday & Tuesday", EscapeHelper.html("Monday & 
Tuesday"));
+        assertEquals("<b>bold</b>", 
EscapeHelper.html("<b>bold</b>"));
+        assertEquals("say &quot;hi&quot; &amp; &#39;bye&#39;", 
EscapeHelper.html("say \"hi\" & 'bye'"));
+        assertEquals("caf\u00e9", EscapeHelper.html("caf\u00e9"));
+    }
+
+    @Test
+    public void testXml() {
+        assertEquals("Monday &amp; Tuesday", EscapeHelper.xml("Monday & 
Tuesday"));
+        assertEquals("&lt;a href=&quot;x&quot;&gt;it&apos;s&lt;/a&gt;", 
EscapeHelper.xml("<a href=\"x\">it's</a>"));
+    }
+
+    @Test
+    public void testJson() {
+        assertEquals("say \\\"hi\\\"", EscapeHelper.json("say \"hi\""));
+        assertEquals("c:\\\\temp", EscapeHelper.json("c:\\temp"));
+        assertEquals("line1\\nline2\\ttab\\r\\b\\f", 
EscapeHelper.json("line1\nline2\ttab\r\b\f"));
+        assertEquals("a\\u0001b", EscapeHelper.json("a\u0001b"));
+        assertEquals("it's <b>", EscapeHelper.json("it's <b>"));
+    }
+
+    @Test
+    public void testJs() {
+        assertEquals("it\\'s \\\"quoted\\\"", EscapeHelper.js("it's 
\"quoted\""));
+        assertEquals("<\\/script>", EscapeHelper.js("</script>"));
+        assertEquals("a\\nb", EscapeHelper.js("a\nb"));
+        assertEquals("a/b", EscapeHelper.js("a/b"));
+    }
+
+    @Test
+    public void testSql() {
+        assertEquals("O''Reilly", EscapeHelper.sql("O'Reilly"));
+        assertEquals("'''' ", EscapeHelper.sql("'' "));
+        assertEquals("plain", EscapeHelper.sql("plain"));
+    }
+
+    @Test
+    public void testUrl() {
+        assertEquals("Camel%20in%20Action", EscapeHelper.url("Camel in 
Action"));
+        assertEquals("a%26b%3Dc%2Fd%3Fe", EscapeHelper.url("a&b=c/d?e"));
+        assertEquals("caf%C3%A9", EscapeHelper.url("caf\u00e9"));
+        assertEquals("plain-text_1.2*", EscapeHelper.url("plain-text_1.2*"));
+    }
+
+    @Test
+    public void testNoChangeReturnsSameInstance() {
+        String value = "nothing to escape";
+        assertSame(value, EscapeHelper.html(value));
+        assertSame(value, EscapeHelper.xml(value));

Review Comment:
   Added in e36296973a41 — `url("a+b")` now asserts `"a%2Bb"`, pinning that 
`URLEncoder` encodes a literal `+` as `%2B` before the `+` → `%20` replacement.
   
   _Claude Code on behalf of davsclaus_



##########
core/camel-util/src/main/java/org/apache/camel/util/EscapeHelper.java:
##########
@@ -0,0 +1,237 @@
+/*
+ * 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.
+ */
+package org.apache.camel.util;
+
+import java.net.URLEncoder;
+import java.nio.charset.StandardCharsets;
+import java.util.Locale;
+
+/**
+ * Helper for escaping special characters so a value can be embedded safely in 
HTML, XML, JSON, JavaScript, SQL or a
+ * URL.
+ */
+public final class EscapeHelper {
+
+    /**
+     * The supported escape kinds.
+     */
+    public enum Kind {
+        HTML,
+        XML,
+        JSON,
+        JS,
+        SQL,
+        URL;
+
+        /**
+         * Resolves the escape kind from its name (case-insensitive).
+         *
+         * @param  name the name such as html, xml, json, js, sql, or url
+         * @return      the kind, or <tt>null</tt> if the name is not a 
supported kind
+         */
+        public static Kind fromName(String name) {
+            if (name == null) {
+                return null;
+            }
+            String key = name.trim().toUpperCase(Locale.ROOT);
+            if ("JAVASCRIPT".equals(key)) {
+                return JS;
+            }
+            for (Kind kind : values()) {
+                if (kind.name().equals(key)) {
+                    return kind;
+                }
+            }
+            return null;
+        }
+
+        /**
+         * Escapes the value according to this kind.
+         */
+        public String escape(String value) {

Review Comment:
   Added the `@param`/`@return` Javadoc in e36296973a41, using the same 
`<tt>null</tt>` phrasing as the static `escape(Kind, String)` below it so the 
file stays consistent.
   
   _Claude Code on behalf of davsclaus_



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to