001    package biweekly.property.marshaller;
002    
003    import java.util.List;
004    
005    import org.apache.commons.codec.binary.Base64;
006    
007    import biweekly.io.xml.XCalElement;
008    import biweekly.parameter.Encoding;
009    import biweekly.parameter.ICalParameters;
010    import biweekly.parameter.Value;
011    import biweekly.property.Attachment;
012    
013    /*
014     Copyright (c) 2013, Michael Angstadt
015     All rights reserved.
016    
017     Redistribution and use in source and binary forms, with or without
018     modification, are permitted provided that the following conditions are met: 
019    
020     1. Redistributions of source code must retain the above copyright notice, this
021     list of conditions and the following disclaimer. 
022     2. Redistributions in binary form must reproduce the above copyright notice,
023     this list of conditions and the following disclaimer in the documentation
024     and/or other materials provided with the distribution. 
025    
026     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
027     ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
028     WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
029     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
030     ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
031     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
032     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
033     ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
034     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
035     SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
036     */
037    
038    /**
039     * Marshals {@link Attachment} properties.
040     * @author Michael Angstadt
041     */
042    public class AttachmentMarshaller extends ICalPropertyMarshaller<Attachment> {
043            public AttachmentMarshaller() {
044                    super(Attachment.class, "ATTACH");
045            }
046    
047            @Override
048            protected void _prepareParameters(Attachment property, ICalParameters copy) {
049                    if (property.getUri() != null) {
050                            copy.setEncoding(null);
051                            copy.setValue(null);
052                    } else if (property.getData() != null) {
053                            copy.setEncoding(Encoding.BASE64);
054                            copy.setValue(Value.BINARY);
055                    }
056            }
057    
058            @Override
059            protected String _writeText(Attachment property) {
060                    if (property.getUri() != null) {
061                            return property.getUri();
062                    }
063                    if (property.getData() != null) {
064                            return Base64.encodeBase64String(property.getData());
065                    }
066                    return null;
067            }
068    
069            @Override
070            protected Attachment _parseText(String value, ICalParameters parameters, List<String> warnings) {
071                    value = unescape(value);
072    
073                    Attachment attachment = new Attachment(null, (String) null);
074                    if (parameters.getValue() == Value.BINARY || parameters.getEncoding() == Encoding.BASE64) {
075                            attachment.setData(Base64.decodeBase64(value));
076                    } else {
077                            attachment.setUri(value);
078                    }
079                    return attachment;
080            }
081    
082            @Override
083            protected void _writeXml(Attachment property, XCalElement element) {
084                    if (property.getUri() != null) {
085                            element.append(Value.URI, property.getUri());
086                    } else if (property.getData() != null) {
087                            element.append(Value.BINARY, Base64.encodeBase64String(property.getData()));
088                    }
089            }
090    
091            @Override
092            protected Attachment _parseXml(XCalElement element, ICalParameters parameters, List<String> warnings) {
093                    Attachment attachment = new Attachment(null, (String) null);
094    
095                    String value = element.first(Value.BINARY);
096                    if (value != null) {
097                            attachment.setData(Base64.decodeBase64(value));
098                    } else {
099                            value = element.first(Value.URI);
100                            attachment.setUri(value);
101                    }
102    
103                    return attachment;
104            }
105    }