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