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