KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lutris > util > QuotedString


1
2 /*
3  * Enhydra Java Application Server Project
4  *
5  * The contents of this file are subject to the Enhydra Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License on
8  * the Enhydra web site ( http://www.enhydra.org/ ).
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific terms governing rights and limitations
13  * under the License.
14  *
15  * The Initial Developer of the Enhydra Application Server is Lutris
16  * Technologies, Inc. The Enhydra Application Server and portions created
17  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
18  * All Rights Reserved.
19  *
20  * Contributor(s):
21  *
22  * $Id: QuotedString.java,v 1.1 2004/04/21 19:32:00 slobodan Exp $
23  */

24
25
26
27
28 package com.lutris.util;
29
30 /**
31  * Static convenience class for parsing various types of quoted strings.
32  */

33 public final class QuotedString {
34     /**
35      * Parse a C style quoted string. If the first character is a quote,
36      * then all characters up to a closing quote or end-of-string are
37      * gathered into a new String. The '\' character has the same
38      * semantics as in C. That is, it literally quotes the next
39      * character. Also, the four character sequence "\<code>ddd</code>" is
40      * converted to the character represented by the octal value
41      * <code>ddd</code>.
42      *
43      * @param s The string from which the quoted string is to be
44      * parsed.
45      * @return The parsed, quoted string.
46      */

47     public static final
48     String JavaDoc parseCString(String JavaDoc s)
49     {
50     return parseCString(s.toCharArray(), 0);
51     }
52
53     /**
54      * Parse a C style quoted string. If the first character is a quote,
55      * then all characters up to a closing quote or end-of-string are
56      * gathered into a new String. The '\' character has the same
57      * semantics as in C. That is, it literally quotes the next
58      * character. Also, the four character sequence "\<code>ddd</code>" is
59      * converted to the character represented by the octal value
60      * <code>ddd</code>.
61      *
62      * @param s The string from which the quoted string is to be
63      * parsed.
64      * @param offset The index into the string at which parsing is
65      * to begin.
66      * @return The parsed, quoted string.
67      */

68     public static final
69     String JavaDoc parseCString(String JavaDoc s, int offset)
70     {
71     return parseCString(s.toCharArray(), offset);
72     }
73     
74     /**
75      * Parse a C style quoted string. If the first character is a quote,
76      * then all characters up to a closing quote or end-of-string are
77      * gathered into a new String. The '\' character has the same
78      * semantics as in C. That is, it literally quotes the next
79      * character. Also, the four character sequence "\<code>ddd</code>" is
80      * converted to the character represented by the octal value
81      * <code>ddd</code>.
82      *
83      * @param c The character array from which the quoted string
84      * is to be parsed.
85      * @return The parsed, quoted string.
86      */

87     public static final
88     String JavaDoc parseCString(char[] c)
89     {
90     return parseCString(c, 0);
91     }
92     
93     /**
94      * Parse a C style quoted string. If the first character is a quote,
95      * then all characters up to a closing quote or end-of-string are
96      * gathered into a new String. The '\' character has the same
97      * semantics as in C. That is, it literally quotes the next
98      * character. Also, the four character sequence "\<code>ddd</code>" is
99      * converted to the character represented by the octal value
100      * <code>ddd</code>.
101      *
102      * @param c The character array from which the quoted string
103      * is to be parsed.
104      * @param offset The index into the string at which parsing is
105      * to begin.
106      * @return The parsed, quoted string.
107      */

108     public static final
109     String JavaDoc parseCString(char[] c, int offset)
110     {
111     int pos, end, len;
112     len = c.length;
113     pos = offset;
114     
115     if (pos > len) return null;
116     if (pos == len) return "";
117     
118     if (c[pos] == '"') {
119         StringBuffer JavaDoc b = new StringBuffer JavaDoc();
120         while (++pos < len) {
121         switch (c[pos]) {
122             case '"':
123             return new String JavaDoc(b);
124             case '\\':
125             if (++pos >= len) {
126                 b.append('\\');
127                 return new String JavaDoc(b);
128             }
129             if (c[pos] == '0') {
130                 if ((len - pos) >= 3) {
131                 String JavaDoc xs = new String JavaDoc(c, pos, 3);
132                 int i=-1;;
133                 try { i = Integer.parseInt(xs, 8); }
134                 catch (Throwable JavaDoc t) { i = -1; }
135                 if ((i >= 0) && (i <= 255)) {
136                     b.append((char)i);
137                     pos += 2;
138                 } else {
139                     b.append(c[pos]);
140                 }
141                 } else {
142                 b.append(c[pos]);
143                 }
144             } else {
145                 b.append(c[pos]);
146             }
147             break;
148             default:
149             b.append(c[pos]);
150             break;
151         }
152         }
153         return new String JavaDoc(b);
154     } else {
155         end = pos;
156         while ((end < len) && (c[end] > ' ')) end++;
157         if ((end - pos) <= 0) return "";
158         return new String JavaDoc(c, pos, end - pos);
159     }
160     }
161 }
162
Popular Tags