[commons-jexl] branch master updated: [JEXL]: - preparing to add 'escaped' identifiers

2018-05-16 Thread henrib
This is an automated email from the ASF dual-hosted git repository.

henrib pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-jexl.git


The following commit(s) were added to refs/heads/master by this push:
 new 2a46b94  [JEXL]: - preparing to add 'escaped' identifiers
2a46b94 is described below

commit 2a46b941bbe18e5f3a2e6d05032f067bb3ce76a4
Author: henrib <>
AuthorDate: Wed May 16 10:40:19 2018 +0200

[JEXL]:
- preparing to add 'escaped' identifiers
---
 src/test/java/org/apache/commons/jexl3/parser/ParserTest.java | 10 ++
 1 file changed, 10 insertions(+)

diff --git a/src/test/java/org/apache/commons/jexl3/parser/ParserTest.java 
b/src/test/java/org/apache/commons/jexl3/parser/ParserTest.java
index 9bc5fce..4d0fec0 100644
--- a/src/test/java/org/apache/commons/jexl3/parser/ParserTest.java
+++ b/src/test/java/org/apache/commons/jexl3/parser/ParserTest.java
@@ -76,4 +76,14 @@ public class ParserTest {
 Assert.fail(xother.toString());
 }
 }
+
+@Test
+public void testIdentifierEscape() {
+String[] ids = new String[]{"a\\ b", "a\\ b\\ c", "a\\'b\\\"c", "a\\ 
\\ c"};
+for(String id : ids) {
+String esc0 = StringParser.escapeIdentifier(id);
+String esc1 = StringParser.unescapeIdentifier(esc0);
+Assert.assertEquals(id, esc1);
+}
+}
 }

-- 
To stop receiving notification emails like this one, please contact
hen...@apache.org.


[commons-jexl] branch master updated: [JEXL]: - preparing to add 'escaped' identifiers

2018-05-16 Thread henrib
This is an automated email from the ASF dual-hosted git repository.

henrib pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-jexl.git


The following commit(s) were added to refs/heads/master by this push:
 new 8afe559  [JEXL]: - preparing to add 'escaped' identifiers
8afe559 is described below

commit 8afe5595058c727b64b10decaa49bb5a72acc977
Author: henrib <>
AuthorDate: Wed May 16 10:40:19 2018 +0200

[JEXL]:
- preparing to add 'escaped' identifiers
---
 .../apache/commons/jexl3/parser/StringParser.java  | 67 ++
 1 file changed, 67 insertions(+)

diff --git a/src/main/java/org/apache/commons/jexl3/parser/StringParser.java 
b/src/main/java/org/apache/commons/jexl3/parser/StringParser.java
index 8cb4b9d..375c1dd 100644
--- a/src/main/java/org/apache/commons/jexl3/parser/StringParser.java
+++ b/src/main/java/org/apache/commons/jexl3/parser/StringParser.java
@@ -205,5 +205,72 @@ public class StringParser {
 strb.append(delim);
 return strb.toString();
 }
+
+/**
+ * Remove escape char ('\') from an identifier.
+ * @param str the identifier escaped string, ie with a backslash before 
space, quote, double-quote and backslash
+ * @return the string with no '\\' character
+ */
+public static String unescapeIdentifier(String str) {
+StringBuilder strb = null;
+boolean esc = false;
+if (str != null) {
+int n = 0;
+int last = str.length();
+while (n < last) {
+char c = str.charAt(n);
+if (esc) {
+if (strb == null) {
+strb = new StringBuilder(last);
+strb.append(str.substring(0, n));
+} else {
+strb.append(c);
+}
+esc = false;
+} else if (c == '\\') {
+esc = true;
+} else if (strb != null) {
+strb.append(c);
+}
+n += 1;
+}
+}
+return strb == null ? str : strb.toString();
+}
 
+/**
+ * Adds a escape char ('\') where needed in a string form of an ide
+ * @param str the identifier un-escaped string
+ * @return the string with added  backslash character before space, quote, 
double-quote and backslash
+ */
+public static String escapeIdentifier(String str) {
+StringBuilder strb = null;
+if (str != null) {
+int n = 0;
+int last = str.length();
+while (n < last) {
+char c = str.charAt(n);
+switch (c) {
+case ' ':
+case '\'':
+case '"':
+case '\\': {
+if (strb == null) {
+strb = new StringBuilder(last);
+strb.append(str.substring(0, n));
+}
+strb.append('\\');
+strb.append(c);
+break;
+}
+default:
+if (strb != null) {
+strb.append(c);
+}
+}
+n += 1;
+}
+}
+return strb == null ? str : strb.toString();
+}
 }
\ No newline at end of file

-- 
To stop receiving notification emails like this one, please contact
hen...@apache.org.