1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.platform;
16
17 import java.io.IOException;
18 import java.io.InputStream;
19 import java.lang.reflect.InvocationTargetException;
20 import java.util.HashMap;
21 import java.util.List;
22 import java.util.Map;
23
24 import org.apache.commons.lang3.reflect.ConstructorUtils;
25 import org.htmlunit.platform.canvas.rendering.AwtRenderingBackend;
26 import org.htmlunit.platform.canvas.rendering.NoOpRenderingBackend;
27 import org.htmlunit.platform.canvas.rendering.RenderingBackend;
28 import org.htmlunit.platform.font.AwtFontUtil;
29 import org.htmlunit.platform.font.FontUtil;
30 import org.htmlunit.platform.font.NoOpFontUtil;
31 import org.htmlunit.platform.image.ImageData;
32 import org.htmlunit.platform.image.NoOpImageData;
33 import org.w3c.dom.Document;
34 import org.w3c.dom.NamedNodeMap;
35 import org.w3c.dom.Node;
36
37
38
39
40
41
42
43
44 public final class Platform {
45
46
47
48 private static FontUtil FontUtil_;
49
50 private static XmlUtilsHelperAPI HelperXerces_;
51 private static XmlUtilsHelperAPI HelperSunXerces_;
52
53 static {
54 try {
55 HelperSunXerces_ = (XmlUtilsHelperAPI) Class.forName("org.htmlunit.platform.util.XmlUtilsSunXercesHelper")
56 .getDeclaredConstructor().newInstance();
57 }
58 catch (final Exception | LinkageError ignored) {
59
60 }
61
62 try {
63 HelperXerces_ = (XmlUtilsHelperAPI) Class.forName("org.htmlunit.platform.util.XmlUtilsXercesHelper")
64 .getDeclaredConstructor().newInstance();
65 }
66 catch (final Exception | LinkageError ignored) {
67
68 }
69 }
70
71
72
73
74
75
76
77
78
79
80 public static int getIndex(final NamedNodeMap namedNodeMap, final Map<Integer, List<String>> attributesOrderMap,
81 final Node element, final int requiredIndex) {
82 if (HelperXerces_ != null) {
83 final int result = HelperXerces_.getIndex(namedNodeMap, attributesOrderMap, element, requiredIndex);
84 if (result != -1) {
85 return result;
86 }
87 }
88 if (HelperSunXerces_ != null) {
89 final int result = HelperSunXerces_.getIndex(namedNodeMap, attributesOrderMap, element, requiredIndex);
90 if (result != -1) {
91 return result;
92 }
93 }
94
95 return requiredIndex;
96 }
97
98
99
100
101
102
103
104
105 public static Map<Integer, List<String>> getAttributesOrderMap(final Document document) {
106 if (HelperXerces_ != null) {
107 final Map<Integer, List<String>> result = HelperXerces_.getAttributesOrderMap(document);
108 if (result != null) {
109 return result;
110 }
111 }
112 if (HelperSunXerces_ != null) {
113 final Map<Integer, List<String>> result = HelperSunXerces_.getAttributesOrderMap(document);
114 if (result != null) {
115 return result;
116 }
117 }
118
119 return new HashMap<>();
120 }
121
122
123
124
125
126
127
128 public static RenderingBackend getRenderingBackend(final int imageWidth, final int imageHeight) {
129
130 try {
131 final Class<?> backendClass = Class.forName(
132 "org.htmlunit.platform.canvas.rendering.AwtRenderingBackend");
133 return (RenderingBackend) ConstructorUtils.invokeConstructor(backendClass, imageWidth, imageHeight);
134 }
135 catch (final Throwable e) {
136 return new NoOpRenderingBackend(imageWidth, imageHeight);
137 }
138 }
139
140
141
142
143
144 public static FontUtil getFontUtil() {
145
146 if (FontUtil_ != null) {
147 return FontUtil_;
148 }
149
150 try {
151 final Class<?> backendClass = Class.forName("org.htmlunit.platform.font.AwtFontUtil");
152 FontUtil_ = (FontUtil) ConstructorUtils.invokeConstructor(backendClass);
153 return FontUtil_;
154 }
155 catch (final Throwable e) {
156 FontUtil_ = new NoOpFontUtil();
157 return FontUtil_;
158 }
159 }
160
161
162
163
164
165
166 public static ImageData buildImageData(final InputStream inputStream) throws IOException {
167 try {
168 final Class<?> backendClass = Class.forName(
169 "org.htmlunit.platform.image.ImageIOImageData");
170 return (ImageData) ConstructorUtils.invokeConstructor(backendClass, inputStream);
171 }
172 catch (final InvocationTargetException ex) {
173 final Throwable targetEx = ex.getTargetException();
174 if (targetEx instanceof IOException) {
175 throw (IOException) targetEx;
176 }
177
178 return new NoOpImageData();
179 }
180 catch (final RuntimeException ex) {
181 throw ex;
182 }
183 catch (final Throwable ex) {
184 return new NoOpImageData();
185 }
186 }
187
188 private Platform() {
189
190 }
191 }