1 /*
   2  * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.tools.internal.jxc.gen.config;
  27 
  28 import org.xml.sax.Attributes;
  29 import org.xml.sax.SAXException;
  30 
  31 /**
  32  *
  33  *
  34  * @version $Id: NGCCHandler.java,v 1.9 2002/09/29 02:55:48 okajima Exp $
  35  * @author Kohsuke Kawaguchi (kk@kohsuke.org)
  36  */
  37 public abstract class NGCCHandler implements NGCCEventReceiver {
  38     protected NGCCHandler( NGCCEventSource source, NGCCHandler parent, int parentCookie ) {
  39 
  40         _parent = parent;
  41         _source = source;
  42         _cookie = parentCookie;
  43     }
  44 
  45     /**
  46      * Parent NGCCHandler, if any.
  47      * If this is the root handler, this field will be null.
  48      */
  49     protected final NGCCHandler _parent;
  50 
  51     /**
  52      * Event source.
  53      */
  54     protected final NGCCEventSource _source;
  55 
  56     /**
  57      * This method will be implemented by the generated code
  58      * and returns a reference to the current runtime.
  59      */
  60     protected abstract NGCCRuntime getRuntime();
  61 
  62     /**
  63      * Cookie assigned by the parent.
  64      *
  65      * This value will be passed to the onChildCompleted handler
  66      * of the parent.
  67      */
  68     protected final int _cookie;
  69 
  70     // used to copy parameters to (enter|leave)(Element|Attribute) events.
  71     //protected String localName,uri,qname;
  72 
  73 
  74     /**
  75      * Notifies the completion of a child object.
  76      *
  77      * @param result
  78      *      The parsing result of the child state.
  79      * @param cookie
  80      *      The cookie value passed to the child object
  81      *      when it is created.
  82      * @param needAttCheck
  83      *      This flag is true when the callee needs to call the
  84      *      processAttribute method to check attribute transitions.
  85      *      This flag is set to false when this method is triggered by
  86      *      attribute transition.
  87      */
  88     protected abstract void onChildCompleted( Object result, int cookie, boolean needAttCheck ) throws SAXException;
  89 
  90 //
  91 //
  92 // spawns a new child object from event handlers.
  93 //
  94 //
  95     public void spawnChildFromEnterElement( NGCCEventReceiver child,
  96         String uri, String localname, String qname, Attributes atts) throws SAXException {
  97 
  98         int id = _source.replace(this,child);
  99         _source.sendEnterElement(id,uri,localname,qname,atts);
 100     }
 101     public void spawnChildFromEnterAttribute( NGCCEventReceiver child,
 102         String uri, String localname, String qname) throws SAXException {
 103 
 104         int id = _source.replace(this,child);
 105         _source.sendEnterAttribute(id,uri,localname,qname);
 106     }
 107     public void spawnChildFromLeaveElement( NGCCEventReceiver child,
 108         String uri, String localname, String qname) throws SAXException {
 109 
 110         int id = _source.replace(this,child);
 111         _source.sendLeaveElement(id,uri,localname,qname);
 112     }
 113     public void spawnChildFromLeaveAttribute( NGCCEventReceiver child,
 114         String uri, String localname, String qname) throws SAXException {
 115 
 116         int id = _source.replace(this,child);
 117         _source.sendLeaveAttribute(id,uri,localname,qname);
 118     }
 119     public void spawnChildFromText( NGCCEventReceiver child,
 120         String value) throws SAXException {
 121 
 122         int id = _source.replace(this,child);
 123         _source.sendText(id,value);
 124     }
 125 
 126 //
 127 //
 128 // reverts to the parent object from the child handler
 129 //
 130 //
 131     public void revertToParentFromEnterElement( Object result, int cookie,
 132         String uri,String local,String qname, Attributes atts ) throws SAXException {
 133 
 134         int id = _source.replace(this,_parent);
 135         _parent.onChildCompleted(result,cookie,true);
 136         _source.sendEnterElement(id,uri,local,qname,atts);
 137     }
 138     public void revertToParentFromLeaveElement( Object result, int cookie,
 139         String uri,String local,String qname ) throws SAXException {
 140 
 141         if(uri==NGCCRuntime.IMPOSSIBLE && uri==local && uri==qname && _parent==null )
 142             // all the handlers are properly finalized.
 143             // quit now, because we don't have any more NGCCHandler.
 144             // see the endDocument handler for detail
 145             return;
 146 
 147         int id = _source.replace(this,_parent);
 148         _parent.onChildCompleted(result,cookie,true);
 149         _source.sendLeaveElement(id,uri,local,qname);
 150     }
 151     public void revertToParentFromEnterAttribute( Object result, int cookie,
 152         String uri,String local,String qname ) throws SAXException {
 153 
 154         int id = _source.replace(this,_parent);
 155         _parent.onChildCompleted(result,cookie,true);
 156         _source.sendEnterAttribute(id,uri,local,qname);
 157     }
 158     public void revertToParentFromLeaveAttribute( Object result, int cookie,
 159         String uri,String local,String qname ) throws SAXException {
 160 
 161         int id = _source.replace(this,_parent);
 162         _parent.onChildCompleted(result,cookie,true);
 163         _source.sendLeaveAttribute(id,uri,local,qname);
 164     }
 165     public void revertToParentFromText( Object result, int cookie,
 166         String text ) throws SAXException {
 167 
 168         int id = _source.replace(this,_parent);
 169         _parent.onChildCompleted(result,cookie,true);
 170         _source.sendText(id,text);
 171     }
 172 
 173 
 174 //
 175 //
 176 // error handler
 177 //
 178 //
 179     public void unexpectedEnterElement(String qname) throws SAXException {
 180         getRuntime().unexpectedX('<'+qname+'>');
 181     }
 182     public void unexpectedLeaveElement(String qname) throws SAXException {
 183         getRuntime().unexpectedX("</"+qname+'>');
 184     }
 185     public void unexpectedEnterAttribute(String qname) throws SAXException {
 186         getRuntime().unexpectedX('@'+qname);
 187     }
 188     public void unexpectedLeaveAttribute(String qname) throws SAXException {
 189         getRuntime().unexpectedX("/@"+qname);
 190     }
 191 }