1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/tools/internal/jxc/gen/config/NGCCInterleaveFilter.java Tue Mar 06 16:09:35 2012 -0800 1.3 @@ -0,0 +1,350 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.tools.internal.jxc.gen.config; 1.30 + 1.31 +import org.xml.sax.Attributes; 1.32 +import org.xml.sax.SAXException; 1.33 + 1.34 +/** 1.35 + * Dispatches incoming events into sub handlers appropriately 1.36 + * so that the interleaving semantics will be correctly realized. 1.37 + * 1.38 + * @author Kohsuke Kawaguchi (kk@kohsuke.org) 1.39 + */ 1.40 +public abstract class NGCCInterleaveFilter implements NGCCEventSource, NGCCEventReceiver { 1.41 + protected NGCCInterleaveFilter( NGCCHandler parent, int cookie ) { 1.42 + this._parent = parent; 1.43 + this._cookie = cookie; 1.44 + } 1.45 + 1.46 + protected void setHandlers( NGCCEventReceiver[] receivers ) { 1.47 + this._receivers = receivers; 1.48 + } 1.49 + 1.50 + /** event receiverse. */ 1.51 + protected NGCCEventReceiver[] _receivers; 1.52 + 1.53 + public int replace(NGCCEventReceiver oldHandler, NGCCEventReceiver newHandler) { 1.54 + for( int i=0; i<_receivers.length; i++ ) 1.55 + if( _receivers[i]==oldHandler ) { 1.56 + _receivers[i]=newHandler; 1.57 + return i; 1.58 + } 1.59 + throw new InternalError(); // a bug in RelaxNGCC. 1.60 + } 1.61 + 1.62 + 1.63 + /** Parent handler. */ 1.64 + private final NGCCHandler _parent; 1.65 + /** Cookie given by the parent. */ 1.66 + private final int _cookie; 1.67 + 1.68 + 1.69 + 1.70 +// 1.71 +// 1.72 +// event handler 1.73 +// 1.74 +// 1.75 + /** 1.76 + * Receiver that is being locked and therefore receives all the events. 1.77 + * <pre><xmp> 1.78 + * <interleave> 1.79 + * <element name="foo"/> 1.80 + * <element name="bar"> 1.81 + * <element name="foo"/> 1.82 + * </element> 1.83 + * </interlaeve> 1.84 + * </xmp></pre> 1.85 + * When processing inside the bar element, this receiver is 1.86 + * "locked" so that it can correctly receive its child foo element. 1.87 + */ 1.88 + private int lockedReceiver; 1.89 + /** 1.90 + * Nest level. Lock will be release when the lockCount becomes 0. 1.91 + */ 1.92 + private int lockCount=0; 1.93 + 1.94 + public void enterElement( 1.95 + String uri, String localName, String qname,Attributes atts) throws SAXException { 1.96 + 1.97 + if(isJoining) return; // ignore any token if we are joining. See joinByXXXX. 1.98 + 1.99 + if(lockCount++==0) { 1.100 + lockedReceiver = findReceiverOfElement(uri,localName); 1.101 + if(lockedReceiver==-1) { 1.102 + // we can't process this token. join. 1.103 + joinByEnterElement(null,uri,localName,qname,atts); 1.104 + return; 1.105 + } 1.106 + } 1.107 + 1.108 + _receivers[lockedReceiver].enterElement(uri,localName,qname,atts); 1.109 + } 1.110 + public void leaveElement(String uri, String localName, String qname) throws SAXException { 1.111 + if(isJoining) return; // ignore any token if we are joining. See joinByXXXX. 1.112 + 1.113 + if( lockCount-- == 0 ) 1.114 + joinByLeaveElement(null,uri,localName,qname); 1.115 + else 1.116 + _receivers[lockedReceiver].leaveElement(uri,localName,qname); 1.117 + } 1.118 + public void enterAttribute(String uri, String localName, String qname) throws SAXException { 1.119 + if(isJoining) return; // ignore any token if we are joining. See joinByXXXX. 1.120 + 1.121 + if(lockCount++==0) { 1.122 + lockedReceiver = findReceiverOfAttribute(uri,localName); 1.123 + if(lockedReceiver==-1) { 1.124 + // we can't process this token. join. 1.125 + joinByEnterAttribute(null,uri,localName,qname); 1.126 + return; 1.127 + } 1.128 + } 1.129 + 1.130 + _receivers[lockedReceiver].enterAttribute(uri,localName,qname); 1.131 + } 1.132 + public void leaveAttribute(String uri, String localName, String qname) throws SAXException { 1.133 + if(isJoining) return; // ignore any token if we are joining. See joinByXXXX. 1.134 + 1.135 + if( lockCount-- == 0 ) 1.136 + joinByLeaveAttribute(null,uri,localName,qname); 1.137 + else 1.138 + _receivers[lockedReceiver].leaveAttribute(uri,localName,qname); 1.139 + } 1.140 + public void text(String value) throws SAXException { 1.141 + if(isJoining) return; // ignore any token if we are joining. See joinByXXXX. 1.142 + 1.143 + if(lockCount!=0) 1.144 + _receivers[lockedReceiver].text(value); 1.145 + else { 1.146 + int receiver = findReceiverOfText(); 1.147 + if(receiver!=-1) _receivers[receiver].text(value); 1.148 + else joinByText(null,value); 1.149 + } 1.150 + } 1.151 + 1.152 + 1.153 + 1.154 + /** 1.155 + * Implemented by the generated code to determine the handler 1.156 + * that can receive the given element. 1.157 + * 1.158 + * @return 1.159 + * Thread ID of the receiver that can handle this event, 1.160 + * or -1 if none. 1.161 + */ 1.162 + protected abstract int findReceiverOfElement( String uri, String local ); 1.163 + 1.164 + /** 1.165 + * Returns the handler that can receive the given attribute, or null. 1.166 + */ 1.167 + protected abstract int findReceiverOfAttribute( String uri, String local ); 1.168 + 1.169 + /** 1.170 + * Returns the handler that can receive text events, or null. 1.171 + */ 1.172 + protected abstract int findReceiverOfText(); 1.173 + 1.174 + 1.175 + 1.176 + 1.177 +// 1.178 +// 1.179 +// join method 1.180 +// 1.181 +// 1.182 + 1.183 + 1.184 + /** 1.185 + * Set to true when this handler is in the process of 1.186 + * joining all branches. 1.187 + */ 1.188 + private boolean isJoining = false; 1.189 + 1.190 + /** 1.191 + * Joins all the child receivers. 1.192 + * 1.193 + * <p> 1.194 + * This method is called by a child receiver when it sees 1.195 + * something that it cannot handle, or by this object itself 1.196 + * when it sees an event that it can't process. 1.197 + * 1.198 + * <p> 1.199 + * This method forces children to move to its final state, 1.200 + * then revert to the parent. 1.201 + * 1.202 + * @param source 1.203 + * If this method is called by one of the child receivers, 1.204 + * the receiver object. If this method is called by itself, 1.205 + * null. 1.206 + */ 1.207 + public void joinByEnterElement( NGCCEventReceiver source, 1.208 + String uri, String local, String qname, Attributes atts ) throws SAXException { 1.209 + 1.210 + if(isJoining) return; // we are already in the process of joining. ignore. 1.211 + isJoining = true; 1.212 + 1.213 + // send special token to the rest of the branches. 1.214 + // these branches don't understand this token, so they will 1.215 + // try to move to a final state and send the token back to us, 1.216 + // which this object will ignore (because isJoining==true) 1.217 + // Otherwise branches will find an error. 1.218 + for( int i=0; i<_receivers.length; i++ ) 1.219 + if( _receivers[i]!=source ) 1.220 + _receivers[i].enterElement(uri,local,qname,atts); 1.221 + 1.222 + // revert to the parent 1.223 + _parent._source.replace(this,_parent); 1.224 + _parent.onChildCompleted(null,_cookie,true); 1.225 + // send this event to the parent 1.226 + _parent.enterElement(uri,local,qname,atts); 1.227 + } 1.228 + 1.229 + public void joinByLeaveElement( NGCCEventReceiver source, 1.230 + String uri, String local, String qname ) throws SAXException { 1.231 + 1.232 + if(isJoining) return; // we are already in the process of joining. ignore. 1.233 + isJoining = true; 1.234 + 1.235 + // send special token to the rest of the branches. 1.236 + // these branches don't understand this token, so they will 1.237 + // try to move to a final state and send the token back to us, 1.238 + // which this object will ignore (because isJoining==true) 1.239 + // Otherwise branches will find an error. 1.240 + for( int i=0; i<_receivers.length; i++ ) 1.241 + if( _receivers[i]!=source ) 1.242 + _receivers[i].leaveElement(uri,local,qname); 1.243 + 1.244 + // revert to the parent 1.245 + _parent._source.replace(this,_parent); 1.246 + _parent.onChildCompleted(null,_cookie,true); 1.247 + // send this event to the parent 1.248 + _parent.leaveElement(uri,local,qname); 1.249 + } 1.250 + 1.251 + public void joinByEnterAttribute( NGCCEventReceiver source, 1.252 + String uri, String local, String qname ) throws SAXException { 1.253 + 1.254 + if(isJoining) return; // we are already in the process of joining. ignore. 1.255 + isJoining = true; 1.256 + 1.257 + // send special token to the rest of the branches. 1.258 + // these branches don't understand this token, so they will 1.259 + // try to move to a final state and send the token back to us, 1.260 + // which this object will ignore (because isJoining==true) 1.261 + // Otherwise branches will find an error. 1.262 + for( int i=0; i<_receivers.length; i++ ) 1.263 + if( _receivers[i]!=source ) 1.264 + _receivers[i].enterAttribute(uri,local,qname); 1.265 + 1.266 + // revert to the parent 1.267 + _parent._source.replace(this,_parent); 1.268 + _parent.onChildCompleted(null,_cookie,true); 1.269 + // send this event to the parent 1.270 + _parent.enterAttribute(uri,local,qname); 1.271 + } 1.272 + 1.273 + public void joinByLeaveAttribute( NGCCEventReceiver source, 1.274 + String uri, String local, String qname ) throws SAXException { 1.275 + 1.276 + if(isJoining) return; // we are already in the process of joining. ignore. 1.277 + isJoining = true; 1.278 + 1.279 + // send special token to the rest of the branches. 1.280 + // these branches don't understand this token, so they will 1.281 + // try to move to a final state and send the token back to us, 1.282 + // which this object will ignore (because isJoining==true) 1.283 + // Otherwise branches will find an error. 1.284 + for( int i=0; i<_receivers.length; i++ ) 1.285 + if( _receivers[i]!=source ) 1.286 + _receivers[i].leaveAttribute(uri,local,qname); 1.287 + 1.288 + // revert to the parent 1.289 + _parent._source.replace(this,_parent); 1.290 + _parent.onChildCompleted(null,_cookie,true); 1.291 + // send this event to the parent 1.292 + _parent.leaveAttribute(uri,local,qname); 1.293 + } 1.294 + 1.295 + public void joinByText( NGCCEventReceiver source, 1.296 + String value ) throws SAXException { 1.297 + 1.298 + if(isJoining) return; // we are already in the process of joining. ignore. 1.299 + isJoining = true; 1.300 + 1.301 + // send special token to the rest of the branches. 1.302 + // these branches don't understand this token, so they will 1.303 + // try to move to a final state and send the token back to us, 1.304 + // which this object will ignore (because isJoining==true) 1.305 + // Otherwise branches will find an error. 1.306 + for( int i=0; i<_receivers.length; i++ ) 1.307 + if( _receivers[i]!=source ) 1.308 + _receivers[i].text(value); 1.309 + 1.310 + // revert to the parent 1.311 + _parent._source.replace(this,_parent); 1.312 + _parent.onChildCompleted(null,_cookie,true); 1.313 + // send this event to the parent 1.314 + _parent.text(value); 1.315 + } 1.316 + 1.317 + 1.318 + 1.319 +// 1.320 +// 1.321 +// event dispatching methods 1.322 +// 1.323 +// 1.324 + 1.325 + public void sendEnterAttribute( int threadId, 1.326 + String uri, String local, String qname) throws SAXException { 1.327 + 1.328 + _receivers[threadId].enterAttribute(uri,local,qname); 1.329 + } 1.330 + 1.331 + public void sendEnterElement( int threadId, 1.332 + String uri, String local, String qname, Attributes atts) throws SAXException { 1.333 + 1.334 + _receivers[threadId].enterElement(uri,local,qname,atts); 1.335 + } 1.336 + 1.337 + public void sendLeaveAttribute( int threadId, 1.338 + String uri, String local, String qname) throws SAXException { 1.339 + 1.340 + _receivers[threadId].leaveAttribute(uri,local,qname); 1.341 + } 1.342 + 1.343 + public void sendLeaveElement( int threadId, 1.344 + String uri, String local, String qname) throws SAXException { 1.345 + 1.346 + _receivers[threadId].leaveElement(uri,local,qname); 1.347 + } 1.348 + 1.349 + public void sendText(int threadId, String value) throws SAXException { 1.350 + _receivers[threadId].text(value); 1.351 + } 1.352 + 1.353 +}