1 /*
   2  * Copyright (c) 1997, 2013, 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  * Dispatches incoming events into sub handlers appropriately
  33  * so that the interleaving semantics will be correctly realized.
  34  *
  35  * <p><b>
  36  *     Auto-generated, do not edit.
  37  * </b></p>
  38  * @author Kohsuke Kawaguchi (kk@kohsuke.org)
  39  */
  40 public abstract class NGCCInterleaveFilter implements NGCCEventSource, NGCCEventReceiver {
  41     protected NGCCInterleaveFilter( NGCCHandler parent, int cookie ) {
  42         this._parent = parent;
  43         this._cookie = cookie;
  44     }
  45 
  46     protected void setHandlers( NGCCEventReceiver[] receivers ) {
  47         this._receivers = receivers;
  48     }
  49 
  50     /** event receiverse. */
  51     protected NGCCEventReceiver[] _receivers;
  52 
  53     public int replace(NGCCEventReceiver oldHandler, NGCCEventReceiver newHandler) {
  54         for( int i=0; i<_receivers.length; i++ )
  55             if( _receivers[i]==oldHandler ) {
  56                 _receivers[i]=newHandler;
  57                 return i;
  58             }
  59         throw new InternalError(); // a bug in RelaxNGCC.
  60     }
  61 
  62 
  63     /** Parent handler. */
  64     private final NGCCHandler _parent;
  65     /** Cookie given by the parent. */
  66     private final int _cookie;
  67 
  68 
  69 
  70 //
  71 //
  72 // event handler
  73 //
  74 //
  75     /**
  76      * Receiver that is being locked and therefore receives all the events.
  77      * <pre><xmp>
  78      * <interleave>
  79      *   <element name="foo"/>
  80      *   <element name="bar">
  81      *     <element name="foo"/>
  82      *   </element>
  83      * </interlaeve>
  84      * </xmp></pre>
  85      * When processing inside the bar element, this receiver is
  86      * "locked" so that it can correctly receive its child foo element.
  87      */
  88     private int lockedReceiver;
  89     /**
  90      * Nest level. Lock will be release when the lockCount becomes 0.
  91      */
  92     private int lockCount=0;
  93 
  94     public void enterElement(
  95         String uri, String localName, String qname,Attributes atts) throws SAXException {
  96 
  97         if(isJoining)   return; // ignore any token if we are joining. See joinByXXXX.
  98 
  99         if(lockCount++==0) {
 100             lockedReceiver = findReceiverOfElement(uri,localName);
 101             if(lockedReceiver==-1) {
 102                 // we can't process this token. join.
 103                 joinByEnterElement(null,uri,localName,qname,atts);
 104                 return;
 105             }
 106         }
 107 
 108         _receivers[lockedReceiver].enterElement(uri,localName,qname,atts);
 109     }
 110     public void leaveElement(String uri, String localName, String qname) throws SAXException {
 111         if(isJoining)   return; // ignore any token if we are joining. See joinByXXXX.
 112 
 113         if( lockCount-- == 0 )
 114             joinByLeaveElement(null,uri,localName,qname);
 115         else
 116             _receivers[lockedReceiver].leaveElement(uri,localName,qname);
 117     }
 118     public void enterAttribute(String uri, String localName, String qname) throws SAXException {
 119         if(isJoining)   return; // ignore any token if we are joining. See joinByXXXX.
 120 
 121         if(lockCount++==0) {
 122             lockedReceiver = findReceiverOfAttribute(uri,localName);
 123             if(lockedReceiver==-1) {
 124                 // we can't process this token. join.
 125                 joinByEnterAttribute(null,uri,localName,qname);
 126                 return;
 127             }
 128         }
 129 
 130         _receivers[lockedReceiver].enterAttribute(uri,localName,qname);
 131     }
 132     public void leaveAttribute(String uri, String localName, String qname) throws SAXException {
 133         if(isJoining)   return; // ignore any token if we are joining. See joinByXXXX.
 134 
 135         if( lockCount-- == 0 )
 136             joinByLeaveAttribute(null,uri,localName,qname);
 137         else
 138             _receivers[lockedReceiver].leaveAttribute(uri,localName,qname);
 139     }
 140     public void text(String value) throws SAXException {
 141         if(isJoining)   return; // ignore any token if we are joining. See joinByXXXX.
 142 
 143         if(lockCount!=0)
 144             _receivers[lockedReceiver].text(value);
 145         else {
 146             int receiver = findReceiverOfText();
 147             if(receiver!=-1)    _receivers[receiver].text(value);
 148             else                joinByText(null,value);
 149         }
 150     }
 151 
 152 
 153 
 154     /**
 155      * Implemented by the generated code to determine the handler
 156      * that can receive the given element.
 157      *
 158      * @return
 159      *      Thread ID of the receiver that can handle this event,
 160      *      or -1 if none.
 161      */
 162     protected abstract int findReceiverOfElement( String uri, String local );
 163 
 164     /**
 165      * Returns the handler that can receive the given attribute, or null.
 166      */
 167     protected abstract int findReceiverOfAttribute( String uri, String local );
 168 
 169     /**
 170      * Returns the handler that can receive text events, or null.
 171      */
 172     protected abstract int findReceiverOfText();
 173 
 174 
 175 
 176 
 177 //
 178 //
 179 // join method
 180 //
 181 //
 182 
 183 
 184     /**
 185      * Set to true when this handler is in the process of
 186      * joining all branches.
 187      */
 188     private boolean isJoining = false;
 189 
 190     /**
 191      * Joins all the child receivers.
 192      *
 193      * <p>
 194      * This method is called by a child receiver when it sees
 195      * something that it cannot handle, or by this object itself
 196      * when it sees an event that it can't process.
 197      *
 198      * <p>
 199      * This method forces children to move to its final state,
 200      * then revert to the parent.
 201      *
 202      * @param source
 203      *      If this method is called by one of the child receivers,
 204      *      the receiver object. If this method is called by itself,
 205      *      null.
 206      */
 207     public void joinByEnterElement( NGCCEventReceiver source,
 208         String uri, String local, String qname, Attributes atts ) throws SAXException {
 209 
 210         if(isJoining)   return; // we are already in the process of joining. ignore.
 211         isJoining = true;
 212 
 213         // send special token to the rest of the branches.
 214         // these branches don't understand this token, so they will
 215         // try to move to a final state and send the token back to us,
 216         // which this object will ignore (because isJoining==true)
 217         // Otherwise branches will find an error.
 218         for( int i=0; i<_receivers.length; i++ )
 219             if( _receivers[i]!=source )
 220                 _receivers[i].enterElement(uri,local,qname,atts);
 221 
 222         // revert to the parent
 223         _parent._source.replace(this,_parent);
 224         _parent.onChildCompleted(null,_cookie,true);
 225         // send this event to the parent
 226         _parent.enterElement(uri,local,qname,atts);
 227     }
 228 
 229     public void joinByLeaveElement( NGCCEventReceiver source,
 230         String uri, String local, String qname ) throws SAXException {
 231 
 232         if(isJoining)   return; // we are already in the process of joining. ignore.
 233         isJoining = true;
 234 
 235         // send special token to the rest of the branches.
 236         // these branches don't understand this token, so they will
 237         // try to move to a final state and send the token back to us,
 238         // which this object will ignore (because isJoining==true)
 239         // Otherwise branches will find an error.
 240         for( int i=0; i<_receivers.length; i++ )
 241             if( _receivers[i]!=source )
 242                 _receivers[i].leaveElement(uri,local,qname);
 243 
 244         // revert to the parent
 245         _parent._source.replace(this,_parent);
 246         _parent.onChildCompleted(null,_cookie,true);
 247         // send this event to the parent
 248         _parent.leaveElement(uri,local,qname);
 249     }
 250 
 251     public void joinByEnterAttribute( NGCCEventReceiver source,
 252         String uri, String local, String qname ) throws SAXException {
 253 
 254         if(isJoining)   return; // we are already in the process of joining. ignore.
 255         isJoining = true;
 256 
 257         // send special token to the rest of the branches.
 258         // these branches don't understand this token, so they will
 259         // try to move to a final state and send the token back to us,
 260         // which this object will ignore (because isJoining==true)
 261         // Otherwise branches will find an error.
 262         for( int i=0; i<_receivers.length; i++ )
 263             if( _receivers[i]!=source )
 264                 _receivers[i].enterAttribute(uri,local,qname);
 265 
 266         // revert to the parent
 267         _parent._source.replace(this,_parent);
 268         _parent.onChildCompleted(null,_cookie,true);
 269         // send this event to the parent
 270         _parent.enterAttribute(uri,local,qname);
 271     }
 272 
 273     public void joinByLeaveAttribute( NGCCEventReceiver source,
 274         String uri, String local, String qname ) throws SAXException {
 275 
 276         if(isJoining)   return; // we are already in the process of joining. ignore.
 277         isJoining = true;
 278 
 279         // send special token to the rest of the branches.
 280         // these branches don't understand this token, so they will
 281         // try to move to a final state and send the token back to us,
 282         // which this object will ignore (because isJoining==true)
 283         // Otherwise branches will find an error.
 284         for( int i=0; i<_receivers.length; i++ )
 285             if( _receivers[i]!=source )
 286                 _receivers[i].leaveAttribute(uri,local,qname);
 287 
 288         // revert to the parent
 289         _parent._source.replace(this,_parent);
 290         _parent.onChildCompleted(null,_cookie,true);
 291         // send this event to the parent
 292         _parent.leaveAttribute(uri,local,qname);
 293     }
 294 
 295     public void joinByText( NGCCEventReceiver source,
 296         String value ) throws SAXException {
 297 
 298         if(isJoining)   return; // we are already in the process of joining. ignore.
 299         isJoining = true;
 300 
 301         // send special token to the rest of the branches.
 302         // these branches don't understand this token, so they will
 303         // try to move to a final state and send the token back to us,
 304         // which this object will ignore (because isJoining==true)
 305         // Otherwise branches will find an error.
 306         for( int i=0; i<_receivers.length; i++ )
 307             if( _receivers[i]!=source )
 308                 _receivers[i].text(value);
 309 
 310         // revert to the parent
 311         _parent._source.replace(this,_parent);
 312         _parent.onChildCompleted(null,_cookie,true);
 313         // send this event to the parent
 314         _parent.text(value);
 315     }
 316 
 317 
 318 
 319 //
 320 //
 321 // event dispatching methods
 322 //
 323 //
 324 
 325     public void sendEnterAttribute( int threadId,
 326         String uri, String local, String qname) throws SAXException {
 327 
 328         _receivers[threadId].enterAttribute(uri,local,qname);
 329     }
 330 
 331     public void sendEnterElement( int threadId,
 332         String uri, String local, String qname, Attributes atts) throws SAXException {
 333 
 334         _receivers[threadId].enterElement(uri,local,qname,atts);
 335     }
 336 
 337     public void sendLeaveAttribute( int threadId,
 338         String uri, String local, String qname) throws SAXException {
 339 
 340         _receivers[threadId].leaveAttribute(uri,local,qname);
 341     }
 342 
 343     public void sendLeaveElement( int threadId,
 344         String uri, String local, String qname) throws SAXException {
 345 
 346         _receivers[threadId].leaveElement(uri,local,qname);
 347     }
 348 
 349     public void sendText(int threadId, String value) throws SAXException {
 350         _receivers[threadId].text(value);
 351     }
 352 
 353 }