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 022/** 023 * Translates codepoints to their XML numeric entity escaped value. 024 * 025 * @since 1.0 026 */ 027public class NumericEntityEscaper extends CodePointTranslator { 028 029 private final int below; 030 private final int above; 031 private final boolean between; 032 033 /** 034 * <p>Constructs a <code>NumericEntityEscaper</code> for the specified range. This is 035 * the underlying method for the other constructors/builders. The <code>below</code> 036 * and <code>above</code> boundaries are inclusive when <code>between</code> is 037 * <code>true</code> and exclusive when it is <code>false</code>. </p> 038 * 039 * @param below int value representing the lowest codepoint boundary 040 * @param above int value representing the highest codepoint boundary 041 * @param between whether to escape between the boundaries or outside them 042 */ 043 private NumericEntityEscaper(final int below, final int above, final boolean between) { 044 this.below = below; 045 this.above = above; 046 this.between = between; 047 } 048 049 /** 050 * <p>Constructs a <code>NumericEntityEscaper</code> for all characters. </p> 051 */ 052 public NumericEntityEscaper() { 053 this(0, Integer.MAX_VALUE, true); 054 } 055 056 /** 057 * <p>Constructs a <code>NumericEntityEscaper</code> below the specified value (exclusive). </p> 058 * 059 * @param codepoint below which to escape 060 * @return the newly created {@code NumericEntityEscaper} instance 061 */ 062 public static NumericEntityEscaper below(final int codepoint) { 063 return outsideOf(codepoint, Integer.MAX_VALUE); 064 } 065 066 /** 067 * <p>Constructs a <code>NumericEntityEscaper</code> above the specified value (exclusive). </p> 068 * 069 * @param codepoint above which to escape 070 * @return the newly created {@code NumericEntityEscaper} instance 071 */ 072 public static NumericEntityEscaper above(final int codepoint) { 073 return outsideOf(0, codepoint); 074 } 075 076 /** 077 * <p>Constructs a <code>NumericEntityEscaper</code> between the specified values (inclusive). </p> 078 * 079 * @param codepointLow above which to escape 080 * @param codepointHigh below which to escape 081 * @return the newly created {@code NumericEntityEscaper} instance 082 */ 083 public static NumericEntityEscaper between(final int codepointLow, final int codepointHigh) { 084 return new NumericEntityEscaper(codepointLow, codepointHigh, true); 085 } 086 087 /** 088 * <p>Constructs a <code>NumericEntityEscaper</code> outside of the specified values (exclusive). </p> 089 * 090 * @param codepointLow below which to escape 091 * @param codepointHigh above which to escape 092 * @return the newly created {@code NumericEntityEscaper} instance 093 */ 094 public static NumericEntityEscaper outsideOf(final int codepointLow, final int codepointHigh) { 095 return new NumericEntityEscaper(codepointLow, codepointHigh, false); 096 } 097 098 /** 099 * {@inheritDoc} 100 */ 101 @Override 102 public boolean translate(final int codepoint, final Writer out) throws IOException { 103 if(between) { 104 if (codepoint < below || codepoint > above) { 105 return false; 106 } 107 } else { 108 if (codepoint >= below && codepoint <= above) { 109 return false; 110 } 111 } 112 113 out.write("&#"); 114 out.write(Integer.toString(codepoint, 10)); 115 out.write(';'); 116 return true; 117 } 118}