View Javadoc
1   package com.jsql.view.swing.util;
2   
3   import org.apache.commons.lang3.StringUtils;
4   import org.mozilla.universalchardet.UniversalDetector;
5   
6   import java.nio.charset.StandardCharsets;
7   
8   public class UiStringUtil {
9   
10      private UiStringUtil() {
11          // Utility class
12      }
13      
14      public static String detectUtf8Html(String text) {
15          return UiStringUtil.detectUtf8Html(text, false);
16      }
17      
18      public static String detectUtf8HtmlNoWrap(String text) {
19          return UiStringUtil.detectUtf8Html(text, true);
20      }
21      
22      public static String detectUtf8Html(String text, boolean nowrap) {
23          
24          // Fix #35217: NullPointerException on getBytes()
25          if (text == null) {
26              return StringUtils.EMPTY;
27          }
28          
29          var detector = new UniversalDetector(null);
30          
31          // Decode bytes for potentially UTF8 chars
32          // Required by asian and hindi chars, otherwise wrong display in database tree
33          detector.handleData(
34              text.getBytes(StandardCharsets.UTF_8),
35              0,
36              text.length() - 1
37          );
38          
39          detector.dataEnd();
40          String result = text;
41  
42          // Confirm UTF8
43          String encoding = detector.getDetectedCharset();
44          if (encoding != null) {
45              result = I18nViewUtil.formatNonLatin(text, nowrap ? "white-space:nowrap;" : StringUtils.EMPTY);
46          }
47          
48          return result;
49      }
50  }