comparison src/fschmidt/util/java/HtmlUtils.java @ 68:00520880ad02

add fschmidt source
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 05 Oct 2025 17:24:15 -0600
parents
children
comparison
equal deleted inserted replaced
67:9d0fefce6985 68:00520880ad02
1 /*
2 Copyright (c) 2008 Franklin Schmidt <fschmidt@gmail.com>
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 THE SOFTWARE.
21 */
22
23 package fschmidt.util.java;
24
25 import java.lang.reflect.Field;
26 import java.net.URLDecoder;
27 import java.net.URLEncoder;
28 import java.util.regex.Pattern;
29 import java.util.regex.Matcher;
30
31
32 public final class HtmlUtils {
33 private HtmlUtils() {} // never
34
35 public static String htmlEncode(String s) {
36 char[] a = s.toCharArray();
37 StringBuilder buf = new StringBuilder();
38 for( int i=0; i<a.length; i++ ) {
39 char c = a[i];
40 switch(c) {
41 case '&':
42 buf.append("&amp;");
43 break;
44 case '<':
45 buf.append("&lt;");
46 break;
47 case '>':
48 buf.append("&gt;");
49 break;
50 case '"':
51 buf.append("&quot;");
52 break;
53 default:
54 buf.append(c);
55 }
56 }
57 return buf.toString();
58 }
59
60 private static final Pattern entityPtn = Pattern.compile(
61 "&#(\\d+);"
62 );
63
64 public static String htmlDecode(String s) {
65 StringBuffer buf = new StringBuffer();
66 Matcher m = entityPtn.matcher(s);
67 while( m.find() ) {
68 String entity = new String(new char[]{(char)Integer.parseInt(m.group(1))});
69 m.appendReplacement(buf,entity);
70 }
71 m.appendTail(buf);
72 s = buf.toString();
73 s = s.replace("&nbsp;"," ");
74 s = s.replace("&quot;","\"");
75 s = s.replace("&gt;",">");
76 s = s.replace("&lt;","<");
77 s = s.replace("&amp;","&");
78 return s;
79 }
80
81 public static String javascriptStringEncode(String s) {
82 char[] a = s.toCharArray();
83 StringBuilder buf = new StringBuilder();
84 for( int i=0; i<a.length; i++ ) {
85 char c = a[i];
86 switch(c) {
87 case '\\':
88 buf.append("\\\\");
89 break;
90 case '\'':
91 buf.append("\\'");
92 break;
93 case '\"':
94 buf.append("\\\"");
95 break;
96 case '\n':
97 buf.append("\\n");
98 break;
99 case '\r':
100 buf.append("\\r");
101 break;
102 case '<':
103 buf.append("\\x3C");
104 break;
105 case '>':
106 buf.append("\\x3E");
107 break;
108 case '=':
109 buf.append("\\x3D");
110 break;
111 default:
112 buf.append(c);
113 }
114 }
115 return buf.toString();
116 }
117
118 public static String breakUp(final String text,int maxSize,boolean hasCharEntities) {
119 StringBuilder buf = new StringBuilder();
120 int n = 0;
121 int len = text.length();
122 for( int i=0; i<len; i++ ) {
123 char c = text.charAt(i);
124 if( hasCharEntities && c=='&' ) {
125 do {
126 buf.append(c);
127 c = text.charAt(++i);
128 } while( c != ';' );
129 } else if( Character.isWhitespace(c) ) {
130 n = 0;
131 } else {
132 if( ++n > maxSize ) {
133 buf.append("<wbr />");
134 n = 0;
135 }
136 }
137 buf.append(c);
138 }
139 return buf.toString();
140 }
141
142 public static String urlEncode(String s) {
143 try {
144 return URLEncoder.encode(s,"UTF-8");
145 } catch(java.io.UnsupportedEncodingException e) {
146 throw new RuntimeException(e);
147 }
148 }
149
150 public static String urlDecode(String s) {
151 try {
152 return URLDecoder.decode(s,"UTF-8");
153 } catch(java.io.UnsupportedEncodingException e) {
154 throw new RuntimeException(e);
155 }
156 }
157
158 public static String toJson(Object object) {
159 StringBuilder builder = new StringBuilder("{");
160 try {
161 Field[] fields = object.getClass().getDeclaredFields();
162 for (Field field : fields) {
163 field.setAccessible(true);
164 Object value = field.get(object);
165 if (value != null) {
166 if (builder.length() > 1)
167 builder.append(',');
168 builder.append('\"').append(field.getName()).append("\": \"").append(javascriptStringEncode(value.toString())).append("\"\n");
169 }
170 }
171 } catch (IllegalAccessException e) {
172 throw new RuntimeException(e);
173 }
174 builder.append('}');
175 return builder.toString();
176 }
177
178 }