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.htmlunit.SgmlPage;
20  import org.xml.sax.Attributes;
21  
22  /**
23   * A factory for elements encountered in parsing the input which are not represented
24   * by dedicated element classes.
25   *
26   * @author <a href="mailto:cse@dynabean.de">Christian Sell</a>
27   * @author Ahmed Ashour
28   * @author David K. Taylor
29   * @author Ronald Brill
30   * @author Frank Danek
31   */
32  public final class UnknownElementFactory implements ElementFactory {
33  
34      /** The singleton instance. */
35      public static final UnknownElementFactory INSTANCE = new UnknownElementFactory();
36  
37      /** Private singleton constructor. */
38      private UnknownElementFactory() {
39          // util class
40      }
41  
42      /**
43       * {@inheritDoc}
44       */
45      @Override
46      public HtmlElement createElement(final SgmlPage page, final String tagName, final Attributes attributes) {
47          if (page != null && page.isHtmlPage() && tagName.indexOf(':') != -1) {
48              final HtmlPage htmlPage = (HtmlPage) page;
49              final String prefix = tagName.substring(0, tagName.indexOf(':'));
50              final String namespace = htmlPage.getNamespaces().get(prefix);
51              return createElementNS(page, namespace, tagName, attributes);
52          }
53  
54          return createElementNS(page, null, tagName, attributes);
55      }
56  
57      /**
58       * {@inheritDoc}
59       */
60      @Override
61      public HtmlElement createElementNS(final SgmlPage page, final String namespaceURI,
62              final String qualifiedName, final Attributes attributes) {
63          final Map<String, DomAttr> attributeMap = DefaultElementFactory.toMap(page, attributes);
64          return new HtmlUnknownElement(page, qualifiedName, attributeMap);
65      }
66  }