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