Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2005-2010 the original author or authors.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.springframework.ws.server.endpoint.adapter.method;
 
 
Implementation of MethodArgumentResolver that supports StAX javax.xml.stream.XMLStreamReader and javax.xml.stream.XMLEventReader arguments.

Author(s):
Arjen Poutsma
Since:
2.0
 
 
     private final XMLInputFactory inputFactory = createXmlInputFactory();
 
     public boolean supportsParameter(MethodParameter parameter) {
         if (parameter.getParameterAnnotation(RequestPayload.class) == null) {
             return false;
         }
         else {
             Class<?> parameterType = parameter.getParameterType();
             return XMLStreamReader.class.equals(parameterType) || XMLEventReader.class.equals(parameterType);
         }
     }
 
     public Object resolveArgument(MessageContext messageContextMethodParameter parameter)
             throws TransformerExceptionXMLStreamException {
         Source source = messageContext.getRequest().getPayloadSource();
         if (source == null) {
             return null;
         }
         Class<?> parameterType = parameter.getParameterType();
         if (XMLStreamReader.class.equals(parameterType)) {
             return resolveStreamReader(source);
         }
         else if (XMLEventReader.class.equals(parameterType)) {
             return resolveEventReader(source);
         }
         throw new UnsupportedOperationException();
     }
 
     private XMLStreamReader resolveStreamReader(Source requestSourcethrows TransformerExceptionXMLStreamException {
         XMLStreamReader streamReader = null;
         if (StaxUtils.isStaxSource(requestSource)) {
             streamReader = StaxUtils.getXMLStreamReader(requestSource);
             if (streamReader == null) {
                 XMLEventReader eventReader = StaxUtils.getXMLEventReader(requestSource);
                 if (eventReader != null) {
                     try {
                         streamReader = StaxUtils.createEventStreamReader(eventReader);
                     }
                     catch (XMLStreamException ex) {
                         streamReader = null;
                     }
                 }
             }
         }
         if (streamReader == null) {
             try {
                 streamReader = .createXMLStreamReader(requestSource);
             }
             catch (XMLStreamException ex) {
                 streamReader = null;
             }
             catch (UnsupportedOperationException ex) {
                 streamReader = null;
             }
         }
         if (streamReader == null) {
            // as a final resort, transform the source to a stream, and read from that
            ByteArrayInputStream bis = convertToByteArrayInputStream(requestSource);
            streamReader = .createXMLStreamReader(bis);
        }
        return streamReader;
    }
        XMLEventReader eventReader = null;
        if (StaxUtils.isStaxSource(requestSource)) {
            eventReader = StaxUtils.getXMLEventReader(requestSource);
            if (eventReader == null) {
                XMLStreamReader streamReader = StaxUtils.getXMLStreamReader(requestSource);
                if (streamReader != null) {
                    try {
                        eventReader = .createXMLEventReader(streamReader);
                    }
                    catch (XMLStreamException ex) {
                        eventReader = null;
                    }
                }
            }
        }
        if (eventReader == null) {
            try {
                eventReader = .createXMLEventReader(requestSource);
            }
            catch (XMLStreamException ex) {
                eventReader = null;
            }
            catch (UnsupportedOperationException ex) {
                eventReader = null;
            }
        }
        if (eventReader == null) {
            // as a final resort, transform the source to a stream, and read from that
            ByteArrayInputStream bis = convertToByteArrayInputStream(requestSource);
            eventReader = .createXMLEventReader(bis);
        }
        return eventReader;
    }

    
Create a XMLInputFactory that this resolver will use to create javax.xml.stream.XMLStreamReader and javax.xml.stream.XMLEventReader objects.

Can be overridden in subclasses, adding further initialization of the factory. The resulting factory is cached, so this method will only be called once.

Returns:
the created factory
    protected XMLInputFactory createXmlInputFactory() {
        return XMLInputFactory.newInstance();
    }
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        transform(sourcenew StreamResult(bos));
        return new ByteArrayInputStream(bos.toByteArray());
    }
New to GrepCode? Check out our FAQ X