View Javadoc

1   /*
2      Licensed under the Apache License, Version 2.0 (the "License");
3      you may not use this file except in compliance with the License.
4      You may obtain a copy of the License at
5   
6        http://www.apache.org/licenses/LICENSE-2.0
7   
8      Unless required by applicable law or agreed to in writing, software
9      distributed under the License is distributed on an "AS IS" BASIS,
10     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11     See the License for the specific language governing permissions and
12     limitations under the License.
13  */
14  package uk.nhs.interoperability.transform;
15  
16  import java.io.InputStream;
17  import java.io.StringReader;
18  import java.io.StringWriter;
19  import java.util.Map;
20  
21  import javax.xml.transform.Templates;
22  import javax.xml.transform.Transformer;
23  import javax.xml.transform.TransformerConfigurationException;
24  import javax.xml.transform.TransformerException;
25  import javax.xml.transform.TransformerFactory;
26  import javax.xml.transform.stream.StreamResult;
27  import javax.xml.transform.stream.StreamSource;
28  
29  import uk.nhs.interoperability.infrastructure.ITKMessagingException;
30  import uk.nhs.interoperability.util.Logger;
31  
32  /**
33   * The Class TransformManager.
34   *
35   * @author Michael Odling-Smee
36   * @author Nicholas Jones
37   * @since 0.1
38   */
39  public class TransformManager {
40  
41  	// This class would benefit from optimisation, but is kept simple for now
42  	// It will need some rationalisation w.r.t. template locations at least for RI
43  	
44  	/**
45  	 * Do transform.
46  	 *
47  	 * @param tname the tname
48  	 * @param input the input
49  	 * @return the string
50  	 * @throws ITKMessagingException the iTK messaging exception
51  	 */
52  	public static String doTransform(String tname, String input) throws ITKMessagingException {
53  		return doTransform(tname, input, null);
54  	}
55  	
56  	/**
57  	 * Do transform.
58  	 *
59  	 * @param tname the tname
60  	 * @param input the input
61  	 * @param parameters the parameters
62  	 * @return the string
63  	 * @throws ITKMessagingException the iTK messaging exception
64  	 */
65  	public static String doTransform(String tname, String input, Map<String, String> parameters) throws ITKMessagingException {
66      	String output = "";
67      	
68  	    TransformerFactory transformerFactory = TransformerFactory.newInstance();
69  	    InputStream tis = TransformManager.class.getResourceAsStream("/"+tname);
70          StreamSource s = new StreamSource(tis);
71          //StreamSource s = new StreamSource(f);
72          Templates t = null;
73          try {
74  			t = transformerFactory.newTemplates(s);
75  	        Transformer tf = t.newTransformer();
76  	        
77  	        //Set any stylesheet parameters as appropriate
78  	        if (parameters != null) {
79  				for (Map.Entry<String, String> entry: parameters.entrySet()) {;
80  					Logger.trace("Found a stylesheet parameter " + entry);
81  					tf.setParameter(entry.getKey(), entry.getValue());
82  				}
83  			}
84  	        
85  	        StreamSource s2 = new StreamSource(new StringReader(input));
86  	        StringWriter w = new StringWriter();
87  	        StreamResult r = new StreamResult(w);
88  
89  	        tf.transform(s2, r);
90  	        output = w.getBuffer().toString();
91  	        //Logger.trace(output);
92  	        Logger.trace("Transformation complete");
93  		} catch (TransformerConfigurationException tce) {
94  			Logger.error("Error Building ITK Message",tce);
95  			throw new ITKMessagingException("Transformer Configuration Exception");
96  		} catch (TransformerException te) {
97  			Logger.error("Error Building ITK Message",te);
98  			throw new ITKMessagingException("Transformer Exception");
99  		}
100 
101         return output;
102 
103      }
104 
105 }