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 java.util.Map;
020import javax.xml.bind.annotation.XmlAccessType;
021import javax.xml.bind.annotation.XmlAccessorType;
022import javax.xml.bind.annotation.XmlTransient;
023
024import org.apache.camel.CamelContext;
025import org.apache.camel.Expression;
026import org.apache.camel.Predicate;
027import org.apache.camel.spi.NamespaceAware;
028
029/**
030 * A useful base class for any expression which may be namespace or XML content aware
031 * such as {@link XPathExpression} or {@link XQueryExpression}
032 *
033 * @version 
034 */
035@XmlAccessorType(XmlAccessType.FIELD)
036public abstract class NamespaceAwareExpression extends ExpressionDefinition implements NamespaceAware {
037    @XmlTransient
038    private Map<String, String> namespaces;
039
040    public NamespaceAwareExpression() {
041    }
042
043    public NamespaceAwareExpression(String expression) {
044        super(expression);
045    }
046
047
048    public Map<String, String> getNamespaces() {
049        return namespaces;
050    }
051
052    /**
053     * Injects the XML Namespaces of prefix -> uri mappings
054     *
055     * @param namespaces the XML namespaces with the key of prefixes and the value the URIs
056     */
057    public void setNamespaces(Map<String, String> namespaces) {
058        this.namespaces = namespaces;
059    }
060
061    @Override
062    protected void configureExpression(CamelContext camelContext, Expression expression) {
063        configureNamespaceAware(expression);
064        super.configureExpression(camelContext, expression);
065    }
066
067    @Override
068    protected void configurePredicate(CamelContext camelContext, Predicate predicate) {
069        configureNamespaceAware(predicate);
070        super.configurePredicate(camelContext, predicate);
071    }
072
073    protected void configureNamespaceAware(Object builder) {
074        if (namespaces != null && builder instanceof NamespaceAware) {
075            NamespaceAware namespaceAware = (NamespaceAware) builder;
076            namespaceAware.setNamespaces(namespaces);
077        }
078    }
079}