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 */ 017package org.apache.camel.model.language; 018 019import javax.xml.bind.annotation.XmlAccessType; 020import javax.xml.bind.annotation.XmlAccessorType; 021import javax.xml.bind.annotation.XmlAttribute; 022import javax.xml.bind.annotation.XmlRootElement; 023import javax.xml.bind.annotation.XmlTransient; 024 025import org.apache.camel.CamelContext; 026import org.apache.camel.Expression; 027import org.apache.camel.Predicate; 028import org.apache.camel.spi.Metadata; 029import org.apache.camel.util.ObjectHelper; 030 031/** 032 * To use XQuery (XML) in Camel expressions or predicates. 033 * 034 * @version 035 */ 036@Metadata(firstVersion = "1.0.0", label = "language,xml", title = "XQuery") 037@XmlRootElement(name = "xquery") 038@XmlAccessorType(XmlAccessType.FIELD) 039public class XQueryExpression extends NamespaceAwareExpression { 040 @XmlAttribute 041 private String type; 042 @XmlTransient 043 private Class<?> resultType; 044 @XmlAttribute 045 private String headerName; 046 047 public XQueryExpression() { 048 } 049 050 public XQueryExpression(String expression) { 051 super(expression); 052 } 053 054 public String getLanguage() { 055 return "xquery"; 056 } 057 058 public String getType() { 059 return type; 060 } 061 062 /** 063 * Sets the class name of the result type (type from output) 064 * <p/> 065 * The default result type is NodeSet 066 */ 067 public void setType(String type) { 068 this.type = type; 069 } 070 071 public Class<?> getResultType() { 072 return resultType; 073 } 074 075 /** 076 * Sets the class of the result type (type from output). 077 * <p/> 078 * The default result type is NodeSet 079 */ 080 public void setResultType(Class<?> resultType) { 081 this.resultType = resultType; 082 } 083 084 public String getHeaderName() { 085 return headerName; 086 } 087 088 /** 089 * Name of header to use as input, instead of the message body 090 */ 091 public void setHeaderName(String headerName) { 092 this.headerName = headerName; 093 } 094 095 @Override 096 public Expression createExpression(CamelContext camelContext) { 097 if (resultType == null && type != null) { 098 try { 099 resultType = camelContext.getClassResolver().resolveMandatoryClass(type); 100 } catch (ClassNotFoundException e) { 101 throw ObjectHelper.wrapRuntimeCamelException(e); 102 } 103 } 104 105 return super.createExpression(camelContext); 106 } 107 108 @Override 109 protected void configureExpression(CamelContext camelContext, Expression expression) { 110 if (resultType != null) { 111 setProperty(expression, "resultType", resultType); 112 } 113 if (ObjectHelper.isNotEmpty(getHeaderName())) { 114 setProperty(expression, "headerName", getHeaderName()); 115 } 116 super.configureExpression(camelContext, expression); 117 } 118 119 @Override 120 protected void configurePredicate(CamelContext camelContext, Predicate predicate) { 121 if (resultType != null) { 122 setProperty(predicate, "resultType", resultType); 123 } 124 if (ObjectHelper.isNotEmpty(getHeaderName())) { 125 setProperty(predicate, "headerName", getHeaderName()); 126 } 127 super.configurePredicate(camelContext, predicate); 128 } 129 130}