/*
* Enhydra Java Application Server Project
*
* The contents of this file are subject to the Enhydra 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 on
* the Enhydra web site ( http://www.enhydra.org/ ).
*
* 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 terms governing rights and limitations
* under the License.
*
* The Initial Developer of the Enhydra Application Server is Lutris
* Technologies, Inc. The Enhydra Application Server and portions created
* by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
* All Rights Reserved.
*
* Contributor(s):
*
* $Id: QuotedString.java,v 1.15 2005/06/13 09:26:06 draganr Exp $
*/
package com.lutris.util;
/**
* Static convenience class for parsing various types of quoted strings.
*/
public final class QuotedString {
/**
* Parse a C style quoted string. If the first character is a quote,
* then all characters up to a closing quote or end-of-string are
* gathered into a new String. The '\' character has the same
* semantics as in C. That is, it literally quotes the next
* character. Also, the four character sequence "\<code>ddd</code>" is
* converted to the character represented by the octal value
* <code>ddd</code>.
*
* @param s The string from which the quoted string is to be
* parsed.
* @return The parsed, quoted string.
*/
public static final
String parseCString(String s)
{
return parseCString(s.toCharArray(), 0);
}
/**
* Parse a C style quoted string. If the first character is a quote,
* then all characters up to a closing quote or end-of-string are
* gathered into a new String. The '\' character has the same
* semantics as in C. That is, it literally quotes the next
* character. Also, the four character sequence "\<code>ddd</code>" is
* converted to the character represented by the octal value
* <code>ddd</code>.
*
* @param s The string from which the quoted string is to be
* parsed.
* @param offset The index into the string at which parsing is
* to begin.
* @return The parsed, quoted string.
*/
public static final
String parseCString(String s, int offset)
{
return parseCString(s.toCharArray(), offset);
}
/**
* Parse a C style quoted string. If the first character is a quote,
* then all characters up to a closing quote or end-of-string are
* gathered into a new String. The '\' character has the same
* semantics as in C. That is, it literally quotes the next
* character. Also, the four character sequence "\<code>ddd</code>" is
* converted to the character represented by the octal value
* <code>ddd</code>.
*
* @param c The character array from which the quoted string
* is to be parsed.
* @return The parsed, quoted string.
*/
public static final
String parseCString(char[] c)
{
return parseCString(c, 0);
}
/**
* Parse a C style quoted string. If the first character is a quote,
* then all characters up to a closing quote or end-of-string are
* gathered into a new String. The '\' character has the same
* semantics as in C. That is, it literally quotes the next
* character. Also, the four character sequence "\<code>ddd</code>" is
* converted to the character represented by the octal value
* <code>ddd</code>.
*
* @param c The character array from which the quoted string
* is to be parsed.
* @param offset The index into the string at which parsing is
* to begin.
* @return The parsed, quoted string.
*/
public static final
String parseCString(char[] c, int offset)
{
int pos, end, len;
len = c.length;
pos = offset;
if (pos > len) return null;
if (pos == len) return "";
if (c[pos] == '"') {
StringBuffer b = new StringBuffer();
while (++pos < len) {
switch (c[pos]) {
case '"':
return new String(b);
case '\\':
if (++pos >= len) {
b.append('\\');
return new String(b);
}
if (c[pos] == '0') {
if ((len - pos) >= 3) {
String xs = new String(c, pos, 3);
int i=-1;;
try { i = Integer.parseInt(xs, 8); }
catch (Throwable t) { i = -1; }
if ((i >= 0) && (i <= 255)) {
b.append((char)i);
pos += 2;
} else {
b.append(c[pos]);
}
} else {
b.append(c[pos]);
}
} else {
b.append(c[pos]);
}
break;
default:
b.append(c[pos]);
break;
}
}
return new String(b);
} else {
end = pos;
while ((end < len) && (c[end] > ' ')) end++;
if ((end - pos) <= 0) return "";
return new String(c, pos, end - pos);
}
}
}