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 019/** 020 * Translates codepoints to their Unicode escaped value suitable for Java source. 021 * 022 * @since 3.2 023 * @deprecated as of 3.6, use commons-text 024 * <a href="https://commons.apache.org/proper/commons-text/javadocs/api-release/org/apache/commons/text/translate/UnicodeEscaper.html"> 025 * UnicodeEscaper</a> instead 026 */ 027@Deprecated 028public class JavaUnicodeEscaper extends UnicodeEscaper { 029 030 /** 031 * <p> 032 * Constructs a <code>JavaUnicodeEscaper</code> above the specified value (exclusive). 033 * </p> 034 * 035 * @param codepoint 036 * above which to escape 037 * @return the newly created {@code UnicodeEscaper} instance 038 */ 039 public static JavaUnicodeEscaper above(final int codepoint) { 040 return outsideOf(0, codepoint); 041 } 042 043 /** 044 * <p> 045 * Constructs a <code>JavaUnicodeEscaper</code> below the specified value (exclusive). 046 * </p> 047 * 048 * @param codepoint 049 * below which to escape 050 * @return the newly created {@code UnicodeEscaper} instance 051 */ 052 public static JavaUnicodeEscaper below(final int codepoint) { 053 return outsideOf(codepoint, Integer.MAX_VALUE); 054 } 055 056 /** 057 * <p> 058 * Constructs a <code>JavaUnicodeEscaper</code> between the specified values (inclusive). 059 * </p> 060 * 061 * @param codepointLow 062 * above which to escape 063 * @param codepointHigh 064 * below which to escape 065 * @return the newly created {@code UnicodeEscaper} instance 066 */ 067 public static JavaUnicodeEscaper between(final int codepointLow, final int codepointHigh) { 068 return new JavaUnicodeEscaper(codepointLow, codepointHigh, true); 069 } 070 071 /** 072 * <p> 073 * Constructs a <code>JavaUnicodeEscaper</code> outside of the specified values (exclusive). 074 * </p> 075 * 076 * @param codepointLow 077 * below which to escape 078 * @param codepointHigh 079 * above which to escape 080 * @return the newly created {@code UnicodeEscaper} instance 081 */ 082 public static JavaUnicodeEscaper outsideOf(final int codepointLow, final int codepointHigh) { 083 return new JavaUnicodeEscaper(codepointLow, codepointHigh, false); 084 } 085 086 /** 087 * <p> 088 * Constructs a <code>JavaUnicodeEscaper</code> for the specified range. This is the underlying method for the 089 * other constructors/builders. The <code>below</code> and <code>above</code> boundaries are inclusive when 090 * <code>between</code> is <code>true</code> and exclusive when it is <code>false</code>. 091 * </p> 092 * 093 * @param below 094 * int value representing the lowest codepoint boundary 095 * @param above 096 * int value representing the highest codepoint boundary 097 * @param between 098 * whether to escape between the boundaries or outside them 099 */ 100 public JavaUnicodeEscaper(final int below, final int above, final boolean between) { 101 super(below, above, between); 102 } 103 104 /** 105 * Converts the given codepoint to a hex string of the form {@code "\\uXXXX\\uXXXX"} 106 * 107 * @param codepoint 108 * a Unicode code point 109 * @return the hex string for the given codepoint 110 */ 111 @Override 112 protected String toUtf16Escape(final int codepoint) { 113 final char[] surrogatePair = Character.toChars(codepoint); 114 return "\\u" + hex(surrogatePair[0]) + "\\u" + hex(surrogatePair[1]); 115 } 116 117}