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.validator;
018
019import javax.xml.bind.annotation.XmlAccessType;
020import javax.xml.bind.annotation.XmlAccessorType;
021import javax.xml.bind.annotation.XmlAttribute;
022import javax.xml.bind.annotation.XmlType;
023import org.apache.camel.CamelContext;
024import org.apache.camel.model.InputTypeDefinition;
025import org.apache.camel.model.OutputTypeDefinition;
026import org.apache.camel.spi.DataType;
027import org.apache.camel.spi.Metadata;
028import org.apache.camel.spi.Validator;
029
030/**
031 * <p>Represents a {@link Validator} which declaratively validates message content
032 * according to the input type declared by {@link InputTypeDefinition} and/or output type
033 * declared by {@link OutputTypeDefinition}.</p>
034 * <p>If you specify type='xml:ABC', the validator
035 * will be picked up when current message type is 'xml:ABC'.
036 * If you specify type='json', then it will be picked up for all of json validation.
037 * 
038 * {@see Validator}
039 * {@see InputTypeDefinition}
040 * {@see OutputTypeDefinition}
041 */
042@Metadata(label = "validation")
043@XmlType(name = "validator")
044@XmlAccessorType(XmlAccessType.FIELD)
045public abstract class ValidatorDefinition {
046
047    @XmlAttribute
048    private String type;
049
050    public Validator createValidator(CamelContext context) throws Exception {
051        return doCreateValidator(context);
052    };
053
054    protected abstract Validator doCreateValidator(CamelContext context) throws Exception;
055
056    public String getType() {
057        return type;
058    }
059
060    /**
061     * Set the data type name.
062     * If you specify 'xml:XYZ', the validator will be picked up if message type is
063     * 'xml:XYZ'. If you specify just 'xml', the validator matches with all of
064     * 'xml' message type like 'xml:ABC' or 'xml:DEF'.
065     * 
066     * @param type data type name
067     */
068    public void setType(String type) {
069        this.type = type;
070    }
071
072    /**
073     * Set the data type using Java class.
074     * @param clazz Java class
075     */
076    public void setType(Class<?> clazz) {
077        this.type = new DataType(clazz).toString();
078    }
079}
080