001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  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    package org.apache.camel.component.bean;
018    
019    import java.lang.annotation.Annotation;
020    
021    import org.apache.camel.CamelContext;
022    import org.apache.camel.Expression;
023    import org.apache.camel.builder.xml.XPathBuilder;
024    import org.apache.camel.language.LanguageAnnotation;
025    import org.apache.camel.language.NamespacePrefix;
026    import org.apache.camel.util.ObjectHelper;
027    
028    /**
029     * Factory for the XPath expression annotations.
030     *
031     * @version 
032     */
033    public class XPathAnnotationExpressionFactory extends DefaultAnnotationExpressionFactory {
034    
035        @Override
036        public Expression createExpression(CamelContext camelContext, Annotation annotation, LanguageAnnotation languageAnnotation, Class<?> expressionReturnType) {
037            String xpath = getExpressionFromAnnotation(annotation);
038            
039            Class<?> resultType = getResultType(annotation);
040            if (resultType.equals(Object.class)) {
041                resultType = expressionReturnType;
042            }
043            
044            XPathBuilder builder = XPathBuilder.xpath(xpath, resultType);        
045            NamespacePrefix[] namespaces = getExpressionNameSpacePrefix(annotation);
046            if (namespaces != null) {
047                for (NamespacePrefix namespacePrefix : namespaces) {
048                    builder = builder.namespace(namespacePrefix.prefix(), namespacePrefix.uri());
049                }
050            }
051    
052            // Set the header name that we want the XPathBuilder to apply the XPath expression to
053            String headerName = getHeaderName(annotation);
054            if (ObjectHelper.isNotEmpty(headerName)) {
055                builder.setHeaderName(headerName);
056            }
057            
058            return builder;
059        }
060    
061        protected Class<?> getResultType(Annotation annotation) {
062            return (Class<?>) getAnnotationObjectValue(annotation, "resultType");
063        }
064        
065        protected NamespacePrefix[] getExpressionNameSpacePrefix(Annotation annotation) {
066            return (NamespacePrefix[]) getAnnotationObjectValue(annotation, "namespaces");
067        }
068        
069        /**
070         * Extracts the value of the header method in the Annotation. For backwards
071         * compatibility this method will return null if the annotation's method is
072         * not found.
073         * 
074         * @return If the annotation has the method 'header' then the name of the
075         *         header we want to apply the XPath expression to. Otherwise null
076         *         will be returned
077         */
078        protected String getHeaderName(Annotation annotation) {
079            String headerValue = null;
080            try {
081                headerValue = (String)getAnnotationObjectValue(annotation, "headerName");
082            } catch (Exception e) {
083                // Do Nothing
084            }
085            return headerValue;
086        }
087    }