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.platform.util;
16  
17  import java.util.ArrayList;
18  import java.util.HashMap;
19  import java.util.List;
20  import java.util.Map;
21  import java.util.Objects;
22  
23  import org.apache.xerces.dom.DeferredDocumentImpl;
24  import org.apache.xerces.dom.DeferredNode;
25  import org.htmlunit.platform.XmlUtilsHelperAPI;
26  import org.w3c.dom.Document;
27  import org.w3c.dom.NamedNodeMap;
28  import org.w3c.dom.Node;
29  
30  /**
31   * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
32   *
33   * Special processing if the Xerces parser is in use.
34   *
35   * @author Ronald Brill
36   */
37  public final class XmlUtilsXercesHelper implements XmlUtilsHelperAPI {
38  
39      // private static final Log LOG = LogFactory.getLog(XmlUtilsXerces.class);
40  
41      /**
42       * Ctor.
43       */
44      public XmlUtilsXercesHelper() {
45          // Force eager loading of classes in order to flush out any linkage errors early
46          Objects.hash(DeferredDocumentImpl.class, DeferredNode.class);
47      }
48  
49      /**
50       * {@inheritDoc}
51       */
52      @Override
53      public Map<Integer, List<String>> getAttributesOrderMap(final Document document) {
54          if (document instanceof DeferredDocumentImpl) {
55              final Map<Integer, List<String>> map = new HashMap<>();
56              final DeferredDocumentImpl deferredDocument = (DeferredDocumentImpl) document;
57              final int fNodeCount = getPrivate(deferredDocument, "fNodeCount");
58              for (int i = 0; i < fNodeCount; i++) {
59                  final int type = deferredDocument.getNodeType(i, false);
60                  if (type == Node.ELEMENT_NODE) {
61                      int attrIndex = deferredDocument.getNodeExtra(i, false);
62                      final List<String> attributes = new ArrayList<>();
63                      map.put(i, attributes);
64                      while (attrIndex != -1) {
65                          attributes.add(deferredDocument.getNodeName(attrIndex, false));
66                          attrIndex = deferredDocument.getPrevSibling(attrIndex, false);
67                      }
68                  }
69              }
70              return map;
71          }
72          return null;
73      }
74  
75      /**
76       * {@inheritDoc}
77       */
78      @Override
79      public int getIndex(final NamedNodeMap namedNodeMap, final Map<Integer, List<String>> attributesOrderMap,
80              final Node element, final int requiredIndex) {
81          if (attributesOrderMap != null && element instanceof DeferredNode) {
82              final int elementIndex = ((DeferredNode) element).getNodeIndex();
83              final List<String> attributesOrderList = attributesOrderMap.get(elementIndex);
84              if (attributesOrderList != null) {
85                  final String attributeName = attributesOrderList.get(requiredIndex);
86                  for (int i = 0; i < namedNodeMap.getLength(); i++) {
87                      if (namedNodeMap.item(i).getNodeName().equals(attributeName)) {
88                          return i;
89                      }
90                  }
91              }
92              return requiredIndex;
93          }
94          return -1;
95      }
96  }