1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  package net.objectlab.qalab.m2.util;
40  
41  import javax.xml.transform.Source;
42  import javax.xml.transform.Transformer;
43  import javax.xml.transform.TransformerFactory;
44  import javax.xml.transform.TransformerException;
45  
46  import javax.xml.transform.stream.StreamSource;
47  import javax.xml.transform.stream.StreamResult;
48  
49  import java.io.File;
50  import java.io.InputStream;
51  
52  import java.util.Map;
53  import java.util.HashMap;
54  import java.util.Iterator;
55  
56  /**
57   * A simple XML Transformer that is supplied with an XML file, an XSLT
58   * file and an output file.
59   * @author <a href="http://www.davesag.com">Dave Sag</a>.
60   */
61  public class XmlTransformer {
62  
63      /** the XMl source file. */
64      private final Source theXML;
65  
66      /** the XSL source file. */
67      private final Source theXSLT;
68  
69      /** the output file. */
70      private final StreamResult theOutput;
71  
72      /** the map of parameters. */
73      private transient Map theParameters;
74  
75      /**
76       * Constructor that takes the xml and xslt input stream and output file.
77       * @param anXml The file containing well formed XML to be transformed.
78       * @param anXslt The file containing the XSLT that tranforms the XML.
79       * @param anOutput The file to write the outout to.
80       */
81      public XmlTransformer(final InputStream anXml,
82              final InputStream anXslt, final File anOutput) {
83          assert anXml != null : "The supplied XML file was null.";
84          assert anXslt != null : "The supplied XSLT file was null.";
85          assert anOutput != null : "The supplied output file was null.";
86  
87          anOutput.getParentFile().mkdirs();
88  
89          theXML = new StreamSource(anXml);
90          theXSLT = new StreamSource(anXslt);
91          theOutput = new StreamResult(anOutput);
92      }
93  
94      /**
95       * Add an optional XSLT parameter.
96       * @param aKey The key. a two-part string, the namespace URI enclosed in
97       * curly braces ({}), followed by the local name. If the name has a
98       * null URL, the String only contain the local name.
99       * @param aValue The value.
100      */
101     public final void addParameter(final String aKey, final String aValue) {
102         if (theParameters == null) {
103             theParameters = new HashMap();
104         }
105         theParameters.put(aKey, aValue);
106     }
107 
108     /**
109      * Performs the XSLT transformation.
110      * @throws TransformerException if anything went wrong.
111      */
112     public final void transform() throws TransformerException {
113 
114         final TransformerFactory tf = TransformerFactory.newInstance();
115         final Transformer t = tf.newTransformer(theXSLT);
116 
117         
118         if (theParameters != null && !theParameters.isEmpty()) {
119             for (final Iterator i = theParameters.keySet().iterator();
120                     i.hasNext();) {
121                 final String key = (String) i.next();
122                 t.setParameter(key, theParameters.get(key));
123             }
124         }
125 
126         t.transform(theXML, theOutput);
127     }
128 }
129 
130 
131 
132 
133 
134 
135 
136 
137 
138 
139 
140 
141 
142 
143 
144