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       * @return the value of the {@code src} value
57       */
58      public String getSrc() {
59          final String src = getSrcAttribute();
60          if (StringUtils.isEmptyString(src)) {
61              return src;
62          }
63          try {
64              final HtmlPage page = (HtmlPage) getPage();
65              return page.getFullyQualifiedUrl(src).toExternalForm();
66          }
67          catch (final MalformedURLException e) {
68              final String msg = "Unable to create fully qualified URL for src attribute of media " + e.getMessage();
69              throw new RuntimeException(msg, e);
70          }
71      }
72  
73      /**
74       * Sets the value of the {@code src} attribute.
75       * @param src the value of the {@code src} attribute
76       */
77      public void setSrc(final String src) {
78          setAttribute(SRC_ATTRIBUTE, src);
79      }
80  
81      /**
82       * Returns the absolute URL of the chosen media resource.
83       * This could happen, for example, if the web server selects a
84       * media file based on the resolution of the user's display.
85       * The value is an empty string if the networkState property is EMPTY.
86       * @return the absolute URL of the chosen media resource
87       */
88      public String getCurrentSrc() {
89          return "";
90      }
91  }