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.spi.Metadata;
028
029/**
030 * To use Camel message body or header with a XML tokenizer in Camel expressions
031 * or predicates.
032 *
033 * @see org.apache.camel.language.xtokenizer.XMLTokenizeLanguage
034 */
035@Metadata(firstVersion = "2.14.0", label = "language,core,xml", title = "XML Tokenize")
036@XmlRootElement(name = "xtokenize")
037@XmlAccessorType(XmlAccessType.FIELD)
038public class XMLTokenizerExpression extends NamespaceAwareExpression {
039    @XmlAttribute
040    private String headerName;
041    @XmlAttribute
042    private String mode;
043    @XmlAttribute
044    private Integer group;
045
046    public XMLTokenizerExpression() {
047    }
048
049    public XMLTokenizerExpression(String expression) {
050        super(expression);
051    }
052
053    @Override
054    public String getLanguage() {
055        return "xtokenize";
056    }
057
058    public String getHeaderName() {
059        return headerName;
060    }
061
062    /**
063     * Name of header to tokenize instead of using the message body.
064     */
065    public void setHeaderName(String headerName) {
066        this.headerName = headerName;
067    }
068
069    public String getMode() {
070        return mode;
071    }
072
073    /**
074     * The extraction mode. The available extraction modes are:
075     * <ul>
076     * <li>i - injecting the contextual namespace bindings into the extracted
077     * token (default)</li>
078     * <li>w - wrapping the extracted token in its ancestor context</li>
079     * <li>u - unwrapping the extracted token to its child content</li>
080     * <li>t - extracting the text content of the specified element</li>
081     * </ul>
082     */
083    public void setMode(String mode) {
084        this.mode = mode;
085    }
086
087    public Integer getGroup() {
088        return group;
089    }
090
091    /**
092     * To group N parts together
093     */
094    public void setGroup(Integer group) {
095        this.group = group;
096    }
097
098    @Override
099    protected void configureExpression(CamelContext camelContext, Expression expression) {
100        if (headerName != null) {
101            setProperty(camelContext, expression, "headerName", headerName);
102        }
103        if (mode != null) {
104            setProperty(camelContext, expression, "mode", mode);
105        }
106        if (group != null) {
107            setProperty(camelContext, expression, "group", group);
108        }
109        super.configureExpression(camelContext, expression);
110    }
111
112    @Override
113    protected void configurePredicate(CamelContext camelContext, Predicate predicate) {
114        if (headerName != null) {
115            setProperty(camelContext, predicate, "headerName", headerName);
116        }
117        if (mode != null) {
118            setProperty(camelContext, predicate, "mode", mode);
119        }
120        if (group != null) {
121            setProperty(camelContext, predicate, "group", group);
122        }
123        super.configurePredicate(camelContext, predicate);
124    }
125
126}