BrightSide Workbench Full Report + Source Code
org.turro.elephant.impl.util.StringParser Class Reference

Static Public Member Functions

static String processObject (String value, Object object)
 
static String toHTML (String value)
 
static String toHTML (String value, boolean check)
 
static boolean isHTML (String value)
 
static Object getObjectFor (String node, Object object)
 
static Object getValueFrom (String pathToValue, Object object)
 
static Object setObjectFor (String node, Object object, Class javaClass, Object value)
 
static void setValueFrom (String pathToValue, Object object, Class javaClass, Object value)
 
static String cutString (String value, int maxChars)
 
static Object convertToClass (Class javaClass, Object value)
 
static String getTextFromHtml (String html)
 

Detailed Description

Author
Lluis TurrĂ³ Cutiller lluis.nosp@m.@tur.nosp@m.ro.or.nosp@m.g

Definition at line 36 of file StringParser.java.

Member Function Documentation

◆ convertToClass()

static Object org.turro.elephant.impl.util.StringParser.convertToClass ( Class  javaClass,
Object  value 
)
static

Definition at line 155 of file StringParser.java.

155  {
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  }
Here is the caller graph for this function:

◆ cutString()

static String org.turro.elephant.impl.util.StringParser.cutString ( String  value,
int  maxChars 
)
static

Definition at line 148 of file StringParser.java.

148  {
149  if(maxChars > 0 && value.length() > maxChars) {
150  return value.substring(0, lastIndexOfWhiteChar(value, maxChars)) + "...";
151  }
152  return value;
153  }
Here is the caller graph for this function:

◆ getObjectFor()

static Object org.turro.elephant.impl.util.StringParser.getObjectFor ( String  node,
Object  object 
)
static

Definition at line 76 of file StringParser.java.

76  {
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  }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ getTextFromHtml()

static String org.turro.elephant.impl.util.StringParser.getTextFromHtml ( String  html)
static

Definition at line 200 of file StringParser.java.

200  {
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  }

◆ getValueFrom()

static Object org.turro.elephant.impl.util.StringParser.getValueFrom ( String  pathToValue,
Object  object 
)
static

Definition at line 104 of file StringParser.java.

104  {
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  }
static Object getObjectFor(String node, Object object)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ isHTML()

static boolean org.turro.elephant.impl.util.StringParser.isHTML ( String  value)
static

Definition at line 71 of file StringParser.java.

71  {
72  if(value == null) return false;
73  return value.startsWith("<");
74  }
Here is the caller graph for this function:

◆ processObject()

static String org.turro.elephant.impl.util.StringParser.processObject ( String  value,
Object  object 
)
static

Definition at line 38 of file StringParser.java.

38  {
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  }
static Object getValueFrom(String pathToValue, Object object)
Here is the call graph for this function:

◆ setObjectFor()

static Object org.turro.elephant.impl.util.StringParser.setObjectFor ( String  node,
Object  object,
Class  javaClass,
Object  value 
)
static

Definition at line 113 of file StringParser.java.

113  {
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  }
static Object convertToClass(Class javaClass, Object value)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ setValueFrom()

static void org.turro.elephant.impl.util.StringParser.setValueFrom ( String  pathToValue,
Object  object,
Class  javaClass,
Object  value 
)
static

Definition at line 138 of file StringParser.java.

138  {
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  }
static Object setObjectFor(String node, Object object, Class javaClass, Object value)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ toHTML() [1/2]

static String org.turro.elephant.impl.util.StringParser.toHTML ( String  value)
static

Definition at line 56 of file StringParser.java.

56  {
57  return toHTML(value, true);
58  }
static String toHTML(String value)
Here is the caller graph for this function:

◆ toHTML() [2/2]

static String org.turro.elephant.impl.util.StringParser.toHTML ( String  value,
boolean  check 
)
static

Definition at line 60 of file StringParser.java.

60  {
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  }
static boolean isHTML(String value)
Here is the call graph for this function:

The documentation for this class was generated from the following file: