KickJava   Java API By Example, From Geeks To Geeks.

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


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: Import.java,v 1.24 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.net.URL JavaDoc;
24 import java.net.MalformedURLException JavaDoc;
25 import java.util.Enumeration JavaDoc;
26
27 import org.apache.xml.utils.SystemIDResolver;
28 import org.apache.xalan.xsltc.compiler.util.ClassGenerator;
29 import org.apache.xalan.xsltc.compiler.util.ErrorMsg;
30 import org.apache.xalan.xsltc.compiler.util.MethodGenerator;
31 import org.apache.xalan.xsltc.compiler.util.Type;
32 import org.apache.xalan.xsltc.compiler.util.TypeCheckError;
33
34 import org.xml.sax.InputSource JavaDoc;
35 import org.xml.sax.XMLReader JavaDoc;
36
37 /**
38  * @author Jacek Ambroziak
39  * @author Morten Jorgensen
40  * @author Erwin Bolwidt <ejb@klomp.org>
41  * @author Gunnlaugur Briem <gthb@dimon.is>
42  */

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