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