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. package org.apache.tools.ant.taskdefs.optional;
  18. import java.io.File;
  19. import java.io.FileInputStream;
  20. import java.io.FileOutputStream;
  21. import java.io.IOException;
  22. import org.apache.tools.ant.taskdefs.XSLTLiaison;
  23. import org.apache.xalan.xslt.XSLTInputSource;
  24. import org.apache.xalan.xslt.XSLTProcessor;
  25. import org.apache.xalan.xslt.XSLTProcessorFactory;
  26. import org.apache.xalan.xslt.XSLTResultTarget;
  27. /**
  28. * Concrete liaison for Xalan 1.x API.
  29. *
  30. * @since Ant 1.1
  31. */
  32. public class XalanLiaison implements XSLTLiaison {
  33. protected XSLTProcessor processor;
  34. protected File stylesheet;
  35. public XalanLiaison() throws Exception {
  36. processor = XSLTProcessorFactory.getProcessor();
  37. }
  38. public void setStylesheet(File stylesheet) throws Exception {
  39. this.stylesheet = stylesheet;
  40. }
  41. public void transform(File infile, File outfile) throws Exception {
  42. FileInputStream fis = null;
  43. FileOutputStream fos = null;
  44. FileInputStream xslStream = null;
  45. try {
  46. xslStream = new FileInputStream(stylesheet);
  47. fis = new FileInputStream(infile);
  48. fos = new FileOutputStream(outfile);
  49. // systemid such as file:/// + getAbsolutePath() are considered
  50. // invalid here...
  51. XSLTInputSource xslSheet = new XSLTInputSource(xslStream);
  52. xslSheet.setSystemId(stylesheet.getAbsolutePath());
  53. XSLTInputSource src = new XSLTInputSource(fis);
  54. src.setSystemId(infile.getAbsolutePath());
  55. XSLTResultTarget res = new XSLTResultTarget(fos);
  56. processor.process(src, xslSheet, res);
  57. } finally {
  58. // make sure to close all handles, otherwise the garbage
  59. // collector will close them...whenever possible and
  60. // Windows may complain about not being able to delete files.
  61. try {
  62. if (xslStream != null) {
  63. xslStream.close();
  64. }
  65. } catch (IOException ignored) {
  66. //ignore
  67. }
  68. try {
  69. if (fis != null) {
  70. fis.close();
  71. }
  72. } catch (IOException ignored) {
  73. //ignore
  74. }
  75. try {
  76. if (fos != null) {
  77. fos.close();
  78. }
  79. } catch (IOException ignored) {
  80. //ignore
  81. }
  82. }
  83. }
  84. public void addParam(String name, String value) {
  85. processor.setStylesheetParam(name, value);
  86. }
  87. } //-- XalanLiaison