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