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; 023 024import org.apache.camel.CamelContext; 025import org.apache.camel.Expression; 026import org.apache.camel.Predicate; 027import org.apache.camel.language.tokenizer.TokenizeLanguage; 028import org.apache.camel.spi.Metadata; 029import org.apache.camel.support.ExpressionToPredicateAdapter; 030 031/** 032 * To use Camel message body or header with a tokenizer in Camel expressions or 033 * predicates. 034 * 035 * @see TokenizeLanguage 036 */ 037@Metadata(firstVersion = "2.0.0", label = "language,core", title = "Tokenize") 038@XmlRootElement(name = "tokenize") 039@XmlAccessorType(XmlAccessType.FIELD) 040public class TokenizerExpression extends ExpressionDefinition { 041 @XmlAttribute(required = true) 042 private String token; 043 @XmlAttribute 044 private String endToken; 045 @XmlAttribute 046 private String inheritNamespaceTagName; 047 @XmlAttribute 048 private String headerName; 049 @XmlAttribute 050 private Boolean regex; 051 @XmlAttribute 052 private Boolean xml; 053 @XmlAttribute 054 private Boolean includeTokens; 055 @XmlAttribute 056 private String group; 057 @XmlAttribute 058 private String groupDelimiter; 059 @XmlAttribute 060 private Boolean skipFirst; 061 062 public TokenizerExpression() { 063 } 064 065 public TokenizerExpression(String token) { 066 this.token = token; 067 } 068 069 @Override 070 public String getLanguage() { 071 return "tokenize"; 072 } 073 074 public String getToken() { 075 return token; 076 } 077 078 /** 079 * The (start) token to use as tokenizer, for example you can use the new 080 * line token. You can use simple language as the token to support dynamic 081 * tokens. 082 */ 083 public void setToken(String token) { 084 this.token = token; 085 } 086 087 public String getEndToken() { 088 return endToken; 089 } 090 091 /** 092 * The end token to use as tokenizer if using start/end token pairs. You can 093 * use simple language as the token to support dynamic tokens. 094 */ 095 public void setEndToken(String endToken) { 096 this.endToken = endToken; 097 } 098 099 public String getHeaderName() { 100 return headerName; 101 } 102 103 /** 104 * Name of header to tokenize instead of using the message body. 105 */ 106 public void setHeaderName(String headerName) { 107 this.headerName = headerName; 108 } 109 110 /** 111 * If the token is a regular expression pattern. 112 * <p/> 113 * The default value is false 114 */ 115 public void setRegex(boolean regex) { 116 this.regex = regex; 117 } 118 119 public Boolean getRegex() { 120 return regex; 121 } 122 123 public String getInheritNamespaceTagName() { 124 return inheritNamespaceTagName; 125 } 126 127 /** 128 * To inherit namespaces from a root/parent tag name when using XML You can 129 * use simple language as the tag name to support dynamic names. 130 */ 131 public void setInheritNamespaceTagName(String inheritNamespaceTagName) { 132 this.inheritNamespaceTagName = inheritNamespaceTagName; 133 } 134 135 public Boolean getXml() { 136 return xml; 137 } 138 139 /** 140 * Whether the input is XML messages. This option must be set to true if 141 * working with XML payloads. 142 */ 143 public void setXml(Boolean xml) { 144 this.xml = xml; 145 } 146 147 public Boolean getIncludeTokens() { 148 return includeTokens; 149 } 150 151 /** 152 * Whether to include the tokens in the parts when using pairs 153 * <p/> 154 * The default value is false 155 */ 156 public void setIncludeTokens(Boolean includeTokens) { 157 this.includeTokens = includeTokens; 158 } 159 160 public String getGroup() { 161 return group; 162 } 163 164 /** 165 * To group N parts together, for example to split big files into chunks of 166 * 1000 lines. You can use simple language as the group to support dynamic 167 * group sizes. 168 */ 169 public void setGroup(String group) { 170 this.group = group; 171 } 172 173 public String getGroupDelimiter() { 174 return groupDelimiter; 175 } 176 177 /** 178 * Sets the delimiter to use when grouping. If this has not been set then 179 * token will be used as the delimiter. 180 */ 181 public void setGroupDelimiter(String groupDelimiter) { 182 this.groupDelimiter = groupDelimiter; 183 } 184 185 public Boolean getSkipFirst() { 186 return skipFirst; 187 } 188 189 /** 190 * To skip the very first element 191 */ 192 public void setSkipFirst(Boolean skipFirst) { 193 this.skipFirst = skipFirst; 194 } 195 196 @Override 197 public Expression createExpression(CamelContext camelContext) { 198 // special for new line tokens, if defined from XML then its 2 199 // characters, so we replace that back to a single char 200 if (token.startsWith("\\n")) { 201 token = '\n' + token.substring(2); 202 } 203 204 TokenizeLanguage language = new TokenizeLanguage(); 205 language.setToken(token); 206 language.setEndToken(endToken); 207 language.setInheritNamespaceTagName(inheritNamespaceTagName); 208 language.setHeaderName(headerName); 209 language.setGroupDelimiter(groupDelimiter); 210 if (regex != null) { 211 language.setRegex(regex); 212 } 213 if (xml != null) { 214 language.setXml(xml); 215 } 216 if (includeTokens != null) { 217 language.setIncludeTokens(includeTokens); 218 } 219 if (group != null && !"0".equals(group)) { 220 language.setGroup(group); 221 } 222 223 if (skipFirst != null) { 224 language.setSkipFirst(skipFirst); 225 } 226 return language.createExpression(); 227 } 228 229 @Override 230 public Predicate createPredicate(CamelContext camelContext) { 231 Expression exp = createExpression(camelContext); 232 return ExpressionToPredicateAdapter.toPredicate(exp); 233 } 234 235 @Override 236 public String toString() { 237 if (endToken != null) { 238 return "tokenize{body() using tokens: " + token + "..." + endToken + "}"; 239 } else { 240 return "tokenize{" + (headerName != null ? "header: " + headerName : "body()") + " using token: " + token + "}"; 241 } 242 } 243}