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  
20  import javax.xml.bind.annotation.*;
21  import javax.xml.transform.stream.StreamSource;
22  
23  import org.apache.camel.spi.Metadata;
24  import org.apache.camel.spi.RouteContext;
25  import org.openehealth.ipf.commons.core.modules.api.Transmogrifier;
26  import org.openehealth.ipf.commons.xml.SchematronTransmogrifier;
27  import org.openehealth.ipf.commons.xml.XqjTransmogrifier;
28  import org.openehealth.ipf.commons.xml.XsltTransmogrifier;
29  import org.openehealth.ipf.platform.camel.core.adapter.ProcessorAdapter;
30  import org.openehealth.ipf.platform.camel.core.adapter.TransmogrifierAdapter;
31  
32  /**
33   * @author Martin Krasser
34   */
35  @Metadata(label = "ipf,eip,transformation")
36  @XmlRootElement(name = "transmogrify")
37  @XmlAccessorType(XmlAccessType.FIELD)
38  public class TransmogrifierAdapterDefinition extends ProcessorAdapterDefinition {
39  
40      @XmlTransient
41      private Transmogrifier transmogrifier;
42      @XmlAttribute
43      private String transmogrifierBean;
44  
45      public TransmogrifierAdapterDefinition() {
46      }
47  
48      public TransmogrifierAdapterDefinition(Transmogrifier transmogrifier) {
49          this.transmogrifier = transmogrifier;
50          params().headers();
51      }
52  
53      public TransmogrifierAdapterDefinition(String transmogrifierBean) {
54          this.transmogrifierBean = transmogrifierBean;
55          params().headers();
56      }
57  
58      @Override
59      public String toString() {
60          return "TransmogrifierAdapter[" + getOutputs() + "]";
61      }
62  
63      @Override
64      public String getShortName() {
65          return "transmogrifierAdapter";
66      }
67      
68      /**
69       * Specifies that the transformation is done via XSLT
70       */
71      public TransmogrifierAdapterDefinition xslt() {
72          transmogrifier = new XsltTransmogrifier<>(String.class);
73          return (TransmogrifierAdapterDefinition)input(bodyAs(StreamSource.class));
74      }
75      
76      /**
77       * Specifies that the transformation is done via XSLT
78       * 
79       * @param clazz
80       *            the resulting type of the message body after the
81       *            transformation
82       */
83      public <T> TransmogrifierAdapterDefinition xslt(Class<T> clazz) {
84          transmogrifier = new XsltTransmogrifier<>(clazz);
85          return (TransmogrifierAdapterDefinition)input(bodyAs(StreamSource.class));
86      }
87      
88      /**
89       * Specifies that the transformation is done via XQuery
90       * 
91       */
92      public TransmogrifierAdapterDefinition xquery() {
93          transmogrifier = new XqjTransmogrifier<>(String.class);
94          return (TransmogrifierAdapterDefinition) input(bodyAs(StreamSource.class));
95      }
96  
97      /**
98       * Specifies that the transformation is done via XQuery
99       * 
100      * @param clazz
101      *            the resulting type of the message body after the
102      *            transformation
103      */
104     public <T> TransmogrifierAdapterDefinition xquery(Class<T> clazz) {
105         transmogrifier = new XqjTransmogrifier<>(clazz);
106         return (TransmogrifierAdapterDefinition) input(bodyAs(StreamSource.class));
107     }
108 
109     /**
110      * Specifies that a schematron validation report is generated
111      */
112     public TransmogrifierAdapterDefinition schematron() {
113         transmogrifier = new SchematronTransmogrifier<>(String.class);
114         return (TransmogrifierAdapterDefinition)input(bodyAs(StreamSource.class));
115     }
116 
117     /**
118      * Specifies that a schematron validation report is generated
119      * @param clazz
120      *          the resulting type of the message bodyf after the transformation
121      */
122     public <T> TransmogrifierAdapterDefinition schematron(Class<T> clazz) {
123         transmogrifier = new SchematronTransmogrifier<>(clazz);
124         return (TransmogrifierAdapterDefinition)input(bodyAs(StreamSource.class));
125     }
126 
127     
128     @Override
129     protected ProcessorAdapter doCreateProcessor(RouteContext routeContext) {
130         if (transmogrifierBean != null) {
131             transmogrifier = routeContext.lookup(transmogrifierBean, Transmogrifier.class);
132         }
133         return new TransmogrifierAdapter(transmogrifier);
134     }
135 
136 }