Package org.chaidb.db.helper

Source Code of org.chaidb.db.helper.Util

/*
* Copyright (C) 2006  http://www.chaidb.org
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
*/

package org.chaidb.db.helper;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.*;

/**
* Title:
* Description:
* Copyright:    Copyright (c) 2001
* Company:
*
* @author
* @version 1.0
*/

public class Util {

    /**
     * SHANNON added, see http://www.w3.org/TR/xquery/#id-literals
     */
    private static String REGEX_INTEGER = "-?[0-9]+";

    private static String REGEX_DECIMAL = "(\\.[0-9]+)|(-?[0-9]+\\.[0-9]*)";

    private static String REGEX_DOUBLE = "((\\.[0-9]+)|(-?[0-9]+(\\.[0-9]*)?))[eE][+-]?[0-9]+";

    public static boolean isInteger(String str) {
        if (str == null) return false;
        return str.matches(REGEX_INTEGER);
    }

    public static boolean isDecimal(String str) {
        if (str == null) return false;
        return str.matches(REGEX_DECIMAL);
    }

    public static boolean isDouble(String str) {
        if (str == null) return false;
        return str.matches(REGEX_DOUBLE);
    }

    public static boolean isNumeric(String str) {
        if (str == null) return false;
        return isInteger(str) || isDecimal(str) || isDouble(str);
    }

    /**
     * convert non-standard char to "predefined entity reference"
     *
     * @param str
     * @return
     * @see http://www.w3.org/TR/xquery/#id-literals
     */
    public static String strEntitize(String str) {
        if (str == null) return null;
        return str.replaceAll("<", "&lt;").replaceAll(">", "&gt;").replaceAll("&", "&amp;").replaceAll("\"", "&quot;").replaceAll("'", "&apos;");
    }

    /**
     * uitility class to check if a string is a number
     *
     * @param str the string to check
     * @deprecated use isNumeric(String str);
     */
    public static boolean isNumber(String str) {
        int size = str.length();
        for (int i = 0; i < size; i++) {
            if (!Character.isDigit(str.charAt(i))) {
                return false;
            }
        }
        return true;
    }

    /**
     * Convert a string to a boolean
     *
     * @param str the string to check
     */
    public static boolean strToBoolean(String str) {
        Boolean b = Boolean.valueOf(str);
        return b.booleanValue();
    }

    /**
     * This api will convert a String to an integer
     *
     * @param str String to be converted to int
     * @return int after converstion
     * @since chaidb 1.0
     */
    public static int strToInt(String str) {
        Integer i = new Integer(str);
        return i.intValue();
    }

    /**
     * This api will convert a String to an long
     *
     * @param str String to be converted to int
     * @return int after converstion
     * @since chaidb 1.0
     */
    public static long strToLong(String str) {
        Long l = new Long(str);
        return l.longValue();
    }

    /**
     * This api will convert a String to an double
     *
     * @param str String to be converted to double
     * @return int after converstion
     * @since chaidb 1.0
     */
    public static double strToDouble(String str) {
        Double l = new Double(str);
        return l.doubleValue();
    }

    /**
     * Check whether the string passed isNull or Empty
     *
     * @param strInp Input string
     * @return boolean
     * @since chaidb 1.0
     */
    public static boolean isNullOrEmpty(String strInp) {
        if (strInp == null) {
            return true;
        } else {
            if (strInp.trim().length() <= 0) {
                return true;
            }
        }
        return false;
    }

    /**
     * Check whether the URL is an valid Alt Server URL
     * AltserverURL can have the format :
     * format1: ldap://hostname:port
     * format2: hostname:port
     *
     * @param url altServerURL
     * @return boolean
     * @since chaidb 1.0
     */
    public static boolean isAltServerURL(String url) {
        if (url.startsWith("ldap://")) {
            StringTokenizer st = new StringTokenizer(url, "//");
            if (st.countTokens() != 2) return false;
            String str = st.nextToken(); //ldap:
            return isValidHostAndPort(st.nextToken());
        } else {
            return isValidHostAndPort(url);
        }
        //return false;
    }

    private static boolean isValidHostAndPort(String hostAndPort) {
        StringTokenizer st = new StringTokenizer(hostAndPort, ":");
        int numOfTokens = st.countTokens();

        if (numOfTokens == 1) {
            String host = st.nextToken();
            return isValidHost(host);
        } else if (numOfTokens == 2) {
            String host = st.nextToken();
            if (isValidHost(host)) {
                String port = st.nextToken();
                return ByteTool.isNumber(port.getBytes());
            }
        }
        return false;
    }

    private static boolean isValidHost(String host) {
        byte[] src = host.getBytes();
        if (src.length == 0) {
            return false;
        }

        ByteTool.toLowerCase(src);
        for (int i = 0; i < src.length; i++) {
            if ((src[i] == 46) || //.
                    ((src[i] >= 48) && (src[i] <= 57)) || //0-9
                    ((src[i] >= 97) && (src[i] <= 122)) //a-z
                    ) {
                //do nothing
            } else {
                return false;
            }
        }
        return true;
    }

    public static AbstractList splits(String source, char delimiter) {
        ArrayList result = new ArrayList();
        String v_source = source.trim();

        int st_index = 0;
        int srLength = v_source.length();

        int numberOfWords = 1;

        int index = v_source.indexOf(delimiter);

        while (index != -1) {
            numberOfWords++;
            int j = 1;

            while (v_source.charAt(index + j) == delimiter || v_source.charAt(index + j) == '\n')
                // skip the consecutive delimiter
                j++;

            result.add(v_source.substring(st_index, index));

            st_index = index + j;
            index = v_source.indexOf(delimiter, index + j);
        }

        result.add(v_source.substring(st_index, srLength));

        return result;

    }

    public static String timestamp() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddhhmmss");
        return formatter.format(new Date());
    }

    /**
     * convert date to the string, the string format is like:
     * 2002-08-09 16:11:27
     *
     * @param date GregorianCalendar
     * @return String
     */
    public static String dateToStr(GregorianCalendar date) {
        SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return formater.format(date.getTime());
    }

    /**
     * convert String to date, the string format is like:
     * 2002-08-09 16:11:27
     *
     * @param sDate String
     * @return GregorianCalendar
     * @throws java.text.ParseException
     */
    public static GregorianCalendar strToDate(String sDate) throws java.text.ParseException {
        GregorianCalendar date = new GregorianCalendar();
        SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        date.setTime(formater.parse(sDate));
        return date;
    }

    public static String getInvokerMethod(Throwable t) {
        if (t == null) {
            return null;
        }

        try {
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);

            t.printStackTrace(pw);
            String s = sw.toString();

            StringTokenizer tokens = new StringTokenizer(s, "\r\n\t");
            tokens.nextToken();
            tokens.nextToken();
            String line = tokens.nextToken();

            int k = line.lastIndexOf("(");
            String temp = line.substring(0, k);
            return line.substring(temp.lastIndexOf(".") + 1);
        } catch (Throwable tt) {
            return null;
        }
    }
}
TOP

Related Classes of org.chaidb.db.helper.Util

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.