1. /*
  2. * Copyright 2001-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*
  17. * $Id: Include.java,v 1.28 2004/02/16 22:24:29 minchau Exp $
  18. */
  19. package com.sun.org.apache.xalan.internal.xsltc.compiler;
  20. import java.io.File;
  21. import java.io.FileNotFoundException;
  22. import java.net.MalformedURLException;
  23. import java.net.URL;
  24. import java.util.Enumeration;
  25. import com.sun.org.apache.xml.internal.utils.SystemIDResolver;
  26. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;
  27. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
  28. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;
  29. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
  30. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
  31. import org.xml.sax.InputSource;
  32. import org.xml.sax.XMLReader;
  33. /**
  34. * @author Jacek Ambroziak
  35. * @author Morten Jorgensen
  36. * @author Erwin Bolwidt <ejb@klomp.org>
  37. * @author Gunnlaugur Briem <gthb@dimon.is>
  38. */
  39. final class Include extends TopLevelElement {
  40. private Stylesheet _included = null;
  41. public Stylesheet getIncludedStylesheet() {
  42. return _included;
  43. }
  44. public void parseContents(final Parser parser) {
  45. XSLTC xsltc = parser.getXSLTC();
  46. Stylesheet context = parser.getCurrentStylesheet();
  47. String docToLoad = getAttribute("href");
  48. try {
  49. if (context.checkForLoop(docToLoad)) {
  50. final ErrorMsg msg = new ErrorMsg(ErrorMsg.CIRCULAR_INCLUDE_ERR,
  51. docToLoad, this);
  52. parser.reportError(Constants.FATAL, msg);
  53. return;
  54. }
  55. InputSource input = null;
  56. XMLReader reader = null;
  57. String currLoadedDoc = context.getSystemId();
  58. SourceLoader loader = context.getSourceLoader();
  59. // Use SourceLoader if available
  60. if (loader != null) {
  61. input = loader.loadSource(docToLoad, currLoadedDoc, xsltc);
  62. if (input != null) {
  63. docToLoad = input.getSystemId();
  64. reader = xsltc.getXMLReader();
  65. }
  66. }
  67. // No SourceLoader or not resolved by SourceLoader
  68. if (input == null) {
  69. docToLoad = SystemIDResolver.getAbsoluteURI(docToLoad, currLoadedDoc);
  70. input = new InputSource(docToLoad);
  71. }
  72. // Return if we could not resolve the URL
  73. if (input == null) {
  74. final ErrorMsg msg =
  75. new ErrorMsg(ErrorMsg.FILE_NOT_FOUND_ERR, docToLoad, this);
  76. parser.reportError(Constants.FATAL, msg);
  77. return;
  78. }
  79. final SyntaxTreeNode root;
  80. if (reader != null) {
  81. root = parser.parse(reader,input);
  82. }
  83. else {
  84. root = parser.parse(input);
  85. }
  86. if (root == null) return;
  87. _included = parser.makeStylesheet(root);
  88. if (_included == null) return;
  89. _included.setSourceLoader(loader);
  90. _included.setSystemId(docToLoad);
  91. _included.setParentStylesheet(context);
  92. _included.setIncludingStylesheet(context);
  93. _included.setTemplateInlining(context.getTemplateInlining());
  94. // An included stylesheet gets the same import precedence
  95. // as the stylesheet that included it.
  96. final int precedence = context.getImportPrecedence();
  97. _included.setImportPrecedence(precedence);
  98. parser.setCurrentStylesheet(_included);
  99. _included.parseContents(parser);
  100. final Enumeration elements = _included.elements();
  101. final Stylesheet topStylesheet = parser.getTopLevelStylesheet();
  102. while (elements.hasMoreElements()) {
  103. final Object element = elements.nextElement();
  104. if (element instanceof TopLevelElement) {
  105. if (element instanceof Variable) {
  106. topStylesheet.addVariable((Variable) element);
  107. }
  108. else if (element instanceof Param) {
  109. topStylesheet.addParam((Param) element);
  110. }
  111. else {
  112. topStylesheet.addElement((TopLevelElement) element);
  113. }
  114. }
  115. }
  116. }
  117. catch (Exception e) {
  118. e.printStackTrace();
  119. }
  120. finally {
  121. parser.setCurrentStylesheet(context);
  122. }
  123. }
  124. public Type typeCheck(SymbolTable stable) throws TypeCheckError {
  125. return Type.Void;
  126. }
  127. public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
  128. // do nothing
  129. }
  130. }