1 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 ; 36 import java.util.ArrayList ; 37 import java.util.logging.Logger ; 38 39 42 public class SubversionInput { 43 private final L10N L = new L10N(SubversionInput.class); 44 private final Logger 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 58 public String readString() 59 throws IOException  60 { 61 skipWhitespace(); 62 63 long length = readLong(); 64 65 expect(':'); 66 67 StringBuilder sb = new StringBuilder (); 68 69 for (int i = 0; i < length; i++) { 70 sb.append((char) read()); 71 } 72 73 return sb.toString(); 74 } 75 76 79 public String readLiteral() 80 throws IOException  81 { 82 skipWhitespace(); 83 84 StringBuilder sb = new StringBuilder (); 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 100 public long readLong() 101 throws IOException  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 136 public Object readSexp() 137 throws IOException  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 array = new ArrayList (); 149 150 Object 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 sb = new StringBuilder (); 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 (value); 183 } 184 } 185 186 default: 187 if (isStringChar((char) ch)) { 188 StringBuilder sb = new StringBuilder (); 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 211 public boolean skipWhitespace() 212 throws IOException  213 { 214 int ch; 215 216 while (Character.isWhitespace(ch = read())) { 217 } 218 219 _peek = ch; 220 221 return ch >= 0; 222 } 223 224 227 public void expect(char expect) 228 throws IOException  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 error(String msg) 263 { 264 return new IOException (msg); 265 } 266 267 public int read() 268 throws IOException  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 |