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.text.translate;
018
019import java.io.IOException;
020import java.io.Writer;
021
022/**
023 * Translates code points to their Unicode escaped value.
024 *
025 * @since 1.0
026 */
027public class UnicodeEscaper extends CodePointTranslator {
028
029    /**
030     * Constructs a {@code UnicodeEscaper} above the specified value (exclusive).
031     *
032     * @param codePoint above which to escape.
033     * @return The newly created {@code UnicodeEscaper} instance.
034     */
035    public static UnicodeEscaper above(final int codePoint) {
036        return outsideOf(0, codePoint);
037    }
038
039    /**
040     * Constructs a {@code UnicodeEscaper} below the specified value (exclusive).
041     *
042     * @param codePoint below which to escape.
043     * @return The newly created {@code UnicodeEscaper} instance.
044     */
045    public static UnicodeEscaper below(final int codePoint) {
046        return outsideOf(codePoint, Integer.MAX_VALUE);
047    }
048
049    /**
050     * Constructs a {@code UnicodeEscaper} between the specified values (inclusive).
051     *
052     * @param codePointLow above which to escape.
053     * @param codePointHigh below which to escape.
054     * @return The newly created {@code UnicodeEscaper} instance.
055     */
056    public static UnicodeEscaper between(final int codePointLow, final int codePointHigh) {
057        return new UnicodeEscaper(codePointLow, codePointHigh, true);
058    }
059
060    /**
061     * Constructs a {@code UnicodeEscaper} outside of the specified values (exclusive).
062     *
063     * @param codePointLow below which to escape.
064     * @param codePointHigh above which to escape.
065     * @return The newly created {@code UnicodeEscaper} instance.
066     */
067    public static UnicodeEscaper outsideOf(final int codePointLow, final int codePointHigh) {
068        return new UnicodeEscaper(codePointLow, codePointHigh, false);
069    }
070
071    /** The lowest code point boundary. */
072    private final int below;
073
074    /** The highest code point boundary. */
075    private final int above;
076
077    /** Whether to escape between the boundaries or outside them. */
078    private final boolean between;
079
080    /**
081     * Constructs a {@code UnicodeEscaper} for all characters.
082     */
083    public UnicodeEscaper() {
084        this(0, Integer.MAX_VALUE, true);
085    }
086
087    /**
088     * Constructs a {@code UnicodeEscaper} for the specified range. This is
089     * the underlying method for the other constructors/builders. The {@code below}
090     * and {@code above} boundaries are inclusive when {@code between} is
091     * {@code true} and exclusive when it is {@code false}.
092     *
093     * @param below int value representing the lowest code point boundary.
094     * @param above int value representing the highest code point boundary.
095     * @param between whether to escape between the boundaries or outside them.
096     */
097    protected UnicodeEscaper(final int below, final int above, final boolean between) {
098        this.below = below;
099        this.above = above;
100        this.between = between;
101    }
102
103    /**
104     * Converts the given code point to a hexadecimal string of the form {@code "\\uXXXX"}.
105     *
106     * @param codePoint
107     *            a Unicode code point.
108     * @return The hexadecimal string for the given code point.
109     */
110    protected String toUtf16Escape(final int codePoint) {
111        return "\\u" + hex(codePoint);
112    }
113
114    /**
115     * {@inheritDoc}
116     */
117    @Override
118    public boolean translate(final int codePoint, final Writer writer) throws IOException {
119        if (between) {
120            if (codePoint < below || codePoint > above) {
121                return false;
122            }
123        } else if (codePoint >= below && codePoint <= above) {
124            return false;
125        }
126
127        if (codePoint > 0xffff) {
128            writer.write(toUtf16Escape(codePoint));
129        } else {
130          writer.write("\\u");
131          writer.write(HEX_DIGITS[codePoint >> 12 & 15]);
132          writer.write(HEX_DIGITS[codePoint >> 8 & 15]);
133          writer.write(HEX_DIGITS[codePoint >> 4 & 15]);
134          writer.write(HEX_DIGITS[codePoint & 15]);
135        }
136        return true;
137    }
138}