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.language.tokenizer;
018    
019    import org.apache.camel.Expression;
020    import org.apache.camel.IsSingleton;
021    import org.apache.camel.Predicate;
022    import org.apache.camel.builder.ExpressionBuilder;
023    import org.apache.camel.builder.PredicateBuilder;
024    import org.apache.camel.spi.Language;
025    import org.apache.camel.util.ObjectHelper;
026    
027    /**
028     * A language for tokenizer expressions.
029     */
030    public class TokenizeLanguage implements Language, IsSingleton {
031    
032        private String token;
033        private String headerName;
034        private boolean regex;
035    
036        public static Expression tokenize(String token) {
037            return tokenize(token, false);
038        }
039    
040        public static Expression tokenize(String token, boolean regex) {
041            TokenizeLanguage langugage = new TokenizeLanguage();
042            langugage.setToken(token);
043            langugage.setRegex(regex);
044            return langugage.createExpression(null);
045        }
046    
047        public static Expression tokenize(String headerName, String token) {
048            return tokenize(headerName, token, false);
049        }
050    
051        public static Expression tokenize(String headerName, String token, boolean regex) {
052            TokenizeLanguage langugage = new TokenizeLanguage();
053            langugage.setHeaderName(headerName);
054            langugage.setToken(token);
055            langugage.setRegex(regex);
056            return langugage.createExpression(null);
057        }
058    
059        public Predicate createPredicate(String expression) {
060            return PredicateBuilder.toPredicate(createExpression(expression));
061        }
062    
063        /**
064         * Creates a tokenize expression.
065         */
066        public Expression createExpression() {
067            ObjectHelper.notNull(token, "token");
068            Expression exp = headerName == null ? ExpressionBuilder.bodyExpression() : ExpressionBuilder.headerExpression(headerName);
069            if (regex) {
070                return ExpressionBuilder.regexTokenizeExpression(exp, token);
071            } else {
072                return ExpressionBuilder.tokenizeExpression(exp, token);
073            }
074        }
075    
076        public Expression createExpression(String expression) {
077            if (ObjectHelper.isNotEmpty(expression)) {
078                this.token = expression;
079            }
080            return createExpression();
081        }
082    
083        public String getToken() {
084            return token;
085        }
086    
087        public void setToken(String token) {
088            this.token = token;
089        }
090    
091        public String getHeaderName() {
092            return headerName;
093        }
094    
095        public void setHeaderName(String headerName) {
096            this.headerName = headerName;
097        }
098    
099        public boolean isRegex() {
100            return regex;
101        }
102    
103        public void setRegex(boolean regex) {
104            this.regex = regex;
105        }
106    
107        public boolean isSingleton() {
108            return false;
109        }
110    }