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.builder;
018
019import org.apache.camel.CamelContext;
020import org.apache.camel.Expression;
021import org.apache.camel.Predicate;
022import org.apache.camel.impl.validator.ProcessorValidator;
023import org.apache.camel.model.language.ExpressionDefinition;
024import org.apache.camel.model.validator.CustomValidatorDefinition;
025import org.apache.camel.model.validator.EndpointValidatorDefinition;
026import org.apache.camel.model.validator.PredicateValidatorDefinition;
027import org.apache.camel.model.validator.ValidatorDefinition;
028import org.apache.camel.spi.AsPredicate;
029import org.apache.camel.spi.DataType;
030import org.apache.camel.spi.Validator;
031
032/**
033 * A <a href="http://camel.apache.org/dsl.html">Java DSL</a> which is
034 * used to build a {@link org.apache.camel.spi.Validator} and register into {@link org.apache.camel.CamelContext}.
035 * It requires a 'type' to be specified by type() method.
036 * And then you can choose a type of validator by withUri(), withPredicate(), withJava() or withBean() method.
037 */
038public class ValidatorBuilder {
039
040    private String type;
041    private String uri;
042    private ExpressionDefinition expression;
043    private Class<? extends Validator> clazz;
044    private String beanRef;
045
046    /**
047     * Set the data type name.
048     * If you specify 'xml:XYZ', the validator will be picked up if source type is
049     * 'xml:XYZ'. If you specify just 'xml', the validator matches with all of
050     * 'xml' source type like 'xml:ABC' or 'xml:DEF'.
051     *
052     * @param type 'from' data type name
053     */
054    public ValidatorBuilder type(String type) {
055        this.type = type;
056        return this;
057    }
058
059    /**
060     * Set the data type using Java class.
061     *
062     * @param type Java class represents data type
063     */
064    public ValidatorBuilder type(Class<?> type) {
065        this.type = new DataType(type).toString();
066        return this;
067    }
068
069    /**
070     * Set the URI to be used for the endpoint {@link Validator}.
071     * @see {@link EndpointValidatorDefinition}, {@link ProcessorValidator}
072     * 
073     * @param uri endpoint URI
074     */
075    public ValidatorBuilder withUri(String uri) {
076        resetType();
077        this.uri = uri;
078        return this;
079    }
080
081    /**
082     * Set the {@link Expression} to be used for the predicate {@link Validator}.
083     * @see {@link PredicateValidatorDefinition}, {@link ProcessorValidator}
084     * 
085     * @param expression validation expression
086     */
087    public ValidatorBuilder withExpression(@AsPredicate Expression expression) {
088        resetType();
089        this.expression = new ExpressionDefinition(expression);
090        return this;
091    }
092
093    /**
094     * Set the {@link Predicate} to be used for the predicate {@link Validator}.
095     * @see {@link PredicateValidatorDefinition}, {@link ProcessorValidator}
096     * 
097     * @param predicate validation predicate
098     */
099    public ValidatorBuilder withExpression(@AsPredicate Predicate predicate) {
100        resetType();
101        this.expression = new ExpressionDefinition(predicate);
102        return this;
103    }
104
105    /**
106     * Set the Java {@code Class} represents a custom {@code Validator} implementation class.
107     * @see {@code CustomValidatorDefinition}
108     * 
109     * @param clazz {@code Class} object represents custom validator implementation
110     */
111    public ValidatorBuilder withJava(Class<? extends Validator> clazz) {
112        resetType();
113        this.clazz = clazz;
114        return this;
115    }
116
117    /**
118     * Set the Java Bean name to be used for custom {@code Validator}.
119     * @see {@code CustomValidatorDefinition}
120     * 
121     * @param ref bean name for the custom {@code Validator}
122     */
123    public ValidatorBuilder withBean(String ref) {
124        resetType();
125        this.beanRef = ref;
126        return this;
127    }
128
129    private void resetType() {
130        this.uri = null;
131        this.expression = null;
132        this.clazz = null;
133        this.beanRef = null;
134    }
135
136    /**
137     * Configure a Validator according to the configurations built on this builder
138     * and register it into given {@code CamelContext}.
139     * 
140     * @param camelContext {@code CamelContext}
141     */
142    public void configure(CamelContext camelContext) {
143        ValidatorDefinition validator;
144        if (uri != null) {
145            EndpointValidatorDefinition etd = new EndpointValidatorDefinition();
146            etd.setUri(uri);
147            validator = etd;
148        } else if (expression != null) {
149            PredicateValidatorDefinition dtd = new PredicateValidatorDefinition();
150            dtd.setExpression(expression);
151            validator = dtd;
152        } else if (clazz != null) {
153            CustomValidatorDefinition ctd = new CustomValidatorDefinition();
154            ctd.setClassName(clazz.getName());
155            validator = ctd;
156        } else if (beanRef != null) {
157            CustomValidatorDefinition ctd = new CustomValidatorDefinition();
158            ctd.setRef(beanRef);
159            validator = ctd;
160        } else {
161            throw new IllegalArgumentException("No Validator type was specified");
162        }
163        
164        validator.setType(type);
165        camelContext.getValidators().add(validator);
166    }
167}