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