Source code: org/alicebot/server/core/parser/AIMLReader.java
1 // Decompiled by Jad v1.5.8c. Copyright 2001 Pavel Kouznetsov.
2 // Jad home page: http://www.geocities.com/kpdus/jad.html
3 // Decompiler options: packimports(3)
4
5 package org.alicebot.server.core.parser;
6
7 import java.io.BufferedReader;
8 import java.lang.reflect.Field;
9 import org.alicebot.server.core.logging.Log;
10 import org.alicebot.server.core.processor.ProcessorException;
11 import org.alicebot.server.core.util.*;
12
13 // Referenced classes of package org.alicebot.server.core.parser:
14 // GenericReader, AIMLReaderListener, StartupFileParser
15
16 public class AIMLReader extends GenericReader
17 {
18
19 public AIMLReader(String s, BufferedReader bufferedreader, AIMLReaderListener aimlreaderlistener, boolean flag)
20 {
21 super(s, bufferedreader, aimlreaderlistener);
22 categoryCount = 0;
23 pattern = "*";
24 that = "*";
25 topic = "*";
26 template = "*";
27 super.readerInstance = this;
28 warnNonAIML = flag;
29 state = 1;
30 }
31
32 protected void initialize()
33 {
34 try
35 {
36 patternField = getClass().getDeclaredField("pattern");
37 thatField = getClass().getDeclaredField("that");
38 topicField = getClass().getDeclaredField("topic");
39 templateField = getClass().getDeclaredField("template");
40 }
41 catch(NoSuchFieldException nosuchfieldexception)
42 {
43 throw new DeveloperError("The developer has specified a field that does not exist in AIMLReader.");
44 }
45 catch(SecurityException securityexception)
46 {
47 throw new DeveloperError("Security manager prevents AIMLReader from functioning.");
48 }
49 }
50
51 protected void tryStates()
52 throws GenericReader.TransitionMade
53 {
54 switch(state)
55 {
56 case 1: // '\001'
57 transition("<aiml version=\"1.0\">", 2);
58 transition("<aiml>", 2);
59 transition("<programd-startup>", 14);
60 break;
61
62 case 2: // '\002'
63 transition("<category>", 4);
64 transition("<topic name=\"", 3, topicField, "name");
65 break;
66
67 case 3: // '\003'
68 transition("<category>", 4);
69 break;
70
71 case 4: // '\004'
72 transition("<pattern>", 5);
73 // fall through
74
75 case 5: // '\005'
76 transition("</pattern>", 6, patternField);
77 break;
78
79 case 6: // '\006'
80 transition("<template>", 9);
81 transition("<that>", 7);
82 break;
83
84 case 9: // '\t'
85 transition("</template>", 10, templateField);
86 break;
87
88 case 7: // '\007'
89 transition("</that>", 8, thatField);
90 break;
91
92 case 8: // '\b'
93 transition("<template>", 9);
94 break;
95
96 case 10: // '\n'
97 transition("</category>", 11, 0);
98 break;
99
100 case 11: // '\013'
101 transition("<category>", 4);
102 transition("</topic>", 12, 3);
103 transition("<topic name=\"", 3, topicField, "name");
104 transition("</aiml>", 1, 1);
105 break;
106
107 case 12: // '\f'
108 transition("<category>", 4);
109 transition("<topic name=\"", 3, topicField, "name");
110 transition("</aiml>", 13, 1);
111 break;
112
113 case 14: // '\016'
114 transition("</programd-startup>", 15, 4);
115 break;
116 }
117 alertUnexpected();
118 }
119
120 private void transition(String s, int i, int j)
121 throws GenericReader.TransitionMade
122 {
123 if(succeed(s, i))
124 {
125 switch(j)
126 {
127 case 2: // '\002'
128 default:
129 break;
130
131 case 0: // '\0'
132 if(pattern.length() == 0)
133 {
134 abortCategory("Pattern missing from category.");
135 break;
136 }
137 if(template.length() == 0)
138 {
139 abortCategory("Template missing from category.");
140 break;
141 }
142 try
143 {
144 PatternArbiter.checkAIMLPattern(pattern, false);
145 PatternArbiter.checkAIMLPattern(that, false);
146 PatternArbiter.checkAIMLPattern(topic, false);
147 ((AIMLReaderListener)super.listener).newCategory(pattern, that, topic, template);
148 }
149 catch(NotAnAIMLPatternException notanaimlpatternexception)
150 {
151 abortCategory(notanaimlpatternexception.getMessage());
152 }
153 pattern = template = "";
154 that = "*";
155 searchStart = 0;
156 categoryCount++;
157 buffer = new StringBuffer(Math.max(GenericReader.bufferStartCapacity, buffer.length()));
158 buffer.append(bufferString);
159 break;
160
161 case 1: // '\001'
162 done = true;
163 break;
164
165 case 3: // '\003'
166 topic = "*";
167 break;
168
169 case 4: // '\004'
170 try
171 {
172 (new StartupFileParser()).processResponse(bufferString.substring(0, tagStart));
173 }
174 catch(ProcessorException processorexception) { }
175 break;
176 }
177 throw super.TRANSITION_MADE;
178 } else
179 {
180 return;
181 }
182 }
183
184 private void alertUnexpected()
185 {
186 if(state != 9)
187 {
188 for(int i = UNEXPECTED_OUTSIDE_TEMPLATE.length; --i >= 0;)
189 {
190 String s = UNEXPECTED_OUTSIDE_TEMPLATE[i];
191 int k = s.length();
192 if(bufferString.regionMatches(tagStart, s, 0, k))
193 {
194 Log.userinfo(new String[] {
195 "Unexpected " + s + "; aborting category.", " (Line " + lineNumber + ", \"" + fileName + "\")"
196 }, Log.ERROR);
197 return;
198 }
199 }
200
201 for(int j = UNEXPECTED_GENERAL.length; --j >= 0;)
202 {
203 String s1 = UNEXPECTED_GENERAL[j];
204 int i1 = s1.length();
205 if(bufferString.regionMatches(tagStart, s1, 0, i1))
206 {
207 if(s1 == "</aiml>" && categoryCount == 0)
208 Log.userinfo("aiml element does not contain any AIML content in \"" + fileName + "\".", Log.ERROR);
209 else
210 Log.userinfo(new String[] {
211 "Unexpected " + s1 + "; rest of file ignored.", " (Line " + lineNumber + ", \"" + fileName + "\")"
212 }, Log.ERROR);
213 done = true;
214 return;
215 }
216 }
217
218 if(warnNonAIML && state == 2)
219 {
220 int l = bufferString.indexOf(" ", tagStart);
221 if(l > -1)
222 {
223 String s2 = bufferString.substring(tagStart + 1, l);
224 if(s2.indexOf(":") == -1 && !s2.equals("!--"))
225 Log.userinfo(new String[] {
226 "There is no \"" + s2 + "\" element in AIML.", " (Line " + lineNumber + ", \"" + fileName + "\")"
227 }, Log.ERROR);
228 }
229 }
230 }
231 }
232
233 private void abortCategory(String s)
234 {
235 Log.userinfo(new String[] {
236 "Aborting category:", s, " (Category ends line " + lineNumber + ", \"" + fileName + "\")."
237 }, STARTUP_AND_ERROR);
238 }
239
240 private static final String PROPERTY_OPEN = "<property";
241 private static final String TEMPLATE_START = "<template>";
242 private static final String TEMPLATE_END = "</template>";
243 private static final String PATTERN_START = "<pattern>";
244 private static final String PATTERN_END = "</pattern>";
245 private static final String CATEGORY_START = "<category>";
246 private static final String CATEGORY_END = "</category>";
247 private static final String THAT_START = "<that>";
248 private static final String THAT_END = "</that>";
249 private static final String TOPIC_START = "<topic name=\"";
250 private static final String TOPIC_END = "</topic>";
251 private static final String AIML_START = "<aiml>";
252 private static final String AIML_VERSION_START = "<aiml version=\"1.0\">";
253 private static final String AIML_END = "</aiml>";
254 private static final String STARTUP_START = "<programd-startup>";
255 private static final String STARTUP_END = "</programd-startup>";
256 private static final String LOCALHOST = "localhost";
257 private static final String NAME = "name";
258 private static final String PATTERN = "pattern";
259 private static final String THAT = "that";
260 private static final String TOPIC = "topic";
261 private static final String TEMPLATE = "template";
262 private static final String UNEXPECTED_OUTSIDE_TEMPLATE[] = {
263 "</template>", "</pattern>", "<category>", "<template>", "<pattern>", "</that>", "<that>", "</topic>", "<topic name=\""
264 };
265 private static final String UNEXPECTED_GENERAL[] = {
266 "</aiml>", "<aiml>", "<aiml version=\"1.0\">"
267 };
268 private static final String STARTUP_AND_ERROR[];
269 private final int S_NONE = 1;
270 private final int S_IN_AIML = 2;
271 private final int S_IN_TOPIC = 3;
272 private final int S_IN_CATEGORY = 4;
273 private final int S_IN_PATTERN = 5;
274 private final int S_OUT_PATTERN = 6;
275 private final int S_IN_THAT = 7;
276 private final int S_OUT_THAT = 8;
277 private final int S_IN_TEMPLATE = 9;
278 private final int S_OUT_TEMPLATE = 10;
279 private final int S_OUT_CATEGORY = 11;
280 private final int S_OUT_TOPIC = 12;
281 private final int S_OUT_AIML = 13;
282 private final int S_IN_STARTUP = 14;
283 private final int S_OUT_STARTUP = 15;
284 private final int DELIVER_CATEGORY = 0;
285 private final int SET_DONE = 1;
286 private final int ABORT = 2;
287 private final int UNSET_TOPIC = 3;
288 private final int PROCESS_STARTUP = 4;
289 private boolean warnNonAIML;
290 private int categoryCount;
291 protected String pattern;
292 protected Field patternField;
293 protected String that;
294 protected Field thatField;
295 protected String topic;
296 protected Field topicField;
297 protected String template;
298 protected Field templateField;
299
300 static
301 {
302 STARTUP_AND_ERROR = (new String[] {
303 Log.STARTUP, Log.ERROR
304 });
305 }
306 }