01: /*
02: *******************************************************************************
03: * Copyright (C) 2002-2004, International Business Machines Corporation and *
04: * others. All Rights Reserved. *
05: *******************************************************************************
06: */
07: package com.ibm.icu.dev.tool.localeconverter;
08:
09: import java.io.*;
10:
11: /**
12: * A ComplexTransition is conceptually a single transition that
13: * consumes multiple input characters.
14: */
15: public abstract class ComplexTransition implements Lex.Transition {
16: //the value that is returned by subclasses indicating that
17: //the transition was successfull. This value is then
18: //discarded and the value passed to the constructor
19: //is then returned to the caller.
20: protected static final int SUCCESS = Lex.END_OF_FILE - 1;
21: private int success; //value to return if successfull
22: private Lex parser; //the parser used for this transition
23:
24: public ComplexTransition(int success) {
25: this .success = success;
26: this .parser = new Lex(null);
27: //this.parser.debug(true);
28: }
29:
30: public int doAction(int c, PushbackReader input, StringBuffer buffer)
31: throws IOException {
32: input.unread(c);
33: parser.setStates(getStates());
34: parser.setInput(input);
35: try {
36: parser.nextToken();
37: handleSuccess(parser, buffer);
38: return success;
39: } catch (IOException e) {
40: handleFailure(parser, buffer);
41: throw e;
42: }
43: }
44:
45: //called after a successful parse
46: protected void handleSuccess(Lex parser, StringBuffer output)
47: throws IOException {
48: parser.appendDataTo(output);
49: }
50:
51: //called after a failed parse
52: protected void handleFailure(Lex parser, StringBuffer output) {
53: }
54:
55: //subclasses should return the states to use to parse this
56: //transition
57: protected abstract Lex.Transition[][] getStates();
58:
59: public ComplexTransition debug(boolean debug) {
60: parser.debug(debug);
61: return this ;
62: }
63:
64: public ComplexTransition debug(boolean debug, String tag) {
65: parser.debug(debug, tag);
66: return this;
67: }
68: }
|