jaxp/src/share/classes/com/sun/org/apache/xalan/internal/xsltc/compiler/Import.java
changeset 6 7f561c08de6b
child 2669 15024792697e
equal deleted inserted replaced
0:fd16c54261b3 6:7f561c08de6b
       
     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: Import.java,v 1.2.4.1 2005/09/12 10:32:33 pvedula Exp $
       
    22  */
       
    23 
       
    24 package com.sun.org.apache.xalan.internal.xsltc.compiler;
       
    25 
       
    26 import java.io.File;
       
    27 import java.net.URL;
       
    28 import java.net.MalformedURLException;
       
    29 import java.util.Enumeration;
       
    30 
       
    31 import com.sun.org.apache.xml.internal.utils.SystemIDResolver;
       
    32 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;
       
    33 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
       
    34 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;
       
    35 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
       
    36 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
       
    37 
       
    38 import org.xml.sax.InputSource;
       
    39 import org.xml.sax.XMLReader;
       
    40 
       
    41 /**
       
    42  * @author Jacek Ambroziak
       
    43  * @author Morten Jorgensen
       
    44  * @author Erwin Bolwidt <ejb@klomp.org>
       
    45  * @author Gunnlaugur Briem <gthb@dimon.is>
       
    46  */
       
    47 final class Import extends TopLevelElement {
       
    48 
       
    49     private Stylesheet _imported = null;
       
    50 
       
    51     public Stylesheet getImportedStylesheet() {
       
    52         return _imported;
       
    53     }
       
    54 
       
    55     public void parseContents(final Parser parser) {
       
    56         final XSLTC xsltc = parser.getXSLTC();
       
    57         final Stylesheet context = parser.getCurrentStylesheet();
       
    58 
       
    59         try {
       
    60             String docToLoad = getAttribute("href");
       
    61             if (context.checkForLoop(docToLoad)) {
       
    62                 final ErrorMsg msg = new ErrorMsg(ErrorMsg.CIRCULAR_INCLUDE_ERR,
       
    63                                                   docToLoad, this);
       
    64                 parser.reportError(Constants.FATAL, msg);
       
    65                 return;
       
    66             }
       
    67 
       
    68             InputSource input = null;
       
    69             XMLReader reader = null;
       
    70             String currLoadedDoc = context.getSystemId();
       
    71             SourceLoader loader = context.getSourceLoader();
       
    72 
       
    73             // Use SourceLoader if available
       
    74             if (loader != null) {
       
    75                 input = loader.loadSource(docToLoad, currLoadedDoc, xsltc);
       
    76                 if (input != null) {
       
    77                     docToLoad = input.getSystemId();
       
    78                     reader = xsltc.getXMLReader();
       
    79                 } else if (parser.errorsFound()) {
       
    80                     return;
       
    81                 }
       
    82             }
       
    83 
       
    84             // No SourceLoader or not resolved by SourceLoader
       
    85             if (input == null) {
       
    86                 docToLoad = SystemIDResolver.getAbsoluteURI(docToLoad, currLoadedDoc);
       
    87                 input = new InputSource(docToLoad);
       
    88             }
       
    89 
       
    90             // Return if we could not resolve the URL
       
    91             if (input == null) {
       
    92                 final ErrorMsg msg =
       
    93                     new ErrorMsg(ErrorMsg.FILE_NOT_FOUND_ERR, docToLoad, this);
       
    94                 parser.reportError(Constants.FATAL, msg);
       
    95                 return;
       
    96             }
       
    97 
       
    98             final SyntaxTreeNode root;
       
    99             if (reader != null) {
       
   100                 root = parser.parse(reader,input);
       
   101             }
       
   102             else {
       
   103                 root = parser.parse(input);
       
   104             }
       
   105 
       
   106             if (root == null) return;
       
   107             _imported = parser.makeStylesheet(root);
       
   108             if (_imported == null) return;
       
   109 
       
   110             _imported.setSourceLoader(loader);
       
   111             _imported.setSystemId(docToLoad);
       
   112             _imported.setParentStylesheet(context);
       
   113             _imported.setImportingStylesheet(context);
       
   114         _imported.setTemplateInlining(context.getTemplateInlining());
       
   115 
       
   116             // precedence for the including stylesheet
       
   117             final int currPrecedence = parser.getCurrentImportPrecedence();
       
   118             final int nextPrecedence = parser.getNextImportPrecedence();
       
   119             _imported.setImportPrecedence(currPrecedence);
       
   120             context.setImportPrecedence(nextPrecedence);
       
   121             parser.setCurrentStylesheet(_imported);
       
   122             _imported.parseContents(parser);
       
   123 
       
   124             final Enumeration elements = _imported.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 }