Author: bayard
Date: Thu Jun 25 04:56:18 2009
New Revision: 788256

URL: http://svn.apache.org/viewvc?rev=788256&view=rev
Log:
Replacing the NonAsciiAsNumericEntity class with a more generic 
NumericEntityEscaper. cf LANG-505

Added:
    
commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/translate/NumericEntityEscaper.java
   (with props)
Removed:
    
commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/translate/EscapeNonAsciiAsNumericEntity.java
Modified:
    
commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/translate/EscapeUtils.java

Modified: 
commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/translate/EscapeUtils.java
URL: 
http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/translate/EscapeUtils.java?rev=788256&r1=788255&r2=788256&view=diff
==============================================================================
--- 
commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/translate/EscapeUtils.java
 (original)
+++ 
commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/translate/EscapeUtils.java
 Thu Jun 25 04:56:18 2009
@@ -71,7 +71,7 @@
         new AggregateTranslator(
             new LookupTranslator(EntityArrays.BASIC_ESCAPE),
             new LookupTranslator(EntityArrays.APOS_ESCAPE),
-            new EscapeNonAsciiAsNumericEntity()
+            NumericEntityEscaper.above(0x7f)
         );
 
     public static final String escapeXml(String input) {
@@ -82,7 +82,7 @@
         new AggregateTranslator(
             new LookupTranslator(EntityArrays.BASIC_ESCAPE),
             new LookupTranslator(EntityArrays.ISO8859_1_ESCAPE),
-            new EscapeNonAsciiAsNumericEntity()
+            NumericEntityEscaper.above(0x7f)
         );
 
     public static final String escapeHtml3(String input) {
@@ -94,7 +94,7 @@
             new LookupTranslator(EntityArrays.BASIC_ESCAPE),
             new LookupTranslator(EntityArrays.ISO8859_1_ESCAPE),
             new LookupTranslator(EntityArrays.HTML40_EXTENDED_ESCAPE),
-            new EscapeNonAsciiAsNumericEntity()
+            NumericEntityEscaper.above(0x7f)
         );
 
     public static final String escapeHtml4(String input) {

Added: 
commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/translate/NumericEntityEscaper.java
URL: 
http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/translate/NumericEntityEscaper.java?rev=788256&view=auto
==============================================================================
--- 
commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/translate/NumericEntityEscaper.java
 (added)
+++ 
commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/translate/NumericEntityEscaper.java
 Thu Jun 25 04:56:18 2009
@@ -0,0 +1,75 @@
+/*
+ * 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.lang.text.translate;
+
+import java.io.IOException;
+import java.io.Writer;
+
+/**
+ * Translates codepoints to their XML numeric entity escaped value. 
+ * @since 3.0
+ */
+public class NumericEntityEscaper extends CodePointTranslator {
+
+    private int below = 0;
+    private int above = Integer.MAX_VALUE;
+    private boolean between = true;
+
+    public static NumericEntityEscaper below(int codepoint) {
+        return outsideOf(codepoint, Integer.MAX_VALUE);
+    }
+
+    public static NumericEntityEscaper above(int codepoint) {
+        return outsideOf(0, codepoint);
+    }
+
+    public static NumericEntityEscaper between(int codepointLow, int 
codepointHigh) {
+        NumericEntityEscaper escaper = new NumericEntityEscaper();
+        escaper.above = codepointHigh;
+        escaper.below = codepointLow;
+        return escaper;
+    }
+
+    public static NumericEntityEscaper outsideOf(int codepointLow, int 
codepointHigh) {
+        NumericEntityEscaper escaper = new NumericEntityEscaper();
+        escaper.above = codepointHigh;
+        escaper.below = codepointLow;
+        escaper.between = false;
+        return escaper;
+    }
+
+    /**
+     * {...@inheritdoc}
+     */
+    public boolean translate(int codepoint, Writer out) throws IOException {
+        if(between) {
+            if (codepoint < below || codepoint > above) {
+                return false;
+            }
+        } else {
+            if (codepoint >= below && codepoint <= above) {
+                return false;
+            }
+        }
+
+        // TODO: if (codepoint > 0xffff) ?
+        out.write("&#");
+        out.write(Integer.toString(codepoint, 10));
+        out.write(';');
+        return true;
+    }
+}

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


Reply via email to