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

changeset 0
fb9019fb1bf7
equal deleted inserted replaced
-1:000000000000 0:8565d4344098
1 /**
2 * Copyright (c) 2012, Ben Fortuna
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * o Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * o Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * o Neither the name of Ben Fortuna nor the names of any other contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32 package net.fortuna.ical4j.util;
33
34 import java.net.URI;
35 import java.net.URISyntaxException;
36 import java.util.regex.Pattern;
37
38 /**
39 * $Id$
40 *
41 * Created on 11/09/2005
42 *
43 * Utility methods for working with URIs.
44 * @author Ben Fortuna
45 */
46 public final class Uris {
47
48 /**
49 * URI Scheme used when relaxed parsing is enabled and the given input stream lead to an invalid URI.
50 */
51 public static final String INVALID_SCHEME = "net.fortunal.ical4j.invalid";
52
53 private static final Pattern CID_PATTERN = Pattern.compile("(?i)^cid:.*");
54 private static final Pattern NOTES_CID_REPLACEMENT_PATTERN = Pattern.compile("[<>]");
55
56 /**
57 * Constructor made private to enforce static nature.
58 */
59 private Uris() {
60 }
61
62 /**
63 * Encodes the specified URI string using the UTF-8 charset. In the event that an exception is thrown, the specifed
64 * URI string is returned unmodified.
65 * @param s a URI string
66 * @return an encoded URI string
67 */
68 public static String encode(final String s) {
69 /*
70 * try { return URLEncoder.encode(s, ENCODING_CHARSET); } catch (UnsupportedEncodingException use) {
71 * log.error("Error ocurred encoding URI [" + s + "]", use); }
72 */
73
74 /*
75 * Lotus Notes does not correctly strip angle brackets from cid uris. From RFC2392: A "cid" URL is converted to
76 * the corresponding Content-ID message header [MIME] by removing the "cid:" prefix, converting the % encoded
77 * character to their equivalent US-ASCII characters, and enclosing the remaining parts with an angle bracket
78 * pair, "<" and ">". For example, "cid:foo4%25foo1@bar.net" corresponds to Content-ID: <foo4%25foo1@bar.net>
79 * Reversing the process and converting URL special characters to their % encodings produces the original cid. A
80 * "mid" URL is converted to a Message-ID or Message-ID/Content-ID pair in a similar fashion.
81 */
82 if (CompatibilityHints.isHintEnabled(CompatibilityHints.KEY_NOTES_COMPATIBILITY)
83 && CID_PATTERN.matcher(s).matches()) {
84
85 return NOTES_CID_REPLACEMENT_PATTERN.matcher(s).replaceAll("");
86 }
87 return s;
88 }
89
90 /**
91 * Decodes the specified URI string using the UTF-8 charset. In the event that an exception is thrown, the specifed
92 * URI string is returned unmodified.
93 * @param s a URI string
94 * @return an encoded URI string
95 */
96 public static String decode(final String s) {
97 /*
98 * try { return URLDecoder.decode(s, ENCODING_CHARSET); } catch (UnsupportedEncodingException use) {
99 * log.error("Error ocurred decoding URI [" + s + "]", use); }
100 */
101 return s;
102 }
103
104 /**
105 * Attempts to create a URI instance and will optionally swallow any resulting URISyntaxException depending on
106 * configured {@link CompatibilityHints}. Will also automatically attempt encoding of the string representation for
107 * greater compatibility.
108 * <p>When relaxed parsing is enabled and if the string representation is not valid, a second URI creation attempt is made
109 * by extracting the scheme from the scheme specific part and URI encoding that later part. For example,
110 * "mailto: joe smith@example.com" becomes "mailto:joe%20smith@example.com".<p>
111 * <p>If the second attempts also leads to a {@code URISyntaxException}, an opaque URI is constructed with a scheme
112 * of {@code Uris.INVALID_SCHEME} and a value corresponding to the initial representation.</p>
113 *
114 * @param s a string representation of a URI.
115 * @return a URI instance, which may not correspond to the URI string if a valid
116 * URI string is not specified and relaxed parsing is enabled.
117 * @throws URISyntaxException if a valid URI string is not specified and relaxed parsing is disabled
118 */
119 public static URI create(final String s) throws URISyntaxException {
120 try {
121 return new URI(encode(s));
122 }
123 catch (URISyntaxException use) {
124 if (CompatibilityHints
125 .isHintEnabled(CompatibilityHints.KEY_RELAXED_PARSING)) {
126 String encoded = encode(s);
127 int index = encoded.indexOf(':');
128 if (index != -1 && index < encoded.length() -1) {
129 try {
130 return new URI(encoded.substring(0, index), encoded.substring(index + 1), null);
131 } catch (URISyntaxException use2) {
132 }
133 }
134 try {
135 return new URI(INVALID_SCHEME, s, null);
136 } catch (URISyntaxException use2) {
137 // should not happen as we are building an opaque URI
138 throw new IllegalArgumentException("Could not build URI from " + s);
139 }
140 }
141 throw use;
142 }
143 }
144 }

mercurial