commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bay...@apache.org
Subject svn commit: r1059753 - in /commons/proper/lang/trunk/src: main/java/org/apache/commons/lang3/ main/java/org/apache/commons/lang3/text/translate/ test/java/org/apache/commons/lang3/text/translate/
Date Mon, 17 Jan 2011 05:33:33 GMT
Author: bayard
Date: Mon Jan 17 05:33:33 2011
New Revision: 1059753

URL: http://svn.apache.org/viewvc?rev=1059753&view=rev
Log:
Adding an OctalUnescaper to handle Java's support of 1->377 Octal values. LANG-646

Added:
    commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/text/translate/OctalUnescaper.java
  (with props)
    commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/text/translate/OctalUnescaperTest.java
  (with props)
Modified:
    commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/StringEscapeUtils.java

Modified: commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/StringEscapeUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/StringEscapeUtils.java?rev=1059753&r1=1059752&r2=1059753&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/StringEscapeUtils.java
(original)
+++ commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/StringEscapeUtils.java
Mon Jan 17 05:33:33 2011
@@ -24,6 +24,7 @@ import org.apache.commons.lang3.text.tra
 import org.apache.commons.lang3.text.translate.EntityArrays;
 import org.apache.commons.lang3.text.translate.LookupTranslator;
 import org.apache.commons.lang3.text.translate.NumericEntityUnescaper;
+import org.apache.commons.lang3.text.translate.OctalUnescaper;
 import org.apache.commons.lang3.text.translate.UnicodeEscaper;
 import org.apache.commons.lang3.text.translate.UnicodeUnescaper;
 
@@ -128,6 +129,7 @@ public class StringEscapeUtils {
     // TODO: throw "illegal character: \92" as an Exception if a \ on the end of the Java
(as per the compiler)?
     public static final CharSequenceTranslator UNESCAPE_JAVA = 
         new AggregateTranslator(
+            new OctalUnescaper(),     // .between('\1', '\377'),
             new UnicodeUnescaper(),
             new LookupTranslator(EntityArrays.JAVA_CTRL_CHARS_UNESCAPE()),
             new LookupTranslator(

Added: commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/text/translate/OctalUnescaper.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/text/translate/OctalUnescaper.java?rev=1059753&view=auto
==============================================================================
--- commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/text/translate/OctalUnescaper.java
(added)
+++ commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/text/translate/OctalUnescaper.java
Mon Jan 17 05:33:33 2011
@@ -0,0 +1,61 @@
+/*
+ * 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.commons.lang3.text.translate;
+
+import java.io.IOException;
+import java.io.Writer;
+
+/**
+ * Translate escaped octal Strings back to their octal values.
+ *
+ * For example, "\45" should go back to being the specific value (a %).
+ *
+ * Note that this currently only supports the viable range of octal for Java; namely 
+ * 1 to 377. This is both because parsing Java is the main use case and Integer.parseInt
+ * throws an exception when values are larger than octal 377.
+ * 
+ * @author Apache Software Foundation
+ * @since 3.0
+ * @version $Id: OctalUnescaper.java 967237 2010-07-23 20:08:57Z mbenson $
+ */
+public class OctalUnescaper extends CharSequenceTranslator {
+
+    private static int OCTAL_MAX = 377;
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public int translate(CharSequence input, int index, Writer out) throws IOException {
+        if(input.charAt(index) == '\\' && index < (input.length() - 1) &&
Character.isDigit(input.charAt(index + 1)) ) {
+            int start = index + 1;
+
+            int end = index + 2;
+            while ( end < input.length() && Character.isDigit(input.charAt(end))
) {
+                end++;
+                if ( Integer.parseInt(input.subSequence(start, end).toString(), 10) >
OCTAL_MAX) {
+                    end--; // rollback
+                    break;
+                }
+            }
+
+            out.write( Integer.parseInt(input.subSequence(start, end).toString(), 8) );
+            return 1 + end - start;
+        }
+        return 0;
+    }
+}

Propchange: commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/text/translate/OctalUnescaper.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/text/translate/OctalUnescaperTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/text/translate/OctalUnescaperTest.java?rev=1059753&view=auto
==============================================================================
--- commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/text/translate/OctalUnescaperTest.java
(added)
+++ commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/text/translate/OctalUnescaperTest.java
Mon Jan 17 05:33:33 2011
@@ -0,0 +1,56 @@
+/*
+ * 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.commons.lang3.text.translate;
+
+import junit.framework.TestCase;
+
+/**
+ * Unit tests for {@link org.apache.commons.lang3.text.translate.OctalUnescaper}.
+ * @version $Id: OctalUnescaperTest.java 979392 2010-07-26 18:09:52Z mbenson $
+ */
+public class OctalUnescaperTest extends TestCase {
+
+    public void testBetween() {
+        OctalUnescaper oue = new OctalUnescaper();   //.between("1", "377");
+
+        String input = "\\45";
+        String result = oue.translate(input);
+        assertEquals("Failed to unescape octal characters via the between method", "\45",
result);
+
+        input = "\\377";
+        result = oue.translate(input);
+        assertEquals("Failed to unescape octal characters via the between method", "\377",
result);
+
+        input = "\\377 and";
+        result = oue.translate(input);
+        assertEquals("Failed to unescape octal characters via the between method", "\377
and", result);
+
+        input = "\\378 and";
+        result = oue.translate(input);
+        assertEquals("Failed to unescape octal characters via the between method", "\378
and", result);
+
+        input = "\\378";
+        result = oue.translate(input);
+        assertEquals("Failed to unescape octal characters via the between method", "\378",
result);
+
+        input = "\\1";
+        result = oue.translate(input);
+        assertEquals("Failed to unescape octal characters via the between method", "\1",
result);
+    }
+
+}

Propchange: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/text/translate/OctalUnescaperTest.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message