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.language.tokenizer;
018
019import org.apache.camel.Expression;
020import org.apache.camel.Predicate;
021import org.apache.camel.builder.ExpressionBuilder;
022import org.apache.camel.support.LanguageSupport;
023import org.apache.camel.util.ExpressionToPredicateAdapter;
024import org.apache.camel.util.ObjectHelper;
025
026/**
027 * A language for tokenizer expressions.
028 * <p/>
029 * This xmltokenizer language can operate in the following modes:
030 * <ul>
031 *     <li>inject - injecting the contextual namespace bindings into the extracted token</li>
032 *     <li>wrap - wrapping the extracted token in its ancestor context</li>
033 *     <li>unwrap - unwrapping the extracted token to its child content</li>
034 * </ul>
035 */
036public class XMLTokenizeLanguage extends LanguageSupport {
037
038    private String path;
039    private String headerName;
040    private char mode;
041    private int group;
042
043    public static Expression tokenize(String path) {
044        return tokenize(path, 'i');
045    }
046
047    public static Expression tokenize(String path, char mode) {
048        XMLTokenizeLanguage language = new XMLTokenizeLanguage();
049        language.setPath(path);
050        language.setMode(mode);
051        return language.createExpression(null);
052    }
053
054    public static Expression tokenize(String headerName, String path) {
055        return tokenize(headerName, path, 'i');
056    }
057
058    public static Expression tokenize(String headerName, String path, char mode) {
059        XMLTokenizeLanguage language = new XMLTokenizeLanguage();
060        language.setHeaderName(headerName);
061        language.setPath(path);
062        language.setMode(mode);
063        return language.createExpression(null);
064    }
065
066    public Predicate createPredicate(String expression) {
067        return ExpressionToPredicateAdapter.toPredicate(createExpression(expression));
068    }
069
070    /**
071     * Creates a tokenize expression.
072     */
073    public Expression createExpression() {
074        ObjectHelper.notNull(path, "path");
075
076        Expression answer = ExpressionBuilder.tokenizeXMLAwareExpression(path, mode);
077        return answer;
078    }
079
080    public Expression createExpression(String expression) {
081        if (ObjectHelper.isNotEmpty(expression)) {
082            this.path = expression;
083        }
084        return createExpression();
085    }
086
087    public String getPath() {
088        return path;
089    }
090
091    public void setPath(String path) {
092        this.path = path;
093    }
094
095    public String getHeaderName() {
096        return headerName;
097    }
098
099    public void setHeaderName(String headerName) {
100        this.headerName = headerName;
101    }
102
103    public char getMode() {
104        return mode;
105    }
106
107    public void setMode(char mode) {
108        this.mode = mode;
109    }
110
111    public int getGroup() {
112        return group;
113    }
114
115    public void setGroup(int group) {
116        this.group = group;
117    }
118
119    public boolean isSingleton() {
120        return false;
121    }
122}