01: /*
02: * Copyright (C) Chaperon. All rights reserved.
03: * -------------------------------------------------------------------------
04: * This software is published under the terms of the Apache Software License
05: * version 1.1, a copy of which has been included with this distribution in
06: * the LICENSE file.
07: */
08:
09: package net.sourceforge.chaperon.process.extended;
10:
11: import net.sourceforge.chaperon.common.Decoder;
12:
13: import org.xml.sax.ContentHandler;
14: import org.xml.sax.SAXException;
15:
16: public class TerminalStackNode extends StackNode {
17: public TerminalStackNode(char character, State state,
18: StackNode ancestor) {
19: this .character = character;
20: this .state = state;
21:
22: this .ancestor = ancestor;
23:
24: if (ancestor != null)
25: this .ancestors = new StackNode[] { ancestor };
26: else
27: this .ancestors = new StackNode[0];
28:
29: if (ancestor != null) {
30: if ((character == '\n') /* && ((ancestor==null) || (text[position-1]!='\r'))*/) {
31: this .columnNumber = 1;
32: this .lineNumber = ancestor.lineNumber + 1;
33: } else if ((character == '\r') /* && ((position==(text.length-1)) || (text[position+1]!='\n'))*/) {
34: this .columnNumber = 1;
35: this .lineNumber = ancestor.lineNumber + 1;
36: } else {
37: this .columnNumber = ancestor.columnNumber + 1;
38: this .lineNumber = ancestor.lineNumber;
39: }
40: }
41: }
42:
43: public ShiftAction action = null;
44: public char character;
45:
46: public String toString() {
47: return String.valueOf(character);
48: }
49:
50: public String toCanonicalString(ExtendedParserAutomaton automaton) {
51: if (ancestors.length > 1) {
52: StringBuffer buffer = new StringBuffer();
53:
54: buffer.append("(");
55: for (int i = 0; i < ancestors.length; i++) {
56: if (i > 0)
57: buffer.append("|");
58:
59: buffer.append(ancestor.toCanonicalString(automaton));
60: }
61:
62: buffer.append(")");
63:
64: buffer.append("<-" + automaton.indexOf(state) + ":"
65: + Decoder.toChar(character));
66:
67: return buffer.toString();
68: }
69:
70: return (ancestor != null) ? (ancestor
71: .toCanonicalString(automaton)
72: + "<-" + automaton.indexOf(state) + ":" + Decoder
73: .toChar(character)) : (automaton.indexOf(state) + ":^");
74: }
75:
76: public void toXML(ContentHandler contentHandler)
77: throws SAXException {
78: contentHandler.characters(new char[] { character }, 0, 1);
79: }
80: }
|