1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
38
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
72
73
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
88
89
90
91 public ValidatorAdapterDefinition profile(Expression profileExpression) {
92 this.profileExpression = profileExpression;
93 return this;
94 }
95
96
97
98
99
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
109
110 public ValidatorAdapterDefinition xsd() {
111 validator = new XsdValidator();
112 return (ValidatorAdapterDefinition)input(bodyAs(StreamSource.class));
113 }
114
115
116
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
153 }
154
155 }
156
157 }