aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.tools.internal.jxc.gen.config; aoqi@0: aoqi@0: import org.xml.sax.Attributes; aoqi@0: import org.xml.sax.SAXException; aoqi@0: aoqi@0: /** aoqi@0: * Dispatches incoming events into sub handlers appropriately aoqi@0: * so that the interleaving semantics will be correctly realized. aoqi@0: * aoqi@0: *

aoqi@0: * Auto-generated, do not edit. aoqi@0: *

aoqi@0: * @author Kohsuke Kawaguchi (kk@kohsuke.org) aoqi@0: */ aoqi@0: public abstract class NGCCInterleaveFilter implements NGCCEventSource, NGCCEventReceiver { aoqi@0: protected NGCCInterleaveFilter( NGCCHandler parent, int cookie ) { aoqi@0: this._parent = parent; aoqi@0: this._cookie = cookie; aoqi@0: } aoqi@0: aoqi@0: protected void setHandlers( NGCCEventReceiver[] receivers ) { aoqi@0: this._receivers = receivers; aoqi@0: } aoqi@0: aoqi@0: /** event receiverse. */ aoqi@0: protected NGCCEventReceiver[] _receivers; aoqi@0: aoqi@0: public int replace(NGCCEventReceiver oldHandler, NGCCEventReceiver newHandler) { aoqi@0: for( int i=0; i<_receivers.length; i++ ) aoqi@0: if( _receivers[i]==oldHandler ) { aoqi@0: _receivers[i]=newHandler; aoqi@0: return i; aoqi@0: } aoqi@0: throw new InternalError(); // a bug in RelaxNGCC. aoqi@0: } aoqi@0: aoqi@0: aoqi@0: /** Parent handler. */ aoqi@0: private final NGCCHandler _parent; aoqi@0: /** Cookie given by the parent. */ aoqi@0: private final int _cookie; aoqi@0: aoqi@0: aoqi@0: aoqi@0: // aoqi@0: // aoqi@0: // event handler aoqi@0: // aoqi@0: // aoqi@0: /** aoqi@0: * Receiver that is being locked and therefore receives all the events. aoqi@0: *

aoqi@0:      * <interleave>
aoqi@0:      *   <element name="foo"/>
aoqi@0:      *   <element name="bar">
aoqi@0:      *     <element name="foo"/>
aoqi@0:      *   </element>
aoqi@0:      * </interlaeve>
aoqi@0:      * 
aoqi@0: * When processing inside the bar element, this receiver is aoqi@0: * "locked" so that it can correctly receive its child foo element. aoqi@0: */ aoqi@0: private int lockedReceiver; aoqi@0: /** aoqi@0: * Nest level. Lock will be release when the lockCount becomes 0. aoqi@0: */ aoqi@0: private int lockCount=0; aoqi@0: aoqi@0: public void enterElement( aoqi@0: String uri, String localName, String qname,Attributes atts) throws SAXException { aoqi@0: aoqi@0: if(isJoining) return; // ignore any token if we are joining. See joinByXXXX. aoqi@0: aoqi@0: if(lockCount++==0) { aoqi@0: lockedReceiver = findReceiverOfElement(uri,localName); aoqi@0: if(lockedReceiver==-1) { aoqi@0: // we can't process this token. join. aoqi@0: joinByEnterElement(null,uri,localName,qname,atts); aoqi@0: return; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: _receivers[lockedReceiver].enterElement(uri,localName,qname,atts); aoqi@0: } aoqi@0: public void leaveElement(String uri, String localName, String qname) throws SAXException { aoqi@0: if(isJoining) return; // ignore any token if we are joining. See joinByXXXX. aoqi@0: aoqi@0: if( lockCount-- == 0 ) aoqi@0: joinByLeaveElement(null,uri,localName,qname); aoqi@0: else aoqi@0: _receivers[lockedReceiver].leaveElement(uri,localName,qname); aoqi@0: } aoqi@0: public void enterAttribute(String uri, String localName, String qname) throws SAXException { aoqi@0: if(isJoining) return; // ignore any token if we are joining. See joinByXXXX. aoqi@0: aoqi@0: if(lockCount++==0) { aoqi@0: lockedReceiver = findReceiverOfAttribute(uri,localName); aoqi@0: if(lockedReceiver==-1) { aoqi@0: // we can't process this token. join. aoqi@0: joinByEnterAttribute(null,uri,localName,qname); aoqi@0: return; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: _receivers[lockedReceiver].enterAttribute(uri,localName,qname); aoqi@0: } aoqi@0: public void leaveAttribute(String uri, String localName, String qname) throws SAXException { aoqi@0: if(isJoining) return; // ignore any token if we are joining. See joinByXXXX. aoqi@0: aoqi@0: if( lockCount-- == 0 ) aoqi@0: joinByLeaveAttribute(null,uri,localName,qname); aoqi@0: else aoqi@0: _receivers[lockedReceiver].leaveAttribute(uri,localName,qname); aoqi@0: } aoqi@0: public void text(String value) throws SAXException { aoqi@0: if(isJoining) return; // ignore any token if we are joining. See joinByXXXX. aoqi@0: aoqi@0: if(lockCount!=0) aoqi@0: _receivers[lockedReceiver].text(value); aoqi@0: else { aoqi@0: int receiver = findReceiverOfText(); aoqi@0: if(receiver!=-1) _receivers[receiver].text(value); aoqi@0: else joinByText(null,value); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * Implemented by the generated code to determine the handler aoqi@0: * that can receive the given element. aoqi@0: * aoqi@0: * @return aoqi@0: * Thread ID of the receiver that can handle this event, aoqi@0: * or -1 if none. aoqi@0: */ aoqi@0: protected abstract int findReceiverOfElement( String uri, String local ); aoqi@0: aoqi@0: /** aoqi@0: * Returns the handler that can receive the given attribute, or null. aoqi@0: */ aoqi@0: protected abstract int findReceiverOfAttribute( String uri, String local ); aoqi@0: aoqi@0: /** aoqi@0: * Returns the handler that can receive text events, or null. aoqi@0: */ aoqi@0: protected abstract int findReceiverOfText(); aoqi@0: aoqi@0: aoqi@0: aoqi@0: aoqi@0: // aoqi@0: // aoqi@0: // join method aoqi@0: // aoqi@0: // aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * Set to true when this handler is in the process of aoqi@0: * joining all branches. aoqi@0: */ aoqi@0: private boolean isJoining = false; aoqi@0: aoqi@0: /** aoqi@0: * Joins all the child receivers. aoqi@0: * aoqi@0: *

aoqi@0: * This method is called by a child receiver when it sees aoqi@0: * something that it cannot handle, or by this object itself aoqi@0: * when it sees an event that it can't process. aoqi@0: * aoqi@0: *

aoqi@0: * This method forces children to move to its final state, aoqi@0: * then revert to the parent. aoqi@0: * aoqi@0: * @param source aoqi@0: * If this method is called by one of the child receivers, aoqi@0: * the receiver object. If this method is called by itself, aoqi@0: * null. aoqi@0: */ aoqi@0: public void joinByEnterElement( NGCCEventReceiver source, aoqi@0: String uri, String local, String qname, Attributes atts ) throws SAXException { aoqi@0: aoqi@0: if(isJoining) return; // we are already in the process of joining. ignore. aoqi@0: isJoining = true; aoqi@0: aoqi@0: // send special token to the rest of the branches. aoqi@0: // these branches don't understand this token, so they will aoqi@0: // try to move to a final state and send the token back to us, aoqi@0: // which this object will ignore (because isJoining==true) aoqi@0: // Otherwise branches will find an error. aoqi@0: for( int i=0; i<_receivers.length; i++ ) aoqi@0: if( _receivers[i]!=source ) aoqi@0: _receivers[i].enterElement(uri,local,qname,atts); aoqi@0: aoqi@0: // revert to the parent aoqi@0: _parent._source.replace(this,_parent); aoqi@0: _parent.onChildCompleted(null,_cookie,true); aoqi@0: // send this event to the parent aoqi@0: _parent.enterElement(uri,local,qname,atts); aoqi@0: } aoqi@0: aoqi@0: public void joinByLeaveElement( NGCCEventReceiver source, aoqi@0: String uri, String local, String qname ) throws SAXException { aoqi@0: aoqi@0: if(isJoining) return; // we are already in the process of joining. ignore. aoqi@0: isJoining = true; aoqi@0: aoqi@0: // send special token to the rest of the branches. aoqi@0: // these branches don't understand this token, so they will aoqi@0: // try to move to a final state and send the token back to us, aoqi@0: // which this object will ignore (because isJoining==true) aoqi@0: // Otherwise branches will find an error. aoqi@0: for( int i=0; i<_receivers.length; i++ ) aoqi@0: if( _receivers[i]!=source ) aoqi@0: _receivers[i].leaveElement(uri,local,qname); aoqi@0: aoqi@0: // revert to the parent aoqi@0: _parent._source.replace(this,_parent); aoqi@0: _parent.onChildCompleted(null,_cookie,true); aoqi@0: // send this event to the parent aoqi@0: _parent.leaveElement(uri,local,qname); aoqi@0: } aoqi@0: aoqi@0: public void joinByEnterAttribute( NGCCEventReceiver source, aoqi@0: String uri, String local, String qname ) throws SAXException { aoqi@0: aoqi@0: if(isJoining) return; // we are already in the process of joining. ignore. aoqi@0: isJoining = true; aoqi@0: aoqi@0: // send special token to the rest of the branches. aoqi@0: // these branches don't understand this token, so they will aoqi@0: // try to move to a final state and send the token back to us, aoqi@0: // which this object will ignore (because isJoining==true) aoqi@0: // Otherwise branches will find an error. aoqi@0: for( int i=0; i<_receivers.length; i++ ) aoqi@0: if( _receivers[i]!=source ) aoqi@0: _receivers[i].enterAttribute(uri,local,qname); aoqi@0: aoqi@0: // revert to the parent aoqi@0: _parent._source.replace(this,_parent); aoqi@0: _parent.onChildCompleted(null,_cookie,true); aoqi@0: // send this event to the parent aoqi@0: _parent.enterAttribute(uri,local,qname); aoqi@0: } aoqi@0: aoqi@0: public void joinByLeaveAttribute( NGCCEventReceiver source, aoqi@0: String uri, String local, String qname ) throws SAXException { aoqi@0: aoqi@0: if(isJoining) return; // we are already in the process of joining. ignore. aoqi@0: isJoining = true; aoqi@0: aoqi@0: // send special token to the rest of the branches. aoqi@0: // these branches don't understand this token, so they will aoqi@0: // try to move to a final state and send the token back to us, aoqi@0: // which this object will ignore (because isJoining==true) aoqi@0: // Otherwise branches will find an error. aoqi@0: for( int i=0; i<_receivers.length; i++ ) aoqi@0: if( _receivers[i]!=source ) aoqi@0: _receivers[i].leaveAttribute(uri,local,qname); aoqi@0: aoqi@0: // revert to the parent aoqi@0: _parent._source.replace(this,_parent); aoqi@0: _parent.onChildCompleted(null,_cookie,true); aoqi@0: // send this event to the parent aoqi@0: _parent.leaveAttribute(uri,local,qname); aoqi@0: } aoqi@0: aoqi@0: public void joinByText( NGCCEventReceiver source, aoqi@0: String value ) throws SAXException { aoqi@0: aoqi@0: if(isJoining) return; // we are already in the process of joining. ignore. aoqi@0: isJoining = true; aoqi@0: aoqi@0: // send special token to the rest of the branches. aoqi@0: // these branches don't understand this token, so they will aoqi@0: // try to move to a final state and send the token back to us, aoqi@0: // which this object will ignore (because isJoining==true) aoqi@0: // Otherwise branches will find an error. aoqi@0: for( int i=0; i<_receivers.length; i++ ) aoqi@0: if( _receivers[i]!=source ) aoqi@0: _receivers[i].text(value); aoqi@0: aoqi@0: // revert to the parent aoqi@0: _parent._source.replace(this,_parent); aoqi@0: _parent.onChildCompleted(null,_cookie,true); aoqi@0: // send this event to the parent aoqi@0: _parent.text(value); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: aoqi@0: // aoqi@0: // aoqi@0: // event dispatching methods aoqi@0: // aoqi@0: // aoqi@0: aoqi@0: public void sendEnterAttribute( int threadId, aoqi@0: String uri, String local, String qname) throws SAXException { aoqi@0: aoqi@0: _receivers[threadId].enterAttribute(uri,local,qname); aoqi@0: } aoqi@0: aoqi@0: public void sendEnterElement( int threadId, aoqi@0: String uri, String local, String qname, Attributes atts) throws SAXException { aoqi@0: aoqi@0: _receivers[threadId].enterElement(uri,local,qname,atts); aoqi@0: } aoqi@0: aoqi@0: public void sendLeaveAttribute( int threadId, aoqi@0: String uri, String local, String qname) throws SAXException { aoqi@0: aoqi@0: _receivers[threadId].leaveAttribute(uri,local,qname); aoqi@0: } aoqi@0: aoqi@0: public void sendLeaveElement( int threadId, aoqi@0: String uri, String local, String qname) throws SAXException { aoqi@0: aoqi@0: _receivers[threadId].leaveElement(uri,local,qname); aoqi@0: } aoqi@0: aoqi@0: public void sendText(int threadId, String value) throws SAXException { aoqi@0: _receivers[threadId].text(value); aoqi@0: } aoqi@0: aoqi@0: }