001 package ezvcard.property;
002
003 import java.io.File;
004 import java.io.IOException;
005 import java.io.InputStream;
006
007 import ezvcard.parameter.ImageType;
008
009 /*
010 Copyright (c) 2013, Michael Angstadt
011 All rights reserved.
012
013 Redistribution and use in source and binary forms, with or without
014 modification, are permitted provided that the following conditions are met:
015
016 1. Redistributions of source code must retain the above copyright notice, this
017 list of conditions and the following disclaimer.
018 2. Redistributions in binary form must reproduce the above copyright notice,
019 this list of conditions and the following disclaimer in the documentation
020 and/or other materials provided with the distribution.
021
022 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
023 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
024 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
025 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
026 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
027 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
028 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
029 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
030 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
031 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
032
033 The views and conclusions contained in the software and documentation are those
034 of the authors and should not be interpreted as representing official policies,
035 either expressed or implied, of the FreeBSD Project.
036 */
037
038 /**
039 * A company logo.
040 *
041 * <p>
042 * <b>Adding a logo</b>
043 * </p>
044 *
045 * <pre class="brush:java">
046 * VCard vcard = new VCard();
047 *
048 * //URL
049 * Logo logo = new Logo("http://www.company.com/logo.png", ImageType.PNG);
050 * vcard.addLogo(logo);
051 *
052 * //binary data
053 * byte data[] = ...
054 * logo = new Logo(data, ImageType.PNG);
055 * vcard.addLogo(logo);
056 *
057 * //if "ImageType" does not have the pre-defined constant that you need, then create a new instance
058 * //arg 1: the value of the 2.1/3.0 TYPE parameter
059 * //arg 2: the value to use for the 4.0 MEDIATYPE parameter and for 4.0 data URIs
060 * //arg 3: the file extension of the data type (optional)
061 * ImageKeyTypeParameter param = new ImageType("bmp", "image/x-ms-bmp", "bmp");
062 * logo = new Logo("http://www.company.com/logo.bmp", param);
063 * vcard.addLogo(logo);
064 * </pre>
065 *
066 * <p>
067 * <b>Getting the logos</b>
068 * </p>
069 *
070 * <pre class="brush:java">
071 * VCard vcard = ...
072 *
073 * int fileCount = 0;
074 * for (Logo logo : vcard.getLogos()){
075 * //the logo will have either a URL or a binary data
076 * if (logo.getData() == null){
077 * System.out.println("Logo URL: " + logo.getUrl());
078 * } else {
079 * ImageType type = logo.getContentType();
080 *
081 * if (type == null) {
082 * //the vCard may not have any content type data associated with the logo
083 * System.out.println("Saving a logo file...");
084 * } else {
085 * System.out.println("Saving a \"" + type.getMediaType() + "\" file...");
086 * }
087 *
088 * String folder;
089 * if (type == ImageType.PNG){ //it is safe to use "==" instead of "equals()"
090 * folder = "png-files";
091 * } else {
092 * folder = "image-files";
093 * }
094 *
095 * byte data[] = logo.getData();
096 * String filename = "logo" + fileCount;
097 * if (type != null && type.getExtension() != null){
098 * filename += "." + type.getExtension();
099 * }
100 * OutputStream out = new FileOutputStream(new File(folder, filename));
101 * out.write(data);
102 * out.close();
103 * fileCount++;
104 * }
105 * }
106 * </pre>
107 *
108 * <p>
109 * <b>Property name:</b> {@code LOGO}
110 * </p>
111 * <p>
112 * <b>Supported versions:</b> {@code 2.1, 3.0, 4.0}
113 * </p>
114 * @author Michael Angstadt
115 */
116 public class Logo extends ImageProperty {
117 /**
118 * Creates a logo property.
119 * @param url the URL to the logo
120 * @param type the content type (e.g. PNG)
121 */
122 public Logo(String url, ImageType type) {
123 super(url, type);
124 }
125
126 /**
127 * Creates a logo property.
128 * @param data the binary data of the logo
129 * @param type the content type (e.g. PNG)
130 */
131 public Logo(byte[] data, ImageType type) {
132 super(data, type);
133 }
134
135 /**
136 * Creates a logo property.
137 * @param in an input stream to the binary data (will be closed)
138 * @param type the content type (e.g. PNG)
139 * @throws IOException if there's a problem reading from the input stream
140 */
141 public Logo(InputStream in, ImageType type) throws IOException {
142 super(in, type);
143 }
144
145 /**
146 * Creates a logo property.
147 * @param file the image file
148 * @param type the content type (e.g. PNG)
149 * @throws IOException if there's a problem reading from the file
150 */
151 public Logo(File file, ImageType type) throws IOException {
152 super(file, type);
153 }
154
155 @Override
156 public String getLanguage() {
157 return super.getLanguage();
158 }
159
160 @Override
161 public void setLanguage(String language) {
162 super.setLanguage(language);
163 }
164 }