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 */ 017 package org.apache.commons.lang3.text.translate; 018 019 import java.io.IOException; 020 import java.io.Writer; 021 022 /** 023 * Translates codepoints to their unicode escaped value. 024 * 025 * @author Apache Software Foundation 026 * @since 3.0 027 * @version $Id: UnicodeEscaper.java 967237 2010-07-23 20:08:57Z mbenson $ 028 */ 029 public class UnicodeEscaper extends CodePointTranslator { 030 031 private final int below; 032 private final int above; 033 private final boolean between; 034 035 public UnicodeEscaper(){ 036 this(0, Integer.MAX_VALUE, true); 037 } 038 039 private UnicodeEscaper(int below, int above, boolean between) { 040 this.below = below; 041 this.above = above; 042 this.between = between; 043 } 044 045 public static UnicodeEscaper below(int codepoint) { 046 return outsideOf(codepoint, Integer.MAX_VALUE); 047 } 048 049 public static UnicodeEscaper above(int codepoint) { 050 return outsideOf(0, codepoint); 051 } 052 053 public static UnicodeEscaper outsideOf(int codepointLow, int codepointHigh) { 054 UnicodeEscaper escaper = new UnicodeEscaper(codepointLow, codepointHigh, false); 055 return escaper; 056 } 057 058 public static UnicodeEscaper between(int codepointLow, int codepointHigh) { 059 UnicodeEscaper escaper = new UnicodeEscaper(codepointLow, codepointHigh, true); 060 return escaper; 061 } 062 063 /** 064 * {@inheritDoc} 065 */ 066 @Override 067 public boolean translate(int codepoint, Writer out) throws IOException { 068 if(between) { 069 if (codepoint < below || codepoint > above) { 070 return false; 071 } 072 } else { 073 if (codepoint >= below && codepoint <= above) { 074 return false; 075 } 076 } 077 078 // TODO: Handle potential + sign per various unicode escape implementations 079 if (codepoint > 0xffff) { 080 // TODO: Figure out what to do. Output as two unicodes? 081 // Does this make this a Java-specific output class? 082 out.write("\\u" + hex(codepoint)); 083 } else if (codepoint > 0xfff) { 084 out.write("\\u" + hex(codepoint)); 085 } else if (codepoint > 0xff) { 086 out.write("\\u0" + hex(codepoint)); 087 } else if (codepoint > 0xf) { 088 out.write("\\u00" + hex(codepoint)); 089 } else { 090 out.write("\\u000" + hex(codepoint)); 091 } 092 return true; 093 } 094 }