View Javadoc
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.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.utils.SecuritySupport;
28  import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;
29  import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
30  import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;
31  import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
32  import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
33  import com.sun.org.apache.xml.internal.utils.SystemIDResolver;
34  import java.io.File;
35  import java.net.URL;
36  import java.net.MalformedURLException;
37  import java.util.Enumeration;
38  import javax.xml.XMLConstants;
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 Import extends TopLevelElement {
49  
50      private Stylesheet _imported = null;
51  
52      public Stylesheet getImportedStylesheet() {
53          return _imported;
54      }
55  
56      public void parseContents(final Parser parser) {
57          final XSLTC xsltc = parser.getXSLTC();
58          final Stylesheet context = parser.getCurrentStylesheet();
59  
60          try {
61              String docToLoad = getAttribute("href");
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                  String accessError = SecuritySupport.checkAccess(docToLoad,
89                          (String)xsltc.getProperty(XMLConstants.ACCESS_EXTERNAL_STYLESHEET),
90                          XalanConstants.ACCESS_EXTERNAL_ALL);
91  
92                  if (accessError != null) {
93                      final ErrorMsg msg = new ErrorMsg(ErrorMsg.ACCESSING_XSLT_TARGET_ERR,
94                                          SecuritySupport.sanitizePath(docToLoad), accessError,
95                                          this);
96                      parser.reportError(Constants.FATAL, msg);
97                      return;
98                  }
99                  input = new InputSource(docToLoad);
100             }
101 
102             // Return if we could not resolve the URL
103             if (input == null) {
104                 final ErrorMsg msg =
105                     new ErrorMsg(ErrorMsg.FILE_NOT_FOUND_ERR, docToLoad, this);
106                 parser.reportError(Constants.FATAL, msg);
107                 return;
108             }
109 
110             final SyntaxTreeNode root;
111             if (reader != null) {
112                 root = parser.parse(reader,input);
113             }
114             else {
115                 root = parser.parse(input);
116             }
117 
118             if (root == null) return;
119             _imported = parser.makeStylesheet(root);
120             if (_imported == null) return;
121 
122             _imported.setSourceLoader(loader);
123             _imported.setSystemId(docToLoad);
124             _imported.setParentStylesheet(context);
125             _imported.setImportingStylesheet(context);
126         _imported.setTemplateInlining(context.getTemplateInlining());
127 
128             // precedence for the including stylesheet
129             final int currPrecedence = parser.getCurrentImportPrecedence();
130             final int nextPrecedence = parser.getNextImportPrecedence();
131             _imported.setImportPrecedence(currPrecedence);
132             context.setImportPrecedence(nextPrecedence);
133             parser.setCurrentStylesheet(_imported);
134             _imported.parseContents(parser);
135 
136             final Enumeration elements = _imported.elements();
137             final Stylesheet topStylesheet = parser.getTopLevelStylesheet();
138             while (elements.hasMoreElements()) {
139                 final Object element = elements.nextElement();
140                 if (element instanceof TopLevelElement) {
141                     if (element instanceof Variable) {
142                         topStylesheet.addVariable((Variable) element);
143                     }
144                     else if (element instanceof Param) {
145                         topStylesheet.addParam((Param) element);
146                     }
147                     else {
148                         topStylesheet.addElement((TopLevelElement) element);
149                     }
150                 }
151             }
152         }
153         catch (Exception e) {
154             e.printStackTrace();
155         }
156         finally {
157             parser.setCurrentStylesheet(context);
158         }
159     }
160 
161     public Type typeCheck(SymbolTable stable) throws TypeCheckError {
162         return Type.Void;
163     }
164 
165     public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
166         // do nothing
167     }
168 }