org.unbescape.java.JavaEscapeLevel Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of unbescape Show documentation
Show all versions of unbescape Show documentation
Advanced yet easy-to-use escape/unescape library for Java
/*
* =============================================================================
*
* Copyright (c) 2014, The UNBESCAPE team (http://www.unbescape.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* =============================================================================
*/
package org.unbescape.java;
/**
*
* Levels defined for Java escape/unescape operations:
*
*
*
* - Level 1: Escape only the basic escape set. Note the result of a level-1 escape
* operation might still contain non-ASCII characters if they existed in input, and therefore you
* will still need to correctly manage your input/output character encoding settings. Such
* basic set consists of:
*
* - The Single Escape Characters:
* \b (U+0008),
* \t (U+0009),
* \n (U+000A),
* \f (U+000C),
* \r (U+000D),
* \" (U+0022),
* \' (U+0027),
* \\ (U+005C). Note \' is not really needed in
* String literals (only in Character literals), so it won't be used until escape level 3.
*
* -
* Two ranges of non-displayable, control characters (some of which are already part of the
* single escape characters list): U+0000 to U+001F
* and U+007F to U+009F.
*
*
*
* - Level 2: Escape the basic escape set (as defined in level 1), plus all
* non-ASCII characters. The result of a level-2 escape operation is therefore always ASCII-only text, and
* safer to use in complex scenarios with mixed input/output character encodings.
* - Level 3: Escape all non-alphanumeric characters, this is, all but those in the
* A-Z, a-z and 0-9 ranges. This level
* can be safely used for completely escaping texts, including whitespace, line feeds, punctuation, etc. in
* scenarios where this adds an extra level of safety.
* - Level 4: Escape all characters, even alphanumeric ones.
*
*
*
* For further information, see the Glossary and the References sections at the
* documentation for the {@link org.unbescape.java.JavaEscape} class.
*
*
* @author Daniel Fernández
*
* @since 1.0.0
*
*/
public enum JavaEscapeLevel {
/**
* Level 1 escape: escape only the basic escape set: Single Escape Chars plus non-displayable control chars.
*/
LEVEL_1_BASIC_ESCAPE_SET(1),
/**
* Level 2 escape: escape the basic escape set plus all non-ASCII characters (result will always be ASCII).
*/
LEVEL_2_ALL_NON_ASCII_PLUS_BASIC_ESCAPE_SET(2),
/**
* Level 3 escape: escape all non-alphanumeric characteres (escape all but those in the
* A-Z, a-z and 0-9 ranges).
*/
LEVEL_3_ALL_NON_ALPHANUMERIC(3),
/**
* Level 4 escape: escape all characters, including alphanumeric.
*/
LEVEL_4_ALL_CHARACTERS(4);
private final int escapeLevel;
/**
*
* Utility method for obtaining an enum value from its corresponding int level value.
*
*
* @param level the level
* @return the escape level enum constant, or IllegalArgumentException if level does not exist.
*/
public static JavaEscapeLevel forLevel(final int level) {
switch (level) {
case 1: return LEVEL_1_BASIC_ESCAPE_SET;
case 2: return LEVEL_2_ALL_NON_ASCII_PLUS_BASIC_ESCAPE_SET;
case 3: return LEVEL_3_ALL_NON_ALPHANUMERIC;
case 4: return LEVEL_4_ALL_CHARACTERS;
default:
throw new IllegalArgumentException("No escape level enum constant defined for level: " + level);
}
}
JavaEscapeLevel(final int escapeLevel) {
this.escapeLevel = escapeLevel;
}
/**
* Return the int escape level.
*
* @return the escape level.
*/
public int getEscapeLevel() {
return this.escapeLevel;
}
}