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;
|
---|
21 | import java.io.FileInputStream;
|
---|
22 | import java.io.FileOutputStream;
|
---|
23 | import java.io.IOException;
|
---|
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 stylesheet;
|
---|
39 |
|
---|
40 | public XalanLiaison() throws Exception {
|
---|
41 | processor = XSLTProcessorFactory.getProcessor();
|
---|
42 | }
|
---|
43 |
|
---|
44 | public void setStylesheet(File stylesheet) throws Exception {
|
---|
45 | this.stylesheet = stylesheet;
|
---|
46 | }
|
---|
47 |
|
---|
48 | public void transform(File infile, File outfile) throws Exception {
|
---|
49 | FileInputStream fis = null;
|
---|
50 | FileOutputStream fos = null;
|
---|
51 | FileInputStream xslStream = null;
|
---|
52 | try {
|
---|
53 | xslStream = new FileInputStream(stylesheet);
|
---|
54 | fis = new FileInputStream(infile);
|
---|
55 | fos = new FileOutputStream(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 ignored) {
|
---|
73 | //ignore
|
---|
74 | }
|
---|
75 | try {
|
---|
76 | if (fis != null) {
|
---|
77 | fis.close();
|
---|
78 | }
|
---|
79 | } catch (IOException ignored) {
|
---|
80 | //ignore
|
---|
81 | }
|
---|
82 | try {
|
---|
83 | if (fos != null) {
|
---|
84 | fos.close();
|
---|
85 | }
|
---|
86 | } catch (IOException ignored) {
|
---|
87 | //ignore
|
---|
88 | }
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
92 | public void addParam(String name, String value) {
|
---|
93 | processor.setStylesheetParam(name, value);
|
---|
94 | }
|
---|
95 |
|
---|
96 | } //-- XalanLiaison
|
---|