Initial Commit
[packages] / xemacs-packages / xslt-process / java / xslt / Saxon.java
1 /*
2     Saxon.java
3
4     Wrapper for the Saxon processor to be easily invoked from BSH.
5
6     Author: Ovidiu Predescu <ovidiu@cup.hp.com>
7     Date: December 2, 2000
8
9     Copyright (C) 2000 Ovidiu Predescu
10
11     This program is free software; you can redistribute it and/or
12     modify it under the terms of the GNU General Public License as
13     published by the Free Software Foundation; either version 2 of the
14     License, or (at your option) any later version.
15
16     This program is distributed in the hope that it will be useful,
17     but WITHOUT ANY WARRANTY; without even the implied warranty of
18     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19     General Public License for more details.
20
21     You should have received a copy of the GNU General Public License
22     along with this program; if not, write to the Free Software
23     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24     02111-1307, USA.
25  */
26
27 package xslt;
28
29 import java.io.InputStream;
30 import java.net.URL;
31 import com.icl.saxon.StyleSheet;
32 import org.xml.sax.InputSource;
33 import com.icl.saxon.ParameterSet;
34 import com.icl.saxon.trax.Processor;
35 import com.icl.saxon.trax.Templates;
36 import com.icl.saxon.trax.Transformer;
37 import com.icl.saxon.trax.serialize.Method;
38 import com.icl.saxon.trax.serialize.OutputFormat;
39 import java.io.FileOutputStream;
40 import com.icl.saxon.trax.Result;
41
42 public class Saxon 
43 {
44   public static void invoke(String filename, String outFilename)
45   {
46     try {
47       URL top = new URL("file:/");
48       URL url = new URL(top, filename);
49       filename = url.toExternalForm();
50       Processor processor = Processor.newInstance("xslt");
51       InputSource docSource = new InputSource(filename);
52       InputSource[] sources
53         = processor.getAssociatedStylesheets(docSource, null, null, null);
54
55       if (sources == null) {
56         System.err.println("No stylesheet found");
57         return;
58       }
59
60       FileOutputStream out = new FileOutputStream(outFilename);
61       Result result = new Result(out);
62       Templates templates = processor.processMultiple(sources);
63       Transformer transformer = templates.newTransformer();
64       OutputFormat format = new OutputFormat();
65       format.setMethod(Method.XML);
66       format.setIndenting(true);
67
68       transformer.setOutputFormat(format);
69       transformer.transform(docSource, result);
70       out.close();
71     }
72     catch (Exception e) {
73       System.err.println("Cannot process: " + e);
74     }
75   }
76 }
77
78 // Local Variables:
79 // c-basic-offset: 2
80 // End: