1 /* 2 * Copyright (c) 2007, 2017, Oracle and/or its affiliates. All rights reserved. 3 */ 4 /* 5 * Licensed to the Apache Software Foundation (ASF) under one or more 6 * contributor license agreements. See the NOTICE file distributed with 7 * this work for additional information regarding copyright ownership. 8 * The ASF licenses this file to You under the Apache License, Version 2.0 9 * (the "License"); you may not use this file except in compliance with 10 * the License. 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.xsltc.compiler.util.ClassGenerator; 28 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg; 29 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator; 30 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type; 31 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError; 32 import com.sun.org.apache.xml.internal.utils.SystemIDResolver; 33 import java.util.Iterator; 34 import javax.xml.XMLConstants; 35 import jdk.xml.internal.SecuritySupport; 36 import org.xml.sax.InputSource; 37 import org.xml.sax.XMLReader; 38 39 /** 40 * @author Jacek Ambroziak 41 * @author Morten Jorgensen 42 * @author Erwin Bolwidt <ejb@klomp.org> 43 * @author Gunnlaugur Briem <gthb@dimon.is> 44 * @LastModified: Sep 2017 45 */ 46 final class Include extends TopLevelElement { 47 48 private Stylesheet _included = null; 49 50 public Stylesheet getIncludedStylesheet() { 51 return _included; 52 } 53 54 public void parseContents(final Parser parser) { 55 XSLTC xsltc = parser.getXSLTC(); 56 Stylesheet context = parser.getCurrentStylesheet(); 57 58 String docToLoad = getAttribute("href"); 59 try { 60 if (context.checkForLoop(docToLoad)) { 61 final ErrorMsg msg = new ErrorMsg(ErrorMsg.CIRCULAR_INCLUDE_ERR, 62 docToLoad, this); 63 parser.reportError(Constants.FATAL, msg); 64 return; 65 } 66 67 InputSource input = null; 68 XMLReader reader = null; 69 String currLoadedDoc = context.getSystemId(); 70 SourceLoader loader = context.getSourceLoader(); 71 72 // Use SourceLoader if available 73 if (loader != null) { 74 input = loader.loadSource(docToLoad, currLoadedDoc, xsltc); 75 if (input != null) { 76 docToLoad = input.getSystemId(); 77 reader = xsltc.getXMLReader(); 78 } else if (parser.errorsFound()) { 79 return; 80 } 81 } 82 83 // No SourceLoader or not resolved by SourceLoader 84 if (input == null) { 85 docToLoad = SystemIDResolver.getAbsoluteURI(docToLoad, currLoadedDoc); 86 String accessError = SecuritySupport.checkAccess(docToLoad, 87 (String)xsltc.getProperty(XMLConstants.ACCESS_EXTERNAL_STYLESHEET), 88 XalanConstants.ACCESS_EXTERNAL_ALL); 89 90 if (accessError != null) { 91 final ErrorMsg msg = new ErrorMsg(ErrorMsg.ACCESSING_XSLT_TARGET_ERR, 92 SecuritySupport.sanitizePath(docToLoad), accessError, 93 this); 94 parser.reportError(Constants.FATAL, msg); 95 return; 96 } 97 input = new InputSource(docToLoad); 98 } 99 100 // Return if we could not resolve the URL 101 if (input == null) { 102 final ErrorMsg msg = 103 new ErrorMsg(ErrorMsg.FILE_NOT_FOUND_ERR, docToLoad, this); 104 parser.reportError(Constants.FATAL, msg); 105 return; 106 } 107 108 final SyntaxTreeNode root; 109 if (reader != null) { 110 root = parser.parse(reader,input); 111 } 112 else { 113 root = parser.parse(input); 114 } 115 116 if (root == null) return; 117 _included = parser.makeStylesheet(root); 118 if (_included == null) return; 119 120 _included.setSourceLoader(loader); 121 _included.setSystemId(docToLoad); 122 _included.setParentStylesheet(context); 123 _included.setIncludingStylesheet(context); 124 _included.setTemplateInlining(context.getTemplateInlining()); 125 126 // An included stylesheet gets the same import precedence 127 // as the stylesheet that included it. 128 final int precedence = context.getImportPrecedence(); 129 _included.setImportPrecedence(precedence); 130 parser.setCurrentStylesheet(_included); 131 _included.parseContents(parser); 132 133 final Iterator<SyntaxTreeNode> elements = _included.elements(); 134 final Stylesheet topStylesheet = parser.getTopLevelStylesheet(); 135 while (elements.hasNext()) { 136 final Object element = elements.next(); 137 if (element instanceof TopLevelElement) { 138 if (element instanceof Variable) { 139 topStylesheet.addVariable((Variable) element); 140 } 141 else if (element instanceof Param) { 142 topStylesheet.addParam((Param) element); 143 } 144 else { 145 topStylesheet.addElement((TopLevelElement) element); 146 } 147 } 148 } 149 } 150 catch (Exception e) { 151 e.printStackTrace(); 152 } 153 finally { 154 parser.setCurrentStylesheet(context); 155 } 156 } 157 158 public Type typeCheck(SymbolTable stable) throws TypeCheckError { 159 return Type.Void; 160 } 161 162 public void translate(ClassGenerator classGen, MethodGenerator methodGen) { 163 // do nothing 164 } 165 }