View Javadoc
1   /*
2    * Copyright (c) 2002-2026 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.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   * Singleton to handle JDK specific stuff.
33   * This is required to support at least the differences with android.
34   *
35   * @author Ronald Brill
36   * @author Sven Strickroth
37   */
38  public final class Platform {
39  
40      // private static final Log LOG = LogFactory.getLog(Platform.class);
41  
42      private static FontUtil FontUtil_;
43  
44      /**
45       * @param imageWidth the width of the image this backend is for
46       * @param imageHeight the height of the image this backend is for
47       * @return a new {@link RenderingBackend}. If the {@link AwtRenderingBackend} can't be used a
48       *         {@link NoOpRenderingBackend} is used instead.
49       */
50      public static RenderingBackend getRenderingBackend(final int imageWidth, final int imageHeight) {
51          // for Android
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       * @return a new {@link FontUtil}. If the {@link AwtFontUtil} can't be used a
64       *         {@link NoOpFontUtil} is used instead.
65       */
66      public static FontUtil getFontUtil() {
67          // for Android
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       * @param inputStream the {@link InputStream} to read from
85       * @return a new {@link ImageData} object constructed from the given inputStream
86       * @throws IOException in case of error
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         // util class
112     }
113 }