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