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.io.File;
18  import java.io.IOException;
19  import java.io.InputStream;
20  import java.io.OutputStream;
21  import java.net.URL;
22  import java.nio.file.Files;
23  import java.util.Map;
24  
25  import org.apache.commons.io.IOUtils;
26  import org.htmlunit.SgmlPage;
27  import org.htmlunit.WebClient;
28  import org.htmlunit.WebRequest;
29  import org.htmlunit.WebResponse;
30  
31  /**
32   * Wrapper for the HTML element "embed".
33   *
34   * @author Ahmed Ashour
35   * @author Ronald Brill
36   * @author Frank Danek
37   */
38  public class HtmlEmbed extends HtmlElement {
39  
40      /** The HTML tag represented by this element. */
41      public static final String TAG_NAME = "embed";
42  
43      /**
44       * Creates a new instance.
45       *
46       * @param qualifiedName the qualified name of the element type to instantiate
47       * @param page the page that contains this element
48       * @param attributes the initial attributes
49       */
50      HtmlEmbed(final String qualifiedName, final SgmlPage page,
51              final Map<String, DomAttr> attributes) {
52          super(qualifiedName, page, attributes);
53      }
54  
55      /**
56       * Saves this content as the specified file.
57       * @param file the file to save to
58       * @throws IOException if an IO error occurs
59       */
60      public void saveAs(final File file) throws IOException {
61          final HtmlPage page = (HtmlPage) getPage();
62          final WebClient webclient = page.getWebClient();
63  
64          final URL url = page.getFullyQualifiedUrl(getAttributeDirect(SRC_ATTRIBUTE));
65          final WebRequest request = new WebRequest(url, page.getCharset(), page.getUrl());
66          final WebResponse webResponse = webclient.loadWebResponse(request);
67  
68          try (OutputStream fos = Files.newOutputStream(file.toPath());
69                  InputStream content =  webResponse.getContentAsStream()) {
70              IOUtils.copy(content, fos);
71          }
72      }
73  
74      /**
75       * {@inheritDoc}
76       */
77      @Override
78      public DisplayStyle getDefaultStyleDisplay() {
79          return DisplayStyle.INLINE;
80      }
81  }