src/net/fortuna/ical4j/util/Strings.java

branch
ICAL4J_EMBED_1
changeset 15
cc93757aeca3
parent 14
5ae3e5665a0b
child 18
6dcaece8ec41
     1.1 --- a/src/net/fortuna/ical4j/util/Strings.java	Thu Feb 12 18:02:00 2015 +0100
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,238 +0,0 @@
     1.4 -/**
     1.5 - * Copyright (c) 2012, Ben Fortuna
     1.6 - * All rights reserved.
     1.7 - *
     1.8 - * Redistribution and use in source and binary forms, with or without
     1.9 - * modification, are permitted provided that the following conditions
    1.10 - * are met:
    1.11 - *
    1.12 - *  o Redistributions of source code must retain the above copyright
    1.13 - * notice, this list of conditions and the following disclaimer.
    1.14 - *
    1.15 - *  o Redistributions in binary form must reproduce the above copyright
    1.16 - * notice, this list of conditions and the following disclaimer in the
    1.17 - * documentation and/or other materials provided with the distribution.
    1.18 - *
    1.19 - *  o Neither the name of Ben Fortuna nor the names of any other contributors
    1.20 - * may be used to endorse or promote products derived from this software
    1.21 - * without specific prior written permission.
    1.22 - *
    1.23 - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    1.24 - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    1.25 - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    1.26 - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
    1.27 - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
    1.28 - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    1.29 - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
    1.30 - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
    1.31 - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    1.32 - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    1.33 - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.34 - */
    1.35 -package net.fortuna.ical4j.util;
    1.36 -
    1.37 -import java.util.regex.Pattern;
    1.38 -
    1.39 -/**
    1.40 - * $Id$ [23-Apr-2004]
    1.41 - *
    1.42 - * Utility methods for working with parameters.
    1.43 - * @author Ben Fortuna
    1.44 - * <pre>
    1.45 - * 4.3.11 Text
    1.46 - * 
    1.47 - *    Value Name: TEXT
    1.48 - * 
    1.49 - *    Purpose This value type is used to identify values that contain human
    1.50 - *    readable text.
    1.51 - * 
    1.52 - *    Formal Definition: The character sets supported by this revision of
    1.53 - *    iCalendar are UTF-8 and US ASCII thereof. The applicability to other
    1.54 - *    character sets is for future work. The value type is defined by the
    1.55 - *    following notation.
    1.56 - * 
    1.57 - *      text       = *(TSAFE-CHAR / ":" / DQUOTE / ESCAPED-CHAR)
    1.58 - *      ; Folded according to description above
    1.59 - * 
    1.60 - *      ESCAPED-CHAR = "\\" / "\;" / "\," / "\N" / "\n")
    1.61 - *         ; \\ encodes \, \N or \n encodes newline
    1.62 - *         ; \; encodes ;, \, encodes ,
    1.63 - * 
    1.64 - *      TSAFE-CHAR = %x20-21 / %x23-2B / %x2D-39 / %x3C-5B
    1.65 - *                   %x5D-7E / NON-US-ASCII
    1.66 - *         ; Any character except CTLs not needed by the current
    1.67 - *         ; character set, DQUOTE, ";", ":", "\", ","
    1.68 - * 
    1.69 - *      Note: Certain other character sets may require modification of the
    1.70 - *      above definitions, but this is beyond the scope of this document.
    1.71 - * 
    1.72 - *    Description: If the property permits, multiple "text" values are
    1.73 - *    specified by a COMMA character (US-ASCII decimal 44) separated list
    1.74 - *    of values.
    1.75 - * 
    1.76 - *    The language in which the text is represented can be controlled by
    1.77 - *    the "LANGUAGE" property parameter.
    1.78 - * 
    1.79 - *    An intentional formatted text line break MUST only be included in a
    1.80 - *    "TEXT" property value by representing the line break with the
    1.81 - *    character sequence of BACKSLASH (US-ASCII decimal 92), followed by a
    1.82 - *    LATIN SMALL LETTER N (US-ASCII decimal 110) or a LATIN CAPITAL LETTER
    1.83 - *    N (US-ASCII decimal 78), that is "\n" or "\N".
    1.84 - * 
    1.85 - *    The "TEXT" property values may also contain special characters that
    1.86 - *    are used to signify delimiters, such as a COMMA character for lists
    1.87 - *    of values or a SEMICOLON character for structured values. In order to
    1.88 - *    support the inclusion of these special characters in "TEXT" property
    1.89 - *    values, they MUST be escaped with a BACKSLASH character. A BACKSLASH
    1.90 - *    character (US-ASCII decimal 92) in a "TEXT" property value MUST be
    1.91 - *    escaped with another BACKSLASH character. A COMMA character in a
    1.92 - *    "TEXT" property value MUST be escaped with a BACKSLASH character
    1.93 - *    (US-ASCII decimal 92). A SEMICOLON character in a "TEXT" property
    1.94 - *    value MUST be escaped with a BACKSLASH character (US-ASCII decimal
    1.95 - *    92).  However, a COLON character in a "TEXT" property value SHALL NOT
    1.96 - *    be escaped with a BACKSLASH character.Example: A multiple line value
    1.97 - *    of:
    1.98 - * 
    1.99 - *      Project XYZ Final Review
   1.100 - *      Conference Room - 3B
   1.101 - *      Come Prepared.
   1.102 - * 
   1.103 - *    would be represented as:
   1.104 - * 
   1.105 - *      Project XYZ Final Review\nConference Room - 3B\nCome Prepared.
   1.106 - * </pre>
   1.107 - */
   1.108 -public final class Strings {
   1.109 -
   1.110 -    /**
   1.111 -     * Defines a regular expression representing all parameter strings that
   1.112 -     * should be quoted.
   1.113 -     */
   1.114 -    public static final Pattern PARAM_QUOTE_PATTERN = Pattern.compile("[:;,]|[^\\p{ASCII}]");
   1.115 -    
   1.116 -    private static final Pattern ESCAPE_PUNCTUATION_PATTERN = Pattern.compile("([,;])");
   1.117 -    private static final Pattern UNESCAPE_PUNCTUATION_PATTERN = Pattern.compile("\\\\([,;\"])");
   1.118 -    
   1.119 -    private static final Pattern ESCAPE_NEWLINE_PATTERN = Pattern.compile("\r?\n");
   1.120 -    private static final Pattern UNESCAPE_NEWLINE_PATTERN = Pattern.compile("(?<!\\\\)\\\\n");
   1.121 -    
   1.122 -    private static final Pattern ESCAPE_BACKSLASH_PATTERN = Pattern.compile("\\\\");
   1.123 -    private static final Pattern UNESCAPE_BACKSLASH_PATTERN = Pattern.compile("\\\\\\\\");
   1.124 -    
   1.125 -    
   1.126 -    
   1.127 -    /**
   1.128 -     * A string used to denote the start (and end) of iCalendar content lines.
   1.129 -     */
   1.130 -    public static final String LINE_SEPARATOR = "\r\n";
   1.131 -
   1.132 -    /**
   1.133 -     * Constructor made private to prevent instantiation.
   1.134 -     */
   1.135 -    private Strings() {
   1.136 -    }
   1.137 -
   1.138 -    /**
   1.139 -     * Convenience method for adding quotes. The specified
   1.140 -     * object is converted to a string representation by
   1.141 -     * calling its <code>toString()</code> method.
   1.142 -     * @param aValue an object to quote
   1.143 -     * @return a quoted string
   1.144 -     */
   1.145 -    public static String quote(final Object aValue) {
   1.146 -        if (aValue != null) {
   1.147 -            return "\"" + aValue + "\"";
   1.148 -        }
   1.149 -        return "\"\"";
   1.150 -    }
   1.151 -
   1.152 -    /**
   1.153 -     * Convenience method for removing surrounding quotes
   1.154 -     * from a string value.
   1.155 -     * @param aValue a string to remove quotes from
   1.156 -     * @return an un-quoted string
   1.157 -     */
   1.158 -    public static String unquote(final String aValue) {
   1.159 -        if (aValue != null && aValue.startsWith("\"") && aValue.endsWith("\"")) {
   1.160 -            return aValue.substring(0, aValue.length() - 1).substring(1);
   1.161 -        }
   1.162 -        return aValue;
   1.163 -    }
   1.164 -
   1.165 -    /**
   1.166 -     * Convenience method for escaping special characters.
   1.167 -     * @param aValue a string value to escape
   1.168 -     * @return an escaped representation of the specified
   1.169 -     * string
   1.170 -     */
   1.171 -    public static String escape(final String aValue) {
   1.172 -        return escapePunctuation(escapeNewline(escapeBackslash(aValue)));
   1.173 -    }
   1.174 -    
   1.175 -    /**
   1.176 -     * Convenience method for replacing escaped special characters
   1.177 -     * with their original form.
   1.178 -     * @param aValue a string value to unescape
   1.179 -     * @return a string representation of the specified
   1.180 -     * string with escaped characters replaced with their
   1.181 -     * original form
   1.182 -     */
   1.183 -    public static String unescape(final String aValue) {
   1.184 -        return unescapeBackslash(unescapeNewline(unescapePunctuation(aValue)));
   1.185 -    }
   1.186 -
   1.187 -    private static String escapePunctuation(String value) {
   1.188 -        if (value != null) {
   1.189 -            return ESCAPE_PUNCTUATION_PATTERN.matcher(value).replaceAll("\\\\$1");
   1.190 -        }
   1.191 -        return value;
   1.192 -    }
   1.193 -
   1.194 -    private static String unescapePunctuation(String value) {
   1.195 -        if (value != null) {
   1.196 -            return UNESCAPE_PUNCTUATION_PATTERN.matcher(value).replaceAll("$1");
   1.197 -        }
   1.198 -        return value;
   1.199 -    }
   1.200 -
   1.201 -    public static String escapeNewline(String value) {
   1.202 -        if (value != null) {
   1.203 -            return ESCAPE_NEWLINE_PATTERN.matcher(value).replaceAll("\\\\n");
   1.204 -        }
   1.205 -        return value;
   1.206 -    }
   1.207 -
   1.208 -    private static String unescapeNewline(String value) {
   1.209 -        if (value != null) {
   1.210 -            return UNESCAPE_NEWLINE_PATTERN.matcher(value).replaceAll("\n");
   1.211 -        }
   1.212 -        return value;
   1.213 -    }
   1.214 -
   1.215 -    private static String escapeBackslash(String value) {
   1.216 -        if (value != null) {
   1.217 -            return ESCAPE_BACKSLASH_PATTERN.matcher(value).replaceAll("\\\\\\\\");
   1.218 -        }
   1.219 -        return value;
   1.220 -    }
   1.221 -
   1.222 -    private static String unescapeBackslash(String value) {
   1.223 -        if (value != null) {
   1.224 -            return UNESCAPE_BACKSLASH_PATTERN.matcher(value).replaceAll("\\\\");
   1.225 -        }
   1.226 -        return value;
   1.227 -    }
   1.228 -    
   1.229 -    /**
   1.230 -     * Wraps <code>java.lang.String.valueOf()</code> to return an empty string
   1.231 -     * where the specified object is null.
   1.232 -     * @param object an object instance
   1.233 -     * @return a string representation of the object
   1.234 -     */
   1.235 -    public static String valueOf(final Object object) {
   1.236 -        if (object == null) {
   1.237 -            return "";
   1.238 -        }
   1.239 -        return object.toString();
   1.240 -    }
   1.241 -}

mercurial