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