Tue, 10 Feb 2015 18:12:00 +0100
Import initial revisions of existing project AndroidCaldavSyncAdapater,
forked from upstream repository at 27e8a0f8495c92e0780d450bdf0c7cec77a03a55.
michael@0 | 1 | /** |
michael@0 | 2 | * Copyright (c) 2012, Ben Fortuna |
michael@0 | 3 | * All rights reserved. |
michael@0 | 4 | * |
michael@0 | 5 | * Redistribution and use in source and binary forms, with or without |
michael@0 | 6 | * modification, are permitted provided that the following conditions |
michael@0 | 7 | * are met: |
michael@0 | 8 | * |
michael@0 | 9 | * o Redistributions of source code must retain the above copyright |
michael@0 | 10 | * notice, this list of conditions and the following disclaimer. |
michael@0 | 11 | * |
michael@0 | 12 | * o Redistributions in binary form must reproduce the above copyright |
michael@0 | 13 | * notice, this list of conditions and the following disclaimer in the |
michael@0 | 14 | * documentation and/or other materials provided with the distribution. |
michael@0 | 15 | * |
michael@0 | 16 | * o Neither the name of Ben Fortuna nor the names of any other contributors |
michael@0 | 17 | * may be used to endorse or promote products derived from this software |
michael@0 | 18 | * without specific prior written permission. |
michael@0 | 19 | * |
michael@0 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
michael@0 | 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
michael@0 | 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
michael@0 | 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
michael@0 | 24 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
michael@0 | 25 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
michael@0 | 26 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
michael@0 | 27 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
michael@0 | 28 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
michael@0 | 29 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
michael@0 | 30 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 31 | */ |
michael@0 | 32 | package net.fortuna.ical4j.model.property; |
michael@0 | 33 | |
michael@0 | 34 | import java.io.IOException; |
michael@0 | 35 | import java.io.UnsupportedEncodingException; |
michael@0 | 36 | import java.net.URI; |
michael@0 | 37 | import java.net.URISyntaxException; |
michael@0 | 38 | |
michael@0 | 39 | import net.fortuna.ical4j.model.Parameter; |
michael@0 | 40 | import net.fortuna.ical4j.model.ParameterList; |
michael@0 | 41 | import net.fortuna.ical4j.model.Property; |
michael@0 | 42 | import net.fortuna.ical4j.model.PropertyFactoryImpl; |
michael@0 | 43 | import net.fortuna.ical4j.model.ValidationException; |
michael@0 | 44 | import net.fortuna.ical4j.model.parameter.Encoding; |
michael@0 | 45 | import net.fortuna.ical4j.model.parameter.Value; |
michael@0 | 46 | import net.fortuna.ical4j.util.DecoderFactory; |
michael@0 | 47 | import net.fortuna.ical4j.util.EncoderFactory; |
michael@0 | 48 | import net.fortuna.ical4j.util.ParameterValidator; |
michael@0 | 49 | import net.fortuna.ical4j.util.Strings; |
michael@0 | 50 | import net.fortuna.ical4j.util.Uris; |
michael@0 | 51 | |
michael@0 | 52 | import org.apache.commons.codec.BinaryDecoder; |
michael@0 | 53 | import org.apache.commons.codec.BinaryEncoder; |
michael@0 | 54 | import org.apache.commons.codec.DecoderException; |
michael@0 | 55 | import org.apache.commons.codec.EncoderException; |
michael@0 | 56 | import org.apache.commons.logging.Log; |
michael@0 | 57 | import org.apache.commons.logging.LogFactory; |
michael@0 | 58 | |
michael@0 | 59 | /** |
michael@0 | 60 | * $Id$ |
michael@0 | 61 | * |
michael@0 | 62 | * Created: [Apr 6, 2004] |
michael@0 | 63 | * |
michael@0 | 64 | * Defines an ATTACH iCalendar component property. |
michael@0 | 65 | * |
michael@0 | 66 | * <pre> |
michael@0 | 67 | * 4.8.1.1 Attachment |
michael@0 | 68 | * |
michael@0 | 69 | * Property Name: ATTACH |
michael@0 | 70 | * |
michael@0 | 71 | * Purpose: The property provides the capability to associate a document |
michael@0 | 72 | * object with a calendar component. |
michael@0 | 73 | * |
michael@0 | 74 | * Value Type: The default value type for this property is URI. The |
michael@0 | 75 | * value type can also be set to BINARY to indicate inline binary |
michael@0 | 76 | * encoded content information. |
michael@0 | 77 | * |
michael@0 | 78 | * Property Parameters: Non-standard, inline encoding, format type and |
michael@0 | 79 | * value data type property parameters can be specified on this |
michael@0 | 80 | * property. |
michael@0 | 81 | * |
michael@0 | 82 | * Conformance: The property can be specified in a "VEVENT", "VTODO", |
michael@0 | 83 | * "VJOURNAL" or "VALARM" calendar components. |
michael@0 | 84 | * |
michael@0 | 85 | * Description: The property can be specified within "VEVENT", "VTODO", |
michael@0 | 86 | * "VJOURNAL", or "VALARM" calendar components. This property can be |
michael@0 | 87 | * specified multiple times within an iCalendar object. |
michael@0 | 88 | * |
michael@0 | 89 | * Format Definition: The property is defined by the following notation: |
michael@0 | 90 | * |
michael@0 | 91 | * attach = "ATTACH" attparam ":" uri CRLF |
michael@0 | 92 | * |
michael@0 | 93 | * attach =/ "ATTACH" attparam ";" "ENCODING" "=" "BASE64" |
michael@0 | 94 | * ";" "VALUE" "=" "BINARY" ":" binary |
michael@0 | 95 | * |
michael@0 | 96 | * attparam = *( |
michael@0 | 97 | * |
michael@0 | 98 | * ; the following is optional, |
michael@0 | 99 | * ; but MUST NOT occur more than once |
michael@0 | 100 | * |
michael@0 | 101 | * (";" fmttypeparam) / |
michael@0 | 102 | * |
michael@0 | 103 | * ; the following is optional, |
michael@0 | 104 | * ; and MAY occur more than once |
michael@0 | 105 | * |
michael@0 | 106 | * (";" xparam) |
michael@0 | 107 | * |
michael@0 | 108 | * ) |
michael@0 | 109 | * </pre> |
michael@0 | 110 | * |
michael@0 | 111 | * @author benf |
michael@0 | 112 | */ |
michael@0 | 113 | public class Attach extends Property { |
michael@0 | 114 | |
michael@0 | 115 | private static final long serialVersionUID = 4439949507756383452L; |
michael@0 | 116 | |
michael@0 | 117 | private URI uri; |
michael@0 | 118 | |
michael@0 | 119 | private byte[] binary; |
michael@0 | 120 | |
michael@0 | 121 | /** |
michael@0 | 122 | * Default constructor. |
michael@0 | 123 | */ |
michael@0 | 124 | public Attach() { |
michael@0 | 125 | super(ATTACH, PropertyFactoryImpl.getInstance()); |
michael@0 | 126 | } |
michael@0 | 127 | |
michael@0 | 128 | /** |
michael@0 | 129 | * @param aList a list of parameters for this component |
michael@0 | 130 | * @param aValue a value string for this component |
michael@0 | 131 | * @throws IOException when there is an error reading the binary stream |
michael@0 | 132 | * @throws URISyntaxException where the specified string is not a valid uri |
michael@0 | 133 | */ |
michael@0 | 134 | public Attach(final ParameterList aList, final String aValue) |
michael@0 | 135 | throws IOException, URISyntaxException { |
michael@0 | 136 | super(ATTACH, aList, PropertyFactoryImpl.getInstance()); |
michael@0 | 137 | setValue(aValue); |
michael@0 | 138 | } |
michael@0 | 139 | |
michael@0 | 140 | /** |
michael@0 | 141 | * @param data binary data |
michael@0 | 142 | */ |
michael@0 | 143 | public Attach(final byte[] data) { |
michael@0 | 144 | super(ATTACH, PropertyFactoryImpl.getInstance()); |
michael@0 | 145 | // add required parameters.. |
michael@0 | 146 | getParameters().add(Encoding.BASE64); |
michael@0 | 147 | getParameters().add(Value.BINARY); |
michael@0 | 148 | this.binary = data; |
michael@0 | 149 | } |
michael@0 | 150 | |
michael@0 | 151 | /** |
michael@0 | 152 | * @param aList a list of parameters for this component |
michael@0 | 153 | * @param data binary data |
michael@0 | 154 | */ |
michael@0 | 155 | public Attach(final ParameterList aList, final byte[] data) { |
michael@0 | 156 | super(ATTACH, aList, PropertyFactoryImpl.getInstance()); |
michael@0 | 157 | this.binary = data; |
michael@0 | 158 | } |
michael@0 | 159 | |
michael@0 | 160 | /** |
michael@0 | 161 | * @param aUri a URI |
michael@0 | 162 | */ |
michael@0 | 163 | public Attach(final URI aUri) { |
michael@0 | 164 | super(ATTACH, PropertyFactoryImpl.getInstance()); |
michael@0 | 165 | this.uri = aUri; |
michael@0 | 166 | } |
michael@0 | 167 | |
michael@0 | 168 | /** |
michael@0 | 169 | * @param aList a list of parameters for this component |
michael@0 | 170 | * @param aUri a URI |
michael@0 | 171 | */ |
michael@0 | 172 | public Attach(final ParameterList aList, final URI aUri) { |
michael@0 | 173 | super(ATTACH, aList, PropertyFactoryImpl.getInstance()); |
michael@0 | 174 | this.uri = aUri; |
michael@0 | 175 | } |
michael@0 | 176 | |
michael@0 | 177 | /** |
michael@0 | 178 | * {@inheritDoc} |
michael@0 | 179 | */ |
michael@0 | 180 | public final void validate() throws ValidationException { |
michael@0 | 181 | |
michael@0 | 182 | /* |
michael@0 | 183 | * ; the following is optional, ; but MUST NOT occur more than once (";" fmttypeparam) / |
michael@0 | 184 | */ |
michael@0 | 185 | ParameterValidator.getInstance().assertOneOrLess(Parameter.FMTTYPE, |
michael@0 | 186 | getParameters()); |
michael@0 | 187 | |
michael@0 | 188 | /* |
michael@0 | 189 | * ; the following is optional, ; and MAY occur more than once (";" xparam) |
michael@0 | 190 | */ |
michael@0 | 191 | |
michael@0 | 192 | /* |
michael@0 | 193 | * If the value type parameter is ";VALUE=BINARY", then the inline encoding parameter MUST be specified with the |
michael@0 | 194 | * value ";ENCODING=BASE64". |
michael@0 | 195 | */ |
michael@0 | 196 | if (Value.BINARY.equals(getParameter(Parameter.VALUE))) { |
michael@0 | 197 | ParameterValidator.getInstance().assertOne(Parameter.ENCODING, |
michael@0 | 198 | getParameters()); |
michael@0 | 199 | if (!Encoding.BASE64.equals(getParameter(Parameter.ENCODING))) { |
michael@0 | 200 | throw new ValidationException( |
michael@0 | 201 | "If the value type parameter is [BINARY], the inline" |
michael@0 | 202 | + "encoding parameter MUST be specified with the value [BASE64]"); |
michael@0 | 203 | } |
michael@0 | 204 | } |
michael@0 | 205 | } |
michael@0 | 206 | |
michael@0 | 207 | /** |
michael@0 | 208 | * @return Returns the binary. |
michael@0 | 209 | */ |
michael@0 | 210 | public final byte[] getBinary() { |
michael@0 | 211 | return binary; |
michael@0 | 212 | } |
michael@0 | 213 | |
michael@0 | 214 | /** |
michael@0 | 215 | * @return Returns the uri. |
michael@0 | 216 | */ |
michael@0 | 217 | public final URI getUri() { |
michael@0 | 218 | return uri; |
michael@0 | 219 | } |
michael@0 | 220 | |
michael@0 | 221 | /** |
michael@0 | 222 | * Sets the current value of the Attach instance. If the specified |
michael@0 | 223 | * value is encoded binary data, the value is decoded and stored in |
michael@0 | 224 | * the binary field. Otherwise the value is assumed to be a URI |
michael@0 | 225 | * location to binary data and is stored as such. |
michael@0 | 226 | * |
michael@0 | 227 | * @param aValue a string encoded binary or URI value |
michael@0 | 228 | * @throws IOException where binary data cannot be decoded |
michael@0 | 229 | * @throws URISyntaxException where the specified value is not a valid URI |
michael@0 | 230 | */ |
michael@0 | 231 | public final void setValue(final String aValue) throws IOException, |
michael@0 | 232 | URISyntaxException { |
michael@0 | 233 | |
michael@0 | 234 | // determine if ATTACH is a URI or an embedded |
michael@0 | 235 | // binary.. |
michael@0 | 236 | if (getParameter(Parameter.ENCODING) != null) { |
michael@0 | 237 | // binary = Base64.decode(aValue); |
michael@0 | 238 | try { |
michael@0 | 239 | final BinaryDecoder decoder = DecoderFactory.getInstance() |
michael@0 | 240 | .createBinaryDecoder( |
michael@0 | 241 | (Encoding) getParameter(Parameter.ENCODING)); |
michael@0 | 242 | binary = decoder.decode(aValue.getBytes()); |
michael@0 | 243 | } |
michael@0 | 244 | catch (UnsupportedEncodingException uee) { |
michael@0 | 245 | Log log = LogFactory.getLog(Attach.class); |
michael@0 | 246 | log.error("Error encoding binary data", uee); |
michael@0 | 247 | } |
michael@0 | 248 | catch (DecoderException de) { |
michael@0 | 249 | Log log = LogFactory.getLog(Attach.class); |
michael@0 | 250 | log.error("Error decoding binary data", de); |
michael@0 | 251 | } |
michael@0 | 252 | } |
michael@0 | 253 | // assume URI.. |
michael@0 | 254 | else { |
michael@0 | 255 | uri = Uris.create(aValue); |
michael@0 | 256 | } |
michael@0 | 257 | } |
michael@0 | 258 | |
michael@0 | 259 | /** |
michael@0 | 260 | * {@inheritDoc} |
michael@0 | 261 | */ |
michael@0 | 262 | public final String getValue() { |
michael@0 | 263 | if (getUri() != null) { |
michael@0 | 264 | return Uris.decode(Strings.valueOf(getUri())); |
michael@0 | 265 | } |
michael@0 | 266 | else if (getBinary() != null) { |
michael@0 | 267 | // return Base64.encodeBytes(getBinary(), Base64.DONT_BREAK_LINES); |
michael@0 | 268 | try { |
michael@0 | 269 | final BinaryEncoder encoder = EncoderFactory.getInstance() |
michael@0 | 270 | .createBinaryEncoder( |
michael@0 | 271 | (Encoding) getParameter(Parameter.ENCODING)); |
michael@0 | 272 | return new String(encoder.encode(getBinary())); |
michael@0 | 273 | } |
michael@0 | 274 | catch (UnsupportedEncodingException uee) { |
michael@0 | 275 | Log log = LogFactory.getLog(Attach.class); |
michael@0 | 276 | log.error("Error encoding binary data", uee); |
michael@0 | 277 | } |
michael@0 | 278 | catch (EncoderException ee) { |
michael@0 | 279 | Log log = LogFactory.getLog(Attach.class); |
michael@0 | 280 | log.error("Error encoding binary data", ee); |
michael@0 | 281 | } |
michael@0 | 282 | } |
michael@0 | 283 | return null; |
michael@0 | 284 | } |
michael@0 | 285 | |
michael@0 | 286 | /** |
michael@0 | 287 | * @param binary The binary to set. |
michael@0 | 288 | */ |
michael@0 | 289 | public final void setBinary(final byte[] binary) { |
michael@0 | 290 | this.binary = binary; |
michael@0 | 291 | // unset uri.. |
michael@0 | 292 | this.uri = null; |
michael@0 | 293 | } |
michael@0 | 294 | |
michael@0 | 295 | /** |
michael@0 | 296 | * @param uri The uri to set. |
michael@0 | 297 | */ |
michael@0 | 298 | public final void setUri(final URI uri) { |
michael@0 | 299 | this.uri = uri; |
michael@0 | 300 | // unset binary.. |
michael@0 | 301 | this.binary = null; |
michael@0 | 302 | } |
michael@0 | 303 | } |