KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > bnf > RuleFixed


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.bnf;
6
7 import java.util.HashMap JavaDoc;
8 import java.util.Random JavaDoc;
9
10 import org.h2.util.StringUtils;
11
12 public class RuleFixed implements Rule {
13     public static final int YMD = 0, HMS =1, NANOS = 2;
14     public static final int ANY_EXCEPT_SINGLE_QUOTE = 3;
15     public static final int ANY_EXCEPT_DOUBLE_QUOTE = 4;
16     public static final int ANY_UNTIL_EOL = 5;
17     public static final int ANY_UNTIL_END = 6;
18     public static final int ANY_WORD = 7;
19     public static final int HEXSTART = 10, CONCAT = 11, AZ_ = 12, AF = 13, DIGIT = 14;
20     
21     private int type;
22     
23     public RuleFixed(int type) {
24         this.type = type;
25     }
26     
27     public String JavaDoc random(Bnf config, int level) {
28         Random JavaDoc r = config.getRandom();
29         switch(type) {
30         case YMD:
31             return ""+(1800+r.nextInt(200))+"-"+(1+r.nextInt(12))+"-"+(1+r.nextInt(31));
32         case HMS:
33             return ""+(r.nextInt(24))+"-"+(r.nextInt(60))+"-"+(r.nextInt(60));
34         case NANOS:
35             return ""+(r.nextInt(100000)+r.nextInt(10000));
36         case ANY_UNTIL_EOL:
37         case ANY_EXCEPT_SINGLE_QUOTE:
38         case ANY_EXCEPT_DOUBLE_QUOTE:
39         case ANY_WORD:
40         case ANY_UNTIL_END: {
41             StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
42             int len = r.nextInt(10);
43             for(int i=0; i<len; i++) {
44                 buff.append((char)('A' + r.nextInt('Z'-'A')));
45             }
46             return buff.toString();
47         }
48         case HEXSTART:
49             return "0x";
50         case CONCAT:
51             return "||";
52         case AZ_:
53             return ""+(char)('A' + r.nextInt('Z'-'A'));
54         case AF:
55             return ""+(char)('A' + r.nextInt('F'-'A'));
56         case DIGIT:
57             return ""+(char)('0' + r.nextInt(10));
58         default:
59             throw new Error JavaDoc("type="+type);
60         }
61     }
62
63     public String JavaDoc name() {
64         return "type="+type;
65     }
66
67     public Rule last() {
68         return this;
69     }
70
71     public void setLinks(HashMap JavaDoc ruleMap) {
72     }
73     
74     public String JavaDoc matchRemove(String JavaDoc query, Sentence sentence) {
75         if(sentence.stop()) {
76             return null;
77         }
78         if(query.length()==0) {
79             return null;
80         }
81         String JavaDoc s = query;
82         switch(type) {
83         case YMD:
84             while(s.length() > 0 && "0123456789- ".indexOf(s.charAt(0)) >= 0) {
85                 s = s.substring(1);
86             }
87             break;
88         case HMS:
89             while(s.length() > 0 && "0123456789:. ".indexOf(s.charAt(0)) >= 0) {
90                 s = s.substring(1);
91             }
92             break;
93         case NANOS:
94             while(s.length() > 0 && Character.isDigit(s.charAt(0))) {
95                 s = s.substring(1);
96             }
97             break;
98         case ANY_WORD:
99             while(s.length() > 0 && Character.isWhitespace(s.charAt(0))) {
100                 s = s.substring(1);
101             }
102             break;
103         case ANY_UNTIL_END:
104             while(s.length() > 1 && s.startsWith("*/")) {
105                 s = s.substring(1);
106             }
107             break;
108         case ANY_UNTIL_EOL:
109             while(s.length() > 0 && s.charAt(0)!='\n') {
110                 s = s.substring(1);
111             }
112             break;
113         case ANY_EXCEPT_SINGLE_QUOTE:
114             while(true) {
115                 while(s.length() > 0 && s.charAt(0)!='\'') {
116                     s = s.substring(1);
117                 }
118                 if(s.startsWith("''")) {
119                     s = s.substring(2);
120                 } else {
121                     break;
122                 }
123             }
124             break;
125         case ANY_EXCEPT_DOUBLE_QUOTE:
126             while(true) {
127                 while(s.length() > 0 && s.charAt(0)!='\"') {
128                     s = s.substring(1);
129                 }
130                 if(s.startsWith("\"\"")) {
131                     s = s.substring(2);
132                 } else {
133                     break;
134                 }
135             }
136             break;
137         case HEXSTART:
138             if(StringUtils.toUpperEnglish(s).startsWith("0X")) {
139                 s = s.substring(2);
140             } else if(StringUtils.toUpperEnglish(s).startsWith("0")) {
141                 s = s.substring(1);
142             }
143             break;
144         case CONCAT:
145             if(s.startsWith("||")) {
146                 s = s.substring(2);
147             } else if(s.startsWith("|")) {
148                 s = s.substring(1);
149             }
150             break;
151         case AZ_:
152             if(s.length() > 0 && (Character.isLetter(s.charAt(0)) || s.charAt(0)=='_')) {
153                 s = s.substring(1);
154             }
155             break;
156         case AF:
157             if(s.length() > 0) {
158                 char ch = Character.toUpperCase(s.charAt(0));
159                 if(ch >= 'A' && ch <= 'F') {
160                     s = s.substring(1);
161                 }
162             }
163             break;
164         case DIGIT:
165             if(s.length() > 0 && Character.isDigit(s.charAt(0))) {
166                 s = s.substring(1);
167             }
168             break;
169         default:
170             throw new Error JavaDoc("type="+type);
171         }
172         if(s == query) {
173             return null;
174         }
175         return s;
176     }
177     
178     public void addNextTokenList(String JavaDoc query, Sentence sentence) {
179         if(sentence.stop()) {
180             return;
181         }
182         // String s = matchRemove(query, iteration);
183
switch(type) {
184         case YMD:
185             if(query.length() == 0) {
186                 sentence.add("2006-01-01", "2006-01-01", Sentence.KEYWORD);
187             }
188             break;
189         case HMS:
190             if(query.length() == 0) {
191                 sentence.add("12:00:00", "12:00:00", Sentence.KEYWORD);
192             }
193             break;
194         case NANOS:
195             if(query.length() == 0) {
196                 sentence.add("nanoseconds", "0", Sentence.KEYWORD);
197             }
198             break;
199         case ANY_EXCEPT_SINGLE_QUOTE:
200             if(query.length() == 0) {
201                 sentence.add("anything", "Hello World", Sentence.KEYWORD);
202                 sentence.add("'", "'", Sentence.KEYWORD);
203             }
204             break;
205         case ANY_EXCEPT_DOUBLE_QUOTE:
206             if(query.length() == 0) {
207                 sentence.add("anything", "identifier", Sentence.KEYWORD);
208             }
209             break;
210         case ANY_WORD:
211             break;
212         case HEXSTART:
213             if(query.length() == 0) {
214                 sentence.add("0x", "0x", Sentence.KEYWORD);
215             } else if(query.equals("0")) {
216                 sentence.add("0x", "x", Sentence.KEYWORD);
217             }
218             break;
219         case CONCAT:
220             if(query.length() == 0) {
221                 sentence.add("||", "||", Sentence.KEYWORD);
222             } else if(query.equals("|")) {
223                 sentence.add("||", "|", Sentence.KEYWORD);
224             }
225             break;
226         case AZ_:
227             if(query.length() == 0) {
228                 sentence.add("character", "A", Sentence.KEYWORD);
229             }
230             break;
231         case AF:
232             if(query.length() == 0) {
233                 sentence.add("hex character", "0A", Sentence.KEYWORD);
234             }
235             break;
236         case DIGIT:
237             if(query.length() == 0) {
238                 sentence.add("digit", "1", Sentence.KEYWORD);
239             }
240             break;
241         default:
242             throw new Error JavaDoc("type="+type);
243         }
244     }
245     
246 }
247
Popular Tags