001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.text.translate; 018 019import java.io.IOException; 020import java.io.Writer; 021 022import org.apache.commons.lang3.Range; 023 024/** 025 * Translates code points to their XML numeric entity escaped value. 026 * 027 * @since 1.0 028 */ 029public class NumericEntityEscaper extends CodePointTranslator { 030 031 /** 032 * Constructs a {@code NumericEntityEscaper} above the specified value (exclusive). 033 * 034 * @param codePoint above which to escape 035 * @return The newly created {@code NumericEntityEscaper} instance 036 */ 037 public static NumericEntityEscaper above(final int codePoint) { 038 return outsideOf(0, codePoint); 039 } 040 041 /** 042 * Constructs a {@code NumericEntityEscaper} below the specified value (exclusive). 043 * 044 * @param codePoint below which to escape 045 * @return The newly created {@code NumericEntityEscaper} instance 046 */ 047 public static NumericEntityEscaper below(final int codePoint) { 048 return outsideOf(codePoint, Integer.MAX_VALUE); 049 } 050 051 /** 052 * Constructs a {@code NumericEntityEscaper} between the specified values (inclusive). 053 * 054 * @param codePointLow above which to escape 055 * @param codePointHigh below which to escape 056 * @return The newly created {@code NumericEntityEscaper} instance 057 */ 058 public static NumericEntityEscaper between(final int codePointLow, final int codePointHigh) { 059 return new NumericEntityEscaper(codePointLow, codePointHigh, true); 060 } 061 062 /** 063 * Constructs a {@code NumericEntityEscaper} outside of the specified values (exclusive). 064 * 065 * @param codePointLow below which to escape 066 * @param codePointHigh above which to escape 067 * @return The newly created {@code NumericEntityEscaper} instance 068 */ 069 public static NumericEntityEscaper outsideOf(final int codePointLow, final int codePointHigh) { 070 return new NumericEntityEscaper(codePointLow, codePointHigh, false); 071 } 072 073 /** Whether to escape between the boundaries or outside them. */ 074 private final boolean between; 075 076 /** Range from lowest code point to highest code point. */ 077 private final Range<Integer> range; 078 079 /** 080 * Constructs a {@code NumericEntityEscaper} for all characters. 081 */ 082 public NumericEntityEscaper() { 083 this(0, Integer.MAX_VALUE, true); 084 } 085 086 /** 087 * Constructs a {@code NumericEntityEscaper} for the specified range. This is 088 * the underlying method for the other constructors/builders. The {@code below} 089 * and {@code above} boundaries are inclusive when {@code between} is 090 * {@code true} and exclusive when it is {@code false}. 091 * 092 * @param below int value representing the lowest code point boundary 093 * @param above int value representing the highest code point boundary 094 * @param between whether to escape between the boundaries or outside them 095 */ 096 private NumericEntityEscaper(final int below, final int above, final boolean between) { 097 this.range = Range.of(below, above); 098 this.between = between; 099 } 100 101 /** 102 * {@inheritDoc} 103 */ 104 @Override 105 public boolean translate(final int codePoint, final Writer writer) throws IOException { 106 if (this.between != this.range.contains(codePoint)) { 107 return false; 108 } 109 writer.write("&#"); 110 writer.write(Integer.toString(codePoint, 10)); 111 writer.write(';'); 112 return true; 113 } 114}