Java/Data Type/String Parser
Версия от 18:01, 31 мая 2010; (обсуждение)
Содержание
- 1 Convert a String to an int, returning zero if the conversion fails.
- 2 Decodes a String with Numeric Character References
- 3 Normalize a SQL identifer, up-casing if <regular identifer>, and handling of <delimited identifer> (SQL 2003, section 5.2).
- 4 Parse a method signature or method call signature
- 5 Parse basic types
- 6 Parse Comma Delimited List
- 7 Parse Fraction
- 8 Parse String to array of Strings while treating quoted values as single element
- 9 Parsing primitives from String"s without creating any objects
- 10 Returns true if the argument contains a number
Convert a String to an int, returning zero if the conversion fails.
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
*
*
* @author John Keyes (john at integralsource.ru)
* @version $Revision: 680644 $, $Date: 2008-07-29 01:13:48 -0700 (Tue, 29 Jul 2008) $
*/
public class Main {
//--------------------------------------------------------------------
/**
* <p>Convert a <code>String</code> to an <code>int</code>, returning
* <code>zero</code> if the conversion fails.</p>
*
* @param str the string to convert
* @return the int represented by the string, or <code>zero</code> if
* conversion fails
*/
public static int stringToInt(String str) {
return stringToInt(str, 0);
}
/**
* <p>Convert a <code>String</code> to an <code>int</code>, returning a
* default value if the conversion fails.</p>
*
* @param str the string to convert
* @param defaultValue the default value
* @return the int represented by the string, or the default if conversion fails
*/
public static int stringToInt(String str, int defaultValue) {
try {
return Integer.parseInt(str);
} catch (NumberFormatException nfe) {
return defaultValue;
}
}
}
Decodes a String with Numeric Character References
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is part of dcm4che, an implementation of DICOM(TM) in
* Java(TM), available at http://sourceforge.net/projects/dcm4che.
*
* The Initial Developer of the Original Code is
* TIANI Medgraph AG.
* Portions created by the Initial Developer are Copyright (C) 2003-2005
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Gunter Zeilinger <gunter.zeilinger@tiani.ru>
* Franz Willer <franz.willer@gwi-ag.ru>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
/**
*
* Helper class to encode/decode Strings with Numeric Character References as defined in SGML.
*
* @author franz.willer
*
* @version $Revision: 2101 $
* @since 25.11.2005
*/
public class NumericCharacterReference {
/**
* Decodes a String with Numeric Character References.
* <p>
*
* @param str A NCR encoded String
* @param unknownCh, A character that is used if nnnn of &#nnnn; is not a int.
*
* @return The decoded String.
*/
public static String decode(String str, char unknownCh) {
StringBuffer sb = new StringBuffer();
int i1=0;
int i2=0;
while(i2<str.length()) {
i1 = str.indexOf("&#",i2);
if (i1 == -1 ) {
sb.append(str.substring(i2));
break ;
}
sb.append(str.substring(i2, i1));
i2 = str.indexOf(";", i1);
if (i2 == -1 ) {
sb.append(str.substring(i1));
break ;
}
String tok = str.substring(i1+2, i2);
try {
int radix = 10 ;
if (tok.charAt(0) == "x" || tok.charAt(0) == "X") {
radix = 16 ;
tok = tok.substring(1);
}
sb.append((char) Integer.parseInt(tok, radix));
} catch (NumberFormatException exp) {
sb.append(unknownCh);
}
i2++ ;
}
return sb.toString();
}
/**
* Encode a String with Numeric Character Refernces.
* <p>
* Formats each character < 0x20 or > 0x7f to &#nnnn; where nnnn is the char value as int.
* <p>
*
* @param str The raw String
* @return The encoded String
*/
public static String encode( String str ) {
char[] ch = str.toCharArray();
StringBuffer sb = new StringBuffer();
for ( int i = 0 ; i < ch.length ; i++ ) {
if ( ch[i] < 0x20 || ch[i] > 0x7f )
sb.append("&#").append((int) ch[i]).append(";");
else
sb.append(ch[i]);
}
return sb.toString();
}
}
Normalize a SQL identifer, up-casing if <regular identifer>, and handling of <delimited identifer> (SQL 2003, section 5.2).
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Properties;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Locale;
import java.util.Properties;
/*
Derby - Class org.apache.derby.iapi.util.PropertyUtil
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to you under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
public class Main {
/** Convert string to uppercase
* Always use the java.util.ENGLISH locale
* @param s string to uppercase
* @return uppercased string
*/
public static String SQLToUpperCase(String s)
{
return s.toUpperCase(Locale.ENGLISH);
}
/** Compares two strings
* Strings will be uppercased in english and compared
* equivalent to s1.equalsIgnoreCase(s2)
* throws NPE if s1 is null
*
* @param s1 first string to compare
* @param s2 second string to compare
*
* @return true if the two upppercased ENGLISH values are equal
* return false if s2 is null
*/
public static boolean SQLEqualsIgnoreCase(String s1, String s2)
{
if (s2 == null)
return false;
else
return SQLToUpperCase(s1).equals(SQLToUpperCase(s2));
}
/**
* Normalize a SQL identifer, up-casing if <regular identifer>,
* and handling of <delimited identifer> (SQL 2003, section 5.2).
* The normal form is used internally in Derby.
*
* @param id syntacically correct SQL identifier
*/
public static String normalizeSQLIdentifier(String id) {
if (id.length() == 0) {
return id;
}
if (id.charAt(0) == """ &&
id.length() >= 3 &&
id.charAt(id.length() - 1) == """) {
// assume syntax is OK, thats is, any quotes inside are doubled:
return compressQuotes(
id.substring(1, id.length() - 1), "\"\"");
} else {
return SQLToUpperCase(id);
}
}
/**
* Compress 2 adjacent (single or double) quotes into a single (s or d)
* quote when found in the middle of a String.
*
* NOTE: """" or """" will be compressed into "" or "".
* This function assumes that the leading and trailing quote from a
* string or delimited identifier have already been removed.
* @param source string to be compressed
* @param quotes string containing two single or double quotes.
* @return String where quotes have been compressed
*/
public static String compressQuotes(String source, String quotes)
{
String result = source;
int index;
/* Find the first occurrence of adjacent quotes. */
index = result.indexOf(quotes);
/* Replace each occurrence with a single quote and begin the
* search for the next occurrence from where we left off.
*/
while (index != -1) {
result = result.substring(0, index + 1) +
result.substring(index + 2);
index = result.indexOf(quotes, index + 1);
}
return result;
}
}
Parse a method signature or method call signature
/**************************************************************************************
* Copyright (c) Jonas Bon�r, Alexandre Vasseur. All rights reserved. *
* http://aspectwerkz.codehaus.org *
* ---------------------------------------------------------------------------------- *
* The software in this package is published under the terms of the LGPL license *
* a copy of which has been included with this distribution in the license.txt file. *
**************************************************************************************/
import java.util.List;
import java.util.ArrayList;
/**
* Utility methods for strings.
*
* @author
*/
public class Strings {
/**
* Parse a method signature or method call signature.
* <br/>Given a call signature like "method(Type t)", extract the method name
* and param type and parameter name: [method, Type, t]
* <br/>Given a signature like "method(X x, Y)", extract the method name
* and param name / param type - but leaving empty String if
* the information is not available: [method, X, x, Y, ""]
*
* @param methodCallSignature
* @return each element (2xp+1 sized) (see doc)
*/
public static String[] extractMethodSignature(String methodCallSignature) {
List extracted = new ArrayList();
String methodName = methodCallSignature;
String methodCallDesc = null;
if (methodCallSignature.indexOf("(") > 0) {
methodName = methodName.substring(0, methodCallSignature.indexOf("("));
methodCallDesc =
methodCallSignature.substring(methodCallSignature.indexOf("(") + 1, methodCallSignature.lastIndexOf(")"));
}
extracted.add(methodName);
if (methodCallDesc != null) {
String[] parameters = Strings.splitString(methodCallDesc, ",");
for (int i = 0; i < parameters.length; i++) {
String[] parameterInfo = Strings.splitString(
Strings.replaceSubString(
parameters[i].trim(),
" ",
" "
), " "
);
extracted.add(parameterInfo[0]);
extracted.add((parameterInfo.length > 1) ? parameterInfo[1] : "");
}
}
return (String[]) extracted.toArray(new String[]{});
}
/**
* Removes newline, carriage return and tab characters from a string.
*
* @param toBeEscaped string to escape
* @return the escaped string
*/
public static String removeFormattingCharacters(final String toBeEscaped) {
StringBuffer escapedBuffer = new StringBuffer();
for (int i = 0; i < toBeEscaped.length(); i++) {
if ((toBeEscaped.charAt(i) != "\n") && (toBeEscaped.charAt(i) != "\r") && (toBeEscaped.charAt(i) != "\t")) {
escapedBuffer.append(toBeEscaped.charAt(i));
}
}
String s = escapedBuffer.toString();
return s;//
// Strings.replaceSubString(s, "\"", "")
}
/**
* Replaces all occurences of a substring inside a string.
*
* @param str the string to search and replace in
* @param oldToken the string to search for
* @param newToken the string to replace newToken
* @return the new string
*/
public static String replaceSubString(final String str, final String oldToken, final String newToken) {
return replaceSubString(str, oldToken, newToken, -1);
}
/**
* Replaces all occurences of a substring inside a string.
*
* @param str the string to search and replace in
* @param oldToken the string to search for
* @param newToken the string to replace newToken
* @param max maximum number of values to replace (-1 => no maximum)
* @return the new string
*/
public static String replaceSubString(final String str, final String oldToken, final String newToken, int max) {
if ((str == null) || (oldToken == null) || (newToken == null) || (oldToken.length() == 0)) {
return str;
}
StringBuffer buf = new StringBuffer(str.length());
int start = 0;
int end = 0;
while ((end = str.indexOf(oldToken, start)) != -1) {
buf.append(str.substring(start, end)).append(newToken);
start = end + oldToken.length();
if (--max == 0) {
break;
}
}
buf.append(str.substring(start));
return buf.toString();
}
/**
* String split on multicharacter delimiter. <p/>Written by Tim Quinn (tim.quinn@honeywell.ru)
*
* @param stringToSplit
* @param delimiter
* @return
*/
public static final String[] splitString(String stringToSplit, String delimiter) {
String[] aRet;
int iLast;
int iFrom;
int iFound;
int iRecords;
// return Blank Array if stringToSplit == "")
if (stringToSplit.equals("")) {
return new String[0];
}
// count Field Entries
iFrom = 0;
iRecords = 0;
while (true) {
iFound = stringToSplit.indexOf(delimiter, iFrom);
if (iFound == -1) {
break;
}
iRecords++;
iFrom = iFound + delimiter.length();
}
iRecords = iRecords + 1;
// populate aRet[]
aRet = new String[iRecords];
if (iRecords == 1) {
aRet[0] = stringToSplit;
} else {
iLast = 0;
iFrom = 0;
iFound = 0;
for (int i = 0; i < iRecords; i++) {
iFound = stringToSplit.indexOf(delimiter, iFrom);
if (iFound == -1) { // at End
aRet[i] = stringToSplit.substring(iLast + delimiter.length(), stringToSplit.length());
} else if (iFound == 0) { // at Beginning
aRet[i] = "";
} else { // somewhere in middle
aRet[i] = stringToSplit.substring(iFrom, iFound);
}
iLast = iFound;
iFrom = iFound + delimiter.length();
}
}
return aRet;
}
public static boolean isNullOrEmpty(String s) {
return (s == null) ? true : (s.length() <= 0);
}
}
Parse basic types
/*
* Copyright 2004, 2005, 2006 Odysseus Software GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.text.DateFormat;
import java.text.Format;
import java.text.ParseException;
import java.text.ParsePosition;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import java.math.BigDecimal;
import java.math.BigInteger;
/**
* Parse basic types.
*
* @author Christoph Beck
*/
public class ParseUtils {
private static Locale locale = Locale.US;
private static Object nullValue(Class type) {
if (type.isPrimitive()) {
if (type == boolean.class)
return Boolean.FALSE;
if (type == byte.class)
return new Byte((byte)0);
if (type == char.class)
return new Character((char)0);
if (type == short.class)
return new Short((short)0);
if (type == int.class)
return new Integer(0);
if (type == long.class)
return new Long(0);
if (type == float.class)
return new Float(0);
if (type == double.class)
return new Double(0);
}
return null;
}
private static Class objectType(Class type) {
if (type.isPrimitive()) {
if (type == boolean.class)
return Boolean.class;
if (type == byte.class)
return Byte.class;
if (type == char.class)
return Character.class;
if (type == short.class)
return Short.class;
if (type == int.class)
return Integer.class;
if (type == long.class)
return Long.class;
if (type == float.class)
return Float.class;
if (type == double.class)
return Double.class;
}
return type;
}
private static Object parse(Format format, String value) throws ParseException {
ParsePosition pos = new ParsePosition(0);
Object result = format.parseObject(value, pos);
if (pos.getIndex() < value.length())
throw new ParseException("Cannot parse " + value + " (garbage suffix)!", pos.getIndex());
return result;
}
private static Date parseDate(String value) throws ParseException {
DateFormat format = DateFormat.getDateInstance(DateFormat.SHORT, locale);
format.setTimeZone(TimeZone.getTimeZone("GMT"));
return (Date)parse(format, value);
}
private static Boolean parseBoolean(String value) throws ParseException {
if ("true".equals(value)) {
return Boolean.TRUE;
} else if ("false".equals(value)) {
return Boolean.FALSE;
} else {
throw new ParseException("Cannot parse "" + value + "" as boolean", 0);
}
}
private static Character parseCharacter(String value) throws ParseException {
if (value.length() != 1) {
throw new ParseException("Cannot parse "" + value + "" as character", value.length());
}
return new Character(value.charAt(0));
}
/**
* Parse value of specified type. The string value has to be in
* standard notation for the specified type.
*/
public static Object parse(Class type, String value) throws Exception {
if (value == null) {
return nullValue(type);
} else if (value.length() == 0) {
return type == String.class ? value : nullValue(type);
}
type = objectType(type);
if (type == BigDecimal.class) {
return new BigDecimal(value);
} else if (type == BigInteger.class) {
return new BigInteger(value);
} else if (type == Boolean.class) {
return parseBoolean(value);
} else if (type == Byte.class) {
return Byte.valueOf(value);
} else if (type == Character.class) {
return parseCharacter(value);
} else if (type == Date.class) {
return parseDate(value);
} else if (type == Double.class) {
return Double.valueOf(value);
} else if (type == Float.class) {
return Float.valueOf(value);
} else if (type == Integer.class) {
return Integer.valueOf(value);
} else if (type == Long.class) {
return Long.valueOf(value);
} else if (type == Short.class) {
return Short.valueOf(value);
} else if (type == String.class) {
return value;
}
throw new ParseException("Cannot parse type " + type, 0);
}
}
///////////////////////
/*
* Copyright 2004, 2005, 2006 Odysseus Software GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.odysseus.calyxo.base.util;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Date;
import junit.framework.TestCase;
/**
* ParseUtils test case.
*
* @author Christoph Beck
*/
public class ParseUtilsTest extends TestCase {
/**
* Constructor for ParseUtilsTest.
* @param arg0
*/
public ParseUtilsTest(String arg0) {
super(arg0);
}
public void testNullPrimitive() throws Exception {
assertEquals(Boolean.FALSE, ParseUtils.parse(boolean.class, null));
assertEquals(new Character((char)0), ParseUtils.parse(char.class, null));
assertEquals(new Byte((byte)0), ParseUtils.parse(byte.class, null));
assertEquals(new Short((short)0), ParseUtils.parse(short.class, null));
assertEquals(new Integer(0), ParseUtils.parse(int.class, null));
assertEquals(new Long(0), ParseUtils.parse(long.class, null));
assertEquals(new Float(0), ParseUtils.parse(float.class, null));
assertEquals(new Double(0), ParseUtils.parse(double.class, null));
}
public void testPrimitive() throws Exception {
assertEquals(Boolean.TRUE, ParseUtils.parse(boolean.class, "true"));
assertEquals(Boolean.FALSE, ParseUtils.parse(boolean.class, "false"));
assertEquals(new Character((char)10), ParseUtils.parse(char.class, "\n"));
assertEquals(new Byte((byte)10), ParseUtils.parse(byte.class, "10"));
assertEquals(new Short((short)10), ParseUtils.parse(short.class, "10"));
assertEquals(new Integer(10), ParseUtils.parse(int.class, "10"));
assertEquals(new Long(10), ParseUtils.parse(long.class, "10"));
assertEquals(new Float(10), ParseUtils.parse(float.class, "10"));
assertEquals(new Double(10), ParseUtils.parse(double.class, "10"));
}
public void testNullObject() throws Exception {
assertNull(ParseUtils.parse(Boolean.class, null));
assertNull(ParseUtils.parse(Byte.class, null));
assertNull(ParseUtils.parse(Character.class, null));
assertNull(ParseUtils.parse(Short.class, null));
assertNull(ParseUtils.parse(Integer.class, null));
assertNull(ParseUtils.parse(Long.class, null));
assertNull(ParseUtils.parse(Float.class, null));
assertNull(ParseUtils.parse(Double.class, null));
assertNull(ParseUtils.parse(BigInteger.class, null));
assertNull(ParseUtils.parse(BigDecimal.class, null));
assertNull(ParseUtils.parse(Date.class, null));
assertNull(ParseUtils.parse(String.class, null));
}
public void testObject() throws Exception {
assertEquals(Boolean.TRUE, ParseUtils.parse(Boolean.class, "true"));
assertEquals(Boolean.FALSE, ParseUtils.parse(Boolean.class, "false"));
assertEquals(new Character((char)10), ParseUtils.parse(Character.class, "\n"));
assertEquals(new Byte((byte)10), ParseUtils.parse(Byte.class, "10"));
assertEquals(new Short((short)10), ParseUtils.parse(Short.class, "10"));
assertEquals(new Integer(10), ParseUtils.parse(Integer.class, "10"));
assertEquals(new Long(10), ParseUtils.parse(Long.class, "10"));
assertEquals(new Float(10), ParseUtils.parse(Float.class, "10"));
assertEquals(new Double(10), ParseUtils.parse(Double.class, "10"));
assertEquals(new BigInteger("10"), ParseUtils.parse(BigInteger.class, "10"));
assertEquals(new BigDecimal(10), ParseUtils.parse(BigDecimal.class, "10"));
assertEquals(new Date(0), ParseUtils.parse(Date.class, "1/1/70"));
assertEquals("foo", ParseUtils.parse(String.class, "foo"));
}
public void testBadValues() throws Exception {
try {
ParseUtils.parse(Boolean.class, "no");
fail("Exception expected");
} catch(Exception e) {}
try {
ParseUtils.parse(Character.class, "10");
fail("Exception expected");
} catch(Exception e) {}
try {
ParseUtils.parse(Byte.class, "abc");
fail("Exception expected");
} catch(Exception e) {}
try {
ParseUtils.parse(Short.class, "abc");
fail("Exception expected");
} catch(Exception e) {}
try {
ParseUtils.parse(Integer.class, "abc");
fail("Exception expected");
} catch(Exception e) {}
try {
ParseUtils.parse(Long.class, "abc");
fail("Exception expected");
} catch(Exception e) {}
try {
ParseUtils.parse(Float.class, "abc");
fail("Exception expected");
} catch(Exception e) {}
try {
ParseUtils.parse(Double.class, "abc");
fail("Exception expected");
} catch(Exception e) {}
try {
ParseUtils.parse(BigInteger.class, "abc");
fail("Exception expected");
} catch(Exception e) {}
try {
ParseUtils.parse(BigDecimal.class, "abc");
fail("Exception expected");
} catch(Exception e) {}
try {
ParseUtils.parse(Date.class, "1.1.70");
fail("Exception expected");
} catch(Exception e) {}
}
public void testBadType() throws Exception {
try {
ParseUtils.parse(Cloneable.class, "dolly");
fail("Exception expected");
} catch(Exception e) {}
}
public static void main(String[] args) {
junit.textui.TestRunner.run(ParseUtilsTest.class);
}
}
Parse Comma Delimited List
/*
* The contents of this file are subject to the Sapient Public License
* Version 1.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* http://carbon.sf.net/License.html.
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
*
* The Original Code is The Carbon Component Framework.
*
* The Initial Developer of the Original Code is Sapient Corporation
*
* Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
*/
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.StringTokenizer;
/**
* <p>Utilities for strings.</p>
*
*
* Copyright 2002 Sapient
* @since carbon 1.0
* @author Greg Hinkle, May 2002
* @version $Revision: 1.5 $($Author: dvoet $ / $Date: 2003/05/05 21:21:24 $)
*/
public class StringUtil {
/**
* Parses a comma-separated list into an array of Strings
* Values can contain whitespace, but whitespace at the beginning and
* end of each value is trimmed.
* @return array of Strings
* @param csvList a string of comma seperated values
*/
public static String[] parseCommaDelimitedList(String csvList) {
String[] result = parseList(csvList, ",");
for (int i = 0; i < result.length; i++) {
result[i] = result[i].trim();
}
return result;
}
/**
* Parses a whitepsace-separated list into an array of Strings
* @return array of Strings
* @param wsvList a string of white space seperated values
*/
public static String[] parseWhitespaceDelimitedList(String wsvList) {
return parseList(wsvList, "\t ");
}
/**
* Parses a list according to the specified delimiter into an
* array of Strings.
* @see java.util.StringTokenizer
* @param list a string of token seperated values
* @param delim the delimiter character(s). Each character in the string is a
* single delimeter.
* @return an array of strings
*/
public static String[] parseList(String list, String delim) {
List result = new ArrayList();
StringTokenizer tokenizer = new StringTokenizer(list, delim);
while (tokenizer.hasMoreTokens()) {
result.add(tokenizer.nextToken());
}
return (String[]) result.toArray(new String[0]);
}
}
Parse Fraction
/*
* Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
*
* Project: OpenSubsystems
*
* $Id: StringUtils.java,v 1.14 2007/02/20 04:08:10 bastafidli Exp $
*
* 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; version 2 of the License.
*
* 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
*/
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;
/**
* Utility methods for String manipulation.
*
* @version $Id: StringUtils.java,v 1.14 2007/02/20 04:08:10 bastafidli Exp $
* @author Peter Satury
* @code.reviewer Miro Halas
* @code.reviewed 1.11 2006/04/29 00:24:18 jlegeny
*/
public final class StringUtils {
// Constants ////////////////////////////////////////////////////////////////
/**
* Constant for assigning
*/
public static final String EMPTY_STRING = "";
/**
* Constant for assigning
*/
public static final String COMMA_STRING = ",";
/**
* Keep the original case of the string;
*/
public static final int CASE_ORIGINAL = 0;
/**
* Convert the string to upper case.
*/
public static final int CASE_TOUPPER = 1;
/**
* Convert the string to lower case.
*/
public static final int CASE_TOLOWER = 2;
// Constructors /////////////////////////////////////////////////////////////
/**
* Private constructor since this class cannot be instantiated
*/
private StringUtils() {
// Do nothing
}
// Public methods ///////////////////////////////////////////////////////////
/**
* Count number of occurences of lookup in text.
*
* @param text -
* text to search in for occurences of lookup
* @param lookup -
* character to count
* @return int - number of occurences of lookup in text.
*/
public static int count(String text, char lookup) {
int iCount = 0;
if (text != null) {
int iIndex = text.indexOf(lookup);
while (iIndex != -1) {
iCount++;
iIndex = text.indexOf(lookup, iIndex + 1);
}
}
return iCount;
}
/**
* Parse textual representation of fraction to a floating point number
*
* @param textToParse -
* in the form "any_text whole_part quotient/divisor any_text"
* @param defaultValue -
* if the test is unparsable, what default value to return
* @param bIgnoreRest -
* if true, this will ignore the rest of the string (any_other_text)
* after the fraction, if false then the whole string is considered
* @return double - number coresponding to the fraction
*/
public static double parseFraction(String textToParse, double defaultValue, boolean bIgnoreRest) {
double parsed = defaultValue;
int iLength;
int iIndex;
int iIndexStart;
int iIndexEnd;
int iNumber;
// lets use "xxxxxxx 123 456 / 789 yyyyy" as example or
// lets use "xxxxxxx 123 / 789 yyyyy" as example
iIndexStart = 0;
iLength = textToParse.length();
if (bIgnoreRest) {
// Skip while not number
while ((iIndexStart < iLength) && (!Character.isDigit(textToParse.charAt(iIndexStart)))) {
iIndexStart++;
}
// We skiped "xxxxxxx", iIndexStart is at "123 456 / 789 yyyyy"
}
// We should be at first digit
if (iIndexStart < iLength) {
// Find end of the number
iIndex = iIndexStart;
while ((iIndex < iLength) && (Character.isDigit(textToParse.charAt(iIndex)))) {
iIndex++;
}
iIndexEnd = iIndex;
// We skiped "123", iIndexStart is at "123 456 / 789 yyyyy"
// iIndexEnd is at " 456 / 789 yyyyy"
if (iIndexStart != iIndexEnd) {
// There was at least some digits
iNumber = Integer.parseInt(textToParse.substring(iIndexStart, iIndexEnd));
// iNumber is 123
// There was at least one digit, now is it whole part or quotient?
// Skip spaces
while ((iIndex < iLength)
&& ((textToParse.charAt(iIndex) == " ") || (textToParse.charAt(iIndex) == "-"))) {
iIndex++;
}
// We skiped "123", iIndex is at "456 / 789 yyyyy"
// Now we have stopped because of 2 things, we either reached end of
// string or we have found something other than space, if it is /
// then it was qoutient, if it is digit, then it was whole part
if (iIndex == iLength) {
// it was a whole part and we are done
parsed = iNumber;
} else {
int iQuotient = 0;
int iDivisor = Integer.MAX_VALUE;
if (Character.isDigit(textToParse.charAt(iIndex))) {
int iWholePart = 0;
// it was a whole part and we continue to look for the quotient
iWholePart = iNumber;
// Find end of the number
iIndexStart = iIndex; // Remember start
while ((iIndex < iLength) && (Character.isDigit(textToParse.charAt(iIndex)))) {
iIndex++;
}
iIndexEnd = iIndex;
// We skiped "456", iStartIndex is at "456 / 789 yyyyy"
// And iIndexEnd is at " / 789 yyyyy"
iQuotient = Integer.parseInt(textToParse.substring(iIndexStart, iIndexEnd));
// iQuotient is 456
// Skip spaces
while ((iIndex < iLength) && (textToParse.charAt(iIndex) == " ")) {
iIndex++;
}
// And iIndex is at "/ 789 yyyyy"
if (textToParse.charAt(iIndex) == "/") {
// It was a quotient and we continue to look for divisor
iIndexStart = iIndex + 1;
while ((iIndexStart < iLength) && (textToParse.charAt(iIndexStart) == " ")) {
iIndexStart++;
}
// And iIndexStart is at "789 yyyyy"
// We should be at next digit
if (iIndexStart < iLength) {
// Find end of the number
iIndex = iIndexStart;
while ((iIndex < iLength) && (Character.isDigit(textToParse.charAt(iIndex)))) {
iIndex++;
}
iIndexEnd = iIndex;
// We skiped "789", iStartIndex is at "789 yyyyy"
// And iIndexEnd is at " yyyyy"
if (iIndexStart != iIndexEnd) {
iDivisor = Integer.parseInt(textToParse.substring(iIndexStart, iIndexEnd));
// iDivisor is 789
if (iDivisor != 0) {
if (iIndexEnd == iLength) {
// And we are at the end of the string
parsed = ((double) (iWholePart))
+ (((double) iQuotient) / ((double) iDivisor));
} else {
if (bIgnoreRest) {
// And we can ignore what is after
parsed = ((double) (iWholePart))
+ (((double) iQuotient) / ((double) iDivisor));
} else {
// there was something else we don"t know what so
// return the default value
}
}
}
} else {
// The divisor is missing, return default value
}
} else {
// The divisor is missing, return default value
}
} else {
// The divisor is missing, return default value
}
} else {
if (textToParse.charAt(iIndex) == "/") {
// And iIndex is at "/ 456 yyyyy"
// It was a quotient and we continue to look for divisor
iQuotient = iNumber;
// iQuotient is 123
iIndexStart = iIndex + 1;
while ((iIndexStart < iLength) && (textToParse.charAt(iIndexStart) == " ")) {
iIndexStart++;
}
// And iIndexStart is at "456 yyyyy"
// We should be at next digit
if (iIndexStart < iLength) {
// Find end of the number
iIndex = iIndexStart;
while ((iIndex < iLength) && (Character.isDigit(textToParse.charAt(iIndex)))) {
iIndex++;
}
iIndexEnd = iIndex;
// We skipped "456", iIndexStart is at "456 yyyyy"
// iIndexEnd is at " yyyyy"
if (iIndexStart != iIndexEnd) {
iDivisor = Integer.parseInt(textToParse.substring(iIndexStart, iIndexEnd));
// iDivisor is 456
if (iDivisor != 0) {
if (iIndexEnd == iLength) {
// And we are at the end of the string
parsed = ((double) iQuotient) / ((double) iDivisor);
} else {
if (bIgnoreRest) {
// And we can ignore what is after
parsed = ((double) iQuotient) / ((double) iDivisor);
} else {
// there was something else we don"t know what so
// return the default value
}
}
}
} else {
// The divisor is missing, return default value
}
} else {
// The divisor is missing, return default value
}
} else {
// It was a whole part and there is something else
if (bIgnoreRest) {
// and we are done
parsed = iNumber;
} else {
// there was something else we don"t know what so
// return the default value
}
}
}
}
}
}
return parsed;
}
/**
* Parse String to array of Strings while treating quoted values as single
* element.
*
* @param strParse -
* String to parse
* @param strDel -
* String deliminer
* @param bAllowSingleQuote -
* single qoutes such as " can be used to group value
* @param bAllowDoubleQuote -
* double quote such as " can be used to group value
* @return String[] - parsed list
* @throws OSSInvalidDataException -
* error during parsing
*/
public static String[] parseQuotedStringToStringArray(String strParse, String strDel,
boolean bAllowSingleQuote, boolean bAllowDoubleQuote) throws Exception {
String[] arrayStrings;
if (strParse == null) {
arrayStrings = null;
} else {
List lstElements = new ArrayList();
int iCurrentIndex = 0;
int iNextIndex;
int iDelLength = strDel.length();
int iParseLength = strParse.length();
while (iCurrentIndex < iParseLength) {
if ((bAllowSingleQuote) && (strParse.charAt(iCurrentIndex) == "\"")) {
// Find next single quote and treat the things in the middle as
// single element
iNextIndex = strParse.indexOf("\"", iCurrentIndex + 1);
if (iNextIndex == -1) {
throw new Exception("Incorrect input. " + strParse
+ " No single quote following the one" + " at location " + iCurrentIndex);
}
lstElements.add(strParse.substring(iCurrentIndex + 1, iNextIndex));
iCurrentIndex = iNextIndex + 1;
if (strParse.substring(iCurrentIndex).startsWith(strDel)) {
iCurrentIndex += iDelLength;
}
} else if ((bAllowDoubleQuote) && (strParse.charAt(iCurrentIndex) == """)) {
// Find next double quote and treat the things in the middle as
// single element
iNextIndex = strParse.indexOf(""", iCurrentIndex + 1);
if (iNextIndex == -1) {
throw new Exception("Incorrect input. " + strParse
+ " No double quote following the one" + " at location " + iCurrentIndex);
}
lstElements.add(strParse.substring(iCurrentIndex + 1, iNextIndex));
iCurrentIndex = iNextIndex + 1;
if (strParse.substring(iCurrentIndex).startsWith(strDel)) {
iCurrentIndex += iDelLength;
}
} else {
// Find next separator and treat the things in the middle as
// single element
iNextIndex = strParse.indexOf(strDel, iCurrentIndex);
if (iNextIndex == -1) {
// No other delimiter found so take the rest of the string
lstElements.add(strParse.substring(iCurrentIndex));
iCurrentIndex = iParseLength;
} else {
lstElements.add(strParse.substring(iCurrentIndex, iNextIndex));
iCurrentIndex = iNextIndex + iDelLength;
}
}
}
arrayStrings = (String[]) lstElements.toArray(new String[lstElements.size()]);
}
return arrayStrings;
}
/**
* Parse String to array of integers.
*
* @param strParse -
* String to parse
* @param strDel -
* String deliminer
* @return int[] - parsed list
* @throws OSSException -
* error during parsing
*/
public static int[] parseStringToIntArray(String strParse, String strDel) throws Exception {
int[] arrayInts;
try {
if (strParse == null) {
arrayInts = null;
} else {
// TODO: Performance: Memory vs speed, here we allocate list and then
// another array, how about just counting the number of elements
// and then allocating array and parsing directly to array without
// the extra list and copying from list to array?
List lstInts = parseStringToList(strParse, strDel);
if (lstInts == null || lstInts.size() < 1) {
arrayInts = null;
} else {
Iterator items;
int iCount;
arrayInts = new int[lstInts.size()];
for (iCount = 0, items = lstInts.iterator(); items.hasNext();) {
arrayInts[iCount++] = Integer.parseInt(((String) items.next()).trim());
}
}
}
} catch (NumberFormatException eExc) {
throw new RuntimeException("Problems with parsing String to array of int.", eExc);
}
return arrayInts;
}
/**
* Parse String to array of Integers.
*
* @param strParse -
* String to parse
* @param strDel -
* String deliminer
* @return Integer[] - parsed list
* @throws OSSException -
* error during parsing
*/
public static Integer[] parseStringToIntegerArray(String strParse, String strDel)
throws Exception {
Integer[] arrayInts;
try {
if (strParse == null) {
arrayInts = null;
} else {
// TODO: Performance: Memory vs speed, here we allocate list and then
// another array, how about just counting the number of elements
// and then allocating array and parsing directly to array without
// the extra list and copying from list to array?
List strInts = parseStringToList(strParse, strDel);
if (strInts == null || strInts.size() < 1) {
arrayInts = null;
} else {
arrayInts = new Integer[strInts.size()];
for (int iCount = 0; iCount < strInts.size(); iCount++) {
arrayInts[iCount] = Integer.valueOf((String) strInts.get(iCount));
}
}
}
} catch (NumberFormatException eExc) {
throw new RuntimeException("Problems with parsing String to array of int.", eExc);
}
return arrayInts;
}
/**
* Parse String to array of Strings.
*
* @param strParse -
* String to parse
* @param strDel -
* String deliminer
* @return String[] - parsed list
*/
public static String[] parseStringToStringArray(String strParse, String strDel) {
String[] arrayStrings;
if (strParse == null) {
arrayStrings = null;
} else {
// TODO: Performance: Memory vs speed, here we allocate list and then
// another array, how about just counting the number of elements
// and then allocating array and parsing directly to array without
// the extra list and copying from list to array?
List lstStrings = parseStringToList(strParse, strDel);
if ((lstStrings == null) || (lstStrings.isEmpty())) {
arrayStrings = null;
} else {
arrayStrings = (String[]) lstStrings.toArray(new String[lstStrings.size()]);
}
}
return arrayStrings;
}
/**
* Parse array of integers to String.
*
* @param arrParse -
* int array to parse
* @param strDel -
* String deliminer
* @return String - parsed array
*/
public static String parseIntArrayToString(int[] arrParse, String strDel) {
StringBuffer strbInts = new StringBuffer();
if ((arrParse != null) && (arrParse.length > 0)) {
for (int iCount = 0; iCount < arrParse.length; iCount++) {
if (iCount > 0) {
strbInts.append(strDel);
}
strbInts.append(arrParse[iCount]);
}
}
return strbInts.toString();
}
/**
* Parse collection of objects to String by calling toString on each element.
*
* @param colObjects -
* collection of data objects to parse
* @param strDel -
* String deliminer
* @return String - parsed array
*/
public static String parseCollectionToString(Collection colObjects, String strDel) {
StringBuffer strbInts = new StringBuffer();
if ((colObjects != null) && (!colObjects.isEmpty())) {
for (Iterator items = colObjects.iterator(); items.hasNext();) {
if (strbInts.length() > 0) {
strbInts.append(strDel);
}
strbInts.append(items.next().toString());
}
}
return strbInts.toString();
}
/**
* Parse String to List.
*
* @param strParse -
* String to parse
* @param strDel -
* String deliminer
* @return List - parsed list of items in the string delimtied by delimiter
*/
public static List parseStringToList(String strParse, String strDel) {
return (List) parseStringToCollection(strParse, strDel, false, CASE_ORIGINAL, null);
}
/**
* Parse String to ANY collection you specify and trim each item.
*
* @param strParse -
* String to parse
* @param strDel -
* String deliminer
* @param container -
* if specified then it will be filled with items (it WILL NOT be
* emptied first). If this is null, the default collection will be
* allocated. This allows you here to pass list or set so this method
* is more flexible.
* @param bTrim -
* should it be trimmed or not
* @param iConvertCase -
* how to convert the case of the string - one of the CASE_XXX
* costants
* @return Collection - parsed collection, if container was specified, the
* same object will be returned. If it was not specified default
* object will be returned. If strParse was not null, then this will
* be not null.
*/
public static Collection parseStringToCollection(String strParse, String strDel, boolean bTrim,
int iConvertCase, Collection container) {
Collection colReturn;
if (strParse == null || strParse.length() < 1) {
if (container != null) {
colReturn = container;
} else {
colReturn = null;
}
} else {
// TODO: Performance: StringTokenizer is considered to be slow
// because it creates lots of objects internally, consider replacing
// this with String.indexOf(delimiter)
StringTokenizer strTokParse = new StringTokenizer(strParse, strDel);
String strTemp;
if (container == null) {
// This has to be List since parseStringToList requires it
colReturn = new ArrayList();
} else {
container.clear();
colReturn = container;
}
if (strParse.startsWith(strDel)) {
// If the string starts with the delimiter, tokenizer would skip it
// but we have to have empty element in front
colReturn.add("");
}
while (strTokParse.hasMoreTokens()) {
strTemp = (String) strTokParse.nextToken();
if (bTrim) {
strTemp = strTemp.trim();
}
switch (iConvertCase) {
case (CASE_ORIGINAL): {
// do nothing
break;
}
case (CASE_TOUPPER): {
strTemp = strTemp.toUpperCase();
break;
}
case (CASE_TOLOWER): {
strTemp = strTemp.toLowerCase();
break;
}
default: {
assert false : "Incorrect case specification.";
}
}
colReturn.add(strTemp);
}
}
return colReturn;
}
/**
* Method to limit String length for display and add "..." to the end
*
* @param limitLength -
* limit of length
* @param strValue -
* String to limit
* @return String - limited String
*/
public static String limitStringLength(int limitLength, String strValue) {
StringBuffer sbReturn = new StringBuffer();
if ((limitLength > 0) && (strValue.length() > limitLength)) {
// If limit length is lower then 5 we will do just exact substring
if (limitLength < 5) {
sbReturn.append(strValue.substring(0, limitLength));
}
// If limit length is lower then 15 and higher then 4 we will
// return substring of (limit - 3) and "..."
else if (limitLength < 15) {
sbReturn.append(strValue.substring(0, limitLength - 3));
sbReturn.append("...");
}
// If limit length is higher then 15 we will try to find
// some space " " near before limit and cut string there
else {
// if we will not find " " near before limit
// we will return substring of (limit - 3) and "..."
if ((strValue.indexOf(" ", limitLength - 12) > (limitLength - 4))
|| (strValue.indexOf(" ", limitLength - 12) < 0)) {
sbReturn.append(strValue.substring(0, limitLength - 3));
sbReturn.append("...");
}
// if we will find " " near before limit
// we will return substring until " " and " ..."
else {
sbReturn.append(strValue.substring(0, strValue.indexOf(" ", limitLength - 12)));
sbReturn.append(" ...");
}
}
} else {
sbReturn.append(strValue);
}
return sbReturn.toString();
}
/**
* Method to remove comma from start and from end of the string for examples
* to use it as parameter to SQL IN operator
*
* @param strToRemoveFrom -
* String to remove comma from
* @return String - string with removed commas from the start and end of the
* string
*/
public static String removeComma(String strToRemoveFrom) {
// we have to remove comma from start and from end of the string
// because then it can be used for SQL IN operator
if (strToRemoveFrom.length() > 2) {
strToRemoveFrom = strToRemoveFrom.substring(1, strToRemoveFrom.length() - 1);
} else {
strToRemoveFrom = "";
}
return strToRemoveFrom;
}
/**
* Concat all the specified strings to a single one
*
* @param strings -
* strings to concat, all null and empty ones will be ignored
* @param separator -
* separator to put in between the string elements
* @param quote -
* quote string to put around string elements, if null nothing will
* be put around them
* @return String with concatenated inputs
*/
public static String concat(String[] strings, String separator, String quote) {
StringBuffer output = new StringBuffer();
if (strings != null) {
int iIndex;
boolean bSeparator;
boolean bQuote;
bSeparator = (separator != null) && (separator.length() > 0);
bQuote = (quote != null) && (quote.length() > 0);
for (iIndex = 0; iIndex < strings.length; iIndex++) {
if ((strings[iIndex] != null) && (strings[iIndex].length() > 0)) {
if ((output.length() > 0) && (bSeparator)) {
output.append(separator);
}
if (bQuote) {
output.append(quote);
}
output.append(strings[iIndex]);
if (bQuote) {
output.append(quote);
}
}
}
}
return output.toString();
}
/**
* Test if any element in the container contains given string.
*
* @param container -
* container of which elements will be searched to see if they
* contains given text
* @param strSearch -
* text to search in the elements of specified container
* @return boolean - true if any of the elements in container contains given
* text
*/
public static boolean isContained(Collection container, String strSearch) {
boolean bReturn = false;
if ((container != null) && (!container.isEmpty())) {
for (Iterator itrElements = container.iterator(); (itrElements.hasNext() && (!bReturn));) {
if (((String) itrElements.next()).indexOf(strSearch) != -1) {
bReturn = true;
}
}
}
return bReturn;
}
/**
* Test if given string contains any element in the container.
*
* @param container -
* container of which elements will be searched to see if they are
* contained within given text
* @param strSearch -
* text to search in for the elements of specified container
* @return boolean - true if the search text contains any of the elements in
* container
*/
public static boolean contains(Collection container, String strSearch) {
boolean bReturn = false;
if ((container != null) && (!container.isEmpty())) {
for (Iterator itrElements = container.iterator(); (itrElements.hasNext() && (!bReturn));) {
if (strSearch.indexOf((String) itrElements.next()) != -1) {
bReturn = true;
}
}
}
return bReturn;
}
/**
* Method return boolean result if particular substring is contained within
* the list of substrings separated by a separator.
*
* @param strSearchIn -
* string of all substrings separated by separator to search in
* @param strSearchFor -
* string that will be search for
* @param strSeparator -
* item separator
* @return boolean - true if it contains the ID, false otherwise
*/
public static boolean containsInSeparatedString(String strSearchIn, String strSearchFor,
String strSeparator) {
boolean bReturn = false;
StringBuffer sbInputString = new StringBuffer();
StringBuffer sbSearchString = new StringBuffer();
if (strSearchIn.length() > 0) {
// add separator at the beginning and end of the input string
sbInputString.append(strSeparator);
sbInputString.append(strSearchIn);
sbInputString.append(strSeparator);
// add separator at the beginning and end of the search string
sbSearchString.append(strSeparator);
sbSearchString.append(strSearchFor);
sbSearchString.append(strSeparator);
// search for particular ID
if (sbInputString.indexOf(sbSearchString.toString()) != -1) {
bReturn = true;
}
}
return bReturn;
}
}
Parse String to array of Strings while treating quoted values as single element
/*
* Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
*
* Project: OpenSubsystems
*
* $Id: StringUtils.java,v 1.14 2007/02/20 04:08:10 bastafidli Exp $
*
* 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; version 2 of the License.
*
* 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
*/
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;
/**
* Utility methods for String manipulation.
*
* @version $Id: StringUtils.java,v 1.14 2007/02/20 04:08:10 bastafidli Exp $
* @author Peter Satury
* @code.reviewer Miro Halas
* @code.reviewed 1.11 2006/04/29 00:24:18 jlegeny
*/
public final class StringUtils {
// Constants ////////////////////////////////////////////////////////////////
/**
* Constant for assigning
*/
public static final String EMPTY_STRING = "";
/**
* Constant for assigning
*/
public static final String COMMA_STRING = ",";
/**
* Keep the original case of the string;
*/
public static final int CASE_ORIGINAL = 0;
/**
* Convert the string to upper case.
*/
public static final int CASE_TOUPPER = 1;
/**
* Convert the string to lower case.
*/
public static final int CASE_TOLOWER = 2;
// Constructors /////////////////////////////////////////////////////////////
/**
* Private constructor since this class cannot be instantiated
*/
private StringUtils() {
// Do nothing
}
// Public methods ///////////////////////////////////////////////////////////
/**
* Count number of occurences of lookup in text.
*
* @param text -
* text to search in for occurences of lookup
* @param lookup -
* character to count
* @return int - number of occurences of lookup in text.
*/
public static int count(String text, char lookup) {
int iCount = 0;
if (text != null) {
int iIndex = text.indexOf(lookup);
while (iIndex != -1) {
iCount++;
iIndex = text.indexOf(lookup, iIndex + 1);
}
}
return iCount;
}
/**
* Parse textual representation of fraction to a floating point number
*
* @param textToParse -
* in the form "any_text whole_part quotient/divisor any_text"
* @param defaultValue -
* if the test is unparsable, what default value to return
* @param bIgnoreRest -
* if true, this will ignore the rest of the string (any_other_text)
* after the fraction, if false then the whole string is considered
* @return double - number coresponding to the fraction
*/
public static double parseFraction(String textToParse, double defaultValue, boolean bIgnoreRest) {
double parsed = defaultValue;
int iLength;
int iIndex;
int iIndexStart;
int iIndexEnd;
int iNumber;
// lets use "xxxxxxx 123 456 / 789 yyyyy" as example or
// lets use "xxxxxxx 123 / 789 yyyyy" as example
iIndexStart = 0;
iLength = textToParse.length();
if (bIgnoreRest) {
// Skip while not number
while ((iIndexStart < iLength) && (!Character.isDigit(textToParse.charAt(iIndexStart)))) {
iIndexStart++;
}
// We skiped "xxxxxxx", iIndexStart is at "123 456 / 789 yyyyy"
}
// We should be at first digit
if (iIndexStart < iLength) {
// Find end of the number
iIndex = iIndexStart;
while ((iIndex < iLength) && (Character.isDigit(textToParse.charAt(iIndex)))) {
iIndex++;
}
iIndexEnd = iIndex;
// We skiped "123", iIndexStart is at "123 456 / 789 yyyyy"
// iIndexEnd is at " 456 / 789 yyyyy"
if (iIndexStart != iIndexEnd) {
// There was at least some digits
iNumber = Integer.parseInt(textToParse.substring(iIndexStart, iIndexEnd));
// iNumber is 123
// There was at least one digit, now is it whole part or quotient?
// Skip spaces
while ((iIndex < iLength)
&& ((textToParse.charAt(iIndex) == " ") || (textToParse.charAt(iIndex) == "-"))) {
iIndex++;
}
// We skiped "123", iIndex is at "456 / 789 yyyyy"
// Now we have stopped because of 2 things, we either reached end of
// string or we have found something other than space, if it is /
// then it was qoutient, if it is digit, then it was whole part
if (iIndex == iLength) {
// it was a whole part and we are done
parsed = iNumber;
} else {
int iQuotient = 0;
int iDivisor = Integer.MAX_VALUE;
if (Character.isDigit(textToParse.charAt(iIndex))) {
int iWholePart = 0;
// it was a whole part and we continue to look for the quotient
iWholePart = iNumber;
// Find end of the number
iIndexStart = iIndex; // Remember start
while ((iIndex < iLength) && (Character.isDigit(textToParse.charAt(iIndex)))) {
iIndex++;
}
iIndexEnd = iIndex;
// We skiped "456", iStartIndex is at "456 / 789 yyyyy"
// And iIndexEnd is at " / 789 yyyyy"
iQuotient = Integer.parseInt(textToParse.substring(iIndexStart, iIndexEnd));
// iQuotient is 456
// Skip spaces
while ((iIndex < iLength) && (textToParse.charAt(iIndex) == " ")) {
iIndex++;
}
// And iIndex is at "/ 789 yyyyy"
if (textToParse.charAt(iIndex) == "/") {
// It was a quotient and we continue to look for divisor
iIndexStart = iIndex + 1;
while ((iIndexStart < iLength) && (textToParse.charAt(iIndexStart) == " ")) {
iIndexStart++;
}
// And iIndexStart is at "789 yyyyy"
// We should be at next digit
if (iIndexStart < iLength) {
// Find end of the number
iIndex = iIndexStart;
while ((iIndex < iLength) && (Character.isDigit(textToParse.charAt(iIndex)))) {
iIndex++;
}
iIndexEnd = iIndex;
// We skiped "789", iStartIndex is at "789 yyyyy"
// And iIndexEnd is at " yyyyy"
if (iIndexStart != iIndexEnd) {
iDivisor = Integer.parseInt(textToParse.substring(iIndexStart, iIndexEnd));
// iDivisor is 789
if (iDivisor != 0) {
if (iIndexEnd == iLength) {
// And we are at the end of the string
parsed = ((double) (iWholePart))
+ (((double) iQuotient) / ((double) iDivisor));
} else {
if (bIgnoreRest) {
// And we can ignore what is after
parsed = ((double) (iWholePart))
+ (((double) iQuotient) / ((double) iDivisor));
} else {
// there was something else we don"t know what so
// return the default value
}
}
}
} else {
// The divisor is missing, return default value
}
} else {
// The divisor is missing, return default value
}
} else {
// The divisor is missing, return default value
}
} else {
if (textToParse.charAt(iIndex) == "/") {
// And iIndex is at "/ 456 yyyyy"
// It was a quotient and we continue to look for divisor
iQuotient = iNumber;
// iQuotient is 123
iIndexStart = iIndex + 1;
while ((iIndexStart < iLength) && (textToParse.charAt(iIndexStart) == " ")) {
iIndexStart++;
}
// And iIndexStart is at "456 yyyyy"
// We should be at next digit
if (iIndexStart < iLength) {
// Find end of the number
iIndex = iIndexStart;
while ((iIndex < iLength) && (Character.isDigit(textToParse.charAt(iIndex)))) {
iIndex++;
}
iIndexEnd = iIndex;
// We skipped "456", iIndexStart is at "456 yyyyy"
// iIndexEnd is at " yyyyy"
if (iIndexStart != iIndexEnd) {
iDivisor = Integer.parseInt(textToParse.substring(iIndexStart, iIndexEnd));
// iDivisor is 456
if (iDivisor != 0) {
if (iIndexEnd == iLength) {
// And we are at the end of the string
parsed = ((double) iQuotient) / ((double) iDivisor);
} else {
if (bIgnoreRest) {
// And we can ignore what is after
parsed = ((double) iQuotient) / ((double) iDivisor);
} else {
// there was something else we don"t know what so
// return the default value
}
}
}
} else {
// The divisor is missing, return default value
}
} else {
// The divisor is missing, return default value
}
} else {
// It was a whole part and there is something else
if (bIgnoreRest) {
// and we are done
parsed = iNumber;
} else {
// there was something else we don"t know what so
// return the default value
}
}
}
}
}
}
return parsed;
}
/**
* Parse String to array of Strings while treating quoted values as single
* element.
*
* @param strParse -
* String to parse
* @param strDel -
* String deliminer
* @param bAllowSingleQuote -
* single qoutes such as " can be used to group value
* @param bAllowDoubleQuote -
* double quote such as " can be used to group value
* @return String[] - parsed list
* @throws OSSInvalidDataException -
* error during parsing
*/
public static String[] parseQuotedStringToStringArray(String strParse, String strDel,
boolean bAllowSingleQuote, boolean bAllowDoubleQuote) throws Exception {
String[] arrayStrings;
if (strParse == null) {
arrayStrings = null;
} else {
List lstElements = new ArrayList();
int iCurrentIndex = 0;
int iNextIndex;
int iDelLength = strDel.length();
int iParseLength = strParse.length();
while (iCurrentIndex < iParseLength) {
if ((bAllowSingleQuote) && (strParse.charAt(iCurrentIndex) == "\"")) {
// Find next single quote and treat the things in the middle as
// single element
iNextIndex = strParse.indexOf("\"", iCurrentIndex + 1);
if (iNextIndex == -1) {
throw new Exception("Incorrect input. " + strParse
+ " No single quote following the one" + " at location " + iCurrentIndex);
}
lstElements.add(strParse.substring(iCurrentIndex + 1, iNextIndex));
iCurrentIndex = iNextIndex + 1;
if (strParse.substring(iCurrentIndex).startsWith(strDel)) {
iCurrentIndex += iDelLength;
}
} else if ((bAllowDoubleQuote) && (strParse.charAt(iCurrentIndex) == """)) {
// Find next double quote and treat the things in the middle as
// single element
iNextIndex = strParse.indexOf(""", iCurrentIndex + 1);
if (iNextIndex == -1) {
throw new Exception("Incorrect input. " + strParse
+ " No double quote following the one" + " at location " + iCurrentIndex);
}
lstElements.add(strParse.substring(iCurrentIndex + 1, iNextIndex));
iCurrentIndex = iNextIndex + 1;
if (strParse.substring(iCurrentIndex).startsWith(strDel)) {
iCurrentIndex += iDelLength;
}
} else {
// Find next separator and treat the things in the middle as
// single element
iNextIndex = strParse.indexOf(strDel, iCurrentIndex);
if (iNextIndex == -1) {
// No other delimiter found so take the rest of the string
lstElements.add(strParse.substring(iCurrentIndex));
iCurrentIndex = iParseLength;
} else {
lstElements.add(strParse.substring(iCurrentIndex, iNextIndex));
iCurrentIndex = iNextIndex + iDelLength;
}
}
}
arrayStrings = (String[]) lstElements.toArray(new String[lstElements.size()]);
}
return arrayStrings;
}
/**
* Parse String to array of integers.
*
* @param strParse -
* String to parse
* @param strDel -
* String deliminer
* @return int[] - parsed list
* @throws OSSException -
* error during parsing
*/
public static int[] parseStringToIntArray(String strParse, String strDel) throws Exception {
int[] arrayInts;
try {
if (strParse == null) {
arrayInts = null;
} else {
// TODO: Performance: Memory vs speed, here we allocate list and then
// another array, how about just counting the number of elements
// and then allocating array and parsing directly to array without
// the extra list and copying from list to array?
List lstInts = parseStringToList(strParse, strDel);
if (lstInts == null || lstInts.size() < 1) {
arrayInts = null;
} else {
Iterator items;
int iCount;
arrayInts = new int[lstInts.size()];
for (iCount = 0, items = lstInts.iterator(); items.hasNext();) {
arrayInts[iCount++] = Integer.parseInt(((String) items.next()).trim());
}
}
}
} catch (NumberFormatException eExc) {
throw new RuntimeException("Problems with parsing String to array of int.", eExc);
}
return arrayInts;
}
/**
* Parse String to array of Integers.
*
* @param strParse -
* String to parse
* @param strDel -
* String deliminer
* @return Integer[] - parsed list
* @throws OSSException -
* error during parsing
*/
public static Integer[] parseStringToIntegerArray(String strParse, String strDel)
throws Exception {
Integer[] arrayInts;
try {
if (strParse == null) {
arrayInts = null;
} else {
// TODO: Performance: Memory vs speed, here we allocate list and then
// another array, how about just counting the number of elements
// and then allocating array and parsing directly to array without
// the extra list and copying from list to array?
List strInts = parseStringToList(strParse, strDel);
if (strInts == null || strInts.size() < 1) {
arrayInts = null;
} else {
arrayInts = new Integer[strInts.size()];
for (int iCount = 0; iCount < strInts.size(); iCount++) {
arrayInts[iCount] = Integer.valueOf((String) strInts.get(iCount));
}
}
}
} catch (NumberFormatException eExc) {
throw new RuntimeException("Problems with parsing String to array of int.", eExc);
}
return arrayInts;
}
/**
* Parse String to array of Strings.
*
* @param strParse -
* String to parse
* @param strDel -
* String deliminer
* @return String[] - parsed list
*/
public static String[] parseStringToStringArray(String strParse, String strDel) {
String[] arrayStrings;
if (strParse == null) {
arrayStrings = null;
} else {
// TODO: Performance: Memory vs speed, here we allocate list and then
// another array, how about just counting the number of elements
// and then allocating array and parsing directly to array without
// the extra list and copying from list to array?
List lstStrings = parseStringToList(strParse, strDel);
if ((lstStrings == null) || (lstStrings.isEmpty())) {
arrayStrings = null;
} else {
arrayStrings = (String[]) lstStrings.toArray(new String[lstStrings.size()]);
}
}
return arrayStrings;
}
/**
* Parse array of integers to String.
*
* @param arrParse -
* int array to parse
* @param strDel -
* String deliminer
* @return String - parsed array
*/
public static String parseIntArrayToString(int[] arrParse, String strDel) {
StringBuffer strbInts = new StringBuffer();
if ((arrParse != null) && (arrParse.length > 0)) {
for (int iCount = 0; iCount < arrParse.length; iCount++) {
if (iCount > 0) {
strbInts.append(strDel);
}
strbInts.append(arrParse[iCount]);
}
}
return strbInts.toString();
}
/**
* Parse collection of objects to String by calling toString on each element.
*
* @param colObjects -
* collection of data objects to parse
* @param strDel -
* String deliminer
* @return String - parsed array
*/
public static String parseCollectionToString(Collection colObjects, String strDel) {
StringBuffer strbInts = new StringBuffer();
if ((colObjects != null) && (!colObjects.isEmpty())) {
for (Iterator items = colObjects.iterator(); items.hasNext();) {
if (strbInts.length() > 0) {
strbInts.append(strDel);
}
strbInts.append(items.next().toString());
}
}
return strbInts.toString();
}
/**
* Parse String to List.
*
* @param strParse -
* String to parse
* @param strDel -
* String deliminer
* @return List - parsed list of items in the string delimtied by delimiter
*/
public static List parseStringToList(String strParse, String strDel) {
return (List) parseStringToCollection(strParse, strDel, false, CASE_ORIGINAL, null);
}
/**
* Parse String to ANY collection you specify and trim each item.
*
* @param strParse -
* String to parse
* @param strDel -
* String deliminer
* @param container -
* if specified then it will be filled with items (it WILL NOT be
* emptied first). If this is null, the default collection will be
* allocated. This allows you here to pass list or set so this method
* is more flexible.
* @param bTrim -
* should it be trimmed or not
* @param iConvertCase -
* how to convert the case of the string - one of the CASE_XXX
* costants
* @return Collection - parsed collection, if container was specified, the
* same object will be returned. If it was not specified default
* object will be returned. If strParse was not null, then this will
* be not null.
*/
public static Collection parseStringToCollection(String strParse, String strDel, boolean bTrim,
int iConvertCase, Collection container) {
Collection colReturn;
if (strParse == null || strParse.length() < 1) {
if (container != null) {
colReturn = container;
} else {
colReturn = null;
}
} else {
// TODO: Performance: StringTokenizer is considered to be slow
// because it creates lots of objects internally, consider replacing
// this with String.indexOf(delimiter)
StringTokenizer strTokParse = new StringTokenizer(strParse, strDel);
String strTemp;
if (container == null) {
// This has to be List since parseStringToList requires it
colReturn = new ArrayList();
} else {
container.clear();
colReturn = container;
}
if (strParse.startsWith(strDel)) {
// If the string starts with the delimiter, tokenizer would skip it
// but we have to have empty element in front
colReturn.add("");
}
while (strTokParse.hasMoreTokens()) {
strTemp = (String) strTokParse.nextToken();
if (bTrim) {
strTemp = strTemp.trim();
}
switch (iConvertCase) {
case (CASE_ORIGINAL): {
// do nothing
break;
}
case (CASE_TOUPPER): {
strTemp = strTemp.toUpperCase();
break;
}
case (CASE_TOLOWER): {
strTemp = strTemp.toLowerCase();
break;
}
default: {
assert false : "Incorrect case specification.";
}
}
colReturn.add(strTemp);
}
}
return colReturn;
}
/**
* Method to limit String length for display and add "..." to the end
*
* @param limitLength -
* limit of length
* @param strValue -
* String to limit
* @return String - limited String
*/
public static String limitStringLength(int limitLength, String strValue) {
StringBuffer sbReturn = new StringBuffer();
if ((limitLength > 0) && (strValue.length() > limitLength)) {
// If limit length is lower then 5 we will do just exact substring
if (limitLength < 5) {
sbReturn.append(strValue.substring(0, limitLength));
}
// If limit length is lower then 15 and higher then 4 we will
// return substring of (limit - 3) and "..."
else if (limitLength < 15) {
sbReturn.append(strValue.substring(0, limitLength - 3));
sbReturn.append("...");
}
// If limit length is higher then 15 we will try to find
// some space " " near before limit and cut string there
else {
// if we will not find " " near before limit
// we will return substring of (limit - 3) and "..."
if ((strValue.indexOf(" ", limitLength - 12) > (limitLength - 4))
|| (strValue.indexOf(" ", limitLength - 12) < 0)) {
sbReturn.append(strValue.substring(0, limitLength - 3));
sbReturn.append("...");
}
// if we will find " " near before limit
// we will return substring until " " and " ..."
else {
sbReturn.append(strValue.substring(0, strValue.indexOf(" ", limitLength - 12)));
sbReturn.append(" ...");
}
}
} else {
sbReturn.append(strValue);
}
return sbReturn.toString();
}
/**
* Method to remove comma from start and from end of the string for examples
* to use it as parameter to SQL IN operator
*
* @param strToRemoveFrom -
* String to remove comma from
* @return String - string with removed commas from the start and end of the
* string
*/
public static String removeComma(String strToRemoveFrom) {
// we have to remove comma from start and from end of the string
// because then it can be used for SQL IN operator
if (strToRemoveFrom.length() > 2) {
strToRemoveFrom = strToRemoveFrom.substring(1, strToRemoveFrom.length() - 1);
} else {
strToRemoveFrom = "";
}
return strToRemoveFrom;
}
/**
* Concat all the specified strings to a single one
*
* @param strings -
* strings to concat, all null and empty ones will be ignored
* @param separator -
* separator to put in between the string elements
* @param quote -
* quote string to put around string elements, if null nothing will
* be put around them
* @return String with concatenated inputs
*/
public static String concat(String[] strings, String separator, String quote) {
StringBuffer output = new StringBuffer();
if (strings != null) {
int iIndex;
boolean bSeparator;
boolean bQuote;
bSeparator = (separator != null) && (separator.length() > 0);
bQuote = (quote != null) && (quote.length() > 0);
for (iIndex = 0; iIndex < strings.length; iIndex++) {
if ((strings[iIndex] != null) && (strings[iIndex].length() > 0)) {
if ((output.length() > 0) && (bSeparator)) {
output.append(separator);
}
if (bQuote) {
output.append(quote);
}
output.append(strings[iIndex]);
if (bQuote) {
output.append(quote);
}
}
}
}
return output.toString();
}
/**
* Test if any element in the container contains given string.
*
* @param container -
* container of which elements will be searched to see if they
* contains given text
* @param strSearch -
* text to search in the elements of specified container
* @return boolean - true if any of the elements in container contains given
* text
*/
public static boolean isContained(Collection container, String strSearch) {
boolean bReturn = false;
if ((container != null) && (!container.isEmpty())) {
for (Iterator itrElements = container.iterator(); (itrElements.hasNext() && (!bReturn));) {
if (((String) itrElements.next()).indexOf(strSearch) != -1) {
bReturn = true;
}
}
}
return bReturn;
}
/**
* Test if given string contains any element in the container.
*
* @param container -
* container of which elements will be searched to see if they are
* contained within given text
* @param strSearch -
* text to search in for the elements of specified container
* @return boolean - true if the search text contains any of the elements in
* container
*/
public static boolean contains(Collection container, String strSearch) {
boolean bReturn = false;
if ((container != null) && (!container.isEmpty())) {
for (Iterator itrElements = container.iterator(); (itrElements.hasNext() && (!bReturn));) {
if (strSearch.indexOf((String) itrElements.next()) != -1) {
bReturn = true;
}
}
}
return bReturn;
}
/**
* Method return boolean result if particular substring is contained within
* the list of substrings separated by a separator.
*
* @param strSearchIn -
* string of all substrings separated by separator to search in
* @param strSearchFor -
* string that will be search for
* @param strSeparator -
* item separator
* @return boolean - true if it contains the ID, false otherwise
*/
public static boolean containsInSeparatedString(String strSearchIn, String strSearchFor,
String strSeparator) {
boolean bReturn = false;
StringBuffer sbInputString = new StringBuffer();
StringBuffer sbSearchString = new StringBuffer();
if (strSearchIn.length() > 0) {
// add separator at the beginning and end of the input string
sbInputString.append(strSeparator);
sbInputString.append(strSearchIn);
sbInputString.append(strSeparator);
// add separator at the beginning and end of the search string
sbSearchString.append(strSeparator);
sbSearchString.append(strSearchFor);
sbSearchString.append(strSeparator);
// search for particular ID
if (sbInputString.indexOf(sbSearchString.toString()) != -1) {
bReturn = true;
}
}
return bReturn;
}
}
Parsing primitives from String"s without creating any objects
/*
* Copyright (c) 1998 - 2005 Versant Corporation
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Versant Corporation - initial API and implementation
*/
/**
* Static utility methods for parsing primitives from String"s without
* creating any objects.
*/
public class FastParser {
/**
* Parse the int at index from value. The int is assumed to run until
* the end of the String.
*/
public static int parseInt(String value, int index) {
char c = value.charAt(index++);
int ans;
if (c == "-") ans = - (value.charAt(index++) - "0");
else ans = c - "0";
int n = value.length();
for (; index < n; ) {
ans = ans * 10 + (value.charAt(index++) - "0");
}
return ans;
}
/**
* Parse the long at index from value. The long is assumed to run until
* the end of the String.
*/
public static long parseLong(String value, int index) {
char c = value.charAt(index++);
long ans;
if (c == "-") ans = - (value.charAt(index++) - "0");
else ans = c - "0";
int n = value.length();
for (; index < n; ) {
ans = ans * 10 + (value.charAt(index++) - "0");
}
return ans;
}
}
Returns true if the argument contains a number
/*
JSPWiki - a JSP-based WikiWiki clone.
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
import java.security.SecureRandom;
import java.util.Random;
public class StringUtils
{
/**
* Returns true, if the argument contains a number, otherwise false.
* In a quick test this is roughly the same speed as Integer.parseInt()
* if the argument is a number, and roughly ten times the speed, if
* the argument is NOT a number.
*
* @since 2.4
* @param s String to check
* @return True, if s represents a number. False otherwise.
*/
public static boolean isNumber( String s )
{
if( s == null ) return false;
if( s.length() > 1 && s.charAt(0) == "-" )
s = s.substring(1);
for( int i = 0; i < s.length(); i++ )
{
if( !Character.isDigit(s.charAt(i)) )
return false;
}
return true;
}
}