View Javadoc
1   /*
2    * Copyright 2008 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *     
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.openehealth.ipf.platform.camel.core.model;
17  
18  import static org.apache.camel.builder.Builder.bodyAs;
19  import groovy.lang.Closure;
20  
21  import javax.xml.bind.annotation.*;
22  import javax.xml.transform.stream.StreamSource;
23  
24  import groovy.transform.stc.ClosureParams;
25  import groovy.transform.stc.SimpleType;
26  import org.apache.camel.Expression;
27  import org.apache.camel.spi.Metadata;
28  import org.apache.camel.spi.RouteContext;
29  import org.openehealth.ipf.commons.core.modules.api.Validator;
30  import org.openehealth.ipf.commons.xml.SchematronValidator;
31  import org.openehealth.ipf.commons.xml.XsdValidator;
32  import org.openehealth.ipf.platform.camel.core.adapter.ProcessorAdapter;
33  import org.openehealth.ipf.platform.camel.core.adapter.ValidatorAdapter;
34  import org.openehealth.ipf.platform.camel.core.closures.DelegatingExpression;
35  
36  /**
37   * @author Martin Krasser
38   * @author Christian Ohr
39   */
40  @Metadata(label = "ipf,eip,transformation")
41  @XmlRootElement(name = "verify")
42  @XmlAccessorType(XmlAccessType.FIELD)
43  public class ValidatorAdapterDefinition extends ProcessorAdapterDefinition {
44  
45      @XmlTransient
46      private Validator validator;
47      @XmlAttribute
48      private String validatorBean;
49      @XmlTransient
50      private Object profile;
51      @XmlTransient
52      private Expression profileExpression;
53      
54      public ValidatorAdapterDefinition() {
55          this(new AlwaysValid());
56      }
57      
58      public ValidatorAdapterDefinition(Validator validator) {
59          this.validator = validator;
60      }
61      
62      public ValidatorAdapterDefinition(String validatorBean) {
63          this.validatorBean = validatorBean;
64      }
65      
66      public void setValidator(Validator validator) {
67          this.validator = validator;
68      }
69      
70      /**
71       * Defines the static profile for the validation 
72       * @param profile
73       *          the profile to use
74       */
75      public ValidatorAdapterDefinition staticProfile(Object profile) {
76          this.profile = profile;
77          return this;
78      }
79      
80      @Deprecated
81      public ValidatorAdapterDefinition profile(Object profile) {
82          this.profile = profile;
83          return this;
84      }
85      
86      /**
87       * Defines the profile for the validation via the given expression 
88       * @param profileExpression
89       *          the profile expression
90       */
91      public ValidatorAdapterDefinition profile(Expression profileExpression) {
92          this.profileExpression = profileExpression;
93          return this;
94      }
95      
96      /**
97       * Defines the profile for the validation via the given closure 
98       * @param profileExpression
99       *          the profile closure
100      */
101     public ProcessorAdapterDefinition profile(@ClosureParams(value = SimpleType.class, options = { "org.apache.camel.Expression"})
102                                                       Closure profileExpression) {
103         this.profileExpression = new DelegatingExpression(profileExpression);
104         return this;
105     }
106     
107     /**
108      * Interprets the defined profile as W3C schema location and validates against it 
109      */
110     public ValidatorAdapterDefinition xsd() {
111         validator = new XsdValidator();
112         return (ValidatorAdapterDefinition)input(bodyAs(StreamSource.class));
113     }
114     
115     /**
116      * Interprets the defined profile as Schematron rules location and validates against it 
117      */
118     public ValidatorAdapterDefinition schematron() {
119         validator = new SchematronValidator();
120         return (ValidatorAdapterDefinition)input(bodyAs(StreamSource.class));
121     }
122     
123     @Override
124     public String toString() {
125         return "ValidatorAdapter[" + getOutputs() + "]";
126     }
127 
128     @Override
129     public String getShortName() {
130         return "validatorAdapter";
131     }
132 
133     @Override
134     protected ProcessorAdapter doCreateProcessor(RouteContext routeContext) {
135         if (validatorBean != null) {
136             validator = routeContext.lookup(validatorBean, Validator.class);
137         }
138         ValidatorAdapter adapter = new ValidatorAdapter(validator);
139         if (profile != null) {
140             return adapter.staticProfile(profile);
141         }
142         if (profileExpression != null) {
143             return adapter.profile(profileExpression);
144         }
145         return adapter;
146     }
147 
148     private static class AlwaysValid implements Validator<Object, Object> {
149 
150         @Override
151         public void validate(Object message, Object profile) {
152             // any input is valid
153         }
154         
155     }
156     
157 }