ips1ap101.lib.core.util.VelocityAid.java Source code

Java tutorial

Introduction

Here is the source code for ips1ap101.lib.core.util.VelocityAid.java

Source

/*
 * Este programa es software libre; usted puede redistribuirlo y/o modificarlo bajo los terminos
 * de la licencia "GNU General Public License" publicada por la Fundacion "Free Software Foundation".
 * Este programa se distribuye con la esperanza de que pueda ser util, pero SIN NINGUNA GARANTIA;
 * vea la licencia "GNU General Public License" para obtener mas informacion.
 */
package ips1ap101.lib.core.util;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Stack;
import java.util.TreeMap;
import java.util.TreeSet;
import ips1ap101.lib.base.util.StrUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.velocity.VelocityContext;

/**
 * @author Jorge Campins
 */
public class VelocityAid {

    // <editor-fold defaultstate="collapsed" desc="boolean constants">
    private static final Boolean TRUE = Boolean.TRUE;

    private static final Boolean FALSE = Boolean.FALSE;
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="character constants">
    private static final char bs = '\b';

    private static final char ht = '\t';

    private static final char lf = '\n';

    private static final char vt = '\u000B';

    private static final char ff = '\f';

    private static final char cr = '\r';

    private static final char lcb = '{';

    private static final char rcb = '}';

    private static final char lrb = '(';

    private static final char rrb = ')';

    private static final char lsb = '[';

    private static final char rsb = ']';

    private static final char bsl = '\\';

    private static final char spc = ' ';

    private static final char sqm = '\'';

    private static final char dqm = '\"';

    private static final char dot = '.';

    private static final char com = ',';

    private static final char und = '_';

    private static final char ast = '*';
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="string constants">
    private static final String EOL = System.getProperty("line.separator");

    private static final String TAB = StringUtils.repeat(" ", 4);

    private static final String EMPTY = "";
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="character-string constants">
    private static final String BS = EMPTY + bs;

    private static final String HT = EMPTY + ht;

    private static final String LF = EMPTY + lf;

    private static final String VT = EMPTY + vt;

    private static final String FF = EMPTY + ff;

    private static final String CR = EMPTY + cr;

    private static final String LCB = EMPTY + lcb;

    private static final String RCB = EMPTY + rcb;

    private static final String LRB = EMPTY + lrb;

    private static final String RRB = EMPTY + rrb;

    private static final String LSB = EMPTY + lsb;

    private static final String RSB = EMPTY + rsb;

    private static final String BSL = EMPTY + bsl;

    private static final String SPC = EMPTY + spc;

    private static final String SQM = EMPTY + sqm;

    private static final String DQM = EMPTY + dqm;

    private static final String DOT = EMPTY + dot;

    private static final String COM = EMPTY + com;

    private static final String UND = EMPTY + und;

    private static final String AST = EMPTY + ast;
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="boolean constants getters">
    public static Boolean TRUE() {
        return TRUE;
    }

    public static Boolean FALSE() {
        return FALSE;
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="character constants getters">
    public static char bs() {
        return bs;
    }

    public static char ht() {
        return ht;
    }

    public static char lf() {
        return lf;
    }

    public static char vt() {
        return vt;
    }

    public static char ff() {
        return ff;
    }

    public static char cr() {
        return cr;
    }

    public static char lcb() {
        return lcb;
    }

    public static char rcb() {
        return rcb;
    }

    public static char lrb() {
        return lrb;
    }

    public static char rrb() {
        return rrb;
    }

    public static char lsb() {
        return lsb;
    }

    public static char rsb() {
        return rsb;
    }

    public static char bsl() {
        return bsl;
    }

    public static char spc() {
        return spc;
    }

    public static char sqm() {
        return sqm;
    }

    public static char dqm() {
        return dqm;
    }

    public static char dot() {
        return dot;
    }

    public static char com() {
        return com;
    }

    public static char und() {
        return und;
    }

    public static char ast() {
        return ast;
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="string constants getters">
    public static String EOL() {
        return EOL;
    }

    public static String TAB() {
        return TAB;
    }

    public static String EMPTY() {
        return EMPTY;
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="character-string constants getters">
    public static String BS() {
        return BS;
    }

    public static String HT() {
        return HT;
    }

    public static String LF() {
        return LF;
    }

    public static String VT() {
        return VT;
    }

    public static String FF() {
        return FF;
    }

    public static String CR() {
        return CR;
    }

    public static String LCB() {
        return LCB;
    }

    public static String RCB() {
        return RCB;
    }

    public static String LRB() {
        return LRB;
    }

    public static String RRB() {
        return RRB;
    }

    public static String LSB() {
        return LSB;
    }

    public static String RSB() {
        return RSB;
    }

    public static String BSL() {
        return BSL;
    }

    public static String SPC() {
        return SPC;
    }

    public static String SQM() {
        return SQM;
    }

    public static String DQM() {
        return DQM;
    }

    public static String DOT() {
        return DOT;
    }

    public static String COM() {
        return COM;
    }

    public static String UND() {
        return UND;
    }

    public static String AST() {
        return AST;
    }
    // </editor-fold>

    public static Enum valueOf(Class<?> clazz, String name) {
        if (clazz == null || name == null) {
            return null;
        }
        Object object = values(clazz);
        if (object instanceof Enum[]) {
            Enum[] values = (Enum[]) object;
            for (Enum e : values) {
                if (name.equals(e.name())) {
                    return e;
                }
            }
        }
        return null;
    }

    public static Object values(Class<?> clazz) {
        if (clazz == null) {
            return null;
        }
        try {
            Method method = clazz.getMethod("values");
            Object object = method.invoke(null);
            return object;
        } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException
                | InvocationTargetException ex) {
            return null;
        }
    }

    /**
     * @return a new instance of Stack
     */
    public static Stack getNewStack() {
        return new Stack();
    }

    /**
     * @return a new instance of ArrayList
     */
    public static ArrayList getNewArrayList() {
        return new ArrayList();
    }

    /**
     * @return a new instance of LinkedHashMap
     */
    public static LinkedHashMap getNewLinkedHashMap() {
        return new LinkedHashMap();
    }

    /**
     * @return a new instance of TreeSet
     */
    public static LinkedHashSet getNewLinkedHashSet() {
        return new LinkedHashSet();
    }

    /**
     * @return a new instance of TreeMap
     */
    public static TreeMap getNewTreeMap() {
        return new TreeMap();
    }

    /**
     * @return a new instance of TreeSet
     */
    public static TreeSet getNewTreeSet() {
        return new TreeSet();
    }

    /**
     * @return the first non-null parameter
     */
    public static Object coalesce(Object... objects) {
        if (objects != null && objects.length > 0) {
            for (Object object : objects) {
                if (object != null) {
                    return object;
                }
            }
        }
        return null;
    }

    public static String[] array(String... string) {
        return string;
    }

    public static VelocityContext newVelocityContext() {
        VelocityContext context = new VelocityContext();
        /*
         * classes used in global macros must be loaded here
         */
        putClass(context, StrUtils.class);
        putClass(context, VelocityAid.class);
        putClass(context, org.apache.commons.lang.StringUtils.class);
        putClass(context, org.apache.commons.lang.StringEscapeUtils.class);
        /*
         * java type classes
         */
        putClass(context, java.lang.Boolean.class);
        putClass(context, java.lang.Byte.class);
        putClass(context, java.lang.Character.class);
        putClass(context, java.lang.Double.class);
        putClass(context, java.lang.Float.class);
        putClass(context, java.lang.Integer.class);
        putClass(context, java.lang.Long.class);
        putClass(context, java.lang.Short.class);
        putClass(context, java.lang.String.class);
        putClass(context, java.lang.System.class);
        putClass(context, java.math.BigDecimal.class);
        putClass(context, java.math.BigInteger.class);
        putClass(context, java.sql.Date.class);
        putClass(context, java.sql.Time.class);
        putClass(context, java.sql.Timestamp.class);
        return context;
    }

    private static void putClass(VelocityContext context, Class<?> clazz) {
        context.put(clazz.getSimpleName(), clazz);
    }

}