From b4bdbdfe9c9dbc31e90ccf3ab37761e2abdae0f1 Mon Sep 17 00:00:00 2001 From: Henri Yandell Date: Thu, 25 Jun 2009 04:56:18 +0000 Subject: [PATCH] Replacing the NonAsciiAsNumericEntity class with a more generic NumericEntityEscaper. cf LANG-505 git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@788256 13f79535-47bb-0310-9956-ffa450edef68 --- .../EscapeNonAsciiAsNumericEntity.java | 44 ----------- .../lang/text/translate/EscapeUtils.java | 6 +- .../text/translate/NumericEntityEscaper.java | 75 +++++++++++++++++++ 3 files changed, 78 insertions(+), 47 deletions(-) delete mode 100644 src/java/org/apache/commons/lang/text/translate/EscapeNonAsciiAsNumericEntity.java create mode 100644 src/java/org/apache/commons/lang/text/translate/NumericEntityEscaper.java diff --git a/src/java/org/apache/commons/lang/text/translate/EscapeNonAsciiAsNumericEntity.java b/src/java/org/apache/commons/lang/text/translate/EscapeNonAsciiAsNumericEntity.java deleted file mode 100644 index 4ea9da524..000000000 --- a/src/java/org/apache/commons/lang/text/translate/EscapeNonAsciiAsNumericEntity.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * 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 greater than ASCII 127 to their numerical - * XML entity. - * @since 3.0 - */ -public class EscapeNonAsciiAsNumericEntity extends CodePointTranslator { - - /** - * {@inheritDoc} - */ - public boolean translate(int codepoint, Writer out) throws IOException { - // TODO: if (codepoint > 0xffff) { - if (codepoint > 0x7f) { - out.write("&#"); - out.write(Integer.toString(codepoint, 10)); - out.write(';'); - return true; - } else { - return false; - } - } - -} diff --git a/src/java/org/apache/commons/lang/text/translate/EscapeUtils.java b/src/java/org/apache/commons/lang/text/translate/EscapeUtils.java index ae051477a..c0662266e 100644 --- a/src/java/org/apache/commons/lang/text/translate/EscapeUtils.java +++ b/src/java/org/apache/commons/lang/text/translate/EscapeUtils.java @@ -71,7 +71,7 @@ public class EscapeUtils { 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 @@ public class EscapeUtils { 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 @@ public class EscapeUtils { 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) { diff --git a/src/java/org/apache/commons/lang/text/translate/NumericEntityEscaper.java b/src/java/org/apache/commons/lang/text/translate/NumericEntityEscaper.java new file mode 100644 index 000000000..a6c0298a9 --- /dev/null +++ b/src/java/org/apache/commons/lang/text/translate/NumericEntityEscaper.java @@ -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; + } +}