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
This commit is contained in:
parent
f8a77fc872
commit
b4bdbdfe9c
|
@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -71,7 +71,7 @@ public class EscapeUtils {
|
||||||
new AggregateTranslator(
|
new AggregateTranslator(
|
||||||
new LookupTranslator(EntityArrays.BASIC_ESCAPE),
|
new LookupTranslator(EntityArrays.BASIC_ESCAPE),
|
||||||
new LookupTranslator(EntityArrays.APOS_ESCAPE),
|
new LookupTranslator(EntityArrays.APOS_ESCAPE),
|
||||||
new EscapeNonAsciiAsNumericEntity()
|
NumericEntityEscaper.above(0x7f)
|
||||||
);
|
);
|
||||||
|
|
||||||
public static final String escapeXml(String input) {
|
public static final String escapeXml(String input) {
|
||||||
|
@ -82,7 +82,7 @@ public class EscapeUtils {
|
||||||
new AggregateTranslator(
|
new AggregateTranslator(
|
||||||
new LookupTranslator(EntityArrays.BASIC_ESCAPE),
|
new LookupTranslator(EntityArrays.BASIC_ESCAPE),
|
||||||
new LookupTranslator(EntityArrays.ISO8859_1_ESCAPE),
|
new LookupTranslator(EntityArrays.ISO8859_1_ESCAPE),
|
||||||
new EscapeNonAsciiAsNumericEntity()
|
NumericEntityEscaper.above(0x7f)
|
||||||
);
|
);
|
||||||
|
|
||||||
public static final String escapeHtml3(String input) {
|
public static final String escapeHtml3(String input) {
|
||||||
|
@ -94,7 +94,7 @@ public class EscapeUtils {
|
||||||
new LookupTranslator(EntityArrays.BASIC_ESCAPE),
|
new LookupTranslator(EntityArrays.BASIC_ESCAPE),
|
||||||
new LookupTranslator(EntityArrays.ISO8859_1_ESCAPE),
|
new LookupTranslator(EntityArrays.ISO8859_1_ESCAPE),
|
||||||
new LookupTranslator(EntityArrays.HTML40_EXTENDED_ESCAPE),
|
new LookupTranslator(EntityArrays.HTML40_EXTENDED_ESCAPE),
|
||||||
new EscapeNonAsciiAsNumericEntity()
|
NumericEntityEscaper.above(0x7f)
|
||||||
);
|
);
|
||||||
|
|
||||||
public static final String escapeHtml4(String input) {
|
public static final String escapeHtml4(String input) {
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue