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