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: Import.java,v 1.8 2007/04/09 21:30:40 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 Import extends TopLevelElement {
  47 
  48     private Stylesheet _imported = null;
  49 
  50     public Stylesheet getImportedStylesheet() {
  51         return _imported;
  52     }
  53 
  54     public void parseContents(final Parser parser) {
  55         final XSLTC xsltc = parser.getXSLTC();
  56         final Stylesheet context = parser.getCurrentStylesheet();
  57 
  58         try {
  59             String docToLoad = getAttribute("href");
  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             _imported = parser.makeStylesheet(root);
 118             if (_imported == null) return;
 119 
 120             _imported.setSourceLoader(loader);
 121             _imported.setSystemId(docToLoad);
 122             _imported.setParentStylesheet(context);
 123             _imported.setImportingStylesheet(context);
 124         _imported.setTemplateInlining(context.getTemplateInlining());
 125 
 126             // precedence for the including stylesheet
 127             final int currPrecedence = parser.getCurrentImportPrecedence();
 128             final int nextPrecedence = parser.getNextImportPrecedence();
 129             _imported.setImportPrecedence(currPrecedence);
 130             context.setImportPrecedence(nextPrecedence);
 131             parser.setCurrentStylesheet(_imported);
 132             _imported.parseContents(parser);
 133 
 134             final Iterator<SyntaxTreeNode> elements = _imported.elements();
 135             final Stylesheet topStylesheet = parser.getTopLevelStylesheet();
 136             while (elements.hasNext()) {
 137                 final Object element = elements.next();
 138                 if (element instanceof TopLevelElement) {
 139                     if (element instanceof Variable) {
 140                         topStylesheet.addVariable((Variable) element);
 141                     }
 142                     else if (element instanceof Param) {
 143                         topStylesheet.addParam((Param) element);
 144                     }
 145                     else {
 146                         topStylesheet.addElement((TopLevelElement) element);
 147                     }
 148                 }
 149             }
 150         }
 151         catch (Exception e) {
 152             e.printStackTrace();
 153         }
 154         finally {
 155             parser.setCurrentStylesheet(context);
 156         }
 157     }
 158 
 159     public Type typeCheck(SymbolTable stable) throws TypeCheckError {
 160         return Type.Void;
 161     }
 162 
 163     public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
 164         // do nothing
 165     }
 166 }