KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xalan > xsltc > compiler > Include


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17  * $Id: Include.java,v 1.28 2004/02/16 22:24:29 minchau Exp $
18  */

19
20 package org.apache.xalan.xsltc.compiler;
21
22 import java.io.File JavaDoc;
23 import java.io.FileNotFoundException JavaDoc;
24 import java.net.MalformedURLException JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.util.Enumeration JavaDoc;
27
28 import org.apache.xml.utils.SystemIDResolver;
29 import org.apache.xalan.xsltc.compiler.util.ClassGenerator;
30 import org.apache.xalan.xsltc.compiler.util.ErrorMsg;
31 import org.apache.xalan.xsltc.compiler.util.MethodGenerator;
32 import org.apache.xalan.xsltc.compiler.util.Type;
33 import org.apache.xalan.xsltc.compiler.util.TypeCheckError;
34
35 import org.xml.sax.InputSource JavaDoc;
36 import org.xml.sax.XMLReader JavaDoc;
37
38 /**
39  * @author Jacek Ambroziak
40  * @author Morten Jorgensen
41  * @author Erwin Bolwidt <ejb@klomp.org>
42  * @author Gunnlaugur Briem <gthb@dimon.is>
43  */

44 final class Include extends TopLevelElement {
45
46     private Stylesheet _included = null;
47
48     public Stylesheet getIncludedStylesheet() {
49     return _included;
50     }
51
52     public void parseContents(final Parser parser) {
53     XSLTC xsltc = parser.getXSLTC();
54     Stylesheet context = parser.getCurrentStylesheet();
55         
56     String JavaDoc docToLoad = getAttribute("href");
57     try {
58         if (context.checkForLoop(docToLoad)) {
59         final ErrorMsg msg = new ErrorMsg(ErrorMsg.CIRCULAR_INCLUDE_ERR,
60                                                   docToLoad, this);
61         parser.reportError(Constants.FATAL, msg);
62         return;
63         }
64
65         InputSource JavaDoc input = null;
66         XMLReader JavaDoc reader = null;
67         String JavaDoc currLoadedDoc = context.getSystemId();
68         SourceLoader loader = context.getSourceLoader();
69             
70             // Use SourceLoader if available
71
if (loader != null) {
72         input = loader.loadSource(docToLoad, currLoadedDoc, xsltc);
73                 if (input != null) {
74                     docToLoad = input.getSystemId();
75                     reader = xsltc.getXMLReader();
76                 }
77         }
78
79             // No SourceLoader or not resolved by SourceLoader
80
if (input == null) {
81                 docToLoad = SystemIDResolver.getAbsoluteURI(docToLoad, currLoadedDoc);
82                 input = new InputSource JavaDoc(docToLoad);
83         }
84
85         // Return if we could not resolve the URL
86
if (input == null) {
87         final ErrorMsg msg =
88             new ErrorMsg(ErrorMsg.FILE_NOT_FOUND_ERR, docToLoad, this);
89         parser.reportError(Constants.FATAL, msg);
90         return;
91         }
92
93         final SyntaxTreeNode root;
94             if (reader != null) {
95                 root = parser.parse(reader,input);
96             }
97             else {
98                 root = parser.parse(input);
99             }
100               
101         if (root == null) return;
102         _included = parser.makeStylesheet(root);
103         if (_included == null) return;
104
105         _included.setSourceLoader(loader);
106         _included.setSystemId(docToLoad);
107         _included.setParentStylesheet(context);
108         _included.setIncludingStylesheet(context);
109         _included.setTemplateInlining(context.getTemplateInlining());
110
111         // An included stylesheet gets the same import precedence
112
// as the stylesheet that included it.
113
final int precedence = context.getImportPrecedence();
114         _included.setImportPrecedence(precedence);
115         parser.setCurrentStylesheet(_included);
116         _included.parseContents(parser);
117
118         final Enumeration JavaDoc elements = _included.elements();
119         final Stylesheet topStylesheet = parser.getTopLevelStylesheet();
120         while (elements.hasMoreElements()) {
121         final Object JavaDoc element = elements.nextElement();
122         if (element instanceof TopLevelElement) {
123             if (element instanceof Variable) {
124             topStylesheet.addVariable((Variable) element);
125             }
126             else if (element instanceof Param) {
127             topStylesheet.addParam((Param) element);
128             }
129             else {
130             topStylesheet.addElement((TopLevelElement) element);
131             }
132         }
133         }
134     }
135     catch (Exception JavaDoc e) {
136         e.printStackTrace();
137     }
138     finally {
139         parser.setCurrentStylesheet(context);
140     }
141     }
142     
143     public Type typeCheck(SymbolTable stable) throws TypeCheckError {
144     return Type.Void;
145     }
146     
147     public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
148     // do nothing
149
}
150 }
151
Popular Tags