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