001: /*
002: * Copyright 2001-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: /*
017: * $Id: Include.java,v 1.28 2004/02/16 22:24:29 minchau Exp $
018: */
019:
020: package org.apache.xalan.xsltc.compiler;
021:
022: import java.io.File;
023: import java.io.FileNotFoundException;
024: import java.net.MalformedURLException;
025: import java.net.URL;
026: import java.util.Enumeration;
027:
028: import org.apache.xml.utils.SystemIDResolver;
029: import org.apache.xalan.xsltc.compiler.util.ClassGenerator;
030: import org.apache.xalan.xsltc.compiler.util.ErrorMsg;
031: import org.apache.xalan.xsltc.compiler.util.MethodGenerator;
032: import org.apache.xalan.xsltc.compiler.util.Type;
033: import org.apache.xalan.xsltc.compiler.util.TypeCheckError;
034:
035: import org.xml.sax.InputSource;
036: import org.xml.sax.XMLReader;
037:
038: /**
039: * @author Jacek Ambroziak
040: * @author Morten Jorgensen
041: * @author Erwin Bolwidt <ejb@klomp.org>
042: * @author Gunnlaugur Briem <gthb@dimon.is>
043: */
044: final class Include extends TopLevelElement {
045:
046: private Stylesheet _included = null;
047:
048: public Stylesheet getIncludedStylesheet() {
049: return _included;
050: }
051:
052: public void parseContents(final Parser parser) {
053: XSLTC xsltc = parser.getXSLTC();
054: Stylesheet context = parser.getCurrentStylesheet();
055:
056: String docToLoad = getAttribute("href");
057: try {
058: if (context.checkForLoop(docToLoad)) {
059: final ErrorMsg msg = new ErrorMsg(
060: ErrorMsg.CIRCULAR_INCLUDE_ERR, docToLoad, this );
061: parser.reportError(Constants.FATAL, msg);
062: return;
063: }
064:
065: InputSource input = null;
066: XMLReader reader = null;
067: String currLoadedDoc = context.getSystemId();
068: SourceLoader loader = context.getSourceLoader();
069:
070: // Use SourceLoader if available
071: if (loader != null) {
072: input = loader.loadSource(docToLoad, currLoadedDoc,
073: xsltc);
074: if (input != null) {
075: docToLoad = input.getSystemId();
076: reader = xsltc.getXMLReader();
077: }
078: }
079:
080: // No SourceLoader or not resolved by SourceLoader
081: if (input == null) {
082: docToLoad = SystemIDResolver.getAbsoluteURI(docToLoad,
083: currLoadedDoc);
084: input = new InputSource(docToLoad);
085: }
086:
087: // Return if we could not resolve the URL
088: if (input == null) {
089: final ErrorMsg msg = new ErrorMsg(
090: ErrorMsg.FILE_NOT_FOUND_ERR, docToLoad, this );
091: parser.reportError(Constants.FATAL, msg);
092: return;
093: }
094:
095: final SyntaxTreeNode root;
096: if (reader != null) {
097: root = parser.parse(reader, input);
098: } else {
099: root = parser.parse(input);
100: }
101:
102: if (root == null)
103: return;
104: _included = parser.makeStylesheet(root);
105: if (_included == null)
106: return;
107:
108: _included.setSourceLoader(loader);
109: _included.setSystemId(docToLoad);
110: _included.setParentStylesheet(context);
111: _included.setIncludingStylesheet(context);
112: _included
113: .setTemplateInlining(context.getTemplateInlining());
114:
115: // An included stylesheet gets the same import precedence
116: // as the stylesheet that included it.
117: final int precedence = context.getImportPrecedence();
118: _included.setImportPrecedence(precedence);
119: parser.setCurrentStylesheet(_included);
120: _included.parseContents(parser);
121:
122: final Enumeration elements = _included.elements();
123: final Stylesheet topStylesheet = parser
124: .getTopLevelStylesheet();
125: while (elements.hasMoreElements()) {
126: final Object element = elements.nextElement();
127: if (element instanceof TopLevelElement) {
128: if (element instanceof Variable) {
129: topStylesheet.addVariable((Variable) element);
130: } else if (element instanceof Param) {
131: topStylesheet.addParam((Param) element);
132: } else {
133: topStylesheet
134: .addElement((TopLevelElement) element);
135: }
136: }
137: }
138: } catch (Exception e) {
139: e.printStackTrace();
140: } finally {
141: parser.setCurrentStylesheet(context);
142: }
143: }
144:
145: public Type typeCheck(SymbolTable stable) throws TypeCheckError {
146: return Type.Void;
147: }
148:
149: public void translate(ClassGenerator classGen,
150: MethodGenerator methodGen) {
151: // do nothing
152: }
153: }
|