001: /*
002: * Enhydra Java Application Server Project
003: *
004: * The contents of this file are subject to the Enhydra Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License on
007: * the Enhydra web site ( http://www.enhydra.org/ ).
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
011: * the License for the specific terms governing rights and limitations
012: * under the License.
013: *
014: * The Initial Developer of the Enhydra Application Server is Lutris
015: * Technologies, Inc. The Enhydra Application Server and portions created
016: * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
017: * All Rights Reserved.
018: *
019: * Contributor(s):
020: *
021: * $Id: QuotedString.java,v 1.2 2006-06-15 13:47:01 sinisa Exp $
022: */
023:
024: package com.lutris.util;
025:
026: /**
027: * Static convenience class for parsing various types of quoted strings.
028: */
029: public final class QuotedString {
030: /**
031: * Parse a C style quoted string. If the first character is a quote,
032: * then all characters up to a closing quote or end-of-string are
033: * gathered into a new String. The '\' character has the same
034: * semantics as in C. That is, it literally quotes the next
035: * character. Also, the four character sequence "\<code>ddd</code>" is
036: * converted to the character represented by the octal value
037: * <code>ddd</code>.
038: *
039: * @param s The string from which the quoted string is to be
040: * parsed.
041: * @return The parsed, quoted string.
042: */
043: public static final String parseCString(String s) {
044: return parseCString(s.toCharArray(), 0);
045: }
046:
047: /**
048: * Parse a C style quoted string. If the first character is a quote,
049: * then all characters up to a closing quote or end-of-string are
050: * gathered into a new String. The '\' character has the same
051: * semantics as in C. That is, it literally quotes the next
052: * character. Also, the four character sequence "\<code>ddd</code>" is
053: * converted to the character represented by the octal value
054: * <code>ddd</code>.
055: *
056: * @param s The string from which the quoted string is to be
057: * parsed.
058: * @param offset The index into the string at which parsing is
059: * to begin.
060: * @return The parsed, quoted string.
061: */
062: public static final String parseCString(String s, int offset) {
063: return parseCString(s.toCharArray(), offset);
064: }
065:
066: /**
067: * Parse a C style quoted string. If the first character is a quote,
068: * then all characters up to a closing quote or end-of-string are
069: * gathered into a new String. The '\' character has the same
070: * semantics as in C. That is, it literally quotes the next
071: * character. Also, the four character sequence "\<code>ddd</code>" is
072: * converted to the character represented by the octal value
073: * <code>ddd</code>.
074: *
075: * @param c The character array from which the quoted string
076: * is to be parsed.
077: * @return The parsed, quoted string.
078: */
079: public static final String parseCString(char[] c) {
080: return parseCString(c, 0);
081: }
082:
083: /**
084: * Parse a C style quoted string. If the first character is a quote,
085: * then all characters up to a closing quote or end-of-string are
086: * gathered into a new String. The '\' character has the same
087: * semantics as in C. That is, it literally quotes the next
088: * character. Also, the four character sequence "\<code>ddd</code>" is
089: * converted to the character represented by the octal value
090: * <code>ddd</code>.
091: *
092: * @param c The character array from which the quoted string
093: * is to be parsed.
094: * @param offset The index into the string at which parsing is
095: * to begin.
096: * @return The parsed, quoted string.
097: */
098: public static final String parseCString(char[] c, int offset) {
099: int pos, end, len;
100: len = c.length;
101: pos = offset;
102:
103: if (pos > len)
104: return null;
105: if (pos == len)
106: return "";
107:
108: if (c[pos] == '"') {
109: StringBuffer b = new StringBuffer();
110: while (++pos < len) {
111: switch (c[pos]) {
112: case '"':
113: return new String(b);
114: case '\\':
115: if (++pos >= len) {
116: b.append('\\');
117: return new String(b);
118: }
119: if (c[pos] == '0') {
120: if ((len - pos) >= 3) {
121: String xs = new String(c, pos, 3);
122: int i = -1;
123: ;
124: try {
125: i = Integer.parseInt(xs, 8);
126: } catch (Throwable t) {
127: i = -1;
128: }
129: if ((i >= 0) && (i <= 255)) {
130: b.append((char) i);
131: pos += 2;
132: } else {
133: b.append(c[pos]);
134: }
135: } else {
136: b.append(c[pos]);
137: }
138: } else {
139: b.append(c[pos]);
140: }
141: break;
142: default:
143: b.append(c[pos]);
144: break;
145: }
146: }
147: return new String(b);
148: } else {
149: end = pos;
150: while ((end < len) && (c[end] > ' '))
151: end++;
152: if ((end - pos) <= 0)
153: return "";
154: return new String(c, pos, end - pos);
155: }
156: }
157: }
|