001    package biweekly.property;
002    
003    import java.io.File;
004    import java.io.FileInputStream;
005    import java.io.IOException;
006    import java.util.List;
007    
008    import biweekly.component.ICalComponent;
009    import biweekly.util.IOUtils;
010    
011    /*
012     Copyright (c) 2013, Michael Angstadt
013     All rights reserved.
014    
015     Redistribution and use in source and binary forms, with or without
016     modification, are permitted provided that the following conditions are met: 
017    
018     1. Redistributions of source code must retain the above copyright notice, this
019     list of conditions and the following disclaimer. 
020     2. Redistributions in binary form must reproduce the above copyright notice,
021     this list of conditions and the following disclaimer in the documentation
022     and/or other materials provided with the distribution. 
023    
024     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
025     ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
026     WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
027     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
028     ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
029     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
030     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
031     ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
032     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
033     SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
034     */
035    
036    /**
037     * <p>
038     * Represents a binary resource that is associated with an event, to-do, journal
039     * entry, or alarm.
040     * </p>
041     * 
042     * <p>
043     * <b>Examples:</b>
044     * 
045     * <pre class="brush:java">
046     * //from a byte array
047     * byte[] data = ...
048     * Attachment attach = new Attachment("image/png", data);
049     * 
050     * //reading from a file 
051     * Attachment attach = new Attachment(&quot;image/png&quot;, new File(&quot;image.png&quot;));
052     * 
053     * //referencing a URL
054     * Attachment attach = new Attachment(&quot;image/png&quot;, &quot;http://example.com/image.png&quot;);
055     * </pre>
056     * 
057     * </p>
058     * @author Michael Angstadt
059     * @rfc 5545 p.80-1
060     */
061    public class Attachment extends ICalProperty {
062            private byte[] data;
063            private String uri;
064    
065            /**
066             * Creates a new attachment.
067             * @param formatType the content-type of the data (e.g. "image/png")
068             * @param file the file to attach
069             * @throws IOException if there's a problem reading from the file
070             */
071            public Attachment(String formatType, File file) throws IOException {
072                    this.data = IOUtils.toByteArray(new FileInputStream(file), true);
073                    setFormatType(formatType);
074            }
075    
076            /**
077             * Creates a new attachment.
078             * @param formatType the content-type of the data (e.g. "image/png")
079             * @param data the binary data
080             */
081            public Attachment(String formatType, byte[] data) {
082                    this.data = data;
083                    setFormatType(formatType);
084            }
085    
086            /**
087             * Creates a new attachment.
088             * @param formatType the content-type of the data (e.g. "image/png")
089             * @param uri a URL pointing to the resource (e.g.
090             * "http://example.com/image.png")
091             */
092            public Attachment(String formatType, String uri) {
093                    this.uri = uri;
094                    setFormatType(formatType);
095            }
096    
097            /**
098             * Gets the attachment's binary data.
099             * @return the binary data or null if not set
100             */
101            public byte[] getData() {
102                    return data;
103            }
104    
105            /**
106             * Sets the attachment's binary data. If the attachment has a URI associated
107             * with it, the URI will be set to null.
108             * @param data the binary data
109             */
110            public void setData(byte[] data) {
111                    this.data = data;
112                    uri = null;
113            }
114    
115            /**
116             * Gets the attachment's URI.
117             * @return the URI (e.g. "http://example.com/image.png") or null if not set
118             */
119            public String getUri() {
120                    return uri;
121            }
122    
123            /**
124             * Sets the attachment's URI. If the attachment has binary data associated
125             * with it, the binary data will be set to null.
126             * @param uri the URI (e.g. "http://example.com/image.png")
127             */
128            public void setUri(String uri) {
129                    this.uri = uri;
130                    data = null;
131            }
132    
133            @Override
134            public String getFormatType() {
135                    return super.getFormatType();
136            }
137    
138            @Override
139            public void setFormatType(String formatType) {
140                    super.setFormatType(formatType);
141            }
142    
143            @Override
144            protected void validate(List<ICalComponent> components, List<String> warnings) {
145                    if (uri == null && data == null) {
146                            warnings.add("No URI or data specified.");
147                    }
148            }
149    }