Debian Codegraph @

src/main/java/ezvcard/io/scribe/SoundScribe.java

raw symbols SCIP
1 package ezvcard.io.scribe;
2
3 import ezvcard.io.CannotParseException;
4 import ezvcard.io.ParseContext;
5 import ezvcard.io.html.HCardElement;
6 import ezvcard.parameter.SoundType;
7 import ezvcard.property.Sound;
8 import ezvcard.util.DataUri;
9
10 /*
11 Copyright (c) 2012-2023, Michael Angstadt
12 All rights reserved.
13
14 Redistribution and use in source and binary forms, with or without
15 modification, are permitted provided that the following conditions are met:
16
17 1. Redistributions of source code must retain the above copyright notice, this
18 list of conditions and the following disclaimer.
19 2. Redistributions in binary form must reproduce the above copyright notice,
20 this list of conditions and the following disclaimer in the documentation
21 and/or other materials provided with the distribution.
22
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
27 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35 /**
36 * Marshals {@link Sound} properties.
37 * @author Michael Angstadt
38 */
39 public class SoundScribe extends BinaryPropertyScribe<Sound, SoundType> {
40 public SoundScribe() {
41 super(Sound.class, "SOUND");
42 }
43
44 @Override
45 protected SoundType _mediaTypeFromTypeParameter(String type) {
46 return SoundType.get(type, null, null);
47 }
48
49 @Override
50 protected SoundType _mediaTypeFromMediaTypeParameter(String mediaType) {
51 return SoundType.get(null, mediaType, null);
52 }
53
54 @Override
55 protected SoundType _mediaTypeFromFileExtension(String extension) {
56 return SoundType.find(null, null, extension);
57 }
58
59 @Override
60 protected Sound _newInstance(String uri, SoundType contentType) {
61 return new Sound(uri, contentType);
62 }
63
64 @Override
65 protected Sound _newInstance(byte[] data, SoundType contentType) {
66 return new Sound(data, contentType);
67 }
68
69 @Override
70 protected Sound _parseHtml(HCardElement element, ParseContext context) {
71 String elementName = element.tagName();
72 if (!"audio".equals(elementName) && !"source".equals(elementName)) {
73 return super._parseHtml(element, context);
74 }
75
76 if ("audio".equals(elementName)) {
77 //parse its child "<source>" element
78 org.jsoup.nodes.Element source = element.getElement().getElementsByTag("source").first();
79 if (source == null) {
80 throw new CannotParseException(16);
81 }
82
83 element = new HCardElement(source);
84 }
85
86 String src = element.absUrl("src");
87 if (src.isEmpty()) {
88 throw new CannotParseException(17);
89 }
90
91 String type = element.attr("type");
92 SoundType mediaType = type.isEmpty() ? null : _mediaTypeFromMediaTypeParameter(type);
93
94 try {
95 DataUri uri = DataUri.parse(src);
96 mediaType = _mediaTypeFromMediaTypeParameter(uri.getContentType());
97 return new Sound(uri.getData(), mediaType);
98 } catch (IllegalArgumentException e) {
99 //not a data URI
100 if (mediaType == null) {
101 String extension = getFileExtension(src);
102 mediaType = (extension == null) ? null : _mediaTypeFromFileExtension(extension);
103 }
104 return new Sound(src, mediaType);
105 }
106 }
107 }