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
21 import org.apache.commons.lang3.reflect.ConstructorUtils;
22 import org.htmlunit.platform.canvas.rendering.AwtRenderingBackend;
23 import org.htmlunit.platform.canvas.rendering.NoOpRenderingBackend;
24 import org.htmlunit.platform.canvas.rendering.RenderingBackend;
25 import org.htmlunit.platform.font.AwtFontUtil;
26 import org.htmlunit.platform.font.FontUtil;
27 import org.htmlunit.platform.font.NoOpFontUtil;
28 import org.htmlunit.platform.image.ImageData;
29 import org.htmlunit.platform.image.NoOpImageData;
30
31
32
33
34
35
36
37
38 public final class Platform {
39
40
41
42 private static FontUtil FontUtil_;
43
44
45
46
47
48
49
50 public static RenderingBackend getRenderingBackend(final int imageWidth, final int imageHeight) {
51
52 try {
53 final Class<?> backendClass = Class.forName(
54 "org.htmlunit.platform.canvas.rendering.AwtRenderingBackend");
55 return (RenderingBackend) ConstructorUtils.invokeConstructor(backendClass, imageWidth, imageHeight);
56 }
57 catch (final Throwable e) {
58 return new NoOpRenderingBackend(imageWidth, imageHeight);
59 }
60 }
61
62
63
64
65
66 public static FontUtil getFontUtil() {
67
68 if (FontUtil_ != null) {
69 return FontUtil_;
70 }
71
72 try {
73 final Class<?> backendClass = Class.forName("org.htmlunit.platform.font.AwtFontUtil");
74 FontUtil_ = (FontUtil) ConstructorUtils.invokeConstructor(backendClass);
75 return FontUtil_;
76 }
77 catch (final Throwable e) {
78 FontUtil_ = new NoOpFontUtil();
79 return FontUtil_;
80 }
81 }
82
83
84
85
86
87
88 public static ImageData buildImageData(final InputStream inputStream) throws IOException {
89 try {
90 final Class<?> backendClass = Class.forName(
91 "org.htmlunit.platform.image.ImageIOImageData");
92 return (ImageData) ConstructorUtils.invokeConstructor(backendClass, inputStream);
93 }
94 catch (final InvocationTargetException ex) {
95 final Throwable targetEx = ex.getTargetException();
96 if (targetEx instanceof IOException exception) {
97 throw exception;
98 }
99
100 return new NoOpImageData();
101 }
102 catch (final RuntimeException ex) {
103 throw ex;
104 }
105 catch (final Throwable ex) {
106 return new NoOpImageData();
107 }
108 }
109
110 private Platform() {
111
112 }
113 }