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 java.io.File;
  27 import java.io.FileNotFoundException;
  28 import java.net.MalformedURLException;
  29 import java.net.URL;
  30 import java.util.Enumeration;
  31 
  32 import com.sun.org.apache.xml.internal.utils.SystemIDResolver;
  33 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;
  34 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
  35 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;
  36 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
  37 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
  38 
  39 import org.xml.sax.InputSource;
  40 import org.xml.sax.XMLReader;
  41 
  42 /**
  43  * @author Jacek Ambroziak
  44  * @author Morten Jorgensen
  45  * @author Erwin Bolwidt <ejb@klomp.org>
  46  * @author Gunnlaugur Briem <gthb@dimon.is>
  47  */
  48 final class Include extends TopLevelElement {
  49 
  50     private Stylesheet _included = null;
  51 
  52     public Stylesheet getIncludedStylesheet() {
  53         return _included;
  54     }
  55 
  56     public void parseContents(final Parser parser) {
  57         XSLTC xsltc = parser.getXSLTC();
  58         Stylesheet context = parser.getCurrentStylesheet();
  59 
  60         String docToLoad = getAttribute("href");
  61         try {
  62             if (context.checkForLoop(docToLoad)) {
  63                 final ErrorMsg msg = new ErrorMsg(ErrorMsg.CIRCULAR_INCLUDE_ERR,
  64                                                   docToLoad, this);
  65                 parser.reportError(Constants.FATAL, msg);
  66                 return;
  67             }
  68 
  69             InputSource input = null;
  70             XMLReader reader = null;
  71             String currLoadedDoc = context.getSystemId();
  72             SourceLoader loader = context.getSourceLoader();
  73 
  74             // Use SourceLoader if available
  75             if (loader != null) {
  76                 input = loader.loadSource(docToLoad, currLoadedDoc, xsltc);
  77                 if (input != null) {
  78                     docToLoad = input.getSystemId();
  79                     reader = xsltc.getXMLReader();
  80                 } else if (parser.errorsFound()) {
  81                     return;
  82                 }
  83             }
  84 
  85             // No SourceLoader or not resolved by SourceLoader
  86             if (input == null) {
  87                 docToLoad = SystemIDResolver.getAbsoluteURI(docToLoad, currLoadedDoc);
  88                 input = new InputSource(docToLoad);
  89             }
  90 
  91             // Return if we could not resolve the URL
  92             if (input == null) {
  93                 final ErrorMsg msg =
  94                     new ErrorMsg(ErrorMsg.FILE_NOT_FOUND_ERR, docToLoad, this);
  95                 parser.reportError(Constants.FATAL, msg);
  96                 return;
  97             }
  98 
  99             final SyntaxTreeNode root;
 100             if (reader != null) {
 101                 root = parser.parse(reader,input);
 102             }
 103             else {
 104                 root = parser.parse(input);
 105             }
 106 
 107             if (root == null) return;
 108             _included = parser.makeStylesheet(root);
 109             if (_included == null) return;
 110 
 111             _included.setSourceLoader(loader);
 112             _included.setSystemId(docToLoad);
 113             _included.setParentStylesheet(context);
 114             _included.setIncludingStylesheet(context);
 115             _included.setTemplateInlining(context.getTemplateInlining());
 116 
 117             // An included stylesheet gets the same import precedence
 118             // as the stylesheet that included it.
 119             final int precedence = context.getImportPrecedence();
 120             _included.setImportPrecedence(precedence);
 121             parser.setCurrentStylesheet(_included);
 122             _included.parseContents(parser);
 123 
 124             final Enumeration elements = _included.elements();
 125             final Stylesheet topStylesheet = parser.getTopLevelStylesheet();
 126             while (elements.hasMoreElements()) {
 127                 final Object element = elements.nextElement();
 128                 if (element instanceof TopLevelElement) {
 129                     if (element instanceof Variable) {
 130                         topStylesheet.addVariable((Variable) element);
 131                     }
 132                     else if (element instanceof Param) {
 133                         topStylesheet.addParam((Param) element);
 134                     }
 135                     else {
 136                         topStylesheet.addElement((TopLevelElement) element);
 137                     }
 138                 }
 139             }
 140         }
 141         catch (Exception e) {
 142             e.printStackTrace();
 143         }
 144         finally {
 145             parser.setCurrentStylesheet(context);
 146         }
 147     }
 148 
 149     public Type typeCheck(SymbolTable stable) throws TypeCheckError {
 150         return Type.Void;
 151     }
 152 
 153     public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
 154         // do nothing
 155     }
 156 }