001 package ezvcard.types;
002
003 import java.io.File;
004 import java.io.IOException;
005 import java.io.InputStream;
006 import java.util.List;
007
008 import ezvcard.VCardSubTypes;
009 import ezvcard.io.SkipMeException;
010 import ezvcard.parameters.ImageTypeParameter;
011 import ezvcard.util.DataUri;
012 import ezvcard.util.HCardElement;
013
014 /*
015 Copyright (c) 2012, Michael Angstadt
016 All rights reserved.
017
018 Redistribution and use in source and binary forms, with or without
019 modification, are permitted provided that the following conditions are met:
020
021 1. Redistributions of source code must retain the above copyright notice, this
022 list of conditions and the following disclaimer.
023 2. Redistributions in binary form must reproduce the above copyright notice,
024 this list of conditions and the following disclaimer in the documentation
025 and/or other materials provided with the distribution.
026
027 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
028 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
029 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
030 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
031 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
032 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
033 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
034 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
035 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
036 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
037
038 The views and conclusions contained in the software and documentation are those
039 of the authors and should not be interpreted as representing official policies,
040 either expressed or implied, of the FreeBSD Project.
041 */
042
043 /**
044 * A company logo.
045 *
046 * <p>
047 * <b>Adding a logo</b>
048 * </p>
049 *
050 * <pre>
051 * VCard vcard = new VCard();
052 *
053 * //URL
054 * LogoType logo = new LogoType("http://www.company.com/logo.png", ImageTypeParameter.PNG);
055 * vcard.addLogo(logo);
056 *
057 * //binary data
058 * byte data[] = ...
059 * logo = new LogoType(data, ImageTypeParameter.PNG);
060 * vcard.addLogo(logo);
061 *
062 * //if "ImageTypeParameter" does not have the pre-defined constant that you need, then create a new instance
063 * //arg 1: the value of the 2.1/3.0 TYPE parameter
064 * //arg 2: the value to use for the 4.0 MEDIATYPE parameter and for 4.0 data URIs
065 * //arg 3: the file extension of the data type (optional)
066 * ImageKeyTypeParameter param = new ImageTypeParameter("bmp", "image/x-ms-bmp", "bmp");
067 * logo = new LogoType("http://www.company.com/logo.bmp", param);
068 * vcard.addLogo(logo);
069 * </pre>
070 *
071 * <p>
072 * <b>Getting the logos</b>
073 * </p>
074 *
075 * <pre>
076 * VCard vcard = ...
077 *
078 * int fileCount = 0;
079 * for (LogoType logo : vcard.getLogos()){
080 * //the logo will have either a URL or a binary data
081 * if (logo.getData() == null){
082 * System.out.println("Logo URL: " + logo.getUrl());
083 * } else {
084 * ImageTypeParameter type = logo.getContentType();
085 *
086 * if (type == null) {
087 * //the vCard may not have any content type data associated with the logo
088 * System.out.println("Saving a logo file...");
089 * } else {
090 * System.out.println("Saving a \"" + type.getMediaType() + "\" file...");
091 * }
092 *
093 * String folder;
094 * if (type == ImageTypeParameter.PNG){ //it is safe to use "==" instead of "equals()"
095 * folder = "png-files";
096 * } else {
097 * folder = "image-files";
098 * }
099 *
100 * byte data[] = logo.getData();
101 * String filename = "logo" + fileCount;
102 * if (type != null && type.getExtension() != null){
103 * filename += "." + type.getExtension();
104 * }
105 * OutputStream out = new FileOutputStream(new File(folder, filename));
106 * out.write(data);
107 * out.close();
108 * fileCount++;
109 * }
110 * }
111 * </pre>
112 *
113 * <p>
114 * vCard property name: LOGO
115 * </p>
116 * <p>
117 * vCard versions: 2.1, 3.0, 4.0
118 * </p>
119 * @author Michael Angstadt
120 */
121 public class LogoType extends BinaryType<ImageTypeParameter> {
122 public static final String NAME = "LOGO";
123
124 public LogoType() {
125 super(NAME);
126 }
127
128 /**
129 * @param url the URL to the logo
130 * @param type the content type (e.g. PNG)
131 */
132 public LogoType(String url, ImageTypeParameter type) {
133 super(NAME, url, type);
134 }
135
136 /**
137 * @param data the binary data of the logo
138 * @param type the content type (e.g. PNG)
139 */
140 public LogoType(byte[] data, ImageTypeParameter type) {
141 super(NAME, data, type);
142 }
143
144 /**
145 * @param in an input stream to the binary data (will be closed)
146 * @param type the content type (e.g. PNG)
147 * @throws IOException if there's a problem reading from the input stream
148 */
149 public LogoType(InputStream in, ImageTypeParameter type) throws IOException {
150 super(NAME, in, type);
151 }
152
153 /**
154 * @param file the image file
155 * @param type the content type (e.g. PNG)
156 * @throws IOException if there's a problem reading from the file
157 */
158 public LogoType(File file, ImageTypeParameter type) throws IOException {
159 super(NAME, file, type);
160 }
161
162 /**
163 * Gets the language that the address is written in.
164 * @return the language or null if not set
165 * @see VCardSubTypes#getLanguage
166 */
167 public String getLanguage() {
168 return subTypes.getLanguage();
169 }
170
171 /**
172 * Sets the language that the address is written in.
173 * @param language the language or null to remove
174 * @see VCardSubTypes#setLanguage
175 */
176 public void setLanguage(String language) {
177 subTypes.setLanguage(language);
178 }
179
180 @Override
181 protected void doUnmarshalHtml(HCardElement element, List<String> warnings) {
182 String elementName = element.tagName();
183 if ("img".equals(elementName)) {
184 String src = element.absUrl("src");
185 if (src.length() > 0) {
186 try {
187 DataUri uri = new DataUri(src);
188 ImageTypeParameter mediaType = buildMediaTypeObj(uri.getContentType());
189 setData(uri.getData(), mediaType);
190 } catch (IllegalArgumentException e) {
191 //TODO create buildTypeObjFromExtension() method
192 setUrl(src, null);
193 }
194 } else {
195 throw new SkipMeException("<img> tag does not have a \"src\" attribute.");
196 }
197 } else {
198 super.doUnmarshalHtml(element, warnings);
199 }
200 }
201
202 @Override
203 protected ImageTypeParameter buildTypeObj(String type) {
204 ImageTypeParameter param = ImageTypeParameter.valueOf(type);
205 if (param == null) {
206 param = new ImageTypeParameter(type, "image/" + type, null);
207 }
208 return param;
209 }
210
211 @Override
212 protected ImageTypeParameter buildMediaTypeObj(String mediaType) {
213 ImageTypeParameter p = ImageTypeParameter.findByMediaType(mediaType);
214 if (p == null) {
215 int slashPos = mediaType.indexOf('/');
216 String type;
217 if (slashPos == -1 || slashPos < mediaType.length() - 1) {
218 type = "";
219 } else {
220 type = mediaType.substring(slashPos + 1);
221 }
222 p = new ImageTypeParameter(type, mediaType, null);
223 }
224 return p;
225 }
226 }