001: package com.sun.tools.jxc.gen.config;
002:
003: import org.xml.sax.Attributes;
004: import org.xml.sax.SAXException;
005:
006: /**
007: *
008: *
009: * @version $Id: NGCCHandler.java,v 1.9 2002/09/29 02:55:48 okajima Exp $
010: * @author Kohsuke Kawaguchi (kk@kohsuke.org)
011: */
012: public abstract class NGCCHandler implements NGCCEventReceiver {
013: protected NGCCHandler(NGCCEventSource source, NGCCHandler parent,
014: int parentCookie) {
015:
016: _parent = parent;
017: _source = source;
018: _cookie = parentCookie;
019: }
020:
021: /**
022: * Parent NGCCHandler, if any.
023: * If this is the root handler, this field will be null.
024: */
025: protected final NGCCHandler _parent;
026:
027: /**
028: * Event source.
029: */
030: protected final NGCCEventSource _source;
031:
032: /**
033: * This method will be implemented by the generated code
034: * and returns a reference to the current runtime.
035: */
036: protected abstract NGCCRuntime getRuntime();
037:
038: /**
039: * Cookie assigned by the parent.
040: *
041: * This value will be passed to the onChildCompleted handler
042: * of the parent.
043: */
044: protected final int _cookie;
045:
046: // used to copy parameters to (enter|leave)(Element|Attribute) events.
047: //protected String localName,uri,qname;
048:
049: /**
050: * Notifies the completion of a child object.
051: *
052: * @param result
053: * The parsing result of the child state.
054: * @param cookie
055: * The cookie value passed to the child object
056: * when it is created.
057: * @param needAttCheck
058: * This flag is true when the callee needs to call the
059: * processAttribute method to check attribute transitions.
060: * This flag is set to false when this method is triggered by
061: * attribute transition.
062: */
063: protected abstract void onChildCompleted(Object result, int cookie,
064: boolean needAttCheck) throws SAXException;
065:
066: //
067: //
068: // spawns a new child object from event handlers.
069: //
070: //
071: public void spawnChildFromEnterElement(NGCCEventReceiver child,
072: String uri, String localname, String qname, Attributes atts)
073: throws SAXException {
074:
075: int id = _source.replace(this , child);
076: _source.sendEnterElement(id, uri, localname, qname, atts);
077: }
078:
079: public void spawnChildFromEnterAttribute(NGCCEventReceiver child,
080: String uri, String localname, String qname)
081: throws SAXException {
082:
083: int id = _source.replace(this , child);
084: _source.sendEnterAttribute(id, uri, localname, qname);
085: }
086:
087: public void spawnChildFromLeaveElement(NGCCEventReceiver child,
088: String uri, String localname, String qname)
089: throws SAXException {
090:
091: int id = _source.replace(this , child);
092: _source.sendLeaveElement(id, uri, localname, qname);
093: }
094:
095: public void spawnChildFromLeaveAttribute(NGCCEventReceiver child,
096: String uri, String localname, String qname)
097: throws SAXException {
098:
099: int id = _source.replace(this , child);
100: _source.sendLeaveAttribute(id, uri, localname, qname);
101: }
102:
103: public void spawnChildFromText(NGCCEventReceiver child, String value)
104: throws SAXException {
105:
106: int id = _source.replace(this , child);
107: _source.sendText(id, value);
108: }
109:
110: //
111: //
112: // reverts to the parent object from the child handler
113: //
114: //
115: public void revertToParentFromEnterElement(Object result,
116: int cookie, String uri, String local, String qname,
117: Attributes atts) throws SAXException {
118:
119: int id = _source.replace(this , _parent);
120: _parent.onChildCompleted(result, cookie, true);
121: _source.sendEnterElement(id, uri, local, qname, atts);
122: }
123:
124: public void revertToParentFromLeaveElement(Object result,
125: int cookie, String uri, String local, String qname)
126: throws SAXException {
127:
128: if (uri == NGCCRuntime.IMPOSSIBLE && uri == local
129: && uri == qname && _parent == null)
130: // all the handlers are properly finalized.
131: // quit now, because we don't have any more NGCCHandler.
132: // see the endDocument handler for detail
133: return;
134:
135: int id = _source.replace(this , _parent);
136: _parent.onChildCompleted(result, cookie, true);
137: _source.sendLeaveElement(id, uri, local, qname);
138: }
139:
140: public void revertToParentFromEnterAttribute(Object result,
141: int cookie, String uri, String local, String qname)
142: throws SAXException {
143:
144: int id = _source.replace(this , _parent);
145: _parent.onChildCompleted(result, cookie, true);
146: _source.sendEnterAttribute(id, uri, local, qname);
147: }
148:
149: public void revertToParentFromLeaveAttribute(Object result,
150: int cookie, String uri, String local, String qname)
151: throws SAXException {
152:
153: int id = _source.replace(this , _parent);
154: _parent.onChildCompleted(result, cookie, true);
155: _source.sendLeaveAttribute(id, uri, local, qname);
156: }
157:
158: public void revertToParentFromText(Object result, int cookie,
159: String text) throws SAXException {
160:
161: int id = _source.replace(this , _parent);
162: _parent.onChildCompleted(result, cookie, true);
163: _source.sendText(id, text);
164: }
165:
166: //
167: //
168: // error handler
169: //
170: //
171: public void unexpectedEnterElement(String qname)
172: throws SAXException {
173: getRuntime().unexpectedX('<' + qname + '>');
174: }
175:
176: public void unexpectedLeaveElement(String qname)
177: throws SAXException {
178: getRuntime().unexpectedX("</" + qname + '>');
179: }
180:
181: public void unexpectedEnterAttribute(String qname)
182: throws SAXException {
183: getRuntime().unexpectedX('@' + qname);
184: }
185:
186: public void unexpectedLeaveAttribute(String qname)
187: throws SAXException {
188: getRuntime().unexpectedX("/@" + qname);
189: }
190: }
|