View Javadoc
1   /*
2    * Copyright (c) 2002-2026 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 abstract 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       * Returns the value of the attribute {@code src}. Refer to the
46       * <a href="http://www.w3.org/TR/html401/">HTML 4.01</a>
47       * documentation for details on the use of this attribute.
48       *
49       * @return the value of the attribute {@code src} or an empty string if that attribute isn't defined
50       */
51      public final String getSrcAttribute() {
52          return getSrcAttributeNormalized();
53      }
54  
55      /**
56       * Returns the fully qualified source URL.
57       *
58       * @return the fully qualified value of the {@code src} attribute
59       */
60      public String getSrc() {
61          final String src = getSrcAttribute();
62          if (StringUtils.isEmptyString(src)) {
63              return src;
64          }
65          try {
66              final HtmlPage page = (HtmlPage) getPage();
67              return page.getFullyQualifiedUrl(src).toExternalForm();
68          }
69          catch (final MalformedURLException e) {
70              final String msg = "Unable to create fully qualified URL for src attribute of media " + e.getMessage();
71              throw new RuntimeException(msg, e);
72          }
73      }
74  
75      /**
76       * Sets the value of the {@code src} attribute.
77       * @param src the value of the {@code src} attribute
78       */
79      public void setSrc(final String src) {
80          setAttribute(SRC_ATTRIBUTE, src);
81      }
82  
83      /**
84       * Returns the absolute URL of the chosen media resource.
85       * This could happen, for example, if the web server selects a
86       * media file based on the resolution of the user's display.
87       * The value is an empty string if the networkState property is EMPTY.
88       * @return the absolute URL of the chosen media resource
89       */
90      public String getCurrentSrc() {
91          return "";
92      }
93  }