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    *      http://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 codepoints to their Unicode escaped value.
24   *
25   * @since 3.0
26   * @deprecated as of 3.6, use 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> instead
29   */
30  @Deprecated
31  public class UnicodeEscaper extends CodePointTranslator {
32  
33      private final int below;
34      private final int above;
35      private final boolean between;
36  
37      /**
38       * <p>Constructs a {@code UnicodeEscaper} for all characters. </p>
39       */
40      public UnicodeEscaper() {
41          this(0, Integer.MAX_VALUE, true);
42      }
43  
44      /**
45       * <p>Constructs a {@code UnicodeEscaper} for the specified range. This is
46       * the underlying method for the other constructors/builders. The {@code below}
47       * and {@code above} boundaries are inclusive when {@code between} is
48       * {@code true} and exclusive when it is {@code false}. </p>
49       *
50       * @param below int value representing the lowest codepoint boundary
51       * @param above int value representing the highest codepoint boundary
52       * @param between whether to escape between the boundaries or outside them
53       */
54      protected UnicodeEscaper(final int below, final int above, final boolean between) {
55          this.below = below;
56          this.above = above;
57          this.between = between;
58      }
59  
60      /**
61       * <p>Constructs a {@code UnicodeEscaper} below the specified value (exclusive). </p>
62       *
63       * @param codepoint below which to escape
64       * @return the newly created {@code UnicodeEscaper} instance
65       */
66      public static UnicodeEscaper below(final int codepoint) {
67          return outsideOf(codepoint, Integer.MAX_VALUE);
68      }
69  
70      /**
71       * <p>Constructs a {@code UnicodeEscaper} above the specified value (exclusive). </p>
72       *
73       * @param codepoint above which to escape
74       * @return the newly created {@code UnicodeEscaper} instance
75       */
76      public static UnicodeEscaper above(final int codepoint) {
77          return outsideOf(0, codepoint);
78      }
79  
80      /**
81       * <p>Constructs a {@code UnicodeEscaper} outside of the specified values (exclusive). </p>
82       *
83       * @param codepointLow below which to escape
84       * @param codepointHigh above which to escape
85       * @return the newly created {@code UnicodeEscaper} instance
86       */
87      public static UnicodeEscaper outsideOf(final int codepointLow, final int codepointHigh) {
88          return new UnicodeEscaper(codepointLow, codepointHigh, false);
89      }
90  
91      /**
92       * <p>Constructs a {@code UnicodeEscaper} between the specified values (inclusive). </p>
93       *
94       * @param codepointLow above which to escape
95       * @param codepointHigh below which to escape
96       * @return the newly created {@code UnicodeEscaper} instance
97       */
98      public static UnicodeEscaper between(final int codepointLow, final int codepointHigh) {
99          return new UnicodeEscaper(codepointLow, codepointHigh, true);
100     }
101 
102     /**
103      * {@inheritDoc}
104      */
105     @Override
106     public boolean translate(final int codepoint, final Writer out) throws IOException {
107         if (between) {
108             if (codepoint < below || codepoint > above) {
109                 return false;
110             }
111         } else if (codepoint >= below && codepoint <= above) {
112             return false;
113         }
114 
115         // TODO: Handle potential + sign per various Unicode escape implementations
116         if (codepoint > 0xffff) {
117             out.write(toUtf16Escape(codepoint));
118         } else {
119           out.write("\\u");
120           out.write(HEX_DIGITS[(codepoint >> 12) & 15]);
121           out.write(HEX_DIGITS[(codepoint >> 8) & 15]);
122           out.write(HEX_DIGITS[(codepoint >> 4) & 15]);
123           out.write(HEX_DIGITS[(codepoint) & 15]);
124         }
125         return true;
126     }
127 
128     /**
129      * Converts the given codepoint to a hex string of the form {@code "\\uXXXX"}
130      *
131      * @param codepoint
132      *            a Unicode code point
133      * @return the hex string for the given codepoint
134      *
135      * @since 3.2
136      */
137     protected String toUtf16Escape(final int codepoint) {
138         return "\\u" + hex(codepoint);
139     }
140 }