All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ilex.util.TextUtil Maven / Gradle / Ivy

Go to download

A collection of software for aggregatting and processing environmental data such as from NOAA GOES satellites.

The newest version!
/*
*  $Id: TextUtil.java,v 1.5 2020/01/31 19:42:07 mmaloney Exp $
*
*  $Source: /home/cvs/repo/opendcs/src/ilex/util/TextUtil.java,v $
*
*  $State: Exp $
*
*  $Log: TextUtil.java,v $
*  Revision 1.5  2020/01/31 19:42:07  mmaloney
*  Added intEqual method to compare Integer objects allowing for null.
*
*  Revision 1.4  2019/12/11 14:31:52  mmaloney
*  Added splitQuoted method.
*
*  Revision 1.3  2019/06/10 19:35:05  mmaloney
*  Added dateEqual method.
*
*  Revision 1.2  2019/03/28 13:00:11  mmaloney
*  Added strEqualNE - consider null string the same as blank string.
*
*  Revision 1.1.1.1  2014/05/19 15:28:59  mmaloney
*  OPENDCS 6.0 Initial Checkin
*
*  Revision 1.4  2012/11/12 19:14:05  mmaloney
*  CWMS uses 't' to mean true.
*
*  Revision 1.3  2011/01/17 16:35:23  mmaloney
*  Added getFirstLine method.
*
*  Revision 1.2  2009/10/08 17:15:52  mjmaloney
*  fixed comment
*
*  Revision 1.1  2008/04/04 18:21:10  cvs
*  Added legacy code to repository
*
*  Revision 1.20  2007/09/29 21:58:41  mmaloney
*  dev
*
*  Revision 1.19  2007/05/25 14:07:13  mmaloney
*  dev
*
*  Revision 1.18  2006/12/23 18:16:05  mmaloney
*  dev
*
*  Revision 1.17  2004/08/30 14:50:32  mjmaloney
*  Javadocs
*
*  Revision 1.16  2004/06/21 13:31:53  mjmaloney
*  Added scanAssign method.
*
*  Revision 1.15  2004/05/21 18:28:04  mjmaloney
*  Added startsWithIgnoreCase method.
*
*  Revision 1.14  2004/04/02 18:58:17  mjmaloney
*  Created.
*
*  Revision 1.13  2003/12/15 15:21:14  mjmaloney
*  Improvements to support LRGS Config Editor & EDL files.
*
*  Revision 1.12  2003/11/15 20:36:43  mjmaloney
*  Added compareIgnoreCase method that tolerates null arguments.
*
*  Revision 1.11  2003/09/02 14:37:28  mjmaloney
*  Added TeeLogger. Added more control on msg format to Logger.
*  Added TextUtil.fixedLengthFields method.
*
*  Revision 1.10  2002/10/29 00:57:13  mjmaloney
*  Added right/left justify functions to TextUtil.
*
*  Revision 1.9  2002/08/29 05:59:56  chris
*  Added the split() method; also added some tests.
*
*  Revision 1.8  2001/11/09 14:35:22  mike
*  dev
*
*  Revision 1.7  2001/10/05 17:49:59  mike
*  Added HumanReadableFormatter
*
*  Revision 1.6  2001/03/19 03:11:57  mike
*  *** empty log message ***
*
*  Revision 1.5  2000/12/31 14:14:20  mike
*  Added containsNoWhiteSpace method.
*
*  Revision 1.4  2000/12/29 02:50:05  mike
*  dev
*
*  Revision 1.3  2000/12/27 22:03:54  mike
*  Added isAllWhiteSpace
*
*  Revision 1.2  2000/12/24 02:41:07  mike
*  dev
*
*  Revision 1.1  2000/01/07 23:04:51  mike
*  Created
*
*
*/
package ilex.util;

import java.util.ArrayList;
import java.util.Date;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.text.*;

/**
* This class contains a set of static methods that supplement the methods
* in the java.text package.
*/
public class TextUtil
{
	/**
	* Skip white space in the string starting at the specified parse
	* position. Leave 'pp' updated to the first non-whitespace character.
	* @param s the input string
	* @param pp the ParsePosition object
	* @return true if a non-whitespace character is found, false if the
	* end of the string is reached.
	*/
	public static boolean skipWhitespace( String s, ParsePosition pp )
	{
		int i = pp.getIndex();
		for(; i < s.length() && Character.isWhitespace(s.charAt(i)); i++);

		pp.setIndex(i);
		return i < s.length() ? true : false;
	}

	/**
	* Returns true if the passed string is empty or contains only white space.
	* @param s the input string
	* @return true if string is all whitespace
	*/
	public static boolean isAllWhitespace( String s )
	{
		for(int i = 0; i < s.length(); i++)
			if (!Character.isWhitespace(s.charAt(i)))
				return false;
		return true;
	}

	/**
	* Returns true if the passed string is non-blank and contains no white-
	* space.
	* This is appropriate for strings used as identifiers (e.g. filenames,
	* variable-names etc.)
	* @param s the input string
	* @return true if string has no whitespace
	*/
	public static boolean containsNoWhitespace( String s )
	{
		int len = s.length();
		if (len == 0)
			return false;  // empty string

		for(int i = 0; i < len; i++)
			if (Character.isWhitespace(s.charAt(i)))
				return false;

		return true;
	}

	/**
	* Collapses contiguouse whitespace in the input string into a single
	* space character in the output. This is useful for formatting paragraphs
	* that were read from a file that may contain extraneous newlines and
	* indentation, for example a long descripting field that was indented
	* in an XML file.
	* @param in the input string
	* @return string with all whitespace collapsed to single space
	*/
	public static String collapseWhitespace( String in )
	{
		in = in.trim();
		StringBuffer sb = new StringBuffer(in.length());
		boolean ws = false;

		for(int i=0; i= width)
			return in;

		int lpad = (width - inlen) / 2;
		int rpad = width - inlen - lpad;

		StringBuffer sb = new StringBuffer();
		for(int i=0; i  split("a:b:c", ':')  yields  "a", "b", "c"
	* split("a:b:", ':')  yields  "a", "b", ""
	* split(":b:c", ':')  yields  "", "b", "c"
	* split("a::c", ':')  yields  "a", "", "c"
	* split("", ':')      yields  ""
* @param s the input string * @param c the delimiter * @return results */ public static String[] split( String s, char c ) { Vector v = new Vector(); int i = 0; int next; while ((next = s.indexOf(c, i)) != -1) { v.add(s.substring(i, next)); i = next + 1; } v.add(s.substring(i)); return (String[]) v.toArray(new String[0]); } /** * Compares Strings. Either can be null. * If both are null, equality is true. * @param s1 string 1 * @param s2 string 2 * @return true if equal */ public static boolean strEqual( String s1, String s2 ) { // let's use logic! return !(s1==null ^ s2==null) && (s1==null || s1.equals(s2)); /* if (s1 == null) { if (s2 == null) return true; else return false; } else // s1 != null { if (s2 == null) return false; else return s1.equals(s2); } */ } public static boolean dateEqual(Date d1, Date d2) { if (d1 == null) { if (d2 == null) return true; else return false; } else // d1 != null { if (d2 == null) return false; else return d1.equals(d2); } } public static boolean intEqual(Integer i1, Integer i2) { if (i1 == null) { if (i2 == null) return true; else return false; } else // i1 != null { if (i2 == null) return false; else return i1.equals(i2); } } /** * Compare strings, but consider null the same as an empty string. * @param s1 * @param s2 * @return */ public static boolean strEqualNE(String s1, String s2) { if (s1 == null) s1 = ""; if (s2 == null) s2 = ""; return strEqual(s1, s2); } /** * Compares strings for equality, allowing either string to be null. * Returns true if both arguments are null, or neither is null and they * match, ignoring case. * @param s1 string 1 * @param s2 string 2 * @return true if equal */ public static boolean strEqualIgnoreCase( String s1, String s2 ) { // let's use logic! return !(s1==null ^ s2==null) && (s1==null || s1.equalsIgnoreCase(s2)); /* if (s1 == null) { if (s2 == null) return true; else return false; } else // s1 != null { if (s2 == null) return false; else return s1.equalsIgnoreCase(s2); } */ } /** * Compares strings, allowing either string to be null. * Returns true if both arguments are null, or neither is null and they * match, ignoring case. A null string is considered greater than a * non-null string. * @param s1 string 1 * @param s2 string 2 * @return 0 if equal */ public static int strCompareIgnoreCase( String s1, String s2 ) { if (s1 == null) { if (s2 == null) return 0; else return 1; } else // s1 != null { if (s2 == null) return -1; else return s1.compareToIgnoreCase(s2); } } /** * Returns true if the first string starts with a copy of the second * string, without regard to case. * @param s the input string * @param v the string to check for * @return true if the first string starts with a copy of the second */ public static boolean startsWithIgnoreCase( String s, String v ) { return s.length() >= v.length() && s.substring(0, v.length()).equalsIgnoreCase(v); } /** * Returns true if the first string ends with a copy of the second * string, without regard to case. * @param s the input string * @param v the string to check for * @return true if the first string starts with a copy of the second */ public static boolean endsWithIgnoreCase( String s, String v ) { int len = s.length(); int vlen = v.length(); return len >= vlen && s.substring(len - vlen).equalsIgnoreCase(v); } /** * Sets the length of the string by padding with blanks on the right. * If the passed string's length is greater than len, it is truncated. * The return value is guaranteed to be a string of exactly 'len' characters. * @param s the input string * @param len the desired length * @return padded string */ public static String setLengthLeftJustify( String s, int len ) { if (s == null) s = ""; StringBuffer sb = new StringBuffer(len); int x; for(x=0; x len) s = s.substring(0, len); StringBuffer sb = new StringBuffer(len); int x = len - s.length(); for(int i=0; i= linelen ? line.substring(curpos) : line.substring(curpos, curpos+w); curpos += w; fieldnum++; } if (fieldnum < widths.length) { String newret[] = new String[fieldnum]; for(int i=0; i *
  • The supplied label can be anywhere in the string.
  • *
  • whitespace is optional on either side of equals sign.
  • *
  • Value may be a single whitespace delimited word, or enclosed * in matching double or single quotes.
  • *
  • Minimum length specifies the minimum length of the value and * may also be used to skip over known whitespace in the string.
  • *
  • If the ignoreCase argument is true, label will be matched * regardless of case
  • * * @param line the input string * @param label the label * @param minLength the minimum length of the value * @param ignoreCase true if you want to ignore case of label * @return assigned value if found, null if not. */ public static String scanAssign( String line, String label, int minLength, boolean ignoreCase ) { String tln = ignoreCase ? line.toLowerCase() : line; String tlb = ignoreCase ? label.toLowerCase() : label; int length = tln.length(); // Find the label int idx = tln.indexOf(tlb); if (idx == -1) return null; // Skip past label and any whitespace before equals sign. for(idx += label.length(); idx < length && Character.isWhitespace(tln.charAt(idx)); idx++); if (idx >= length) return null; if (tln.charAt(idx) != '=') return null; // Skip past '=' and any whitespace before value. for(++idx; idx < length && Character.isWhitespace(line.charAt(idx)); idx++); if (idx >= length) return null; if (line.charAt(idx) == '"') { ++idx; int edx = line.indexOf('"', idx); if (edx == -1) return line.substring(idx); else return line.substring(idx, edx); } else if (line.charAt(idx) == '\'') { ++idx; int edx = line.indexOf('\'', idx); if (edx == -1) return line.substring(idx); else return line.substring(idx, edx); } // Value goes to first white space or end of line int start = idx; idx = start + minLength; if (idx >= length) idx = length; for(; idx < length && !Character.isWhitespace(line.charAt(idx)); idx++); return line.substring(start, idx); } /** * Collapses the passed string to something suitable for use as a * variable name. Whitespace and special characters are removed. If * it starts with a digit, an underscore is prefixed. */ public static String collapse2Name(String txt) { StringBuilder sb = new StringBuilder(removeAllSpace(txt)); if (sb.length() == 0) return "_null"; if (Character.isDigit(sb.charAt(0))) sb.insert(0, '_'); for(int i=0; i 60) ci = 60; int i = tmp.indexOf('\r'); if (i > 0 && i < ci) ci = i; i = tmp.indexOf('\n'); if (i > 0 && i < ci) ci = i; i = tmp.indexOf('.'); if (i > 0 && i < ci) ci = i; if (ci < len) return tmp.substring(0,ci); else return tmp; } /** * Splits a string into words. Strings within double quotes are a single String * in the output. * @param text * @return */ public static String[] splitQuoted(String text) { ArrayList results = new ArrayList(); String regex = "\"([^\"]*)\"|(\\S+)"; Matcher m = Pattern.compile(regex).matcher(text); while (m.find()) { String s = (m.group(1) != null) ? m.group(1) : m.group(2); results.add(s); } String ret[] = new String[results.size()]; for(int idx = 0; idx < ret.length; idx++) ret[idx] = results.get(idx); return ret; } }




    © 2015 - 2024 Weber Informatics LLC | Privacy Policy