001    /** 
002     * 
003     * Copyright 2004 Protique Ltd
004     * 
005     * Licensed under the Apache License, Version 2.0 (the "License"); 
006     * you may not use this file except in compliance with the License. 
007     * You may obtain a copy of the License at 
008     * 
009     * http://www.apache.org/licenses/LICENSE-2.0
010     * 
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS, 
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
014     * See the License for the specific language governing permissions and 
015     * limitations under the License. 
016     * 
017     **/
018    package org.codehaus.activesoap.handler;
019    
020    import javanet.staxutils.XMLStreamUtils;
021    import org.codehaus.activesoap.Handler;
022    import org.codehaus.activesoap.MessageExchange;
023    import org.codehaus.activesoap.HandlerRegistry;
024    import org.codehaus.activesoap.HandlerRegistry;
025    import org.codehaus.activesoap.util.DocumentFilterXMLStreamReader;
026    import org.codehaus.activesoap.util.XMLStreamHelper;
027    
028    import javax.xml.namespace.QName;
029    import javax.xml.stream.XMLStreamConstants;
030    import javax.xml.stream.XMLStreamReader;
031    import javax.xml.stream.XMLStreamWriter;
032    
033    /**
034     * Looks up the correct {@link Handler} to use
035     * based on the {@link QName} of the root element
036     *
037     * @version $Revision: 1.10 $
038     */
039    public class QNameHandler implements Handler, XMLStreamConstants {
040        private HandlerRegistry handlerRegistry;
041    
042        public QNameHandler() {
043            this(new DefaultHandlerRegistry());
044        }
045    
046        public QNameHandler(HandlerRegistry handlerRegistry) {
047            this.handlerRegistry = handlerRegistry;
048        }
049    
050        public HandlerRegistry getHandlerRegistry() {
051            return handlerRegistry;
052        }
053    
054    
055        public void invoke(MessageExchange exchange) throws Exception {
056            XMLStreamReader in = exchange.getIn();
057            XMLStreamWriter out = exchange.getOut();
058            if (XMLStreamHelper.skipToStartOfElement(in)) {
059                QName name = in.getName();
060                Handler child = handlerRegistry.getHandler(name);
061                if (child != null) {
062                    child.invoke(exchange.newInstance(new DocumentFilterXMLStreamReader(name, in), out));
063                }
064                else {
065                    Handler defaultHandler = handlerRegistry.getDefaultHandler();
066                    if (defaultHandler != null) {
067                        defaultHandler.invoke(exchange.newInstance(new DocumentFilterXMLStreamReader(name, in), out));
068                    }
069                    else {
070                        // lets skip the content
071                        XMLStreamUtils.skipElement(in);
072                    }
073                }
074            }
075        }
076    }