001: /*
002: *******************************************************************************
003: * Copyright (C) 2002-2004, International Business Machines Corporation and *
004: * others. All Rights Reserved. *
005: *******************************************************************************
006: */
007: package com.ibm.icu.dev.tool.localeconverter;
008:
009: import java.io.*;
010:
011: public class SymbolTransition extends ComplexTransition {
012: private static PosixCharMap mapping = new PosixCharMap();
013: public static final SymbolTransition GLOBAL = new SymbolTransition(
014: SUCCESS);
015:
016: public static void setCharMap(PosixCharMap mappingIn) {
017: mapping = mappingIn;
018: if (mapping == null) {
019: mapping = new PosixCharMap();
020: }
021: }
022:
023: public static PosixCharMap getCharMap() {
024: return mapping;
025: }
026:
027: public SymbolTransition(int success) {
028: super (success);
029: //{{INIT_CONTROLS
030: //}}
031: }
032:
033: public boolean accepts(int c) {
034: return '<' == (char) c;
035: }
036:
037: protected void handleSuccess(Lex parser, StringBuffer output) {
038: String text = parser.getData();
039: String mappedText = mapping.mapKey(text);
040: if (mappedText != null) {
041: output.append(mappedText);
042: } else {
043: output.append(text);
044: }
045: }
046:
047: protected Lex.Transition[][] getStates() {
048: synchronized (getClass()) {
049: if (states == null) {
050: states = new Lex.Transition[][] {
051: { //state 0:
052: new Lex.CharTransition('<',
053: Lex.ACCUMULATE_CONSUME, -1),
054: new Lex.ParseExceptionTransition(
055: "illegal characters in symbol") },
056: { //state 1:
057: new Lex.CharTransition('/',
058: Lex.ACCUMULATE_CONSUME, -2),
059: new Lex.CharTransition('>',
060: Lex.ACCUMULATE_CONSUME, SUCCESS),
061: new Lex.StringTransition(
062: EOLTransition.EOL_CHARS,
063: Lex.IGNORE_PUTBACK, -3),
064: new Lex.EOFTransition(-3),
065: new Lex.DefaultTransition(
066: Lex.ACCUMULATE_CONSUME, -1) },
067: { //state 2:
068: new Lex.CharTransition('>',
069: Lex.ACCUMULATE_CONSUME, -1),
070: new Lex.CharTransition('/',
071: Lex.ACCUMULATE_CONSUME, -1),
072: new Lex.ParseExceptionTransition(
073: "illegal escape character in symbol") },
074: { //state 3: failure
075: new Lex.ParseExceptionTransition(
076: "unexpected end of line/file") } };
077: }
078: }
079: return states;
080: }
081:
082: private static Lex.Transition[][] states;
083:
084: public static void main(String args[]) {
085: try {
086: Lex.Transition[][] states = { {
087: new SymbolTransition(SUCCESS),
088: new Lex.EOFTransition(),
089: new Lex.ParseExceptionTransition("bad test input") } };
090: //String text = "<CAPITAL><\"<><//><V%><N6><CYRILLIC>";
091: String text = "<U><S><D> ";
092: StringReader sr = new StringReader(text);
093: PushbackReader pr = new PushbackReader(sr);
094: Lex parser = new Lex(states, pr);
095: //parser.debug(true);
096: int s = parser.nextToken();
097: while (s == SUCCESS) {
098: System.out.println(parser.getData());
099: s = parser.nextToken();
100: }
101: } catch (Exception e) {
102: System.out.println(e);
103: }
104: }
105: //{{DECLARE_CONTROLS
106: //}}
107: }
|