View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.lang3.text.translate;
18  
19  import java.io.IOException;
20  import java.io.Writer;
21  
22  /**
23   * Translates code points to their Unicode escaped value.
24   *
25   * @since 3.0
26   * @deprecated As of <a href="https://commons.apache.org/proper/commons-lang/changes-report.html#a3.6">3.6</a>, use Apache Commons Text
27   * <a href="https://commons.apache.org/proper/commons-text/javadocs/api-release/org/apache/commons/text/translate/UnicodeEscaper.html">
28   * UnicodeEscaper</a>.
29   */
30  @Deprecated
31  public class UnicodeEscaper extends CodePointTranslator {
32  
33      /**
34       * Constructs a {@link UnicodeEscaper} above the specified value (exclusive).
35       *
36       * @param codePoint above which to escape.
37       * @return The newly created {@link UnicodeEscaper} instance.
38       */
39      public static UnicodeEscaper above(final int codePoint) {
40          return outsideOf(0, codePoint);
41      }
42  
43      /**
44       * Constructs a {@link UnicodeEscaper} below the specified value (exclusive).
45       *
46       * @param codePoint below which to escape.
47       * @return The newly created {@link UnicodeEscaper} instance.
48       */
49      public static UnicodeEscaper below(final int codePoint) {
50          return outsideOf(codePoint, Integer.MAX_VALUE);
51      }
52  
53      /**
54       * Constructs a {@link UnicodeEscaper} between the specified values (inclusive).
55       *
56       * @param codePointLow above which to escape.
57       * @param codePointHigh below which to escape.
58       * @return The newly created {@link UnicodeEscaper} instance.
59       */
60      public static UnicodeEscaper between(final int codePointLow, final int codePointHigh) {
61          return new UnicodeEscaper(codePointLow, codePointHigh, true);
62      }
63  
64      /**
65       * Constructs a {@link UnicodeEscaper} outside of the specified values (exclusive).
66       *
67       * @param codePointLow below which to escape.
68       * @param codePointHigh above which to escape.
69       * @return The newly created {@link UnicodeEscaper} instance.
70       */
71      public static UnicodeEscaper outsideOf(final int codePointLow, final int codePointHigh) {
72          return new UnicodeEscaper(codePointLow, codePointHigh, false);
73      }
74  
75      private final int below;
76  
77      private final int above;
78  
79      private final boolean between;
80  
81      /**
82       * Constructs a {@link UnicodeEscaper} for all characters.
83       */
84      public UnicodeEscaper() {
85          this(0, Integer.MAX_VALUE, true);
86      }
87  
88      /**
89       * Constructs a {@link UnicodeEscaper} for the specified range. This is
90       * the underlying method for the other constructors/builders. The {@code below}
91       * and {@code above} boundaries are inclusive when {@code between} is
92       * {@code true} and exclusive when it is {@code false}.
93       *
94       * @param below int value representing the lowest code point boundary.
95       * @param above int value representing the highest code point boundary.
96       * @param between whether to escape between the boundaries or outside them.
97       */
98      protected UnicodeEscaper(final int below, final int above, final boolean between) {
99          this.below = below;
100         this.above = above;
101         this.between = between;
102     }
103 
104     /**
105      * Converts the given code point to a hexadecimal string of the form {@code "\\uXXXX"}, or the
106      * surrogate pair form {@code "\\uXXXX\\uXXXX"} for a supplementary code point.
107      *
108      * @param codePoint
109      *            a Unicode code point.
110      * @return The hexadecimal string for the given code point.
111      * @since 3.2
112      */
113     protected String toUtf16Escape(final int codePoint) {
114         if (Character.isBmpCodePoint(codePoint)) {
115             return "\\u" + hex(codePoint);
116         }
117         final char[] surrogatePair = Character.toChars(codePoint);
118         return "\\u" + hex(surrogatePair[0]) + "\\u" + hex(surrogatePair[1]);
119     }
120 
121     /**
122      * {@inheritDoc}
123      */
124     @Override
125     public boolean translate(final int codePoint, final Writer out) throws IOException {
126         if (between) {
127             if (codePoint < below || codePoint > above) {
128                 return false;
129             }
130         } else if (codePoint >= below && codePoint <= above) {
131             return false;
132         }
133 
134         // TODO: Handle potential + sign per various Unicode escape implementations
135         if (codePoint > 0xffff) {
136             out.write(toUtf16Escape(codePoint));
137         } else {
138           out.write("\\u");
139           out.write(HEX_DIGITS[codePoint >> 12 & 15]);
140           out.write(HEX_DIGITS[codePoint >> 8 & 15]);
141           out.write(HEX_DIGITS[codePoint >> 4 & 15]);
142           out.write(HEX_DIGITS[codePoint & 15]);
143         }
144         return true;
145     }
146 }