001: /*
002: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.tools.internal.jxc.gen.config;
027:
028: import org.xml.sax.Attributes;
029: import org.xml.sax.SAXException;
030:
031: /**
032: *
033: *
034: * @version $Id: NGCCHandler.java,v 1.9 2002/09/29 02:55:48 okajima Exp $
035: * @author Kohsuke Kawaguchi (kk@kohsuke.org)
036: */
037: public abstract class NGCCHandler implements NGCCEventReceiver {
038: protected NGCCHandler(NGCCEventSource source, NGCCHandler parent,
039: int parentCookie) {
040:
041: _parent = parent;
042: _source = source;
043: _cookie = parentCookie;
044: }
045:
046: /**
047: * Parent NGCCHandler, if any.
048: * If this is the root handler, this field will be null.
049: */
050: protected final NGCCHandler _parent;
051:
052: /**
053: * Event source.
054: */
055: protected final NGCCEventSource _source;
056:
057: /**
058: * This method will be implemented by the generated code
059: * and returns a reference to the current runtime.
060: */
061: protected abstract NGCCRuntime getRuntime();
062:
063: /**
064: * Cookie assigned by the parent.
065: *
066: * This value will be passed to the onChildCompleted handler
067: * of the parent.
068: */
069: protected final int _cookie;
070:
071: // used to copy parameters to (enter|leave)(Element|Attribute) events.
072: //protected String localName,uri,qname;
073:
074: /**
075: * Notifies the completion of a child object.
076: *
077: * @param result
078: * The parsing result of the child state.
079: * @param cookie
080: * The cookie value passed to the child object
081: * when it is created.
082: * @param needAttCheck
083: * This flag is true when the callee needs to call the
084: * processAttribute method to check attribute transitions.
085: * This flag is set to false when this method is triggered by
086: * attribute transition.
087: */
088: protected abstract void onChildCompleted(Object result, int cookie,
089: boolean needAttCheck) throws SAXException;
090:
091: //
092: //
093: // spawns a new child object from event handlers.
094: //
095: //
096: public void spawnChildFromEnterElement(NGCCEventReceiver child,
097: String uri, String localname, String qname, Attributes atts)
098: throws SAXException {
099:
100: int id = _source.replace(this , child);
101: _source.sendEnterElement(id, uri, localname, qname, atts);
102: }
103:
104: public void spawnChildFromEnterAttribute(NGCCEventReceiver child,
105: String uri, String localname, String qname)
106: throws SAXException {
107:
108: int id = _source.replace(this , child);
109: _source.sendEnterAttribute(id, uri, localname, qname);
110: }
111:
112: public void spawnChildFromLeaveElement(NGCCEventReceiver child,
113: String uri, String localname, String qname)
114: throws SAXException {
115:
116: int id = _source.replace(this , child);
117: _source.sendLeaveElement(id, uri, localname, qname);
118: }
119:
120: public void spawnChildFromLeaveAttribute(NGCCEventReceiver child,
121: String uri, String localname, String qname)
122: throws SAXException {
123:
124: int id = _source.replace(this , child);
125: _source.sendLeaveAttribute(id, uri, localname, qname);
126: }
127:
128: public void spawnChildFromText(NGCCEventReceiver child, String value)
129: throws SAXException {
130:
131: int id = _source.replace(this , child);
132: _source.sendText(id, value);
133: }
134:
135: //
136: //
137: // reverts to the parent object from the child handler
138: //
139: //
140: public void revertToParentFromEnterElement(Object result,
141: int cookie, String uri, String local, String qname,
142: Attributes atts) throws SAXException {
143:
144: int id = _source.replace(this , _parent);
145: _parent.onChildCompleted(result, cookie, true);
146: _source.sendEnterElement(id, uri, local, qname, atts);
147: }
148:
149: public void revertToParentFromLeaveElement(Object result,
150: int cookie, String uri, String local, String qname)
151: throws SAXException {
152:
153: if (uri == NGCCRuntime.IMPOSSIBLE && uri == local
154: && uri == qname && _parent == null)
155: // all the handlers are properly finalized.
156: // quit now, because we don't have any more NGCCHandler.
157: // see the endDocument handler for detail
158: return;
159:
160: int id = _source.replace(this , _parent);
161: _parent.onChildCompleted(result, cookie, true);
162: _source.sendLeaveElement(id, uri, local, qname);
163: }
164:
165: public void revertToParentFromEnterAttribute(Object result,
166: int cookie, String uri, String local, String qname)
167: throws SAXException {
168:
169: int id = _source.replace(this , _parent);
170: _parent.onChildCompleted(result, cookie, true);
171: _source.sendEnterAttribute(id, uri, local, qname);
172: }
173:
174: public void revertToParentFromLeaveAttribute(Object result,
175: int cookie, String uri, String local, String qname)
176: throws SAXException {
177:
178: int id = _source.replace(this , _parent);
179: _parent.onChildCompleted(result, cookie, true);
180: _source.sendLeaveAttribute(id, uri, local, qname);
181: }
182:
183: public void revertToParentFromText(Object result, int cookie,
184: String text) throws SAXException {
185:
186: int id = _source.replace(this , _parent);
187: _parent.onChildCompleted(result, cookie, true);
188: _source.sendText(id, text);
189: }
190:
191: //
192: //
193: // error handler
194: //
195: //
196: public void unexpectedEnterElement(String qname)
197: throws SAXException {
198: getRuntime().unexpectedX('<' + qname + '>');
199: }
200:
201: public void unexpectedLeaveElement(String qname)
202: throws SAXException {
203: getRuntime().unexpectedX("</" + qname + '>');
204: }
205:
206: public void unexpectedEnterAttribute(String qname)
207: throws SAXException {
208: getRuntime().unexpectedX('@' + qname);
209: }
210:
211: public void unexpectedLeaveAttribute(String qname)
212: throws SAXException {
213: getRuntime().unexpectedX("/@" + qname);
214: }
215: }
|