4 Wrapper for the Xalan 1.x processor to be easily invoked from BSH.
6 Author: Ovidiu Predescu <ovidiu@cup.hp.com>
9 Copyright (C) 2000 Ovidiu Predescu
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.
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.
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
28 This file contains portions of code from the Xalan 1.2
29 distribution, released under the Apache Software License, Version
30 1.1, copyrighted as follows:
32 Copyright (c) 1998-1999 Lotus Corporation, Inc. All Rights
33 Reserved. This software is provided without a warranty of any
39 import org.apache.xalan.xslt.XSLTProcessorFactory;
40 import org.apache.xalan.xslt.XSLTProcessor;
41 import org.apache.xalan.xslt.XSLTInputSource;
42 import org.w3c.dom.Node;
43 import org.w3c.dom.NamedNodeMap;
44 import org.apache.xalan.xslt.XSLTResultTarget;
45 import org.w3c.dom.ProcessingInstruction;
46 import java.util.Hashtable;
47 import java.util.StringTokenizer;
49 import java.io.InputStream;
50 import java.io.FileOutputStream;
54 public static void invoke(String filename, String outFilename)
57 XSLTProcessor processor = XSLTProcessorFactory.getProcessor();
58 URL top = new URL("file:/");
59 URL xmlDocURL = new URL(top, filename);
61 // InputStream xmlDocStream = new URL(context, filename).openStream();
62 XSLTInputSource xmlDoc = new XSLTInputSource(xmlDocURL.openStream());
63 Node document = processor.getSourceTreeFromInput(xmlDoc);
64 String xslDocFilename = null;
66 /* Search for a processing instruction that identifies the
67 * stylesheet to be used and obtain the XSLT sheet to be applied
69 for (Node child = document.getFirstChild();
71 child = child.getNextSibling()) {
72 if (child.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) {
73 ProcessingInstruction pi = (ProcessingInstruction)child;
74 if (pi.getNodeName().equals("xml-stylesheet")) {
75 PIA pia = new PIA(pi);
77 String type = pia.getAttribute("type");
79 if (type == null || !type.equals("text/xsl"))
82 String href = pia.getAttribute("href");
86 xslDocFilename = href;
92 if (xslDocFilename == null) {
93 System.out.println("Cannot process " + filename
94 + ": no stylesheet found!");
98 // Create the XSL document URI relative to the one of the XML
100 URL xslDocURL = new URL(xmlDocURL, xslDocFilename);
101 InputStream xslDocStream = xslDocURL.openStream();
102 XSLTInputSource xslDoc = new XSLTInputSource(xslDocStream);
103 FileOutputStream out = new FileOutputStream(outFilename);
104 // Create a new instance of xmlDoc as the one used to parse the
105 // original document cannot be passed to the process() method
106 xmlDoc = new XSLTInputSource(xmlDocURL.openStream());
107 // Finally process the XML document through the XSLT processor
108 processor.process(xmlDoc, xslDoc, new XSLTResultTarget(out));
112 System.out.println("Cannot process: " + e);
117 /* Class stolen from the Xalan sample Servlet/DefaultApplyXSL.java */
120 * Parses a processing instruction's (PI) attributes for easy
125 private Hashtable piAttributes = null;
129 * @param pi The processing instruction whose attributes are to be parsed
131 PIA(ProcessingInstruction pi)
133 piAttributes = new Hashtable();
134 StringTokenizer tokenizer = new StringTokenizer(pi.getNodeValue(), "=\"");
135 while(tokenizer.hasMoreTokens()) {
136 piAttributes.put(tokenizer.nextToken().trim(),
137 tokenizer.nextToken().trim());
142 * Returns value of specified attribute.
143 * @param name Attribute name
144 * @return Attribute value, or null if the attribute name does not exist
146 String getAttribute(String name)
148 return (String) piAttributes.get(name);