KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > style > XSLGeneralIncorporate


1 package net.sf.saxon.style;
2 import net.sf.saxon.Configuration;
3 import net.sf.saxon.PreparedStylesheet;
4 import net.sf.saxon.expr.Expression;
5 import net.sf.saxon.instruct.Executable;
6 import net.sf.saxon.om.AttributeCollection;
7 import net.sf.saxon.trans.StaticError;
8 import net.sf.saxon.trans.XPathException;
9 import net.sf.saxon.tree.DocumentImpl;
10 import net.sf.saxon.tree.ElementImpl;
11
12 import javax.xml.transform.Source JavaDoc;
13 import javax.xml.transform.TransformerException JavaDoc;
14
15
16 /**
17 * Abstract class to represent xsl:include or xsl:import element in the stylesheet. <br>
18 * The xsl:include and xsl:import elements have mandatory attribute href
19 */

20
21 public abstract class XSLGeneralIncorporate extends StyleElement {
22
23     String JavaDoc href;
24     DocumentImpl includedDoc;
25
26     /**
27     * isImport() returns true if this is an xsl:import statement rather than an xsl:include
28     */

29
30     public abstract boolean isImport();
31
32     public void prepareAttributes() throws XPathException {
33
34         AttributeCollection atts = getAttributeList();
35
36         for (int a=0; a<atts.getLength(); a++) {
37             int nc = atts.getNameCode(a);
38             String JavaDoc f = getNamePool().getClarkName(nc);
39             if (f==StandardNames.HREF) {
40                 href = atts.getValue(a).trim();
41             } else {
42                 checkUnknownAttribute(nc);
43             }
44         }
45
46         if (href==null) {
47             reportAbsence("href");
48         }
49     }
50
51     public void validate() throws XPathException {
52         // The node will never be validated, because it replaces itself
53
// by the contents of the included file.
54
checkEmpty();
55         checkTopLevel(null);
56     }
57
58     public XSLStylesheet getIncludedStylesheet(XSLStylesheet importer, int precedence)
59                  throws XPathException {
60
61         if (href==null) {
62             // error already reported
63
return null;
64         }
65
66         checkEmpty();
67         checkTopLevel((this instanceof XSLInclude ? "XTSE0170" : "XTSE0190"));
68
69         try {
70             XSLStylesheet thisSheet = (XSLStylesheet)getParent();
71             PreparedStylesheet pss = getPreparedStylesheet();
72             Configuration config = pss.getConfiguration();
73
74             // System.err.println("GeneralIncorporate: HREF=" + href + " base=" + getBaseURI());
75
Source JavaDoc source;
76             try {
77                 source = config.getURIResolver().resolve(href, getBaseURI());
78             } catch (TransformerException JavaDoc e) {
79                 throw StaticError.makeStaticError(e);
80             }
81
82             // if a user URI resolver returns null, try the standard one
83
// (Note, the standard URI resolver never returns null)
84
if (source==null) {
85                 source = config.getSystemURIResolver().resolve(href, getBaseURI());
86             }
87
88             // check for recursion
89

90             XSLStylesheet anc = thisSheet;
91
92             if (source.getSystemId() != null) {
93                 while(anc!=null) {
94                     if (source.getSystemId().equals(anc.getSystemId())) {
95                         compileError("A stylesheet cannot " + getLocalPart() + " itself",
96                                 (this instanceof XSLInclude ? "XTSE0180" : "XTSE0210"));
97                         return null;
98                     }
99                     anc = anc.getImporter();
100                 }
101             }
102
103             StyleNodeFactory snFactory = new StyleNodeFactory(config);
104             includedDoc = PreparedStylesheet.loadStylesheetModule(source, config, getNamePool(), snFactory);
105
106             // allow the included document to use "Literal Result Element as Stylesheet" syntax
107

108             ElementImpl outermost = includedDoc.getDocumentElement();
109
110             if (outermost instanceof LiteralResultElement) {
111                 includedDoc = ((LiteralResultElement)outermost)
112                         .makeStylesheet(getPreparedStylesheet(), snFactory);
113                 outermost = includedDoc.getDocumentElement();
114             }
115
116             if (!(outermost instanceof XSLStylesheet)) {
117                 compileError("Included document " + href + " is not a stylesheet", "XTSE0165");
118                 return null;
119             }
120             XSLStylesheet incSheet = (XSLStylesheet)outermost;
121
122             if (incSheet.validationError!=null) {
123                 if (reportingCircumstances == REPORT_ALWAYS) {
124                     incSheet.compileError(incSheet.validationError);
125                 } else if (incSheet.reportingCircumstances == REPORT_UNLESS_FORWARDS_COMPATIBLE
126                               && !incSheet.forwardsCompatibleModeIsEnabled()) {
127                     incSheet.compileError(incSheet.validationError);
128                 }
129             }
130
131             incSheet.setPrecedence(precedence);
132             incSheet.setImporter(importer);
133             incSheet.spliceIncludes(); // resolve any nested includes;
134

135             // Check the consistency of input-type-annotations
136
thisSheet.setInputTypeAnnotations(incSheet.getInputTypeAnnotationsAttribute() |
137                     incSheet.getInputTypeAnnotations());
138
139             return incSheet;
140
141         } catch (XPathException err) {
142             err.setErrorCode("XTSE0165");
143             compileError(err);
144             return null;
145         }
146     }
147
148     public Expression compile(Executable exec) throws XPathException {
149         return null;
150         // no action. The node will never be compiled, because it replaces itself
151
// by the contents of the included file.
152
}
153 }
154
155 //
156
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
157
// you may not use this file except in compliance with the License. You may obtain a copy of the
158
// License at http://www.mozilla.org/MPL/
159
//
160
// Software distributed under the License is distributed on an "AS IS" basis,
161
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
162
// See the License for the specific language governing rights and limitations under the License.
163
//
164
// The Original Code is: all this file.
165
//
166
// The Initial Developer of the Original Code is Michael H. Kay.
167
//
168
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
169
//
170
// Contributor(s): none.
171
//
172
Popular Tags