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