KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jcr > svn > SubversionInput


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.jcr.svn;
31
32 import com.caucho.util.L10N;
33 import com.caucho.vfs.ReadStream;
34
35 import java.io.IOException JavaDoc;
36 import java.util.ArrayList JavaDoc;
37 import java.util.logging.Logger JavaDoc;
38
39 /**
40  * Subversion input class.
41  */

42 public class SubversionInput {
43   private final L10N L = new L10N(SubversionInput.class);
44   private final Logger JavaDoc log
45     = Logger.getLogger(SubversionInput.class.getName());
46
47   private ReadStream _is;
48   private int _peek;
49
50   public SubversionInput(ReadStream is)
51   {
52     _is = is;
53   }
54
55   /**
56    * Reads a string.
57    */

58   public String JavaDoc readString()
59     throws IOException JavaDoc
60   {
61     skipWhitespace();
62
63     long length = readLong();
64
65     expect(':');
66
67     StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
68
69     for (int i = 0; i < length; i++) {
70       sb.append((char) read());
71     }
72
73     return sb.toString();
74   }
75
76   /**
77    * Reads a string literal.
78    */

79   public String JavaDoc readLiteral()
80     throws IOException JavaDoc
81   {
82     skipWhitespace();
83
84     StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
85
86     int ch;
87
88     while (isStringChar((ch = read()))) {
89       sb.append((char) ch);
90     }
91
92     _peek = ch;
93
94     return sb.toString();
95   }
96
97   /**
98    * Reads a long.
99    */

100   public long readLong()
101     throws IOException JavaDoc
102   {
103     skipWhitespace();
104
105     int sign = 1;
106     long value = 0;
107
108     int ch = read();
109
110     if (ch == '-') {
111       sign = -1;
112       ch = read();
113     }
114     else if (ch == '+') {
115       sign = -1;
116       ch = read();
117     }
118
119     if (! ('0' <= ch && ch <= '9'))
120       throw error(L.l("expected digit (0-9) at '{0}' (0x{1})",
121               String.valueOf((char) ch),
122               Integer.toHexString(ch)));
123
124     for (; '0' <= ch && ch <= '9'; ch = read()) {
125       value = 10 * value + ch - '0';
126     }
127
128     _peek = ch;
129
130     return sign * value;
131   }
132
133   /**
134    * Reads a s-exp
135    */

136   public Object JavaDoc readSexp()
137     throws IOException JavaDoc
138   {
139     int ch;
140
141     while ((ch = read()) >= 0) {
142       switch (ch) {
143       case ' ': case '\t': case '\r': case '\n':
144     break;
145     
146       case '(':
147     {
148       ArrayList JavaDoc array = new ArrayList JavaDoc();
149
150       Object JavaDoc value;
151
152       while ((value = readSexp()) != null) {
153         array.add(value);
154       }
155
156       expect(')');
157
158       return array;
159     }
160       case ')':
161     _peek = ch;
162     return null;
163
164       case '0': case '1': case '2': case '3': case '4':
165       case '5': case '6': case '7': case '8': case '9':
166     {
167       _peek = ch;
168       
169       long value = readLong();
170
171       ch = read();
172
173       if (ch == ':') {
174         StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
175         for (int i = 0; i < value; i++)
176           sb.append((char) read());
177         return sb.toString();
178       }
179       else {
180         _peek = ch;
181
182         return new Long JavaDoc(value);
183       }
184     }
185
186       default:
187     if (isStringChar((char) ch)) {
188       StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
189
190       sb.append((char) ch);
191       while (isStringChar(ch = read())) {
192         sb.append((char) ch);
193       }
194
195       _peek = ch;
196
197       return sb.toString();
198     }
199     else
200       throw error(L.l("Unexpected character"));
201       }
202     }
203
204     return null;
205   }
206     
207   
208   /**
209    * Skips whitespace
210    */

211   public boolean skipWhitespace()
212     throws IOException JavaDoc
213   {
214     int ch;
215
216     while (Character.isWhitespace(ch = read())) {
217     }
218
219     _peek = ch;
220
221     return ch >= 0;
222   }
223
224   /**
225    * Reads until an open brace.
226    */

227   public void expect(char expect)
228     throws IOException JavaDoc
229   {
230     int ch;
231     
232     while ((ch = read()) >= 0) {
233       if (ch == expect)
234     return;
235       else if (Character.isWhitespace(ch)) {
236       }
237       else
238     throw error(L.l("Expected '{0}' at '{1}' (0x{2})",
239             String.valueOf((char) expect),
240             String.valueOf((char) ch),
241             Integer.toHexString(ch)));
242     }
243     
244     throw error(L.l("Expected '{0}' at end of file",
245             String.valueOf((char) expect)));
246   }
247
248   private boolean isStringChar(int ch)
249   {
250     switch (ch) {
251     case ' ': case '\t': case '\n': case '\r':
252       return false;
253     case -1:
254       return false;
255     case '(': case ')':
256       return false;
257     default:
258       return true;
259     }
260   }
261
262   private IOException JavaDoc error(String JavaDoc msg)
263   {
264     return new IOException JavaDoc(msg);
265   }
266
267   public int read()
268     throws IOException JavaDoc
269   {
270     if (_peek > 0) {
271       int peek = _peek;
272       _peek = 0;
273       return peek;
274     }
275
276     int ch = _is.read();
277
278     if (ch >= 0)
279       System.out.print((char) ch);
280     
281     return ch;
282   }
283
284   public void close()
285   {
286     ReadStream is = _is;
287     _is = null;
288
289     if (is != null) {
290       is.close();
291     }
292   }
293 }
294
Popular Tags