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
18 package org.apache.commons.text.translate;
19
20 /**
21 * Translates code points to their Unicode escaped value suitable for Java source.
22 *
23 * @since 1.0
24 */
25 public class JavaUnicodeEscaper extends UnicodeEscaper {
26
27 /**
28 * Constructs a {@code JavaUnicodeEscaper} above the specified value (exclusive).
29 *
30 * @param codePoint above which to escape.
31 * @return The newly created {@code UnicodeEscaper} instance.
32 */
33 public static JavaUnicodeEscaper above(final int codePoint) {
34 return outsideOf(0, codePoint);
35 }
36
37 /**
38 * Constructs a {@code JavaUnicodeEscaper} below the specified value (exclusive).
39 *
40 * @param codePoint below which to escape.
41 * @return The newly created {@code UnicodeEscaper} instance.
42 */
43 public static JavaUnicodeEscaper below(final int codePoint) {
44 return outsideOf(codePoint, Integer.MAX_VALUE);
45 }
46
47 /**
48 * Constructs a {@code JavaUnicodeEscaper} between the specified values (inclusive).
49 *
50 * @param codePointLow above which to escape.
51 * @param codePointHigh below which to escape.
52 * @return The newly created {@code UnicodeEscaper} instance.
53 */
54 public static JavaUnicodeEscaper between(final int codePointLow, final int codePointHigh) {
55 return new JavaUnicodeEscaper(codePointLow, codePointHigh, true);
56 }
57
58 /**
59 * Constructs a {@code JavaUnicodeEscaper} outside of the specified values (exclusive).
60 *
61 * @param codePointLow below which to escape.
62 * @param codePointHigh above which to escape.
63 * @return The newly created {@code UnicodeEscaper} instance.
64 */
65 public static JavaUnicodeEscaper outsideOf(final int codePointLow, final int codePointHigh) {
66 return new JavaUnicodeEscaper(codePointLow, codePointHigh, false);
67 }
68
69 /**
70 * Constructs a {@code JavaUnicodeEscaper} for the specified range. This is the underlying method for the other constructors/builders. The {@code below} and
71 * {@code above} boundaries are inclusive when {@code between} is {@code true} and exclusive when it is {@code false}.
72 *
73 * @param below int value representing the lowest code point boundary.
74 * @param above int value representing the highest code point boundary.
75 * @param between whether to escape between the boundaries or outside them.
76 */
77 public JavaUnicodeEscaper(final int below, final int above, final boolean between) {
78 super(below, above, between);
79 }
80
81 /**
82 * Converts the given code point to a hexadecimal string of the form {@code "\\uXXXX\\uXXXX"}.
83 *
84 * @param codePoint a Unicode code point.
85 * @return The hexadecimal string for the given code point.
86 */
87 @Override
88 protected String toUtf16Escape(final int codePoint) {
89 final char[] surrogatePair = Character.toChars(codePoint);
90 return "\\u" + hex(surrogatePair[0]) + "\\u" + hex(surrogatePair[1]);
91 }
92 }