FileDocCategorySizeDatePackage
StringScanner.javaAPI DocGlassfish v2 API18086Fri May 04 22:35:18 BST 2007com.sun.jdo.spi.persistence.support.sqlstore.utility

StringScanner

public class StringScanner extends Object
Helper class for breaking up a string.

Fields Summary
private static String[]
intStrTable
Constructors Summary
Methods Summary
static charcharAt(java.lang.String s, int offset)

		if (offset < 0 || offset >= s.length())
			return '\0";
		return s.charAt(offset);
	
static java.lang.StringcreateParamString(java.lang.String message, int argcnt, java.lang.Object param1, java.lang.Object param2, java.lang.Object param3, java.lang.Object param4, java.lang.Object param5, java.lang.Object param6, java.lang.Object param7, java.lang.Object param8, java.lang.Object param9)

		if (argcnt < 1 || argcnt > 9)
			return message;

		Object[] p = null;
		if (argcnt == 1)
		{
			Object[] params = {param1};
			p = params;
		}
		else if (argcnt == 2)
		{
			Object[] params = {param1, param2};
			p = params;
		}
		else if (argcnt == 3)
		{
			Object[] params = {param1, param2, param3};
			p = params;
		}
		else if (argcnt == 4)
		{
			Object[] params = {param1, param2, param3, param4};
			p = params;
		}
		else if (argcnt == 5)
		{
			Object[] params = {param1, param2, param3, param4, param5};
			p = params;
		}
		else if (argcnt == 6)
		{
			Object[] params = {param1, param2, param3, param4, param5,
								   param6};
			p = params;
		}
		else if (argcnt == 7)
		{
			Object[] params = {param1, param2, param3, param4, param5,
								   param6, param7};
			p = params;
		}
		else if (argcnt == 8)
		{
			Object[] params = {param1, param2, param3, param4, param5,
								   param6, param7, param8};
			p = params;
		}
		else if (argcnt == 9)
		{
			Object[] params = {param1, param2, param3, param4, param5,
								   param6, param7, param8, param9};
			p = params;
		}

		//
		// Decide if we should use our own formatting algorithm or
		// call Java's MessageFormat.format().  Our own algorithm is
		// about 20 times faster but cannot deal with templates.
		// So, use our own if no template is involved. i.e. only
		// positional parameters.
		// e.g. {0} is OK but {0,ddd-mmm-yyyy} is not.
		//
		boolean useJava = false;
		int lbrace, rbrace = 0;
		int i = 0;

		// look for the start of next positional parameter
		while ((lbrace = message.indexOf('{", i)) >= 0)
		{
			// if the parameter doesn't end with a right brace, get
			// out and use the Java formatter. Or if there are more
			// than 1 character between the braces (e.g. {0,ddd-mmm-yyyy})
			if ((rbrace = message.indexOf('}", lbrace + 1)) < 0 ||
				(rbrace - lbrace) > 2)
			{
				useJava = true;
				break;
			}

			i = rbrace + 1;
		}

		if (useJava)
		{
			return java.text.MessageFormat.format(message, p);
		}

		// Since we are using our own algorithm, we need to get rid of
		// the pesty single quote escape character
		int esc;
		boolean foundEsc = false;
		StringBuffer msg = new StringBuffer();

		i = 0;
		while ((esc = message.indexOf('\'", i)) >= 0)
		{
			msg.append(message.substring(i, esc));
			msg.append(message.substring(esc + 1, esc + 2));
			i = esc + 2;
			foundEsc = true;
		}

		if (foundEsc)
		{
			msg.append(message.substring(i, message.length()));
			message = msg.toString();
		}

		char c;
		i = 0;
		int msglen = message.length();
		msg = new StringBuffer();
		while (   (lbrace = message.indexOf('{", i)) >= 0
			   && lbrace < msglen-1)
		{
			c = message.charAt(lbrace + 1);

			if (Character.isDigit(c))
			{
				int pnum = c - '0";

				if (0 <= pnum && pnum < argcnt)
				{
					Object pp = p[pnum];

					msg.append(message.substring(i, lbrace));

					if (pp == null)
					{
						msg.append("<null>"); // NOI18N
					}
					else
					{
						msg.append(pp.toString());
					}
				}
				else
				{
					if (argcnt > 0)
					{
						/*						throw (StringIndexOutOfBoundsException)
							ErrorManager.createFormatAdd(
								java.lang.StringIndexOutOfBoundsException.class,
								ErrorManager.USER,
								UtilMsgCat.SP_ERR_FMT_OUT_OF_RANGE,
								(new Integer(pnum)),
								(new Integer(1)), (new Integer(argcnt)), msg);
						*/
					}
					else
					{
						/*						throw (StringIndexOutOfBoundsException)
							ErrorManager.createFormatAdd(
								java.lang.StringIndexOutOfBoundsException.class,
								ErrorManager.USER,
								UtilMsgCat.SP_ERR_FMT_MISSING_PARAM,
								(new Integer(pnum)), msg);
						*/
					}
				}
				i = lbrace + 3;
			}
			else
			{
				i = lbrace + 1;
			}
		}
		msg.append(message.substring(i, msglen));
		return msg.toString();
	
public static java.lang.StringcreateParamString(java.lang.String fmt)

		return fmt;
	
public static java.lang.StringcreateParamString(java.lang.String fmt, java.lang.Object obj1)

		return StringScanner.createParamString(fmt, 1, obj1, null, null, null,
								null, null, null, null, null);
	
public static java.lang.StringcreateParamString(java.lang.String fmt, java.lang.Object obj1, java.lang.Object obj2)

		return StringScanner.createParamString(fmt, 2, obj1, obj2, null, null,
								null, null, null, null, null);
	
public static java.lang.StringcreateParamString(java.lang.String fmt, java.lang.Object obj1, java.lang.Object obj2, java.lang.Object obj3)

		return StringScanner.createParamString(fmt, 3, obj1, obj2, obj3, null,
								null, null, null, null, null);
	
public static java.lang.StringcreateParamString(java.lang.String fmt, java.lang.Object obj1, java.lang.Object obj2, java.lang.Object obj3, java.lang.Object obj4)

		return StringScanner.createParamString(fmt, 4, obj1, obj2, obj3, obj4,
								null, null, null, null, null);
	
public static java.lang.StringcreateParamString(java.lang.String fmt, java.lang.Object obj1, java.lang.Object obj2, java.lang.Object obj3, java.lang.Object obj4, java.lang.Object obj5)

		return StringScanner.createParamString(fmt, 5, obj1, obj2, obj3, obj4,
								obj5, null, null, null, null);
	
public static java.lang.StringcreateParamString(java.lang.String fmt, java.lang.Object obj1, java.lang.Object obj2, java.lang.Object obj3, java.lang.Object obj4, java.lang.Object obj5, java.lang.Object obj6)

		return StringScanner.createParamString(fmt, 6, obj1, obj2, obj3, obj4,
								obj5, obj6, null, null, null);
	
public static java.lang.StringcreateParamString(java.lang.String fmt, java.lang.Object obj1, java.lang.Object obj2, java.lang.Object obj3, java.lang.Object obj4, java.lang.Object obj5, java.lang.Object obj6, java.lang.Object obj7)

		return StringScanner.createParamString(fmt, 7, obj1, obj2, obj3, obj4,
								obj5, obj6, obj7, null, null);
	
public static java.lang.StringcreateParamString(java.lang.String fmt, java.lang.Object obj1, java.lang.Object obj2, java.lang.Object obj3, java.lang.Object obj4, java.lang.Object obj5, java.lang.Object obj6, java.lang.Object obj7, java.lang.Object obj8)

		return StringScanner.createParamString(fmt, 8, obj1, obj2, obj3, obj4,
								obj5, obj6, obj7, obj8, null);
	
public static java.lang.StringcreateParamString(java.lang.String fmt, java.lang.Object obj1, java.lang.Object obj2, java.lang.Object obj3, java.lang.Object obj4, java.lang.Object obj5, java.lang.Object obj6, java.lang.Object obj7, java.lang.Object obj8, java.lang.Object obj9)

		return StringScanner.createParamString(fmt, 9, obj1, obj2, obj3, obj4,
								obj5, obj6, obj7, obj8, obj9);
	
public static java.lang.Stringfill(java.lang.String str, int len, boolean left)
Fills a string with blanks to a given size.

Left or right fills the given string with spaces (' ').

return
A new filled string. false.
param
left true when the text should be left justified, otherwise
param
len The desired length of the formatted string.
param
str The string to process.

		String	nstr;
		int		startLen;
		int		diff;

		if (str == null)
			str = "null"; // NOI18N

		startLen = str.length();
		nstr = str;
		while (startLen < len)
		{
			diff = len - startLen;
			if (diff >= 10)
			{
				startLen += 10;
				if (left)
					nstr = nstr + "          "; // NOI18N
				else
					nstr = "          " + nstr; // NOI18N
				continue;
			}
			else if (diff >= 8)
			{
				startLen += 8;
				if (left)
					nstr = nstr + "        "; // NOI18N
				else
					nstr = "        " + nstr; // NOI18N
				continue;
			}
			else if (diff >= 4)
			{
				startLen += 4;
				if (left)
					nstr = nstr + "    "; // NOI18N
				else
					nstr = "    " + nstr; // NOI18N
				continue;
			}
			else if (diff >= 2)
			{
				startLen += 2;
				if (left)
					nstr = nstr + "  "; // NOI18N
				else
					nstr = "  " + nstr; // NOI18N
				continue;
			}
			else
			{
				startLen++;
				if (left)
					nstr = nstr + " "; // NOI18N
				else
					nstr = " " + nstr; // NOI18N
				continue;
			}
		}
		return nstr;
	
public static java.lang.Stringformat(java.lang.StringBuffer buf, java.lang.String fmt, java.lang.Object o1, java.lang.Object o2, java.lang.Object o3, java.lang.Object o4, java.lang.Object o5)

		return format(buf, fmt, 5, o1, o2, o3, o4, o5, null, null, null, null);
	
public static java.lang.Stringformat(java.lang.StringBuffer buf, java.lang.String fmt, java.lang.Object o1, java.lang.Object o2, java.lang.Object o3, java.lang.Object o4, java.lang.Object o5, java.lang.Object o6)

		return format(buf, fmt, 6, o1, o2, o3, o4, o5, o6, null, null, null);
	
public static java.lang.Stringformat(java.lang.StringBuffer buf, java.lang.String fmt, java.lang.Object o1, java.lang.Object o2, java.lang.Object o3, java.lang.Object o4, java.lang.Object o5, java.lang.Object o6, java.lang.Object o7)

		return format(buf, fmt, 7, o1, o2, o3, o4, o5, o6, o7, null, null);
	
public static java.lang.Stringformat(java.lang.StringBuffer buf, java.lang.String fmt, java.lang.Object o1, java.lang.Object o2, java.lang.Object o3, java.lang.Object o4, java.lang.Object o5, java.lang.Object o6, java.lang.Object o7, java.lang.Object o8)

		return format(buf, fmt, 8, o1, o2, o3, o4, o5, o6, o7, o8, null);
	
public static java.lang.Stringformat(java.lang.StringBuffer buf, java.lang.String fmt, java.lang.Object o1, java.lang.Object o2, java.lang.Object o3, java.lang.Object o4, java.lang.Object o5, java.lang.Object o6, java.lang.Object o7, java.lang.Object o8, java.lang.Object o9)

		return format(buf, fmt, 9, o1, o2, o3, o4, o5, o6, o7, o8, o9);
	
public static java.lang.Stringformat(java.lang.StringBuffer buf, java.lang.String fmt, int argcnt, java.lang.Object o1, java.lang.Object o2, java.lang.Object o3, java.lang.Object o4, java.lang.Object o5, java.lang.Object o6, java.lang.Object o7, java.lang.Object o8, java.lang.Object o9)

		Object[] params = {o1, o2, o3, o4, o5, o6, o7, o8, o9};

		int i = 0;
		int begSubstr = 0;
		int percent;
		int nextParam = 0;
		StringBuffer msg;
		int fmtLen;

		if (buf == null)
			msg = new StringBuffer();
		else
			msg = buf;

		fmtLen = fmt.length();

		while ((percent = fmt.indexOf('%", i)) >= 0 && percent < (fmtLen - 1))
		{
			char 	c = fmt.charAt(percent + 1);
			boolean leftJustify = false;
			boolean raw = false;
			int     nextChar = percent;

			if (c == '-")
			{
				nextChar++;
				leftJustify = true;
				if (nextChar + 1 < fmtLen)
					c = fmt.charAt(nextChar + 1);
			}

			if (Character.isDigit(c))
			{
				int endInt = StringScanner.skipInt(fmt, nextChar + 1);
				int size = Integer.parseInt(fmt.substring(nextChar + 1,endInt));

				if (size == 0)
					raw = true;

				msg.append(fmt.substring(begSubstr, percent));
				if (nextParam < argcnt)
				{
					Object	p = params[nextParam++];
					String	val = p.toString();
					int		len = val.length();
					
					if (!raw)
					{
						if (!leftJustify && len < size)
						{
							for (int j = 0; j < size - len; j++)
								msg.append(" "); // NOI18N
						}
						else if (len > size)
						{
							val = val.substring(0, size);
						}
					}
					msg.append(val);
					if (leftJustify && len < size)
					{
						for (int j = 0; j < size - len; j++)
							msg.append(" "); // NOI18N
					}
				}
				else
				{
					/*					throw (StringIndexOutOfBoundsException)
						ErrorManager.createFormatAdd(
							java.lang.StringIndexOutOfBoundsException.class,
							ErrorManager.USER,
							UtilMsgCat.SP_ERR_FMT_OUT_OF_RANGE,
							(new Integer(nextParam)),
							(new Integer(1)), (new Integer(argcnt)), fmt);
					*/
				}
				i = endInt;
				begSubstr = i;
			}
			else
			{
				i = nextChar + 1;
			}
		}
		msg.append(fmt.substring(begSubstr, fmtLen));
		return msg.toString();
	
public static java.lang.Stringformat(java.lang.StringBuffer buf, java.lang.String fmt, java.lang.Object o1)

		return format(buf, fmt, 1, o1, null, null, null, null, null, null,
				null, null);
	
public static java.lang.Stringformat(java.lang.StringBuffer buf, java.lang.String fmt, java.lang.Object o1, java.lang.Object o2)

		return format(buf, fmt, 2, o1, o2, null, null, null, null, null,
				null, null);
	
public static java.lang.Stringformat(java.lang.StringBuffer buf, java.lang.String fmt, java.lang.Object o1, java.lang.Object o2, java.lang.Object o3)

		return format(buf, fmt, 3, o1, o2, o3, null, null, null, null,
					null, null);
	
public static java.lang.Stringformat(java.lang.StringBuffer buf, java.lang.String fmt, java.lang.Object o1, java.lang.Object o2, java.lang.Object o3, java.lang.Object o4)

		return format(buf, fmt, 4, o1, o2, o3, o4, null, null, null,
					null, null);
	
public static java.lang.StringgetIntStr(int num)

		String	str;
		boolean	big;

		try
		{
			str = (String) StringScanner.intStrTable[num];
			big = false;
		}
		catch (ArrayIndexOutOfBoundsException e)
		{
			str = null;
			big = true;
		}

		if (str == null)
		{
			str = Integer.toString(num);
			if (!big)
				StringScanner.intStrTable[num] = str;
		}
		return str;
	
public static java.lang.StringlevelString(int level)
Return a string containing a "level" number of spaces.

Used for formatted print, this will return a string containing 4 spaces for each specified level.

return
The requested string.
param
level The level to fill to.

		int		i;
		String	str;

		if (level <= 0)
			return ""; // NOI18N

		switch (level)
		{
		case 1:
			return "    "; // NOI18N
		case 2:
			return "        "; // NOI18N
		case 3:
			return "            "; // NOI18N
		case 4:
			return "                "; // NOI18N
		case 5:
			return "                    "; // NOI18N
		case 6:
			return "                       "; // NOI18N
		default:
			str = new String("                       "); // NOI18N
			for (i = 6; i < level; i++)
				str = str.concat("    "); // NOI18N
			return str;
		}
	
static intskipInt(java.lang.String s, int offset)

		int end = s.length();

		if (offset < 0 || !Character.isDigit(s.charAt(offset)))
			return end;
		//
		// Unlike the others, this can return the offset past the end
		// of the string.  The idea is that the caller wants to
		// do a substring to get the integer.
		//
		while (offset < end && Character.isDigit(s.charAt(offset)))
			offset++;
		return offset;
	
static intskipTo(java.lang.String s, int offset, java.lang.String delim)

		int end = s.length();

		if (offset < 0)
			return end;
		while (offset < end && delim.indexOf(s.charAt(offset)) == -1)
			offset++;
		return offset;
	
static intskipTo(java.lang.String s, int offset, char delim)

		int end = s.length();

		if (offset < 0)
			return end;
		while (offset < end && delim != s.charAt(offset))
			offset++;
		return offset;
	
static intskipWhite(java.lang.String s, int offset)

	

	     
	
		int	end = s.length();

		if (offset < 0)
			return end;
		while (offset < end && Character.isWhitespace(s.charAt(offset)))
			offset++;
		return offset;
	
public static java.util.VectorsplitString(java.lang.String str, java.lang.String delimeter)
Split a string into sub-strings based on a given delimeter.

Split str based on the delimeter and return as an array of Strings.

return
An array of sub-strings
param
delimeter The string representing the delimeters.
param
str The string to process.

    	int		mark;
		int		start;
		int		delLen;
    	Vector	list;
	
		list = new Vector();

    	if (str != null)
    	{
        	mark = 0;
			start = 0;

        	delLen = delimeter.length();
        	while ((start = str.indexOf(delimeter, mark)) != -1)
        	{
				if (start != 0)
            		list.addElement((Object) str.substring(mark, start));
				mark = start + delLen;
        	}

			// Put in the last bit.
			if (mark < str.length())
            	list.addElement((Object) str.substring(mark));
  		}
    	return list;