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    @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 \n for a 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 Integer 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     */
159    public void setGroup(Integer group) {
160        this.group = group;
161    }
162
163    public Boolean getSkipFirst() {
164        return skipFirst;
165    }
166
167    /**
168     * To skip the very first element
169     */
170    public void setSkipFirst(Boolean skipFirst) {
171        this.skipFirst = skipFirst;
172    }
173
174    @Override
175    public Expression createExpression(CamelContext camelContext) {
176        // special for new line tokens, if defined from XML then its 2 characters, so we replace that back to a single char
177        if (token.startsWith("\\n")) {
178            token = '\n' + token.substring(2);
179        }
180
181        TokenizeLanguage language = new TokenizeLanguage();
182        language.setToken(token);
183        language.setEndToken(endToken);
184        language.setInheritNamespaceTagName(inheritNamespaceTagName);
185        language.setHeaderName(headerName);
186        if (regex != null) {
187            language.setRegex(regex);
188        }
189        if (xml != null) {
190            language.setXml(xml);
191        }
192        if (includeTokens != null) {
193            language.setIncludeTokens(includeTokens);
194        }
195        if (group != null) {
196            if (group <= 0) {
197                throw new IllegalArgumentException("Group must be a positive number, was: " + group);
198            }
199            language.setGroup(group);
200        }
201        if (skipFirst != null) {
202            language.setSkipFirst(skipFirst);
203        }
204        return language.createExpression();
205    }
206
207    @Override
208    public Predicate createPredicate(CamelContext camelContext) {
209        Expression exp = createExpression(camelContext);
210        return ExpressionToPredicateAdapter.toPredicate(exp);
211    }
212
213    @Override
214    public String toString() {
215        if (endToken != null) {
216            return "tokenize{body() using tokens: " + token + "..." + endToken + "}";
217        } else {
218            return "tokenize{" + (headerName != null ? "header: " + headerName : "body()") + " using token: " + token + "}";
219        }
220    }
221}