1 /* 2 * reserved comment block 3 * DO NOT REMOVE OR ALTER! 4 */ 5 /* 6 * Copyright 2001-2004 The Apache Software Foundation. 7 * 8 * Licensed under the Apache License, Version 2.0 (the "License"); 9 * you may not use this file except in compliance with the License. 10 * You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, software 15 * distributed under the License is distributed on an "AS IS" BASIS, 16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 * See the License for the specific language governing permissions and 18 * limitations under the License. 19 */ 20 /* 21 * $Id: Include.java,v 1.8 2007/04/09 21:30:41 joehw Exp $ 22 */ 23 24 package com.sun.org.apache.xalan.internal.xsltc.compiler; 25 26 import com.sun.org.apache.xalan.internal.XalanConstants; 27 import com.sun.org.apache.xalan.internal.utils.SecuritySupport; 28 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator; 29 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg; 30 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator; 31 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type; 32 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError; 33 import com.sun.org.apache.xml.internal.utils.SystemIDResolver; 34 import java.io.File; 35 import java.io.FileNotFoundException; 36 import java.net.MalformedURLException; 37 import java.net.URL; 38 import java.util.Enumeration; 39 import javax.xml.XMLConstants; 40 import org.xml.sax.InputSource; 41 import org.xml.sax.XMLReader; 42 43 /** 44 * @author Jacek Ambroziak 45 * @author Morten Jorgensen 46 * @author Erwin Bolwidt <ejb@klomp.org> 47 * @author Gunnlaugur Briem <gthb@dimon.is> 48 */ 49 final class Include extends TopLevelElement { 50 51 private Stylesheet _included = null; 52 53 public Stylesheet getIncludedStylesheet() { 54 return _included; 55 } 56 57 public void parseContents(final Parser parser) { 58 XSLTC xsltc = parser.getXSLTC(); 59 Stylesheet context = parser.getCurrentStylesheet(); 60 61 String docToLoad = getAttribute("href"); 62 try { 63 if (context.checkForLoop(docToLoad)) { 64 final ErrorMsg msg = new ErrorMsg(ErrorMsg.CIRCULAR_INCLUDE_ERR, 65 docToLoad, this); 66 parser.reportError(Constants.FATAL, msg); 67 return; 68 } 69 70 InputSource input = null; 71 XMLReader reader = null; 72 String currLoadedDoc = context.getSystemId(); 73 SourceLoader loader = context.getSourceLoader(); 74 75 // Use SourceLoader if available 76 if (loader != null) { 77 input = loader.loadSource(docToLoad, currLoadedDoc, xsltc); 78 if (input != null) { 79 docToLoad = input.getSystemId(); 80 reader = xsltc.getXMLReader(); 81 } else if (parser.errorsFound()) { 82 return; 83 } 84 } 85 86 // No SourceLoader or not resolved by SourceLoader 87 if (input == null) { 88 docToLoad = SystemIDResolver.getAbsoluteURI(docToLoad, currLoadedDoc); 89 String accessError = SecuritySupport.checkAccess(docToLoad, 90 xsltc.getProperty(XMLConstants.ACCESS_EXTERNAL_STYLESHEET), 91 XalanConstants.ACCESS_EXTERNAL_ALL); 92 93 if (accessError != null) { 94 final ErrorMsg msg = new ErrorMsg(ErrorMsg.ACCESSING_XSLT_TARGET_ERR, 95 SecuritySupport.sanitizePath(docToLoad), accessError, 96 this); 97 parser.reportError(Constants.FATAL, msg); 98 return; 99 } 100 input = new InputSource(docToLoad); 101 } 102 103 // Return if we could not resolve the URL 104 if (input == null) { 105 final ErrorMsg msg = 106 new ErrorMsg(ErrorMsg.FILE_NOT_FOUND_ERR, docToLoad, this); 107 parser.reportError(Constants.FATAL, msg); 108 return; 109 } 110 111 final SyntaxTreeNode root; 112 if (reader != null) { 113 root = parser.parse(reader,input); 114 } 115 else { 116 root = parser.parse(input); 117 } 118 119 if (root == null) return; 120 _included = parser.makeStylesheet(root); 121 if (_included == null) return; 122 123 _included.setSourceLoader(loader); 124 _included.setSystemId(docToLoad); 125 _included.setParentStylesheet(context); 126 _included.setIncludingStylesheet(context); 127 _included.setTemplateInlining(context.getTemplateInlining()); 128 129 // An included stylesheet gets the same import precedence 130 // as the stylesheet that included it. 131 final int precedence = context.getImportPrecedence(); 132 _included.setImportPrecedence(precedence); 133 parser.setCurrentStylesheet(_included); 134 _included.parseContents(parser); 135 136 final Enumeration elements = _included.elements(); 137 final Stylesheet topStylesheet = parser.getTopLevelStylesheet(); 138 while (elements.hasMoreElements()) { 139 final Object element = elements.nextElement(); 140 if (element instanceof TopLevelElement) { 141 if (element instanceof Variable) { 142 topStylesheet.addVariable((Variable) element); 143 } 144 else if (element instanceof Param) { 145 topStylesheet.addParam((Param) element); 146 } 147 else { 148 topStylesheet.addElement((TopLevelElement) element); 149 } 150 } 151 } 152 } 153 catch (Exception e) { 154 e.printStackTrace(); 155 } 156 finally { 157 parser.setCurrentStylesheet(context); 158 } 159 } 160 161 public Type typeCheck(SymbolTable stable) throws TypeCheckError { 162 return Type.Void; 163 } 164 165 public void translate(ClassGenerator classGen, MethodGenerator methodGen) { 166 // do nothing 167 } 168 }