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