View Javadoc
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.util.Map;
18  
19  import org.apache.commons.lang3.StringUtils;
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  import org.htmlunit.SgmlPage;
23  import org.htmlunit.javascript.host.html.HTMLObjectElement;
24  import org.htmlunit.xml.XmlPage;
25  
26  /**
27   * Wrapper for the HTML element "object".
28   *
29   * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
30   * @author David K. Taylor
31   * @author <a href="mailto:cse@dynabean.de">Christian Sell</a>
32   * @author Ahmed Ashour
33   * @author Ronald Brill
34   * @author Frank Danek
35   */
36  public class HtmlObject extends HtmlElement implements ValidatableElement {
37  
38      private static final Log LOG = LogFactory.getLog(HtmlObject.class);
39  
40      /** The HTML tag represented by this element. */
41      public static final String TAG_NAME = "object";
42  
43      private String customValidity_;
44  
45      /**
46       * Creates an instance of HtmlObject
47       *
48       * @param qualifiedName the qualified name of the element type to instantiate
49       * @param page the HtmlPage that contains this element
50       * @param attributes the initial attributes
51       */
52      HtmlObject(final String qualifiedName, final SgmlPage page,
53              final Map<String, DomAttr> attributes) {
54          super(qualifiedName, page, attributes);
55      }
56  
57      /**
58       * Returns the value of the attribute {@code declare}. Refer to the
59       * <a href="http://www.w3.org/TR/html401/">HTML 4.01</a>
60       * documentation for details on the use of this attribute.
61       *
62       * @return the value of the attribute {@code declare}
63       *         or an empty string if that attribute isn't defined.
64       */
65      public final String getDeclareAttribute() {
66          return getAttributeDirect("declare");
67      }
68  
69      /**
70       * Returns the value of the attribute {@code classid}. Refer to the
71       * <a href="http://www.w3.org/TR/html401/">HTML 4.01</a>
72       * documentation for details on the use of this attribute.
73       *
74       * @return the value of the attribute {@code classid}
75       *         or an empty string if that attribute isn't defined.
76       */
77      public final String getClassIdAttribute() {
78          return getAttributeDirect("classid");
79      }
80  
81      /**
82       * Returns the value of the attribute "codebase". Refer to the
83       * <a href="http://www.w3.org/TR/html401/">HTML 4.01</a>
84       * documentation for details on the use of this attribute.
85       *
86       * @return the value of the attribute "codebase"
87       *         or an empty string if that attribute isn't defined.
88       */
89      public final String getCodebaseAttribute() {
90          return getAttributeDirect("codebase");
91      }
92  
93      /**
94       * Returns the value of the attribute {@code data}. Refer to the
95       * <a href="http://www.w3.org/TR/html401/">HTML 4.01</a>
96       * documentation for details on the use of this attribute.
97       *
98       * @return the value of the attribute {@code data}
99       *         or an empty string if that attribute isn't defined.
100      */
101     public final String getDataAttribute() {
102         return getAttributeDirect("data");
103     }
104 
105     /**
106      * Returns the value of the attribute {@code type}. Refer to the
107      * <a href="http://www.w3.org/TR/html401/">HTML 4.01</a>
108      * documentation for details on the use of this attribute.
109      *
110      * @return the value of the attribute {@code type}
111      *         or an empty string if that attribute isn't defined.
112      */
113     public final String getTypeAttribute() {
114         return getAttributeDirect(TYPE_ATTRIBUTE);
115     }
116 
117     /**
118      * Returns the value of the attribute "codetype". Refer to the
119      * <a href="http://www.w3.org/TR/html401/">HTML 4.01</a>
120      * documentation for details on the use of this attribute.
121      *
122      * @return the value of the attribute "codetype"
123      *         or an empty string if that attribute isn't defined.
124      */
125     public final String getCodeTypeAttribute() {
126         return getAttributeDirect("codetype");
127     }
128 
129     /**
130      * Returns the value of the attribute {@code archive}. Refer to the
131      * <a href="http://www.w3.org/TR/html401/">HTML 4.01</a>
132      * documentation for details on the use of this attribute.
133      *
134      * @return the value of the attribute {@code archive}
135      *         or an empty string if that attribute isn't defined.
136      */
137     public final String getArchiveAttribute() {
138         return getAttributeDirect("archive");
139     }
140 
141     /**
142      * Returns the value of the attribute {@code standby}. Refer to the
143      * <a href="http://www.w3.org/TR/html401/">HTML 4.01</a>
144      * documentation for details on the use of this attribute.
145      *
146      * @return the value of the attribute {@code standby}
147      *         or an empty string if that attribute isn't defined.
148      */
149     public final String getStandbyAttribute() {
150         return getAttributeDirect("standby");
151     }
152 
153     /**
154      * Returns the value of the attribute {@code height}. Refer to the
155      * <a href="http://www.w3.org/TR/html401/">HTML 4.01</a>
156      * documentation for details on the use of this attribute.
157      *
158      * @return the value of the attribute {@code height}
159      *         or an empty string if that attribute isn't defined.
160      */
161     public final String getHeightAttribute() {
162         return getAttributeDirect("height");
163     }
164 
165     /**
166      * Returns the value of the attribute {@code width}. Refer to the
167      * <a href="http://www.w3.org/TR/html401/">HTML 4.01</a>
168      * documentation for details on the use of this attribute.
169      *
170      * @return the value of the attribute {@code width}
171      *         or an empty string if that attribute isn't defined.
172      */
173     public final String getWidthAttribute() {
174         return getAttributeDirect("width");
175     }
176 
177     /**
178      * Returns the value of the attribute {@code usemap}. Refer to the
179      * <a href="http://www.w3.org/TR/html401/">HTML 4.01</a>
180      * documentation for details on the use of this attribute.
181      *
182      * @return the value of the attribute {@code usemap}
183      *         or an empty string if that attribute isn't defined.
184      */
185     public final String getUseMapAttribute() {
186         return getAttributeDirect("usemap");
187     }
188 
189     /**
190      * Returns the value of the attribute {@code name}. Refer to the
191      * <a href="http://www.w3.org/TR/html401/">HTML 4.01</a>
192      * documentation for details on the use of this attribute.
193      *
194      * @return the value of the attribute {@code name}
195      *         or an empty string if that attribute isn't defined.
196      */
197     public final String getNameAttribute() {
198         return getAttributeDirect(NAME_ATTRIBUTE);
199     }
200 
201     /**
202      * Returns the value of the attribute {@code tabindex}. Refer to the
203      * <a href="http://www.w3.org/TR/html401/">HTML 4.01</a>
204      * documentation for details on the use of this attribute.
205      *
206      * @return the value of the attribute {@code tabindex}
207      *         or an empty string if that attribute isn't defined.
208      */
209     public final String getTabIndexAttribute() {
210         return getAttributeDirect("tabindex");
211     }
212 
213     /**
214      * Returns the value of the attribute {@code align}. Refer to the
215      * <a href="http://www.w3.org/TR/html401/">HTML 4.01</a>
216      * documentation for details on the use of this attribute.
217      *
218      * @return the value of the attribute {@code align}
219      *         or an empty string if that attribute isn't defined.
220      */
221     public final String getAlignAttribute() {
222         return getAttributeDirect("align");
223     }
224 
225     /**
226      * Returns the value of the attribute {@code border}. Refer to the
227      * <a href="http://www.w3.org/TR/html401/">HTML 4.01</a>
228      * documentation for details on the use of this attribute.
229      *
230      * @return the value of the attribute {@code border}
231      *         or an empty string if that attribute isn't defined.
232      */
233     public final String getBorderAttribute() {
234         return getAttributeDirect("border");
235     }
236 
237     /**
238      * Returns the value of the attribute {@code hspace}. Refer to the
239      * <a href="http://www.w3.org/TR/html401/">HTML 4.01</a>
240      * documentation for details on the use of this attribute.
241      *
242      * @return the value of the attribute {@code hspace}
243      *         or an empty string if that attribute isn't defined.
244      */
245     public final String getHspaceAttribute() {
246         return getAttributeDirect("hspace");
247     }
248 
249     /**
250      * Returns the value of the attribute {@code vspace}. Refer to the
251      * <a href="http://www.w3.org/TR/html401/">HTML 4.01</a>
252      * documentation for details on the use of this attribute.
253      *
254      * @return the value of the attribute {@code vspace}
255      *         or an empty string if that attribute isn't defined.
256      */
257     public final String getVspaceAttribute() {
258         return getAttributeDirect("vspace");
259     }
260 
261     /**
262      * Initialize the clsid.
263      * {@inheritDoc}
264      */
265     @Override
266     public void onAllChildrenAddedToPage(final boolean postponed) {
267         if (getOwnerDocument() instanceof XmlPage) {
268             return;
269         }
270 
271         if (LOG.isDebugEnabled()) {
272             LOG.debug("Object node added: " + asXml());
273         }
274 
275         final String clsId = getClassIdAttribute();
276         if (ATTRIBUTE_NOT_DEFINED != clsId && getPage().getWebClient().isJavaScriptEngineEnabled()) {
277             ((HTMLObjectElement) getScriptableObject()).setClassid(clsId);
278         }
279     }
280 
281     /**
282      * {@inheritDoc}
283      */
284     @Override
285     public DisplayStyle getDefaultStyleDisplay() {
286         return DisplayStyle.INLINE;
287     }
288 
289     /**
290      * {@inheritDoc}
291      */
292     @Override
293     public boolean isValid() {
294         return true;
295     }
296 
297     /**
298      * {@inheritDoc}
299      */
300     @Override
301     public boolean willValidate() {
302         return false;
303     }
304 
305     /**
306      * {@inheritDoc}
307      */
308     @Override
309     public void setCustomValidity(final String message) {
310         customValidity_ = message;
311     }
312 
313     @Override
314     public boolean isCustomErrorValidityState() {
315         return !StringUtils.isEmpty(customValidity_);
316     }
317 
318     @Override
319     public boolean isValidValidityState() {
320         return !isCustomErrorValidityState();
321     }
322 }