001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements. See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership. The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the  "License");
007     * you may not use this file except in compliance with the License.
008     * You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    /*
019     * $Id: Include.java 1225842 2011-12-30 15:14:35Z mrglavas $
020     */
021    
022    package org.apache.xalan.xsltc.compiler;
023    
024    import java.util.Enumeration;
025    
026    import org.apache.xalan.xsltc.compiler.util.ClassGenerator;
027    import org.apache.xalan.xsltc.compiler.util.ErrorMsg;
028    import org.apache.xalan.xsltc.compiler.util.MethodGenerator;
029    import org.apache.xalan.xsltc.compiler.util.Type;
030    import org.apache.xalan.xsltc.compiler.util.TypeCheckError;
031    import org.apache.xml.utils.SystemIDResolver;
032    import org.xml.sax.InputSource;
033    import org.xml.sax.XMLReader;
034    
035    /**
036     * @author Jacek Ambroziak
037     * @author Morten Jorgensen
038     * @author Erwin Bolwidt <ejb@klomp.org>
039     * @author Gunnlaugur Briem <gthb@dimon.is>
040     */
041    final class Include extends TopLevelElement {
042    
043        private Stylesheet _included = null;
044    
045        public Stylesheet getIncludedStylesheet() {
046            return _included;
047        }
048    
049        public void parseContents(final Parser parser) {
050            XSLTC xsltc = parser.getXSLTC();
051            Stylesheet context = parser.getCurrentStylesheet();
052            
053            String docToLoad = getAttribute("href");
054            try {
055                if (context.checkForLoop(docToLoad)) {
056                    final ErrorMsg msg = new ErrorMsg(ErrorMsg.CIRCULAR_INCLUDE_ERR,
057                                                      docToLoad, this);
058                    parser.reportError(Constants.FATAL, msg);
059                    return;
060                }
061    
062                InputSource input = null;
063                XMLReader reader = null;
064                String currLoadedDoc = context.getSystemId();
065                SourceLoader loader = context.getSourceLoader();
066                
067                // Use SourceLoader if available
068                if (loader != null) {
069                    input = loader.loadSource(docToLoad, currLoadedDoc, xsltc);
070                    if (input != null) {
071                        docToLoad = input.getSystemId();
072                        reader = xsltc.getXMLReader();
073                    }
074                }
075    
076                // No SourceLoader or not resolved by SourceLoader
077                if (input == null) {
078                    docToLoad = SystemIDResolver.getAbsoluteURI(docToLoad, currLoadedDoc);
079                    input = new InputSource(docToLoad);
080                }
081    
082                // Return if we could not resolve the URL
083                if (input == null) {
084                    final ErrorMsg msg = 
085                        new ErrorMsg(ErrorMsg.FILE_NOT_FOUND_ERR, docToLoad, this);
086                    parser.reportError(Constants.FATAL, msg);
087                    return;
088                }
089    
090                final SyntaxTreeNode root;
091                if (reader != null) {
092                    root = parser.parse(reader,input);
093                }
094                else {
095                    root = parser.parse(input);
096                }
097                  
098                if (root == null) return;
099                _included = parser.makeStylesheet(root);
100                if (_included == null) return;
101    
102                _included.setSourceLoader(loader);
103                _included.setSystemId(docToLoad);
104                _included.setParentStylesheet(context);
105                _included.setIncludingStylesheet(context);
106                _included.setTemplateInlining(context.getTemplateInlining());
107    
108                // An included stylesheet gets the same import precedence
109                // as the stylesheet that included it.
110                final int precedence = context.getImportPrecedence();
111                _included.setImportPrecedence(precedence);
112                parser.setCurrentStylesheet(_included);
113                _included.parseContents(parser);
114    
115                final Enumeration elements = _included.elements();
116                final Stylesheet topStylesheet = parser.getTopLevelStylesheet();
117                while (elements.hasMoreElements()) {
118                    final Object element = elements.nextElement();
119                    if (element instanceof TopLevelElement) {
120                        if (element instanceof Variable) {
121                            topStylesheet.addVariable((Variable) element);
122                        }
123                        else if (element instanceof Param) {
124                            topStylesheet.addParam((Param) element);
125                        }
126                        else {
127                            topStylesheet.addElement((TopLevelElement) element);
128                        }
129                    }
130                }
131            }
132            catch (Exception e) {
133                e.printStackTrace();
134            }
135            finally {
136                parser.setCurrentStylesheet(context);
137            }
138        }
139        
140        public Type typeCheck(SymbolTable stable) throws TypeCheckError {
141            return Type.Void;
142        }
143        
144        public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
145            // do nothing
146        }
147    }