source: MDService2/trunk/MDService2/src/eu/clarin/cmdi/mdservice/action/MDTransformer.java @ 627

Last change on this file since 627 was 627, checked in by vronk, 14 years ago

introducing the user: only real change in GenericProxyAction?
the rest is just removed unused imports.

File size: 7.4 KB
Line 
1package eu.clarin.cmdi.mdservice.action;
2
3import java.io.BufferedInputStream;
4import java.io.ByteArrayInputStream;
5import java.io.ByteArrayOutputStream;
6import java.io.File;
7import java.io.FileInputStream;
8import java.io.IOException;
9import java.io.InputStream;
10import java.io.OutputStream;
11import java.io.Reader;
12import java.io.StringReader;
13import java.net.URL;
14
15import javax.xml.transform.Transformer;
16import javax.xml.transform.TransformerException;
17import javax.xml.transform.TransformerFactory;
18import javax.xml.transform.stream.StreamResult;
19import javax.xml.transform.stream.StreamSource;
20
21/**
22 * Helper class, encapsulating the xsl-transformations handling
23 * the contract is, that the requester passes a key, which can be resolved to a xsl-script (momentary mapped in properties: Admin.getConfig())
24 * Bad things happen, if the key or the appropriate xsl-file do not exist
25 *   
26 *
27 * @author vronk
28 *
29 */
30
31public class MDTransformer {
32
33        private URL srcFile ;
34       
35               
36        private static MDTransformer singleton;
37        TransformerFactory tfactory ; 
38       
39        public MDTransformer () {               
40                tfactory = TransformerFactory.newInstance();   
41        }
42       
43        public static MDTransformer getMDTransformer () {
44                if (singleton == null) {
45                        singleton = new MDTransformer();
46                } 
47                return singleton;
48        }
49       
50       
51        public URL getSrcFile() {
52                return srcFile;
53        }
54
55        public void setSrcFile(URL srcFile) {
56                this.srcFile = srcFile;
57        }
58
59        /**
60         * get the path to the transform-xsl file from properties, based on the key
61         * @param key
62         * @return
63         */
64        private String getXSLPath (String key) {               
65                String xslpath = "";
66                String xslfilename= Admin.getConfig().getProperty("xsl." + key);
67               
68                if (xslfilename!=null) {                       
69                        xslpath =  Admin.getConfig().getProperty("scripts.path") + xslfilename;
70                } 
71                Admin.notifyUser("xslfile:" + xslpath);
72                return xslpath;
73        }
74
75        private StreamSource getXSLStreamSource (String key){           
76               
77                InputStream xslstream;
78                                       
79                //URL myURL = new URL (getXSLPath(key));
80                //xslstream = myURL.openStream();
81                String xslPath = getXSLPath(key);
82                xslstream = this.getClass().getClassLoader().getResourceAsStream(xslPath);
83                StreamSource streamSource = new StreamSource(xslstream);
84
85                //Admin.notifyUser(this.getClass().getClassLoader().getResource(xslPath).toString());
86                //Admin.notifyUser(this.getClass().getClassLoader().getResource(xslPath).getPath());
87               
88                streamSource.setSystemId(this.getClass().getClassLoader().getResource(xslPath).toString());
89                return streamSource ;           
90               
91        }
92
93        /**
94         * The main method for transforming, applies the appropriate (based on the transkey) stylesheet on the xml-stream
95         * and writes the result into the output stream.   
96         * @param in InpuStream with xml 
97         * @param transkey this defines the stylesheet to use and is also passed to the stylesheet as "format"-parameter
98         * @param out the stream to write the output to
99         * @throws TransformerException
100         * @throws IOException
101         */             
102        public void transformXML (InputStream in, String transkey, OutputStream out ) throws TransformerException, IOException {
103       
104                // Create a transform factory instance.
105                TransformerFactory tfactory = TransformerFactory.newInstance();
106                //OutputStream os = new ByteArrayOutputStream();
107                StreamResult stream = new StreamResult(out);
108               
109                // Create a transformer for the stylesheet.
110                        //String xslpath = getXSLPath(transkey);               
111                Transformer transformer = tfactory.newTransformer(
112                                                                        getXSLStreamSource(transkey));
113               
114                transformer.setParameter("format", transkey);
115               
116                if (srcFile!=null) {
117                       
118                        File f = new File(srcFile.getPath());
119                           
120                        //Admin.notifyUser("src:" + srcFile.getFile());
121                        //Admin.notifyUser("fpath:" + f.getAbsolutePath());
122                        String root_uri =  srcFile.toString();
123                        String xsrcfile =  srcFile.getFile();
124                        if  (srcFile.getProtocol().equals("file")) {
125                                root_uri = "file:///" + f.getParent().replace('\\', '/');
126                                xsrcfile = "file:///" + f.getPath().replace('\\', '/');
127                        } 
128                        Admin.notifyUser("root_uri:" +  root_uri );
129                        transformer.setParameter("root_uri", root_uri );
130                        transformer.setParameter("src_file", xsrcfile);
131                } else {
132                        Admin.notifyUser("transformXML(): srcFile not set!!");                         
133                }
134                //
135               
136                StreamSource src =new StreamSource();                   
137                src.setInputStream(in);
138                // Transform the source XML to out-stream
139                transformer.transform(src, stream );
140            }
141       
142        /**
143         * just a wrapper for the main method translating the output-stream into a input-stream (expected by the Controller-Actions to return as response)
144         * @param xmlStream the source xml stream
145         * @param transkey
146         * @return result-stream (converted to InputStream)
147         * @throws IOException
148         * @throws InterruptedException
149         * @throws TransformerException
150         */
151        public InputStream transformXML ( InputStream xmlStream, String transkey) throws IOException, InterruptedException, TransformerException {
152               
153                ByteArrayOutputStream out = new ByteArrayOutputStream();
154                transformXML(xmlStream, transkey, out);         
155            InputStream transformedStream = new ByteArrayInputStream(out.toByteArray());
156            //Admin.notifyUser("transformedStream:" + transformedStream.toString());
157            return transformedStream;
158        }
159
160        /**
161         * another wrapper for the main method allowing to directly pass a URL to the source-xml   
162         * @param xmlFile URL of the source-file   
163         * @param transkey
164         * @return the result-stream (already converted to an InputStream)
165         * @throws TransformerException
166         * @throws IOException
167         */
168        public InputStream transformXML (URL xmlFile, String transkey ) throws IOException, InterruptedException, TransformerException {
169                srcFile= xmlFile;
170                InputStream  xmlStream =
171             new BufferedInputStream(new FileInputStream(xmlFile.getPath()));
172           
173                return transformXML ( xmlStream, transkey); 
174        }
175
176
177        /**
178         * this is for xml present as string.
179         * if xml in a file or a stream, use the other methods
180         * @param xml xml as string
181         * @param transkey
182         * @return
183         */
184        public String transformXML (String xml, String transkey ) {
185                String result="";
186                try {
187                // Create a transform factory instance.
188                        System.setProperty("javax.xml.transform.TransformerFactory", "net.sf.saxon.TransformerFactoryImpl");
189                TransformerFactory tfactory = TransformerFactory.newInstance();
190                OutputStream os = new ByteArrayOutputStream();
191                StreamResult stream = new StreamResult(os);
192                // Create a transformer for the stylesheet.
193                Transformer transformer = tfactory.newTransformer(
194                                                getXSLStreamSource(transkey));
195/* instead of:
196                InputStream  xslIS       =
197                    new BufferedInputStream(new FileInputStream(getXSLPath(transkey)));
198               
199                Transformer transformer =
200                    tfactory.newTransformer(new StreamSource(xslIS));
201                    */
202                transformer.setParameter("format", transkey);
203                //Admin.notifyUser("transformer.parmformat" + transformer.getParameter("format"));
204
205                // Transform the source XML to System.out.
206                StreamSource src =new StreamSource();       
207                Reader reader  = new StringReader(xml);
208                src.setReader(reader);
209               
210                transformer.transform(src, stream );
211                                    //  new StreamResult(new File("Simple2.out")));
212               
213                result = os.toString(); 
214               
215                        } catch (TransformerException e) {
216                                e.printStackTrace();
217                        }
218                       
219                        return result;
220        }
221
222}
Note: See TracBrowser for help on using the repository browser.