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.stax;
019    
020    import org.codehaus.activesoap.Handler;
021    import org.codehaus.activesoap.MessageExchange;
022    import org.codehaus.activesoap.handler.stax.AnyAttribute;
023    import org.codehaus.activesoap.handler.stax.AnyContent;
024    import org.codehaus.activesoap.handler.stax.AnyElementMarshaler;
025    
026    import javax.xml.namespace.QName;
027    import javax.xml.stream.XMLStreamConstants;
028    import javax.xml.stream.XMLStreamException;
029    import javax.xml.stream.XMLStreamReader;
030    
031    /**
032     * A useful base class for any handlers implementd directly on top of StAX\ 
033     * @version $Revision: 1.2 $
034     */
035    public abstract class StaxHandler implements Handler {
036    
037        protected void populateAnyContent(MessageExchange exchange, AnyElementMarshaler anyElementMarshaler, AnyContent property) throws XMLStreamException {
038            XMLStreamReader in = exchange.getIn();
039            boolean complete = false;
040            int elements = 0;
041            while (in.hasNext() && !complete) {
042                int code = in.next();
043                switch (code) {
044                    case XMLStreamConstants.START_ELEMENT:
045                        elements++;
046                        property.getAny().add(anyElementMarshaler.parseElement(in));
047                        break;
048    
049                    case XMLStreamConstants.END_ELEMENT:
050                        if (--elements <= 0) {
051                            complete = true;
052                        }
053                        break;
054    
055                    case XMLStreamConstants.END_DOCUMENT:
056                        complete = true;
057                        break;
058                }
059            }
060        }
061    
062        protected void populateAnyAttributes(MessageExchange exchange, AnyAttribute value) throws XMLStreamException {
063            XMLStreamReader in = exchange.getIn();
064            int elements = 0;
065            boolean complete = false;
066    
067            while (in.hasNext() && !complete) {
068                int code = in.next();
069                switch (code) {
070                    case XMLStreamConstants.CHARACTERS:
071                        value.putValue(in.getNamespaceContext(), in.getText().trim());
072                        break;
073    
074                    case XMLStreamConstants.START_ELEMENT:
075                        ++elements;
076                        populateAttributes(in, value);
077                        break;
078    
079                    case XMLStreamConstants.END_ELEMENT:
080                        if (--elements <= 0) {
081                            complete = true;
082                        }
083                        break;
084    
085                    case XMLStreamConstants.END_DOCUMENT:
086                        complete = true;
087                        break;
088    
089                    default:
090                }
091            }
092        }
093    
094        protected void populateAttributes(XMLStreamReader in, AnyAttribute value) {
095            for (int i = 0, size = in.getAttributeCount(); i < size; i++) {
096                QName attributeName = in.getAttributeName(i);
097                String attributeValue = in.getAttributeValue(i);
098                value.putAttributeValue(in.getNamespaceContext(), attributeName, attributeValue);
099            }
100        }
101    
102    }