001 package ezvcard.property;
002
003 import java.io.File;
004 import java.io.FileInputStream;
005 import java.io.IOException;
006 import java.io.InputStream;
007 import java.util.List;
008
009 import ezvcard.VCard;
010 import ezvcard.VCardVersion;
011 import ezvcard.Warning;
012 import ezvcard.parameter.MediaTypeParameter;
013 import ezvcard.util.IOUtils;
014
015 /*
016 Copyright (c) 2013, Michael Angstadt
017 All rights reserved.
018
019 Redistribution and use in source and binary forms, with or without
020 modification, are permitted provided that the following conditions are met:
021
022 1. Redistributions of source code must retain the above copyright notice, this
023 list of conditions and the following disclaimer.
024 2. Redistributions in binary form must reproduce the above copyright notice,
025 this list of conditions and the following disclaimer in the documentation
026 and/or other materials provided with the distribution.
027
028 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
029 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
030 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
031 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
032 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
033 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
034 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
035 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
036 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
037 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
038
039 The views and conclusions contained in the software and documentation are those
040 of the authors and should not be interpreted as representing official policies,
041 either expressed or implied, of the FreeBSD Project.
042 */
043
044 /**
045 * Represents a property whose value contains binary data (for example,
046 * {@link Photo}).
047 * @author Michael Angstadt
048 *
049 * @param <T> the class used for representing the content type of the resource
050 */
051 public abstract class BinaryProperty<T extends MediaTypeParameter> extends VCardProperty implements HasAltId {
052 /**
053 * The decoded data.
054 */
055 protected byte[] data;
056
057 /**
058 * The URL to the resource.
059 */
060 protected String url;
061
062 /**
063 * The content type of the resource (for example, a JPEG image).
064 */
065 protected T contentType;
066
067 public BinaryProperty() {
068 //empty
069 }
070
071 /**
072 * Creates a binary property.
073 * @param url the URL to the resource
074 * @param type the content type
075 */
076 public BinaryProperty(String url, T type) {
077 setUrl(url, type);
078 }
079
080 /**
081 * Creates a binary property.
082 * @param data the binary data
083 * @param type the content type
084 */
085 public BinaryProperty(byte[] data, T type) {
086 setData(data, type);
087 }
088
089 /**
090 * Creates a binary property.
091 * @param in an input stream to the binary data (will be closed)
092 * @param type the content type
093 * @throws IOException if there is a problem reading from the input stream
094 */
095 public BinaryProperty(InputStream in, T type) throws IOException {
096 this(IOUtils.toByteArray(in, true), type);
097 }
098
099 /**
100 * Creates a binary property.
101 * @param file the file containing the binary data
102 * @param type the content type
103 * @throws IOException if there is a problem reading from the file
104 */
105 public BinaryProperty(File file, T type) throws IOException {
106 this(new FileInputStream(file), type);
107 }
108
109 /**
110 * Gets the binary data of the resource.
111 * @return the binary data or null if there is none
112 */
113 public byte[] getData() {
114 return data;
115 }
116
117 /**
118 * Sets the binary data of the resource.
119 * @param data the binary data
120 * @param type the content type (e.g. "JPEG image")
121 */
122 public void setData(byte[] data, T type) {
123 this.url = null;
124 this.data = data;
125 setContentType(type);
126 }
127
128 /**
129 * Gets the URL to the resource
130 * @return the URL or null if there is none
131 */
132 public String getUrl() {
133 return url;
134 }
135
136 /**
137 * Sets the URL to the resource.
138 * @param url the URL
139 * @param type the content type (e.g. "JPEG image")
140 */
141 public void setUrl(String url, T type) {
142 this.url = url;
143 this.data = null;
144 setContentType(type);
145 }
146
147 /**
148 * Gets the content type of the resource.
149 * @return the content type (e.g. "JPEG image")
150 */
151 public T getContentType() {
152 return contentType;
153 }
154
155 /**
156 * Sets the content type of the resource.
157 * @param contentType the content type (e.g. "JPEG image")
158 */
159 public void setContentType(T contentType) {
160 this.contentType = contentType;
161 }
162
163 /**
164 * Gets the vCard 4.0 TYPE parameter. This should NOT be used to get the
165 * TYPE parameter for 2.1/3.0 vCards. Use {@link #getContentType} instead.
166 * <p>
167 * <b>Supported versions:</b> {@code 4.0}
168 * </p>
169 * @return the TYPE value (typically, this will be either "work" or "home")
170 * or null if it doesn't exist
171 */
172 public String getType() {
173 return parameters.getType();
174 }
175
176 /**
177 * Sets the vCard 4.0 TYPE parameter. This should NOT be used to set the
178 * TYPE parameter for 2.1/3.0 vCards. Use {@link #setContentType} instead.
179 * <p>
180 * <b>Supported versions:</b> {@code 4.0}
181 * </p>
182 * @param type the TYPE value (should be either "work" or "home") or null to
183 * remove
184 */
185 public void setType(String type) {
186 parameters.setType(type);
187 }
188
189 @Override
190 public List<Integer[]> getPids() {
191 return super.getPids();
192 }
193
194 @Override
195 public void addPid(int localId, int clientPidMapRef) {
196 super.addPid(localId, clientPidMapRef);
197 }
198
199 @Override
200 public void removePids() {
201 super.removePids();
202 }
203
204 @Override
205 public Integer getPref() {
206 return super.getPref();
207 }
208
209 @Override
210 public void setPref(Integer pref) {
211 super.setPref(pref);
212 }
213
214 //@Override
215 public String getAltId() {
216 return parameters.getAltId();
217 }
218
219 //@Override
220 public void setAltId(String altId) {
221 parameters.setAltId(altId);
222 }
223
224 @Override
225 protected void _validate(List<Warning> warnings, VCardVersion version, VCard vcard) {
226 if (url == null && data == null) {
227 warnings.add(new Warning(8));
228 }
229 }
230 }