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;
16  
17  import java.lang.reflect.Field;
18  import java.util.List;
19  import java.util.Map;
20  
21  import org.w3c.dom.Document;
22  import org.w3c.dom.NamedNodeMap;
23  import org.w3c.dom.Node;
24  
25  /**
26   * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
27   *
28   * API of the helper.
29   *
30   * @author Ronald Brill
31   */
32  public interface XmlUtilsHelperAPI {
33  
34      /**
35       * Returns internal Xerces details about all elements in the specified document.
36       * The id of the returned {@link Map} is the {@code nodeIndex} of an element, and the list
37       * is the array of ordered attributes names.
38       * @param document the document
39       * @return the map of an element index with its ordered attribute names or null if the
40       *         provided document is not supported
41       */
42      Map<Integer, List<String>> getAttributesOrderMap(Document document);
43  
44      /**
45       * Helper.
46       *
47       * @param namedNodeMap the node map
48       * @param attributesOrderMap the order map
49       * @param element the node
50       * @param requiredIndex the required index
51       * @return the index or -1 if the provided element is not supported
52       */
53      int getIndex(NamedNodeMap namedNodeMap, Map<Integer, List<String>> attributesOrderMap,
54              Node element, int requiredIndex);
55  
56      /**
57       * Helper to get the value of an private field.
58       * @param <T> the expected value type
59       * @param object the object that holds the field
60       * @param fieldName the name of the field
61       * @return the value casted to T
62       */
63      @SuppressWarnings("unchecked")
64      default <T> T getPrivate(final Object object, final String fieldName) {
65          try {
66              final Field f = object.getClass().getDeclaredField(fieldName);
67              f.setAccessible(true);
68              return (T) f.get(object);
69          }
70          catch (final Exception e) {
71              throw new RuntimeException(e);
72          }
73      }
74  }