KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.icl.saxon.style;
2 import com.icl.saxon.Context;
3 import com.icl.saxon.StylesheetStripper;
4 import com.icl.saxon.TransformerFactoryImpl;
5 import com.icl.saxon.StandardURIResolver;
6 import com.icl.saxon.om.NodeInfo;
7 import com.icl.saxon.om.DocumentInfo;
8 import com.icl.saxon.om.NamePool;
9 import com.icl.saxon.tree.TreeBuilder;
10 import com.icl.saxon.tree.DocumentImpl;
11 import com.icl.saxon.tree.ElementImpl;
12 import com.icl.saxon.tree.AttributeCollection;
13 import javax.xml.transform.Source JavaDoc;
14
15 import javax.xml.transform.TransformerConfigurationException JavaDoc;
16 import javax.xml.transform.TransformerException JavaDoc;
17 import javax.xml.transform.URIResolver JavaDoc;
18 import javax.xml.transform.sax.SAXSource JavaDoc;
19 import javax.xml.transform.dom.DOMSource JavaDoc;
20
21 import org.w3c.dom.Node JavaDoc;
22
23
24 /**
25 * Abstract class to represent xsl:include or xsl:import element in the stylesheet.<BR>
26 * The xsl:include and xsl:import elements have mandatory attribute href
27 */

28
29 public abstract class XSLGeneralIncorporate extends StyleElement {
30
31     String JavaDoc href;
32     DocumentImpl includedDoc;
33
34     /**
35     * isImport() returns true if this is an xsl:import statement rather than an xsl:include
36     */

37
38     public abstract boolean isImport();
39
40     public void prepareAttributes() throws TransformerConfigurationException JavaDoc {
41
42         StandardNames sn = getStandardNames();
43         AttributeCollection atts = getAttributeList();
44             
45         for (int a=0; a<atts.getLength(); a++) {
46             int nc = atts.getNameCode(a);
47             int f = nc & 0xfffff;
48             if (f==sn.HREF) {
49                 href = atts.getValue(a);
50             } else {
51                 checkUnknownAttribute(nc);
52             }
53         }
54
55         if (href==null) {
56             reportAbsence("href");
57         }
58     }
59
60     public void validate() throws TransformerConfigurationException JavaDoc {
61         // The node will never be validated, because it replaces itself
62
// by the contents of the included file.
63
checkEmpty();
64         checkTopLevel();
65     }
66
67     public XSLStyleSheet getIncludedStyleSheet(XSLStyleSheet importer, int precedence)
68                  throws TransformerConfigurationException JavaDoc {
69
70         if (href==null) {
71             // error already reported
72
return null;
73         }
74   
75         checkEmpty();
76         checkTopLevel();
77
78         try {
79             XSLStyleSheet thisSheet = (XSLStyleSheet)getParentNode();
80             DocumentInfo thisDoc = getDocumentRoot();
81             TransformerFactoryImpl factory =
82                 getPreparedStyleSheet().getTransformerFactory();
83             Source JavaDoc source = factory.getURIResolver().resolve(href, getBaseURI());
84
85             // if a user URI resolver returns null, try the standard one
86
// (Note, the standard URI resolver never returns null)
87
if (source==null) {
88                 source = (new StandardURIResolver (factory)).resolve(href, getBaseURI());
89             }
90
91             if (source instanceof NodeInfo) {
92                 if (source instanceof Node JavaDoc) {
93                     source = new DOMSource JavaDoc((Node JavaDoc)source);
94                 } else {
95                     throw new TransformerException JavaDoc("URIResolver must not return a " + source.getClass());
96                 }
97             }
98             SAXSource JavaDoc saxSource = factory.getSAXSource(source, true);
99                 
100             // check for recursion
101

102             XSLStyleSheet anc = thisSheet;
103             while(anc!=null) {
104                 if (saxSource.getSystemId().equals(anc.getSystemId())) {
105                     compileError("A stylesheet cannot " + getLocalName() + " itself");
106                     return null;
107                 }
108                 anc = anc.getImporter();
109             }
110
111             // load the included stylesheet
112

113             NamePool pool = getDocumentRoot().getNamePool();
114             StylesheetStripper styleStripper = new StylesheetStripper();
115             styleStripper.setStylesheetRules(pool);
116         
117             TreeBuilder builder = new TreeBuilder();
118             builder.setNamePool(pool);
119             builder.setStripper(styleStripper);
120             builder.setNodeFactory(new StyleNodeFactory(pool));
121             builder.setDiscardCommentsAndPIs(true);
122             builder.setLineNumbering(true);
123
124             includedDoc = (DocumentImpl)builder.build(saxSource);
125
126             // allow the included document to use "Literal Result Element as Stylesheet" syntax
127

128             ElementImpl outermost = (ElementImpl)includedDoc.getDocumentElement();
129             if (outermost instanceof LiteralResultElement) {
130                 includedDoc = ((LiteralResultElement)outermost).makeStyleSheet(getPreparedStyleSheet());
131                 outermost = (ElementImpl)includedDoc.getDocumentElement();
132             }
133         
134             if (!(outermost instanceof XSLStyleSheet)) {
135                 compileError("Included document " + href + " is not a stylesheet");
136                 return null;
137             }
138             XSLStyleSheet incSheet = (XSLStyleSheet)outermost;
139       
140             incSheet.setPrecedence(precedence);
141             incSheet.setImporter(importer);
142             incSheet.spliceIncludes(); // resolve any nested includes;
143

144             return incSheet;
145             
146         } catch (TransformerException JavaDoc err) {
147             compileError(err);
148             return null;
149         }
150     }
151
152     public void process(Context context)
153     {
154         // no action. The node will never be processed, because it replaces itself
155
// by the contents of the included file.
156
}
157 }
158
159 //
160
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
161
// you may not use this file except in compliance with the License. You may obtain a copy of the
162
// License at http://www.mozilla.org/MPL/
163
//
164
// Software distributed under the License is distributed on an "AS IS" basis,
165
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
166
// See the License for the specific language governing rights and limitations under the License.
167
//
168
// The Original Code is: all this file.
169
//
170
// The Initial Developer of the Original Code is
171
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
172
//
173
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
174
//
175
// Contributor(s): none.
176
//
177
Popular Tags