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.policy.addressing.handler;
019    
020    import org.codehaus.activesoap.MessageExchange;
021    import org.codehaus.activesoap.handler.stax.AnyElementMarshaler;
022    import org.codehaus.activesoap.handler.stax.StaxHandler;
023    import org.codehaus.activesoap.policy.addressing.AddressingContext;
024    import org.codehaus.activesoap.policy.addressing.AttributedQName;
025    import org.codehaus.activesoap.policy.addressing.AttributedURI;
026    import org.codehaus.activesoap.policy.addressing.EndpointReferenceType;
027    import org.codehaus.activesoap.policy.addressing.ReferencePropertiesType;
028    import org.codehaus.activesoap.policy.addressing.ServiceNameType;
029    
030    import javax.xml.namespace.QName;
031    import javax.xml.stream.XMLStreamConstants;
032    import javax.xml.stream.XMLStreamException;
033    import javax.xml.stream.XMLStreamReader;
034    
035    /**
036     * @version $Revision: 1.6 $
037     */
038    public abstract class EndpointReferenceTypeHandler extends StaxHandler {
039    
040    
041        private AnyElementMarshaler anyElementMarshaler;
042    
043        protected EndpointReferenceTypeHandler(AnyElementMarshaler anyElementMarshaler) {
044            this.anyElementMarshaler = anyElementMarshaler;
045        }
046    
047        public void invoke(MessageExchange exchange) throws Exception {
048            EndpointReferenceType value = new EndpointReferenceType();
049            populateEndpointReferenceType(exchange, value);
050            setValue(AddressingContext.getContext(exchange), value);
051        }
052    
053        protected void populateEndpointReferenceType(MessageExchange exchange, EndpointReferenceType value) throws XMLStreamException {
054            XMLStreamReader in = exchange.getIn();
055            boolean complete = false;
056            int elements = 0;
057            int code = in.getEventType();
058            while (!complete) {
059                switch (code) {
060                    case XMLStreamConstants.START_ELEMENT:
061                        if (elements++ == 0) {
062                            populateAttributes(in, value);
063                        }
064                        else {
065                            processElement(exchange, value);
066                        }
067                        break;
068    
069                    case XMLStreamConstants.END_DOCUMENT:
070                        complete = true;
071                        break;
072                }
073                if (!in.hasNext()) {
074                    complete = true;
075                }
076                else {
077                    code = in.next();
078                }
079            }
080        }
081    
082        protected void processElement(MessageExchange exchange, EndpointReferenceType value) throws XMLStreamException {
083            XMLStreamReader in = exchange.getIn();
084            QName name = in.getName();
085            if (name.equals(EndpointReferenceType.Address_QNAME)) {
086                AttributedURI property = new AttributedURI();
087                populateAnyAttributes(exchange, property);
088                value.setAddress(property);
089            }
090            else if (name.equals(EndpointReferenceType.ReferenceProperties_QNAME)) {
091                ReferencePropertiesType property = new ReferencePropertiesType();
092                populateAnyContent(exchange, anyElementMarshaler, property);
093                value.setReferenceProperties(property);
094            }
095            else if (name.equals(EndpointReferenceType.PortType_QNAME)) {
096                AttributedQName property = new AttributedQName();
097                populateAnyAttributes(exchange, property);
098                value.setPortType(property);
099            }
100            else if (name.equals(EndpointReferenceType.ServiceName_QNAME)) {
101                ServiceNameType property = new ServiceNameType();
102                populateAnyAttributes(exchange, property);
103                value.setServiceName(property);
104            }
105            else {
106                populateAnyContent(exchange, anyElementMarshaler, value);
107            }
108        }
109    
110        protected abstract void setValue(AddressingContext context, EndpointReferenceType value);
111    }