Initial Commit
[packages] / xemacs-packages / xslt-process / java / xslt / Xalan1.java
1 /*
2     Xalan1.java
3
4     Wrapper for the Xalan 1.x processor to be easily invoked from BSH.
5
6     Author: Ovidiu Predescu <ovidiu@cup.hp.com>
7     Date: December 6, 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 /*
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:
31
32     Copyright (c) 1998-1999 Lotus Corporation, Inc. All Rights
33     Reserved.  This software is provided without a warranty of any
34     kind.
35 */
36
37 package xslt;
38
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;
48 import java.net.URL;
49 import java.io.InputStream;
50 import java.io.FileOutputStream;
51
52 public class Xalan1 
53 {
54   public static void invoke(String filename, String outFilename)
55   {
56     try {
57       XSLTProcessor processor = XSLTProcessorFactory.getProcessor();
58       URL top = new URL("file:/");
59       URL xmlDocURL = new URL(top, filename);
60       
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;
65
66       /* Search for a processing instruction that identifies the
67        * stylesheet to be used and obtain the XSLT sheet to be applied
68        * on the document. */
69       for (Node child = document.getFirstChild();
70            child != null;
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);
76
77             String type = pia.getAttribute("type");
78               
79             if (type == null || !type.equals("text/xsl"))
80               continue;
81
82             String href = pia.getAttribute("href");
83
84             if (href == null)
85               continue;
86             xslDocFilename = href;
87             break;
88           }
89         }
90       }
91
92       if (xslDocFilename == null) {
93         System.out.println("Cannot process " + filename
94                            + ":  no stylesheet found!");
95         return;
96       }
97
98       // Create the XSL document URI relative to the one of the XML
99       // document
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));
109       out.close();
110     }
111     catch(Exception e) {
112       System.out.println("Cannot process: " + e);
113     }
114   }
115 }
116
117 /* Class stolen from the Xalan sample Servlet/DefaultApplyXSL.java */
118
119 /**
120  * Parses a processing instruction's (PI) attributes for easy
121  * retrieval.
122  */
123 class PIA
124 {
125   private Hashtable piAttributes = null;
126
127   /**
128    * Constructor.
129    * @param pi The processing instruction whose attributes are to be parsed
130    */
131   PIA(ProcessingInstruction pi)
132   {
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());
138     }
139   }
140
141   /**
142    * Returns value of specified attribute.
143    *  @param name Attribute name
144    *  @return Attribute value, or null if the attribute name does not exist
145    */
146   String getAttribute(String name)
147   {
148     return (String) piAttributes.get(name);
149   }
150 }
151
152 // Local Variables:
153 // c-basic-offset: 2
154 // End: