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.model.language;
018
019 import javax.xml.bind.annotation.XmlAccessType;
020 import javax.xml.bind.annotation.XmlAccessorType;
021 import javax.xml.bind.annotation.XmlAttribute;
022 import javax.xml.bind.annotation.XmlRootElement;
023
024 import org.apache.camel.CamelContext;
025 import org.apache.camel.Expression;
026 import org.apache.camel.Predicate;
027 import org.apache.camel.language.tokenizer.TokenizeLanguage;
028 import org.apache.camel.util.ExpressionToPredicateAdapter;
029
030 /**
031 * For expressions and predicates using a body or header tokenizer.
032 *
033 * @see TokenizeLanguage
034 */
035 @XmlRootElement(name = "tokenize")
036 @XmlAccessorType(XmlAccessType.FIELD)
037 public class TokenizerExpression extends ExpressionDefinition {
038 @XmlAttribute(required = true)
039 private String token;
040 @XmlAttribute
041 private String endToken;
042 @XmlAttribute
043 private String inheritNamespaceTagName;
044 @XmlAttribute
045 private String headerName;
046 @XmlAttribute
047 private Boolean regex;
048 @XmlAttribute
049 private Boolean xml;
050 @XmlAttribute
051 private Boolean includeTokens;
052 @XmlAttribute
053 private Integer group;
054
055 public TokenizerExpression() {
056 }
057
058 @Override
059 public String getLanguage() {
060 return "tokenize";
061 }
062
063 public String getToken() {
064 return token;
065 }
066
067 public void setToken(String token) {
068 this.token = token;
069 }
070
071 public String getEndToken() {
072 return endToken;
073 }
074
075 public void setEndToken(String endToken) {
076 this.endToken = endToken;
077 }
078
079 public String getHeaderName() {
080 return headerName;
081 }
082
083 public void setHeaderName(String headerName) {
084 this.headerName = headerName;
085 }
086
087 public void setRegex(boolean regex) {
088 this.regex = regex;
089 }
090
091 public Boolean getRegex() {
092 return regex;
093 }
094
095 public String getInheritNamespaceTagName() {
096 return inheritNamespaceTagName;
097 }
098
099 public void setInheritNamespaceTagName(String inheritNamespaceTagName) {
100 this.inheritNamespaceTagName = inheritNamespaceTagName;
101 }
102
103 public Boolean getXml() {
104 return xml;
105 }
106
107 public void setXml(Boolean xml) {
108 this.xml = xml;
109 }
110
111 public Boolean getIncludeTokens() {
112 return includeTokens;
113 }
114
115 public void setIncludeTokens(Boolean includeTokens) {
116 this.includeTokens = includeTokens;
117 }
118
119 public Integer getGroup() {
120 return group;
121 }
122
123 public void setGroup(Integer group) {
124 this.group = group;
125 }
126
127 @Override
128 public Expression createExpression(CamelContext camelContext) {
129 // special for new line tokens, if defined from XML then its 2 characters, so we replace that back to a single char
130 if (token.startsWith("\\n")) {
131 token = '\n' + token.substring(2);
132 }
133
134 TokenizeLanguage language = new TokenizeLanguage();
135 language.setToken(token);
136 language.setEndToken(endToken);
137 language.setInheritNamespaceTagName(inheritNamespaceTagName);
138 language.setHeaderName(headerName);
139 if (regex != null) {
140 language.setRegex(regex);
141 }
142 if (xml != null) {
143 language.setXml(xml);
144 }
145 if (includeTokens != null) {
146 language.setIncludeTokens(includeTokens);
147 }
148 if (group != null) {
149 if (group <= 0) {
150 throw new IllegalArgumentException("Group must be a positive number, was: " + group);
151 }
152 language.setGroup(group);
153 }
154 return language.createExpression();
155 }
156
157 @Override
158 public Predicate createPredicate(CamelContext camelContext) {
159 Expression exp = createExpression(camelContext);
160 return ExpressionToPredicateAdapter.toPredicate(exp);
161 }
162
163 @Override
164 public String toString() {
165 if (endToken != null) {
166 return "tokenize{body() using tokens: " + token + "..." + endToken + "}";
167 } else {
168 return "tokenize{" + (headerName != null ? "header: " + headerName : "body()") + " using token: " + token + "}";
169 }
170 }
171 }