KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > util > ScriptReader


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.util;
6
7 import java.io.IOException JavaDoc;
8 import java.io.Reader JavaDoc;
9 import java.sql.SQLException JavaDoc;
10
11 import org.h2.message.Message;
12
13 public class ScriptReader {
14     private Reader JavaDoc reader;
15     private boolean end;
16     private boolean insideRemark;
17     private boolean blockRemark;
18     private boolean skipRemarks;
19     
20     public ScriptReader(Reader JavaDoc reader) {
21         this.reader = reader;
22     }
23     
24     private int read() throws SQLException JavaDoc {
25         try {
26             return reader.read();
27         } catch (IOException JavaDoc e) {
28             throw Message.convert(e);
29         }
30     }
31     
32     public String JavaDoc readStatement() throws SQLException JavaDoc {
33         if(end) {
34             return null;
35         }
36         StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
37         int c = read();
38         while(true) {
39             if(c<0) {
40                 end = true;
41                 return buff.length() == 0 ? null : buff.toString();
42             } else if(c==';') {
43                 break;
44             }
45             switch (c) {
46             case '\'':
47                 buff.append((char)c);
48                 while(true) {
49                     c = read();
50                     if(c<0) {
51                         break;
52                     }
53                     buff.append((char)c);
54                     if(c=='\'') {
55                         break;
56                     }
57                 }
58                 c = read();
59                 break;
60             case '"':
61                 buff.append((char)c);
62                 while(true) {
63                     c = read();
64                     if(c<0) {
65                         break;
66                     }
67                     buff.append((char)c);
68                     if(c=='\"') {
69                         break;
70                     }
71                 }
72                 c = read();
73                 break;
74             case '/': {
75                 int last = c;
76                 c = read();
77                 if(c=='*') {
78                     // block comment
79
insideRemark = true;
80                     blockRemark = true;
81                     if(!skipRemarks) {
82                         buff.append((char)last);
83                         buff.append((char)c);
84                     }
85                     while(true) {
86                         c = read();
87                         if(c<0) {
88                             break;
89                         }
90                         if(!skipRemarks) {
91                             buff.append((char)c);
92                         }
93                         if(c=='*') {
94                             c = read();
95                             if(c<0) {
96                                 break;
97                             }
98                             if(!skipRemarks) {
99                                 buff.append((char)c);
100                             }
101                             if(c == '/') {
102                                 insideRemark = false;
103                                 break;
104                             }
105                         }
106                     }
107                     c = read();
108                 } else if (c == '/') {
109                     // single line comment
110
insideRemark = true;
111                     blockRemark = false;
112                     if(!skipRemarks) {
113                         buff.append((char)last);
114                         buff.append((char)c);
115                     }
116                     while(true) {
117                         c = read();
118                         if(c<0) {
119                             break;
120                         }
121                         if(!skipRemarks) {
122                             buff.append((char)c);
123                         }
124                         if(c=='\r' || c=='\n') {
125                             insideRemark = false;
126                             break;
127                         }
128                     }
129                     c = read();
130                 } else {
131                     buff.append((char)last);
132                 }
133                 break;
134             }
135             case '-': {
136                 int last = c;
137                 c = read();
138                 if(c=='-') {
139                     // single line comment
140
insideRemark = true;
141                     blockRemark = false;
142                     if(!skipRemarks) {
143                         buff.append((char)last);
144                         buff.append((char)c);
145                     }
146                     while(true) {
147                         c = read();
148                         if(c<0) {
149                             break;
150                         }
151                         if(!skipRemarks) {
152                             buff.append((char)c);
153                         }
154                         if(c=='\r' || c=='\n') {
155                             insideRemark = false;
156                             break;
157                         }
158                     }
159                     c = read();
160                 } else {
161                     buff.append((char)last);
162                 }
163                 break;
164             }
165             default:
166                 buff.append((char)c);
167                 c = read();
168             }
169         }
170         return buff.toString();
171     }
172
173     public boolean isInsideRemark() {
174         return insideRemark;
175     }
176
177     public boolean isBlockRemark() {
178         return blockRemark;
179     }
180
181     public void setSkipRemarks(boolean skipRemarks) {
182         this.skipRemarks = skipRemarks;
183     }
184 }
185
Popular Tags