package art.servers.controller.http.webpages;
|
|
|
import art.servers.Shared;
|
import art.servers.types.HttpAuthentication;
|
import java.lang.reflect.Array;
|
import java.lang.reflect.Field;
|
import java.lang.reflect.Method;
|
import java.text.DecimalFormat;
|
import java.text.SimpleDateFormat;
|
import java.util.ArrayList;
|
import java.util.List;
|
|
public class Page
|
{
|
public HttpAuthentication authentication = null;
|
public String html = null;
|
|
|
public Page(HttpAuthentication authentication)
|
{
|
this.authentication = authentication;
|
}
|
|
|
|
public byte[] toHTML() throws Exception
|
{
|
return new byte[0];
|
}
|
|
|
|
// <editor-fold defaultstate="collapsed" desc="Values">
|
|
|
protected String value_double(long value)
|
{
|
DecimalFormat formato1 = new DecimalFormat("###,###,###");
|
return formato1.format(value);
|
}
|
|
|
protected String value_double(double value)
|
{
|
DecimalFormat formato1 = new DecimalFormat("###,###,###.#");
|
return formato1.format(value);
|
}
|
|
|
protected String value_datetime(long timestamp)
|
{
|
SimpleDateFormat formato1 = new SimpleDateFormat(Shared.getMessage("dd/MM/yyyy HH:mm:ss"));
|
if (timestamp == 0) return "";
|
return formato1.format(timestamp);
|
}
|
|
// </editor-fold>
|
|
|
|
// <editor-fold defaultstate="collapsed" desc="tr">
|
|
|
protected String tr_string(String tag, String value)
|
{
|
if (value != null)
|
{
|
return "<tr><td>" + Shared.getMessage(tag) + "</td><td>" + value + "</td></tr>";
|
}
|
else
|
{
|
return "<tr><td>" + Shared.getMessage(tag) + "</td><td></td></tr>";
|
}
|
}
|
|
|
protected String tr_string(String tag, String value, String units)
|
{
|
if (value != null)
|
{
|
return "<tr><td>" + Shared.getMessage(tag) + "</td><td>" + value + " " + units + "</td></tr>";
|
}
|
else
|
{
|
return "<tr><td>" + Shared.getMessage(tag) + "</td><td></td></tr>";
|
}
|
}
|
|
|
|
protected String tr_integer(String tag, long value, String units)
|
{
|
DecimalFormat formato1 = new DecimalFormat("###,###,###");
|
return "<tr><td>" + Shared.getMessage(tag) + "</td><td>" + formato1.format(value) + " " + units + "</td></tr>";
|
}
|
|
|
|
protected String tr_double(String tag, double value, String units)
|
{
|
DecimalFormat formato1 = new DecimalFormat("###,###,###.#");
|
return "<tr><td>" + Shared.getMessage(tag) + "</td><td>" + formato1.format(value) + " " + units + "</td></tr>";
|
}
|
|
|
|
protected String tr_boolean(String tag, boolean value, boolean reverse)
|
{
|
if (reverse == false)
|
{
|
if (value == true)
|
{
|
return "<tr><td>" + Shared.getMessage(tag) + "</td><td><label>" + Shared.getMessage("Yes") + "</label><span class=\"icon_ok\"></span></td></tr>";
|
}
|
else
|
{
|
return "<tr><td>" + Shared.getMessage(tag) + "</td><td><label>" + Shared.getMessage("No") + "</label><span class=\"icon_ko\"></span></td></tr>";
|
}
|
}
|
else
|
{
|
if (value == false)
|
{
|
return "<tr><td>" + Shared.getMessage(tag) + "</td><td><label>" + Shared.getMessage("No") + "</label><span class=\"icon_ok\"></span></td></tr>";
|
}
|
else
|
{
|
return "<tr><td>" + Shared.getMessage(tag) + "</td><td><label>" + Shared.getMessage("Yes") + "</label><span class=\"icon_ko\"></span></td></tr>";
|
}
|
}
|
}
|
|
|
|
protected String tr_datetimeicon(String tag, long timestamp)
|
{
|
SimpleDateFormat formato1 = new SimpleDateFormat(Shared.getMessage("dd/MM/yyyy HH:mm:ss"));
|
|
if (timestamp > 0)
|
{
|
return "<tr><td>" + tag + "</td><td><span class=\"icon_ko\"></span><label>" + formato1.format(timestamp) + "</label></td></tr>";
|
}
|
else
|
{
|
return "<tr><td>" + tag + "</td><td><span class=\"icon_ok\"></span><label></label></td></tr>";
|
}
|
}
|
|
|
|
|
protected String tr_datetime(String tag, long timestamp)
|
{
|
SimpleDateFormat formato1 = new SimpleDateFormat(Shared.getMessage("dd/MM/yyyy HH:mm:ss"));
|
|
if (timestamp == 0)
|
{
|
return "<tr><td>" + tag + "</td><td></td></tr>";
|
}
|
else
|
{
|
return "<tr><td>" + tag + "</td><td>" + formato1.format(timestamp) + "</td></tr>";
|
}
|
}
|
|
|
protected String tr_datetimemillis(String tag, long timestamp)
|
{
|
SimpleDateFormat formato1 = new SimpleDateFormat(Shared.getMessage("dd/MM/yyyy HH:mm:ss.SSS"));
|
|
if (timestamp == 0)
|
{
|
return "<tr><td>" + tag + "</td><td></td></tr>";
|
}
|
else
|
{
|
return "<tr><td>" + tag + "</td><td>" + formato1.format(timestamp) + "</td></tr>";
|
}
|
}
|
|
|
// </editor-fold>
|
|
|
|
// <editor-fold defaultstate="collapsed" desc="Fields">
|
|
|
public List<String> getFields(String html) throws Exception
|
{
|
List<String> result = new ArrayList<String>();
|
|
int current = 0;
|
|
try
|
{
|
while (current >=0)
|
{
|
int index1 = html.indexOf("{{", current);
|
int index2 = html.indexOf("}}", index1 + 3) + 2;
|
result.add(html.substring(index1, index2));
|
current = index2 + 1;
|
}
|
|
}
|
catch (Exception e)
|
{
|
}
|
|
return result;
|
}
|
|
|
|
|
|
protected String invoke(String html, String field)
|
{
|
try
|
{
|
String[] values = field.replace("{{", "").replaceAll("}}", "").split(";");
|
|
if ((values[0].equalsIgnoreCase("read") == true) && (values[2].equalsIgnoreCase("this") == true))
|
{
|
return html.replace(field, getValue(this, values[3]).toString());
|
}
|
|
}
|
catch (Exception e)
|
{
|
e.printStackTrace();
|
}
|
|
return html.replace(field, "");
|
|
}
|
|
|
// </editor-fold>
|
|
|
|
// <editor-fold defaultstate="collapsed" desc="Reflection">
|
|
|
protected Object getValue(Object object, String name) throws Exception
|
{
|
int indice = name.indexOf(".");
|
|
if (indice > 0)
|
{
|
// Es un campo de una clase
|
|
String nombreClase = name.substring(0, indice);
|
String nombreRestante = name.substring(indice + 1, name.length());
|
|
try
|
{
|
Field field = getDeclaredField(object.getClass(), nombreClase);
|
field.setAccessible(true);
|
Object child = field.get(object);
|
Object value = getValue(child, nombreRestante);
|
if (value == null) return "";
|
return value;
|
}
|
catch (Exception e)
|
{
|
try
|
{
|
Method method = getDeclaredMethod(object.getClass(), nombreClase);
|
method.setAccessible(true);
|
Object child = method.invoke(object, new Object[0]);
|
Object value = getValue(child, nombreRestante);
|
if (value == null) return "";
|
return value;
|
}
|
catch (Exception exception)
|
{
|
return "";
|
}
|
}
|
}
|
else
|
{
|
try
|
{
|
Field field = null;
|
int fieldArrayIndex = 0;
|
|
// Array
|
|
if (name.indexOf("@") > 0)
|
{
|
String[] tokens = name.split("@");
|
field = getDeclaredField(object.getClass(), tokens[0]);
|
fieldArrayIndex = Integer.parseInt(tokens[1]);
|
field.setAccessible(true);
|
return getValue(field.get(object), fieldArrayIndex);
|
}
|
|
field = getDeclaredField(object.getClass(), name);
|
field.setAccessible(true);
|
return field.get(object);
|
}
|
catch (Exception e)
|
{
|
try
|
{
|
Method method = getDeclaredMethod(object.getClass(), name);
|
method.setAccessible(true);
|
return method.invoke(object, new Object[0]);
|
}
|
catch (Exception exception)
|
{
|
return "";
|
}
|
}
|
}
|
}
|
|
|
|
|
protected Object getValue(Object object, String name, Object parameter) throws Exception
|
{
|
int indice = name.indexOf(".");
|
|
if (indice > 0)
|
{
|
// Es un campo de una clase
|
|
String nombreClase = name.substring(0, indice);
|
String nombreRestante = name.substring(indice + 1, name.length());
|
|
try
|
{
|
Method method = getDeclaredMethod(object.getClass(), nombreClase);
|
method.setAccessible(true);
|
Object child = method.invoke(object, parameter);
|
return getValue(child, nombreRestante, parameter);
|
}
|
catch (Exception exception)
|
{
|
return null;
|
}
|
}
|
else
|
{
|
try
|
{
|
Method method = getDeclaredMethod(object.getClass(), name);
|
method.setAccessible(true);
|
return method.invoke(object, parameter);
|
}
|
catch (Exception exception)
|
{
|
return "";
|
}
|
}
|
}
|
|
|
|
|
|
|
protected Method getDeclaredMethod(Class clazz, String name) throws NoSuchMethodException
|
{
|
try
|
{
|
Method method = clazz.getDeclaredMethod(name);
|
return method;
|
}
|
catch (NoSuchMethodException e)
|
{
|
for (Method declaredMethod : clazz.getDeclaredMethods())
|
{
|
if (declaredMethod.getName().equals(name))
|
{
|
return declaredMethod;
|
}
|
}
|
|
if (clazz.getSuperclass() != null)
|
{
|
return getDeclaredMethod(clazz.getSuperclass(), name);
|
}
|
|
}
|
catch (Exception e)
|
{
|
if (clazz.getSuperclass() != null)
|
{
|
return getDeclaredMethod(clazz.getSuperclass(), name);
|
}
|
}
|
|
throw new NoSuchMethodException();
|
}
|
|
|
protected Field getDeclaredField(Class clase, String name) throws NoSuchFieldException
|
{
|
try
|
{
|
return clase.getDeclaredField(name);
|
}
|
catch (NoSuchFieldException e)
|
{
|
Class superclase = clase.getSuperclass();
|
return getDeclaredField(superclase, name);
|
}
|
}
|
|
|
|
protected void setValue(String name, Object value, Object object) throws Exception
|
{
|
if ((name == null) || (object == null)) return;
|
|
int indice = name.indexOf(".");
|
|
if (indice > 0)
|
{
|
// Es un campo de una clase
|
|
String nombreClase = name.substring(0, indice);
|
String nombreRestante = name.substring(indice + 1, name.length());
|
Field field = object.getClass().getDeclaredField(nombreClase);
|
field.setAccessible(true);
|
Object child = field.get(object);
|
setValue(nombreRestante, value, child);
|
}
|
else
|
{
|
try
|
{
|
Field field = null;
|
int fieldArrayIndex = 0;
|
|
// Array
|
|
if (name.indexOf("@") > 0)
|
{
|
String[] tokens = name.split("@");
|
field = getDeclaredField(object.getClass(), tokens[0]);
|
fieldArrayIndex = Integer.parseInt(tokens[1]);
|
}
|
else
|
{
|
field = getDeclaredField(object.getClass(), name);
|
}
|
|
field.setAccessible(true);
|
Class classType = field.getType();
|
|
if (value instanceof String)
|
{
|
if (classType.isAssignableFrom(Boolean.class) || classType.isAssignableFrom(boolean.class))field.set(object, ((String)value).equalsIgnoreCase("true"));
|
else if (classType.isAssignableFrom(Byte.class) || classType.isAssignableFrom(byte.class)) field.set(object, Byte.parseByte((String)value));
|
else if (classType.isAssignableFrom(Short.class) || classType.isAssignableFrom(short.class)) field.set(object, Short.parseShort((String)value));
|
else if (classType.isAssignableFrom(Integer.class) || classType.isAssignableFrom(int.class)) field.set(object, Integer.parseInt((String)value));
|
else if (classType.isAssignableFrom(Long.class) || classType.isAssignableFrom(long.class)) field.set(object, Long.parseLong((String)value));
|
else if (classType.isAssignableFrom(Float.class) || classType.isAssignableFrom(float.class)) field.set(object, Float.parseFloat((String)value));
|
else if (classType.isAssignableFrom(Double.class) || classType.isAssignableFrom(double.class)) field.set(object, Double.parseDouble((String)value));
|
else if (classType.isAssignableFrom(String.class)) field.set(object, value);
|
else if (classType.isArray() == true)
|
{
|
Class<?> componentType = classType.getComponentType();
|
|
if ((componentType.isAssignableFrom(Boolean.class)) || componentType.isAssignableFrom(boolean.class))
|
{
|
boolean[] array = (boolean[])field.get(object); Array.set(array, fieldArrayIndex, ((String)value).equalsIgnoreCase("true"));
|
}
|
else if ((componentType.isAssignableFrom(Byte.class)) || componentType.isAssignableFrom(byte.class))
|
{
|
byte[] array = (byte[])field.get(object); Array.set(array, fieldArrayIndex, Byte.parseByte((String)value));
|
}
|
else if ((componentType.isAssignableFrom(Short.class)) || componentType.isAssignableFrom(short.class))
|
{
|
short[] array = (short[])field.get(object); Array.set(array, fieldArrayIndex, Short.parseShort((String)value));
|
}
|
else if ((componentType.isAssignableFrom(Integer.class)) || componentType.isAssignableFrom(int.class))
|
{
|
int[] array = (int[])field.get(object); Array.set(array, fieldArrayIndex, Integer.parseInt((String)value));
|
}
|
else if ((componentType.isAssignableFrom(Long.class)) || componentType.isAssignableFrom(long.class))
|
{
|
long[] array = (long[])field.get(object); Array.set(array, fieldArrayIndex, Long.parseLong((String)value));
|
}
|
else if ((componentType.isAssignableFrom(Float.class)) || componentType.isAssignableFrom(float.class))
|
{
|
float[] array = (float[])field.get(object); Array.set(array, fieldArrayIndex, Float.parseFloat((String)value));
|
}
|
else if ((componentType.isAssignableFrom(Double.class)) || componentType.isAssignableFrom(double.class))
|
{
|
double[] array = (double[])field.get(object); Array.set(array, fieldArrayIndex, Double.parseDouble((String)value));
|
}
|
else
|
{
|
}
|
}
|
|
else field.set(object, value);
|
}
|
else
|
{
|
field.set(object, value);
|
}
|
}
|
catch (Exception e)
|
{
|
for (Method method : object.getClass().getDeclaredMethods())
|
{
|
if (method.getName().equals(name))
|
{
|
Object finalvalue = value;
|
Class classType = method.getParameterTypes()[0];
|
|
if (value instanceof String)
|
{
|
if (classType.isAssignableFrom(Boolean.class) || classType.isAssignableFrom(boolean.class)) finalvalue = new Boolean(value.toString().equalsIgnoreCase("true"));
|
else if (classType.isAssignableFrom(Byte.class) || classType.isAssignableFrom(byte.class)) finalvalue = new Byte(value.toString());
|
else if (classType.isAssignableFrom(Short.class) || classType.isAssignableFrom(short.class)) finalvalue = new Short(value.toString());
|
else if (classType.isAssignableFrom(Integer.class) || classType.isAssignableFrom(int.class)) finalvalue = new Integer(value.toString());
|
else if (classType.isAssignableFrom(Long.class) || classType.isAssignableFrom(long.class)) finalvalue = new Long(value.toString());
|
else if (classType.isAssignableFrom(Float.class) || classType.isAssignableFrom(float.class)) finalvalue = new Float(value.toString());
|
else if (classType.isAssignableFrom(Double.class) || classType.isAssignableFrom(double.class))finalvalue = new Double(value.toString());
|
else if (classType.isAssignableFrom(String.class)) finalvalue = value.toString();
|
}
|
|
method.setAccessible(true);
|
method.invoke(object, finalvalue);
|
return;
|
}
|
}
|
|
}
|
}
|
}
|
|
|
|
protected Object getValue(Object value, int arrayIndex)
|
{
|
try
|
{
|
if(value.getClass().isArray())
|
{
|
if(value instanceof Object[])
|
{
|
return ((Object[])value)[arrayIndex];
|
}
|
else // box primitive arrays
|
{
|
return Array.get(value, arrayIndex); // automatic boxing
|
}
|
}
|
}
|
catch (Exception e)
|
{
|
}
|
|
return null;
|
}
|
|
|
// </editor-fold>
|
|
|
|
}
|