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 * For expressions and predicates using a body or header tokenizer. 033 * 034 * @see TokenizeLanguage 035 */ 036@Metadata(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 Integer group; 056 057 public TokenizerExpression() { 058 } 059 060 @Override 061 public String getLanguage() { 062 return "tokenize"; 063 } 064 065 public String getToken() { 066 return token; 067 } 068 069 /** 070 * The (start) token to use as tokenizer, for example \n for a new line token 071 */ 072 public void setToken(String token) { 073 this.token = token; 074 } 075 076 public String getEndToken() { 077 return endToken; 078 } 079 080 /** 081 * The end token to use as tokenizer if using start/end token pairs. 082 */ 083 public void setEndToken(String endToken) { 084 this.endToken = endToken; 085 } 086 087 public String getHeaderName() { 088 return headerName; 089 } 090 091 /** 092 * Name of header to tokenize instead of using the message body. 093 */ 094 public void setHeaderName(String headerName) { 095 this.headerName = headerName; 096 } 097 098 /** 099 * If the token is a regular expression pattern. 100 * <p/> 101 * The default value is false 102 */ 103 public void setRegex(boolean regex) { 104 this.regex = regex; 105 } 106 107 public Boolean getRegex() { 108 return regex; 109 } 110 111 public String getInheritNamespaceTagName() { 112 return inheritNamespaceTagName; 113 } 114 115 /** 116 * To inherit namepaces from a root/parent tag name when using XML 117 */ 118 public void setInheritNamespaceTagName(String inheritNamespaceTagName) { 119 this.inheritNamespaceTagName = inheritNamespaceTagName; 120 } 121 122 public Boolean getXml() { 123 return xml; 124 } 125 126 /** 127 * Whether the input is XML messages. 128 * This option must be set to true if working with XML payloads. 129 */ 130 public void setXml(Boolean xml) { 131 this.xml = xml; 132 } 133 134 public Boolean getIncludeTokens() { 135 return includeTokens; 136 } 137 138 /** 139 * Whether to include the tokens in the parts when using pairs 140 * <p/> 141 * The default value is false 142 */ 143 public void setIncludeTokens(Boolean includeTokens) { 144 this.includeTokens = includeTokens; 145 } 146 147 public Integer getGroup() { 148 return group; 149 } 150 151 /** 152 * To group N parts together, for example to split big files into chunks of 1000 lines. 153 */ 154 public void setGroup(Integer group) { 155 this.group = group; 156 } 157 158 @Override 159 public Expression createExpression(CamelContext camelContext) { 160 // special for new line tokens, if defined from XML then its 2 characters, so we replace that back to a single char 161 if (token.startsWith("\\n")) { 162 token = '\n' + token.substring(2); 163 } 164 165 TokenizeLanguage language = new TokenizeLanguage(); 166 language.setToken(token); 167 language.setEndToken(endToken); 168 language.setInheritNamespaceTagName(inheritNamespaceTagName); 169 language.setHeaderName(headerName); 170 if (regex != null) { 171 language.setRegex(regex); 172 } 173 if (xml != null) { 174 language.setXml(xml); 175 } 176 if (includeTokens != null) { 177 language.setIncludeTokens(includeTokens); 178 } 179 if (group != null) { 180 if (group <= 0) { 181 throw new IllegalArgumentException("Group must be a positive number, was: " + group); 182 } 183 language.setGroup(group); 184 } 185 return language.createExpression(); 186 } 187 188 @Override 189 public Predicate createPredicate(CamelContext camelContext) { 190 Expression exp = createExpression(camelContext); 191 return ExpressionToPredicateAdapter.toPredicate(exp); 192 } 193 194 @Override 195 public String toString() { 196 if (endToken != null) { 197 return "tokenize{body() using tokens: " + token + "..." + endToken + "}"; 198 } else { 199 return "tokenize{" + (headerName != null ? "header: " + headerName : "body()") + " using token: " + token + "}"; 200 } 201 } 202}