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 the @XPathParam annotation.

This resolver supports parameters annotated with @XPathParam that specifies the XPath expression that should be bound to that parameter. The parameter can either a "natively supported" XPath type ( boolean, double, java.lang.String, org.w3c.dom.Node, or org.w3c.dom.NodeList), or a type that is org.springframework.core.convert.ConversionService.canConvert(java.lang.Class,java.lang.Class) by the org.springframework.core.convert.ConversionService.

Author(s):
Arjen Poutsma
Since:
2.0
 
 
     private final XPathFactory xpathFactory = createXPathFactory();
 
 
     private ConversionService conversionService = ConversionServiceFactory.createDefaultConversionService();

    
Sets the conversion service to use.

Defaults to the default conversion service.

 
     public void setConversionService(ConversionService conversionService) {
         this. = conversionService;
     }
 
     public void setTransformerHelper(TransformerHelper transformerHelper) {
         this. = transformerHelper;
     }
 
     public boolean supportsParameter(MethodParameter parameter) {
         if (parameter.getParameterAnnotation(XPathParam.class) == null) {
             return false;
         }
         Class<?> parameterType = parameter.getParameterType();
         if (Boolean.class.equals(parameterType) || ..equals(parameterType) ||
                 Double.class.equals(parameterType) || ..equals(parameterType) ||
                 Node.class.isAssignableFrom(parameterType) || NodeList.class.isAssignableFrom(parameterType) ||
                 String.class.isAssignableFrom(parameterType)) {
             return true;
         }
         else {
             return .canConvert(String.classparameterType);
         }
     }
 
     public Object resolveArgument(MessageContext messageContextMethodParameter parameter)
             throws TransformerExceptionXPathExpressionException {
         Class<?> parameterType = parameter.getParameterType();
         QName evaluationReturnType = getReturnType(parameterType);
         boolean useConversionService = false;
         if (evaluationReturnType == null) {
             evaluationReturnType = .;
             useConversionService = true;
         }
 
        XPath xpath = createXPath();
        xpath.setNamespaceContext(NamespaceUtils.getNamespaceContext(parameter.getMethod()));
        Element rootElement = getRootElement(messageContext.getRequest().getPayloadSource());
        String expression = parameter.getParameterAnnotation(XPathParam.class).value();
        Object result = xpath.evaluate(expressionrootElementevaluationReturnType);
        return useConversionService ? .convert(resultparameterType) : result;
    }
    private QName getReturnType(Class<?> parameterType) {
        if (Boolean.class.equals(parameterType) || ..equals(parameterType)) {
            return .;
        }
        else if (Double.class.equals(parameterType) || ..equals(parameterType)) {
            return .;
        }
        else if (Node.class.equals(parameterType)) {
            return .;
        }
        else if (NodeList.class.equals(parameterType)) {
            return .;
        }
        else if (String.class.equals(parameterType)) {
            return .;
        }
        else {
            return null;
        }
    }
    private XPath createXPath() {
        synchronized () {
            return .newXPath();
        }
    }
    private Element getRootElement(Source sourcethrows TransformerException {
        DOMResult domResult = new DOMResult();
        .transform(sourcedomResult);
        Document document = (DocumentdomResult.getNode();
        return document.getDocumentElement();
    }

    
Create a XPathFactory that this resolver will use to create javax.xml.xpath.XPath 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 XPathFactory createXPathFactory() {
        return XPathFactory.newInstance();
    }
New to GrepCode? Check out our FAQ X