KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icl > saxon > style > XSLDocument


1 package com.icl.saxon.style;
2 import com.icl.saxon.tree.AttributeCollection;
3 import com.icl.saxon.*;
4 import com.icl.saxon.om.Name;
5 import com.icl.saxon.om.NamePool;
6 import com.icl.saxon.om.Namespace;
7
8 import com.icl.saxon.om.NamespaceException;
9 import com.icl.saxon.expr.*;
10 import com.icl.saxon.output.*;
11 import org.xml.sax.*;
12 import java.io.*;
13 import java.net.URL JavaDoc;
14 import java.util.StringTokenizer JavaDoc;
15 import java.util.Properties JavaDoc;
16
17 import javax.xml.transform.*;
18 import javax.xml.transform.stream.StreamResult JavaDoc;
19 import javax.xml.transform.sax.SAXSource JavaDoc;
20 import javax.xml.transform.sax.TransformerHandler JavaDoc;
21
22 /**
23 * An xsl:document (formerly saxon:output) element in the stylesheet. <BR>
24 * The xsl:document element takes an attribute HREF="filename". The filename will
25 * often contain parameters, e.g. {position()} to ensure that a different file is produced
26 * for each element instance. <BR>
27 * There is a further attribute method=xml|html|text which determines the format of the
28 * output file (default XML).
29 * Alternatively the xsl:document element may take a next-in-chain attribute in which case
30 * output is directed to another stylesheet.
31 * Any unrecognized namespaced attributes are interepreted as attribute value templates,
32 * and their values are added to the output properties, for use by a user-defined Emitter.
33 */

34
35 public class XSLDocument extends XSLGeneralOutput {
36
37     /**
38     * Determine whether this node is an instruction.
39     * @return true - it is an instruction
40     */

41
42     public boolean isInstruction() {
43         return true;
44     }
45
46     /**
47     * Determine whether this type of element is allowed to contain a template-body
48     * @return true: yes, it may contain a template-body
49     */

50
51     public boolean mayContainTemplateBody() {
52         return true;
53     }
54
55     public void prepareAttributes() throws TransformerConfigurationException {
56         super.prepareAttributes();
57         if (href==null) {
58             reportAbsence("href");
59         }
60     }
61
62     public void validate() throws TransformerConfigurationException {
63         if (getURI().equals(Namespace.XSLT)) {
64             // saxon:output is OK, but xsl:document requires version="1.1"
65
if (!forwardsCompatibleModeIsEnabled()) {
66                 compileError("To use xsl:document, set xsl:stylesheet version='1.1'");
67             }
68         }
69         checkWithinTemplate();
70     }
71     
72     public void process(Context context) throws TransformerException
73     {
74         Controller c = context.getController();
75         Outputter oldOutputter = c.getOutputter();
76         Properties JavaDoc prevProps = oldOutputter.getOutputProperties();
77         Properties JavaDoc details = new Properties JavaDoc(prevProps);
78         updateOutputProperties(details, context);
79         Result JavaDoc result = null;
80         FileOutputStream stream;
81
82         // following code to create any directory that doesn't exist is courtesy of
83
// Brett Knights [brett@knightsofthenet.com]
84
// Modified by MHK to work with JDK 1.1
85

86         String JavaDoc outFileName = href.evaluateAsString(context);
87         try {
88             File outFile = new File(outFileName);
89             if (!outFile.exists()) {
90                 String JavaDoc parent = outFile.getParent(); // always returns null with Microsoft JVM?
91
if (parent!=null && !Version.isPreJDK12()) {
92                     File parentPath = new File(parent);
93                     if (parentPath != null && !parentPath.exists()) {
94                         parentPath.mkdirs();
95                     }
96                     outFile.createNewFile(); // JDK 1.2 method
97
}
98             }
99             stream = new FileOutputStream(outFile);
100             result = new StreamResult JavaDoc(stream);
101         } catch (java.io.IOException JavaDoc err) {
102             throw new TransformerException("Failed to create output file " + outFileName, err);
103         }
104
105         if (nextInChain != null) {
106             String JavaDoc href = nextInChain.evaluateAsString(context);
107             TransformerHandler JavaDoc nextStyleSheet = prepareNextStylesheet(href, context);
108             ContentHandlerProxy emitter = new ContentHandlerProxy();
109             emitter.setSystemId(this.getSystemId()); // pragmatic choice of URI
110
emitter.setUnderlyingContentHandler(nextStyleSheet);
111             emitter.setRequireWellFormed(false);
112             nextStyleSheet.setResult(result);
113             result = emitter;
114         }
115         
116         c.changeOutputDestination(details, result);
117         processChildren(context);
118         c.resetOutputDestination(oldOutputter);
119         try {
120             stream.close();
121         } catch (java.io.IOException JavaDoc err) {
122             throw new TransformerException("Failed to close output file", err);
123         }
124     }
125
126 }
127
128 //
129
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
130
// you may not use this file except in compliance with the License. You may obtain a copy of the
131
// License at http://www.mozilla.org/MPL/
132
//
133
// Software distributed under the License is distributed on an "AS IS" basis,
134
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
135
// See the License for the specific language governing rights and limitations under the License.
136
//
137
// The Original Code is: all this file.
138
//
139
// The Initial Developer of the Original Code is
140
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
141
//
142
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
143
//
144
// Additional Contributor(s): Brett Knights [brett@knightsofthenet.com]
145
//
146
Popular Tags