18 package org.turro.elephant.impl.util;
20 import java.lang.reflect.InvocationTargetException;
21 import java.lang.reflect.Method;
22 import java.util.logging.Level;
23 import java.util.logging.Logger;
24 import java.util.regex.Matcher;
25 import java.util.regex.Pattern;
26 import org.turro.string.Strings;
27 import org.jsoup.Jsoup;
28 import org.jsoup.safety.Safelist;
29 import org.turro.elephant.context.ElephantContext;
30 import org.turro.util.Chars;
39 if(value ==
null ||
object ==
null) {
42 Pattern pat = Pattern.compile(
"(\\{(.+?)\\})");
43 Matcher mat = pat.matcher(value);
44 StringBuffer result =
new StringBuffer();
47 mat.appendReplacement(
49 (x ==
null ? mat.group(1) : x.toString())
52 mat.appendTail(result);
53 return result.toString();
56 public static String
toHTML(String value) {
57 return toHTML(value,
true);
60 public static String
toHTML(String value,
boolean check) {
61 if(check &&
isHTML(value))
return value;
62 if(value ==
null)
return "";
63 value = value.replaceAll(
"^\\s*",
"");
64 value = value.replaceAll(
"\\s*$",
"");
65 value = value.replaceAll(
"\\<",
"<");
66 value = value.replaceAll(
"\\s*\\n\\n\\s*",
"<br/><br/>");
67 value = value.replaceAll(
"\\n",
"<br/>");
71 public static boolean isHTML(String value) {
72 if(value ==
null)
return false;
73 return value.startsWith(
"<");
77 if(
"self".equals(node) ||
object ==
null)
return object;
79 String nodeName = node.substring(0, 1).toUpperCase() + node.substring(1);
82 method =
object.getClass().getMethod(
"get" + nodeName);
83 }
catch (NoSuchMethodException ex) {
84 method =
object.getClass().getMethod(
"is" + nodeName);
85 }
catch (SecurityException ex) {
89 return method.invoke(
object);
91 }
catch (IllegalAccessException ex) {
93 }
catch (IllegalArgumentException ex) {
95 }
catch (InvocationTargetException ex) {
97 }
catch (NoSuchMethodException ex) {
98 }
catch (SecurityException ex) {
105 if(pathToValue ==
null)
return null;
106 String nodes[] = pathToValue.split(
"\\.");
107 for(String node : nodes) {
113 public static Object
setObjectFor(String node, Object
object, Class javaClass, Object value) {
115 String nodeName = node.substring(0, 1).toUpperCase() + node.substring(1);
116 Method method =
null;
118 method =
object.getClass().getMethod(
"set" + nodeName,
new Class[] { javaClass });
119 }
catch (SecurityException ex) {
125 }
catch (IllegalAccessException ex) {
127 }
catch (IllegalArgumentException ex) {
129 }
catch (InvocationTargetException ex) {
131 }
catch (NoSuchMethodException ex) {
132 }
catch (SecurityException ex) {
138 public static void setValueFrom(String pathToValue, Object
object, Class javaClass, Object value) {
139 String nodes[] = pathToValue.split(
"\\.");
140 for(
int i = 0; i < nodes.length - 1; i++) {
144 setObjectFor(nodes[nodes.length - 1],
object, javaClass, value);
148 public static String
cutString(String value,
int maxChars) {
149 if(maxChars > 0 && value.length() > maxChars) {
150 return value.substring(0, lastIndexOfWhiteChar(value, maxChars)) +
"...";
156 if(value ==
null)
return null;
157 if(!javaClass.equals(value.getClass())) {
158 if(value.getClass().equals(String.class)) {
159 if(javaClass.equals(Long.class)) {
160 return Long.valueOf((String) value);
161 }
else if(javaClass.equals(Integer.class)) {
162 return Integer.valueOf((String) value);
163 }
else if(javaClass.equals(Short.class)) {
164 return Short.valueOf((String) value);
165 }
else if(javaClass.equals(Double.class)) {
166 return Double.valueOf((String) value);
167 }
else if(javaClass.equals(Float.class)) {
168 return Float.valueOf((String) value);
169 }
else if(javaClass.equals(Boolean.class)) {
170 return Boolean.valueOf((String) value);
172 }
else if(Number.class.isAssignableFrom(value.getClass())) {
173 if(javaClass.equals(Long.class)) {
174 return ((Number) value).longValue();
175 }
else if(javaClass.equals(Integer.class)) {
176 return ((Number) value).intValue();
177 }
else if(javaClass.equals(Short.class)) {
178 return ((Number) value).shortValue();
179 }
else if(javaClass.equals(Double.class)) {
180 return ((Number) value).doubleValue();
181 }
else if(javaClass.equals(Float.class)) {
182 return ((Number) value).floatValue();
183 }
else if(javaClass.equals(
long.class)) {
184 return ((Number) value).longValue();
185 }
else if(javaClass.equals(
int.class)) {
186 return ((Number) value).intValue();
187 }
else if(javaClass.equals(
short.class)) {
188 return ((Number) value).shortValue();
189 }
else if(javaClass.equals(
double.class)) {
190 return ((Number) value).doubleValue();
191 }
else if(javaClass.equals(
float.class)) {
192 return ((Number) value).floatValue();
201 if(Strings.isBlank(html))
return null;
202 html = html.replaceAll(
"(</div>|</p>|</h1>|</h2>|</h3>|</ul>|</ol>)",
"$1" + Chars.nl().repeat(2).toString());
203 html = html.replaceAll(
"(<br>|<br/>|</tr>|</h4>|</h5>|</table>)",
"$1" + Chars.nl().toString());
204 html = html.replaceAll(
"(<li>)", Chars.nl().toString() +
"$1");
205 html = Jsoup.clean(html, Safelist.none());
206 return html.replaceAll(Chars.nl().toString() +
"\\s*",
"\n");
209 private static int lastIndexOfWhiteChar(String value,
int maxChars) {
210 int maxLast = 0, last;
211 last = value.lastIndexOf(
' ', maxChars);
212 if(last > maxLast) maxLast = last;
213 last = value.lastIndexOf(
'\n', maxChars);
214 if(last > maxLast) maxLast = last;
215 last = value.lastIndexOf(
'.', maxChars);
216 if(last > maxLast) maxLast = last;
217 last = value.lastIndexOf(
',', maxChars);
218 if(last > maxLast) maxLast = last;
219 return maxLast == 0 ? maxChars : maxLast;
222 private StringParser() {
static String logMsg(String msg)
static String getTextFromHtml(String html)
static void setValueFrom(String pathToValue, Object object, Class javaClass, Object value)
static String toHTML(String value)
static Object getValueFrom(String pathToValue, Object object)
static Object getObjectFor(String node, Object object)
static String processObject(String value, Object object)
static String toHTML(String value, boolean check)
static Object convertToClass(Class javaClass, Object value)
static Object setObjectFor(String node, Object object, Class javaClass, Object value)
static String cutString(String value, int maxChars)
static boolean isHTML(String value)