📄 miscutils.java
字号:
/* * MiscUtils.java * * Copyright (C) 2002, 2003, 2004, 2005, 2006 Takis Diakoumis * * 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 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. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * */package org.underworldlabs.util;import java.awt.event.KeyEvent;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.IOException;import java.io.InputStream;import java.lang.reflect.InvocationTargetException;import java.net.MalformedURLException;import java.net.URL;import java.net.URLClassLoader;import java.sql.SQLException;import java.text.DecimalFormat;import java.text.NumberFormat;import java.util.ArrayList;import java.util.Arrays;import java.util.Enumeration;import java.util.Properties;import java.util.StringTokenizer;import java.util.Vector;import java.util.jar.JarFile;import java.util.zip.ZipEntry;import javax.swing.Action;import javax.swing.ActionMap;import javax.swing.InputMap;import javax.swing.JComponent;import javax.swing.KeyStroke;/* ---------------------------------------------------------- * CVS NOTE: Changes to the CVS repository prior to the * release of version 3.0.0beta1 has meant a * resetting of CVS revision numbers. * ---------------------------------------------------------- *//** * * @author Takis Diakoumis * @author Dragan Vasic * @version $Revision: 1.9 $ * @date $Date: 2006/09/21 07:24:23 $ */public class MiscUtils { public static final String ACTION_DELIMETER = "+"; private static DecimalFormat oneDigitFormat; private static DecimalFormat twoDigitFormat; private static DecimalFormat threeDigitFormat; private MiscUtils() {} /** * Checks if the specified value is <code>null</code>. * This will also return <code>true</code> if the length * of the specified value is zero. * * @param value the value to check for <code>null</code> * @return <code>true</code> | <code>false</code> */ public static boolean isNull(String value) { return value == null || value.trim().length() == 0; } /** * Tests if the specified value contains the specified * word as a WHOLE word. * * @param value the value to test for the word * @param word the whole word we are looking for * @return <code>true</code> if found, <code>false</code> otherwise */ public static boolean containsWholeWord(String value, String word) { int index = value.indexOf(word); if (index == -1) return false; int valueLength = value.length(); int wordLength = word.length(); int indexLength = index + wordLength; if (indexLength == valueLength) // same word return true; if (indexLength != valueLength) { // check for embedded word if (index > 0) { return Character.isWhitespace(value.charAt(indexLength)) && Character.isWhitespace(value.charAt(index - 1)); } else { return Character.isWhitespace(value.charAt(indexLength)); } } else { return true; } } public static String getExceptionName(Throwable e) { String exceptionName = ""; if (e.getCause() != null) { Throwable _e = e.getCause(); exceptionName = _e.getClass().getName(); } else { exceptionName = e.getClass().getName(); } int index = exceptionName.lastIndexOf('.'); if (index != -1) { exceptionName = exceptionName.substring(index+1); } return exceptionName; } public static String firstLetterToUpper(String value) { boolean nextUpper = false; char[] chars = value.toCharArray(); StringBuffer sb = new StringBuffer(chars.length); for (int i = 0; i < chars.length; i++) { if (i == 0 || nextUpper) { sb.append(Character.toUpperCase(chars[i])); nextUpper = false; continue; } if (Character.isWhitespace(chars[i])) { nextUpper = true; sb.append(chars[i]); continue; } sb.append(Character.toLowerCase(chars[i])); nextUpper = false; } return sb.toString(); } /** * Formats the specified the SQL exception object * displaying the error message, error code and * the SQL state code. * * @param e - the SQL exception */ public static String formatSQLError(SQLException e) { if (e == null) { return ""; } StringBuffer sb = new StringBuffer(); sb.append(e.getMessage()); sb.append("\nError Code: " + e.getErrorCode()); String state = e.getSQLState(); if (state != null) { sb.append("\nSQL State Code: " + state); } sb.append("\n"); return sb.toString(); } /** * Returns a <code>String</code> array of the the CSV value * specified with the specfied delimiter. * * @param csvString the CSV value * @param delim the delimiter used in the CSV value * @return an array of split values */ public static String[] splitSeparatedValues(String csvString, String delim) { StringTokenizer st = new StringTokenizer(csvString, delim); ArrayList list = new ArrayList(st.countTokens()); while (st.hasMoreTokens()) { list.add(st.nextToken()); } String[] values = (String[])list.toArray(new String[list.size()]); return values; } public static boolean containsValue(String[] values, String value) { for (int i = 0; i < values.length; i++) { if (values[i].compareTo(value) == 0) { return true; } } return false; } public static boolean isValidNumber(String number) { char[] chars = number.toCharArray(); for (int i = 0; i < chars.length; i++) { if (!Character.isDigit(chars[i])) { return false; } } return true; } public static String getClassName(String path) { int index = path.indexOf(".class"); if (index == -1) { return null; } char dot = '.'; char pathSeparator = '/'; char[] chars = path.toCharArray(); StringBuffer sb = new StringBuffer(chars.length); for (int i = 0; i < chars.length; i++) { if (i == index) { break; } if (chars[i] == pathSeparator) { sb.append(dot); } else { sb.append(chars[i]); } } return sb.toString(); } public static String[] findImplementingClasses( String interfaceName, String paths) throws MalformedURLException, IOException { return findImplementingClasses(interfaceName, paths, true); } public static String[] findImplementingClasses( String interfaceName, String paths, boolean interfaceOnly) throws MalformedURLException, IOException { URL[] urls = loadURLs(paths); URLClassLoader loader = new URLClassLoader( urls, ClassLoader.getSystemClassLoader()); JarFile jarFile = null; String className = null; String[] files = splitSeparatedValues(paths, ";"); ArrayList clazzes = new ArrayList(); for (int i = 0; i < files.length; i++) { jarFile = new JarFile(files[i]); for (Enumeration j = jarFile.entries(); j.hasMoreElements();) { ZipEntry entry = (ZipEntry)j.nextElement(); className = getClassName(entry.getName()); if (className == null) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -