1 /*
2 * Copyright (c) 2002-2025 Gargoyle Software Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 * https://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 package org.htmlunit.html;
16
17 import java.net.MalformedURLException;
18 import java.util.Map;
19
20 import org.htmlunit.SgmlPage;
21 import org.htmlunit.util.StringUtils;
22
23 /**
24 * HTML Media element, e.g. {@link HtmlAudio} or {@link HtmlVideo}.
25 *
26 * @author Ahmed Ashour
27 * @author Frank Danek
28 * @author Ronald Brill
29 */
30 public class HtmlMedia extends HtmlElement {
31
32 /**
33 * Creates an instance.
34 *
35 * @param qualifiedName the qualified name of the element type to instantiate
36 * @param page the HtmlPage that contains this element
37 * @param attributes the initial attributes
38 */
39 HtmlMedia(final String qualifiedName, final SgmlPage page,
40 final Map<String, DomAttr> attributes) {
41 super(qualifiedName, page, attributes);
42 }
43
44 /**
45 * Determines whether the specified media type can be played back.
46 * @param type the type
47 * @return "probably", "maybe", or "". The current implementation returns ""
48 */
49 public String canPlayType(final String type) {
50 if (StringUtils.isBlank(type)) {
51 return "";
52 }
53
54 final int semPos = type.indexOf(';');
55 final int codecPos = type.indexOf("codec");
56
57 if (semPos > 0 && codecPos > semPos) {
58 return "probably";
59 }
60
61 return "maybe";
62 }
63
64 /**
65 * Returns the value of the attribute {@code src}. Refer to the
66 * <a href="http://www.w3.org/TR/html401/">HTML 4.01</a>
67 * documentation for details on the use of this attribute.
68 *
69 * @return the value of the attribute {@code src} or an empty string if that attribute isn't defined
70 */
71 public final String getSrcAttribute() {
72 return getSrcAttributeNormalized();
73 }
74
75 /**
76 * @return the value of the {@code src} value
77 */
78 public String getSrc() {
79 final String src = getSrcAttribute();
80 if (StringUtils.isEmptyString(src)) {
81 return src;
82 }
83 try {
84 final HtmlPage page = (HtmlPage) getPage();
85 return page.getFullyQualifiedUrl(src).toExternalForm();
86 }
87 catch (final MalformedURLException e) {
88 final String msg = "Unable to create fully qualified URL for src attribute of media " + e.getMessage();
89 throw new RuntimeException(msg, e);
90 }
91 }
92
93 /**
94 * Sets the value of the {@code src} attribute.
95 * @param src the value of the {@code src} attribute
96 */
97 public void setSrc(final String src) {
98 setAttribute(SRC_ATTRIBUTE, src);
99 }
100
101 /**
102 * Returns the absolute URL of the chosen media resource.
103 * This could happen, for example, if the web server selects a
104 * media file based on the resolution of the user's display.
105 * The value is an empty string if the networkState property is EMPTY.
106 * @return the absolute URL of the chosen media resource
107 */
108 public String getCurrentSrc() {
109 return "";
110 }
111 }