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.SoundTypeParameter;
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 sound to attach to the vCard, such as a pronunciation of the person's name.
045 *
046 * <p>
047 * <b>Adding a sound</b>
048 * </p>
049 *
050 * <pre>
051 * VCard vcard = new VCard();
052 *
053 * //URL
054 * SoundType sound = new SoundType("http://www.mywebsite.com/myname.ogg", SoundTypeParameter.OGG);
055 * vcard.addSound(sound);
056 *
057 * //binary data
058 * byte data[] = ...
059 * sound = new SoundType(data, SoundTypeParameter.OGG);
060 * vcard.addSound(sound);
061 *
062 * //if "SoundTypeParameter" 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 * SoundTypeParameter param = new SoundTypeParameter("wav", "audio/wav", "wav");
067 * sound = new SoundType("http://www.mywebsite.com/myname.wav", SoundTypeParameter.WAV);
068 * vcard.addSound(sound);
069 * </pre>
070 *
071 * <p>
072 * <b>Getting the sounds</b>
073 * </p>
074 *
075 * <pre>
076 * VCard vcard = ...
077 *
078 * int fileCount = 0;
079 * for (SoundType sound : vcard.getSounds()){
080 * //the sound will have either a URL or a binary data
081 * if (sound.getData() == null){
082 * System.out.println("Sound URL: " + sound.getUrl());
083 * } else {
084 * SoundTypeParameter type = sound.getContentType();
085 *
086 * if (type == null) {
087 * //the vCard may not have any content type data associated with the sound
088 * System.out.println("Saving a sound file...");
089 * } else {
090 * System.out.println("Saving a \"" + type.getMediaType() + "\" file...");
091 * }
092 *
093 * String folder;
094 * if (type == SoundTypeParameter.OGG){ //it is safe to use "==" instead of "equals()"
095 * folder = "ogg-files";
096 * } else {
097 * folder = "sound-files";
098 * }
099 *
100 * byte data[] = sound.getData();
101 * String filename = "sound" + 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: SOUND
115 * </p>
116 * <p>
117 * vCard versions: 2.1, 3.0, 4.0
118 * </p>
119 * @author Michael Angstadt
120 */
121 public class SoundType extends BinaryType<SoundTypeParameter> {
122 public static final String NAME = "SOUND";
123
124 public SoundType() {
125 super(NAME);
126 }
127
128 /**
129 * @param url the URL to the sound file
130 * @param type the content type (e.g. OGG)
131 */
132 public SoundType(String url, SoundTypeParameter type) {
133 super(NAME, url, type);
134 }
135
136 /**
137 * @param data the binary data of the sound file
138 * @param type the content type (e.g. OGG)
139 */
140 public SoundType(byte[] data, SoundTypeParameter 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. OGG)
147 * @throws IOException if there's a problem reading from the input stream
148 */
149 public SoundType(InputStream in, SoundTypeParameter type) throws IOException {
150 super(NAME, in, type);
151 }
152
153 /**
154 * @param file the sound file
155 * @param type the content type (e.g. OGG)
156 * @throws IOException if there's a problem reading from the file
157 */
158 public SoundType(File file, SoundTypeParameter type) throws IOException {
159 super(NAME, file, type);
160 }
161
162 /**
163 * Gets the language.
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.
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 SoundTypeParameter buildTypeObj(String type) {
182 SoundTypeParameter param = SoundTypeParameter.valueOf(type);
183 if (param == null) {
184 param = new SoundTypeParameter(type, "audio/" + type, null);
185 }
186 return param;
187 }
188
189 @Override
190 protected SoundTypeParameter buildMediaTypeObj(String mediaType) {
191 SoundTypeParameter p = SoundTypeParameter.findByMediaType(mediaType);
192 if (p == null) {
193 int slashPos = mediaType.indexOf('/');
194 String type;
195 if (slashPos == -1 || slashPos < mediaType.length() - 1) {
196 type = "";
197 } else {
198 type = mediaType.substring(slashPos + 1);
199 }
200 p = new SoundTypeParameter(type, mediaType, null);
201 }
202 return p;
203 }
204
205 @Override
206 protected void doUnmarshalHtml(HCardElement element, List<String> warnings) {
207 String elementName = element.tagName();
208 if ("audio".equals(elementName)) {
209 org.jsoup.nodes.Element sourceElement = element.getElement().getElementsByTag("source").first();
210 if (sourceElement == null) {
211 throw new SkipMeException("No <source> element found beneath <audio> element.");
212 }
213 }
214 if ("source".equals(elementName)) {
215 SoundTypeParameter mediaType = null;
216 String type = element.attr("type");
217 if (type.length() > 0) {
218 mediaType = buildMediaTypeObj(type);
219 }
220
221 String src = element.absUrl("src");
222 if (src.length() > 0) {
223 try {
224 DataUri uri = new DataUri(src);
225 mediaType = buildMediaTypeObj(uri.getContentType());
226 setData(uri.getData(), mediaType);
227 } catch (IllegalArgumentException e) {
228 //TODO create buildTypeObjFromExtension() method
229 setUrl(src, null);
230 }
231 } else {
232 throw new SkipMeException("<source> tag does not have a \"src\" attribute.");
233 }
234 } else {
235 super.doUnmarshalHtml(element, warnings);
236 }
237 }
238 }