michael@0: /** michael@0: * Copyright (c) 2012, Ben Fortuna michael@0: * All rights reserved. michael@0: * michael@0: * Redistribution and use in source and binary forms, with or without michael@0: * modification, are permitted provided that the following conditions michael@0: * are met: michael@0: * michael@0: * o Redistributions of source code must retain the above copyright michael@0: * notice, this list of conditions and the following disclaimer. michael@0: * michael@0: * o Redistributions in binary form must reproduce the above copyright michael@0: * notice, this list of conditions and the following disclaimer in the michael@0: * documentation and/or other materials provided with the distribution. michael@0: * michael@0: * o Neither the name of Ben Fortuna nor the names of any other contributors michael@0: * may be used to endorse or promote products derived from this software michael@0: * without specific prior written permission. michael@0: * michael@0: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR michael@0: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, michael@0: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, michael@0: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR michael@0: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF michael@0: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING michael@0: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS michael@0: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: */ michael@0: package net.fortuna.ical4j.util; michael@0: michael@0: import java.util.regex.Pattern; michael@0: michael@0: /** michael@0: * $Id$ [23-Apr-2004] michael@0: * michael@0: * Utility methods for working with parameters. michael@0: * @author Ben Fortuna michael@0: *
michael@0: * 4.3.11 Text michael@0: * michael@0: * Value Name: TEXT michael@0: * michael@0: * Purpose This value type is used to identify values that contain human michael@0: * readable text. michael@0: * michael@0: * Formal Definition: The character sets supported by this revision of michael@0: * iCalendar are UTF-8 and US ASCII thereof. The applicability to other michael@0: * character sets is for future work. The value type is defined by the michael@0: * following notation. michael@0: * michael@0: * text = *(TSAFE-CHAR / ":" / DQUOTE / ESCAPED-CHAR) michael@0: * ; Folded according to description above michael@0: * michael@0: * ESCAPED-CHAR = "\\" / "\;" / "\," / "\N" / "\n") michael@0: * ; \\ encodes \, \N or \n encodes newline michael@0: * ; \; encodes ;, \, encodes , michael@0: * michael@0: * TSAFE-CHAR = %x20-21 / %x23-2B / %x2D-39 / %x3C-5B michael@0: * %x5D-7E / NON-US-ASCII michael@0: * ; Any character except CTLs not needed by the current michael@0: * ; character set, DQUOTE, ";", ":", "\", "," michael@0: * michael@0: * Note: Certain other character sets may require modification of the michael@0: * above definitions, but this is beyond the scope of this document. michael@0: * michael@0: * Description: If the property permits, multiple "text" values are michael@0: * specified by a COMMA character (US-ASCII decimal 44) separated list michael@0: * of values. michael@0: * michael@0: * The language in which the text is represented can be controlled by michael@0: * the "LANGUAGE" property parameter. michael@0: * michael@0: * An intentional formatted text line break MUST only be included in a michael@0: * "TEXT" property value by representing the line break with the michael@0: * character sequence of BACKSLASH (US-ASCII decimal 92), followed by a michael@0: * LATIN SMALL LETTER N (US-ASCII decimal 110) or a LATIN CAPITAL LETTER michael@0: * N (US-ASCII decimal 78), that is "\n" or "\N". michael@0: * michael@0: * The "TEXT" property values may also contain special characters that michael@0: * are used to signify delimiters, such as a COMMA character for lists michael@0: * of values or a SEMICOLON character for structured values. In order to michael@0: * support the inclusion of these special characters in "TEXT" property michael@0: * values, they MUST be escaped with a BACKSLASH character. A BACKSLASH michael@0: * character (US-ASCII decimal 92) in a "TEXT" property value MUST be michael@0: * escaped with another BACKSLASH character. A COMMA character in a michael@0: * "TEXT" property value MUST be escaped with a BACKSLASH character michael@0: * (US-ASCII decimal 92). A SEMICOLON character in a "TEXT" property michael@0: * value MUST be escaped with a BACKSLASH character (US-ASCII decimal michael@0: * 92). However, a COLON character in a "TEXT" property value SHALL NOT michael@0: * be escaped with a BACKSLASH character.Example: A multiple line value michael@0: * of: michael@0: * michael@0: * Project XYZ Final Review michael@0: * Conference Room - 3B michael@0: * Come Prepared. michael@0: * michael@0: * would be represented as: michael@0: * michael@0: * Project XYZ Final Review\nConference Room - 3B\nCome Prepared. michael@0: *michael@0: */ michael@0: public final class Strings { michael@0: michael@0: /** michael@0: * Defines a regular expression representing all parameter strings that michael@0: * should be quoted. michael@0: */ michael@0: public static final Pattern PARAM_QUOTE_PATTERN = Pattern.compile("[:;,]|[^\\p{ASCII}]"); michael@0: michael@0: private static final Pattern ESCAPE_PUNCTUATION_PATTERN = Pattern.compile("([,;])"); michael@0: private static final Pattern UNESCAPE_PUNCTUATION_PATTERN = Pattern.compile("\\\\([,;\"])"); michael@0: michael@0: private static final Pattern ESCAPE_NEWLINE_PATTERN = Pattern.compile("\r?\n"); michael@0: private static final Pattern UNESCAPE_NEWLINE_PATTERN = Pattern.compile("(?toString() method. michael@0: * @param aValue an object to quote michael@0: * @return a quoted string michael@0: */ michael@0: public static String quote(final Object aValue) { michael@0: if (aValue != null) { michael@0: return "\"" + aValue + "\""; michael@0: } michael@0: return "\"\""; michael@0: } michael@0: michael@0: /** michael@0: * Convenience method for removing surrounding quotes michael@0: * from a string value. michael@0: * @param aValue a string to remove quotes from michael@0: * @return an un-quoted string michael@0: */ michael@0: public static String unquote(final String aValue) { michael@0: if (aValue != null && aValue.startsWith("\"") && aValue.endsWith("\"")) { michael@0: return aValue.substring(0, aValue.length() - 1).substring(1); michael@0: } michael@0: return aValue; michael@0: } michael@0: michael@0: /** michael@0: * Convenience method for escaping special characters. michael@0: * @param aValue a string value to escape michael@0: * @return an escaped representation of the specified michael@0: * string michael@0: */ michael@0: public static String escape(final String aValue) { michael@0: return escapePunctuation(escapeNewline(escapeBackslash(aValue))); michael@0: } michael@0: michael@0: /** michael@0: * Convenience method for replacing escaped special characters michael@0: * with their original form. michael@0: * @param aValue a string value to unescape michael@0: * @return a string representation of the specified michael@0: * string with escaped characters replaced with their michael@0: * original form michael@0: */ michael@0: public static String unescape(final String aValue) { michael@0: return unescapeBackslash(unescapeNewline(unescapePunctuation(aValue))); michael@0: } michael@0: michael@0: private static String escapePunctuation(String value) { michael@0: if (value != null) { michael@0: return ESCAPE_PUNCTUATION_PATTERN.matcher(value).replaceAll("\\\\$1"); michael@0: } michael@0: return value; michael@0: } michael@0: michael@0: private static String unescapePunctuation(String value) { michael@0: if (value != null) { michael@0: return UNESCAPE_PUNCTUATION_PATTERN.matcher(value).replaceAll("$1"); michael@0: } michael@0: return value; michael@0: } michael@0: michael@0: public static String escapeNewline(String value) { michael@0: if (value != null) { michael@0: return ESCAPE_NEWLINE_PATTERN.matcher(value).replaceAll("\\\\n"); michael@0: } michael@0: return value; michael@0: } michael@0: michael@0: private static String unescapeNewline(String value) { michael@0: if (value != null) { michael@0: return UNESCAPE_NEWLINE_PATTERN.matcher(value).replaceAll("\n"); michael@0: } michael@0: return value; michael@0: } michael@0: michael@0: private static String escapeBackslash(String value) { michael@0: if (value != null) { michael@0: return ESCAPE_BACKSLASH_PATTERN.matcher(value).replaceAll("\\\\\\\\"); michael@0: } michael@0: return value; michael@0: } michael@0: michael@0: private static String unescapeBackslash(String value) { michael@0: if (value != null) { michael@0: return UNESCAPE_BACKSLASH_PATTERN.matcher(value).replaceAll("\\\\"); michael@0: } michael@0: return value; michael@0: } michael@0: michael@0: /** michael@0: * Wraps
java.lang.String.valueOf()
to return an empty string
michael@0: * where the specified object is null.
michael@0: * @param object an object instance
michael@0: * @return a string representation of the object
michael@0: */
michael@0: public static String valueOf(final Object object) {
michael@0: if (object == null) {
michael@0: return "";
michael@0: }
michael@0: return object.toString();
michael@0: }
michael@0: }