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