KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > XalanLiaison


1 /*
2  * Copyright 2000-2002,2004 The Apache Software Foundation
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  */

17
18 package org.apache.tools.ant.taskdefs.optional;
19
20 import java.io.File JavaDoc;
21 import java.io.FileInputStream JavaDoc;
22 import java.io.FileOutputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import org.apache.tools.ant.taskdefs.XSLTLiaison;
25 import org.apache.xalan.xslt.XSLTInputSource;
26 import org.apache.xalan.xslt.XSLTProcessor;
27 import org.apache.xalan.xslt.XSLTProcessorFactory;
28 import org.apache.xalan.xslt.XSLTResultTarget;
29
30 /**
31  * Concrete liaison for Xalan 1.x API.
32  *
33  * @since Ant 1.1
34  */

35 public class XalanLiaison implements XSLTLiaison {
36
37     protected XSLTProcessor processor;
38     protected File JavaDoc stylesheet;
39
40     public XalanLiaison() throws Exception JavaDoc {
41         processor = XSLTProcessorFactory.getProcessor();
42     }
43
44     public void setStylesheet(File JavaDoc stylesheet) throws Exception JavaDoc {
45         this.stylesheet = stylesheet;
46     }
47
48     public void transform(File JavaDoc infile, File JavaDoc outfile) throws Exception JavaDoc {
49         FileInputStream JavaDoc fis = null;
50         FileOutputStream JavaDoc fos = null;
51         FileInputStream JavaDoc xslStream = null;
52         try {
53             xslStream = new FileInputStream JavaDoc(stylesheet);
54             fis = new FileInputStream JavaDoc(infile);
55             fos = new FileOutputStream JavaDoc(outfile);
56             // systemid such as file:/// + getAbsolutePath() are considered
57
// invalid here...
58
XSLTInputSource xslSheet = new XSLTInputSource(xslStream);
59             xslSheet.setSystemId(stylesheet.getAbsolutePath());
60             XSLTInputSource src = new XSLTInputSource(fis);
61             src.setSystemId(infile.getAbsolutePath());
62             XSLTResultTarget res = new XSLTResultTarget(fos);
63             processor.process(src, xslSheet, res);
64         } finally {
65             // make sure to close all handles, otherwise the garbage
66
// collector will close them...whenever possible and
67
// Windows may complain about not being able to delete files.
68
try {
69                 if (xslStream != null) {
70                     xslStream.close();
71                 }
72             } catch (IOException JavaDoc ignored) {
73                 //ignore
74
}
75             try {
76                 if (fis != null) {
77                     fis.close();
78                 }
79             } catch (IOException JavaDoc ignored) {
80                 //ignore
81
}
82             try {
83                 if (fos != null) {
84                     fos.close();
85                 }
86             } catch (IOException JavaDoc ignored) {
87                 //ignore
88
}
89         }
90     }
91
92     public void addParam(String JavaDoc name, String JavaDoc value) {
93         processor.setStylesheetParam(name, value);
94     }
95
96 } //-- XalanLiaison
97
Popular Tags