BrightSide Workbench Full Report + Source Code
StringParser.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.turro.elephant.impl.util;
19 
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;
31 
36 public class StringParser {
37 
38  public static String processObject(String value, Object object) {
39  if(value == null || object == null) {
40  return value;
41  }
42  Pattern pat = Pattern.compile("(\\{(.+?)\\})");
43  Matcher mat = pat.matcher(value);
44  StringBuffer result = new StringBuffer();
45  while(mat.find()) {
46  Object x = getValueFrom(mat.group(2), object);
47  mat.appendReplacement(
48  result,
49  (x == null ? mat.group(1) : x.toString())
50  );
51  }
52  mat.appendTail(result);
53  return result.toString();
54  }
55 
56  public static String toHTML(String value) {
57  return toHTML(value, true);
58  }
59 
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("\\<", "&lt;");
66  value = value.replaceAll("\\s*\\n\\n\\s*", "<br/><br/>");
67  value = value.replaceAll("\\n", "<br/>");
68  return value;
69  }
70 
71  public static boolean isHTML(String value) {
72  if(value == null) return false;
73  return value.startsWith("<");
74  }
75 
76  public static Object getObjectFor(String node, Object object) {
77  if("self".equals(node) || object == null) return object;
78  try {
79  String nodeName = node.substring(0, 1).toUpperCase() + node.substring(1);
80  Method method = null;
81  try {
82  method = object.getClass().getMethod("get" + nodeName);
83  } catch (NoSuchMethodException ex) {
84  method = object.getClass().getMethod("is" + nodeName);
85  } catch (SecurityException ex) {
86  Logger.getLogger(StringParser.class.getName()).log(Level.SEVERE, ElephantContext.logMsg("Node:" + node), ex);
87  }
88  if(method != null) {
89  return method.invoke(object);
90  }
91  } catch (IllegalAccessException ex) {
92  Logger.getLogger(StringParser.class.getName()).log(Level.INFO, ElephantContext.logMsg("Node:" + node), ex);
93  } catch (IllegalArgumentException ex) {
94  Logger.getLogger(StringParser.class.getName()).log(Level.INFO, ElephantContext.logMsg("Node:" + node), ex);
95  } catch (InvocationTargetException ex) {
96  //Logger.getLogger(StringParser.class.getName()).log(Level.INFO, ElephantContext.logMsg("Node:" + node), ex);
97  } catch (NoSuchMethodException ex) {
98  } catch (SecurityException ex) {
99  Logger.getLogger(StringParser.class.getName()).log(Level.INFO, ElephantContext.logMsg("Node:" + node), ex);
100  }
101  return null;
102  }
103 
104  public static Object getValueFrom(String pathToValue, Object object) {
105  if(pathToValue == null) return null;
106  String nodes[] = pathToValue.split("\\.");
107  for(String node : nodes) {
108  object = getObjectFor(node, object);
109  }
110  return object;
111  }
112 
113  public static Object setObjectFor(String node, Object object, Class javaClass, Object value) {
114  try {
115  String nodeName = node.substring(0, 1).toUpperCase() + node.substring(1);
116  Method method = null;
117  try {
118  method = object.getClass().getMethod("set" + nodeName, new Class[] { javaClass });
119  } catch (SecurityException ex) {
120  Logger.getLogger(StringParser.class.getName()).log(Level.SEVERE, ElephantContext.logMsg("Node:" + node), ex);
121  }
122  if(method != null) {
123  return method.invoke(object, convertToClass(javaClass, value));
124  }
125  } catch (IllegalAccessException ex) {
126  Logger.getLogger(StringParser.class.getName()).log(Level.INFO, ElephantContext.logMsg("Node:" + node), ex);
127  } catch (IllegalArgumentException ex) {
128  Logger.getLogger(StringParser.class.getName()).log(Level.INFO, ElephantContext.logMsg("Node:" + node), ex);
129  } catch (InvocationTargetException ex) {
130  Logger.getLogger(StringParser.class.getName()).log(Level.INFO, ElephantContext.logMsg("Node:" + node), ex);
131  } catch (NoSuchMethodException ex) {
132  } catch (SecurityException ex) {
133  Logger.getLogger(StringParser.class.getName()).log(Level.INFO, ElephantContext.logMsg("Node:" + node), ex);
134  }
135  return null;
136  }
137 
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++) {
141  object = getObjectFor(nodes[i], object);
142  }
143  if(object != null) {
144  setObjectFor(nodes[nodes.length - 1], object, javaClass, value);
145  }
146  }
147 
148  public static String cutString(String value, int maxChars) {
149  if(maxChars > 0 && value.length() > maxChars) {
150  return value.substring(0, lastIndexOfWhiteChar(value, maxChars)) + "...";
151  }
152  return value;
153  }
154 
155  public static Object convertToClass(Class javaClass, Object value) {
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);
171  }
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();
193  }
194  }
195  }
196  return value;
197  }
198 
199  @Deprecated
200  public static String getTextFromHtml(String html) {
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");
207  }
208 
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;
220  }
221 
222  private StringParser() {
223  }
224 
225 }
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)