You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Import.java 7.1 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 2000-2002 The Apache Software Foundation. All rights
  5. * reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if
  20. * any, must include the following acknowlegement:
  21. * "This product includes software developed by the
  22. * Apache Software Foundation (http://www.apache.org/)."
  23. * Alternately, this acknowlegement may appear in the software itself,
  24. * if and wherever such third-party acknowlegements normally appear.
  25. *
  26. * 4. The names "The Jakarta Project", "Ant", and "Apache Software
  27. * Foundation" must not be used to endorse or promote products derived
  28. * from this software without prior written permission. For written
  29. * permission, please contact apache@apache.org.
  30. *
  31. * 5. Products derived from this software may not be called "Apache"
  32. * nor may "Apache" appear in their names without prior written
  33. * permission of the Apache Group.
  34. *
  35. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This software consists of voluntary contributions made by many
  50. * individuals on behalf of the Apache Software Foundation. For more
  51. * information on the Apache Software Foundation, please see
  52. * <http://www.apache.org/>.
  53. */
  54. package org.apache.tools.ant.tasks;
  55. import org.apache.tools.ant.*;
  56. import org.apache.tools.ant.helper.*;
  57. import java.io.File;
  58. import java.io.FileInputStream;
  59. import java.io.FileNotFoundException;
  60. import java.io.IOException;
  61. import java.io.UnsupportedEncodingException;
  62. import java.util.Hashtable;
  63. import java.util.Vector;
  64. import java.util.Enumeration;
  65. import java.util.Locale;
  66. import java.util.Stack;
  67. import org.xml.sax.Locator;
  68. import org.xml.sax.InputSource;
  69. import org.xml.sax.SAXParseException;
  70. import org.xml.sax.XMLReader;
  71. import org.xml.sax.SAXException;
  72. import org.xml.sax.DocumentHandler;
  73. import org.xml.sax.Attributes;
  74. import org.xml.sax.AttributeList;
  75. import org.xml.sax.helpers.XMLReaderAdapter;
  76. import org.xml.sax.helpers.DefaultHandler;
  77. import org.xml.sax.helpers.AttributeListImpl;
  78. import org.apache.tools.ant.util.JAXPUtils;
  79. /**
  80. * Import task.
  81. *
  82. * It must be 'top level'. On execution it'll read another file
  83. * into the same Project.
  84. *
  85. * @author Nicola Ken Barozzi nicolaken@apache.org
  86. * @author Dominique Devienne DDevienne@lgc.com
  87. * @author Costin Manolache
  88. */
  89. public class Import extends Task {
  90. String file;
  91. public void setFile( String file ) {
  92. // I don't think we can use File - different rules
  93. // for relative paths.
  94. this.file=file;
  95. }
  96. /**
  97. * Initialisation routine called after handler creation
  98. * with the element name and attributes. The attributes which
  99. * this handler can deal with are: <code>"default"</code>,
  100. * <code>"name"</code>, <code>"id"</code> and <code>"basedir"</code>.
  101. *
  102. * @param tag Name of the element which caused this handler
  103. * to be created. Should not be <code>null</code>.
  104. * Ignored in this implementation.
  105. * @param attrs Attributes of the element which caused this
  106. * handler to be created. Must not be <code>null</code>.
  107. *
  108. * @exception SAXParseException if an unexpected attribute is
  109. * encountered or if the <code>"default"</code> attribute
  110. * is missing.
  111. */
  112. public void execute() throws BuildException
  113. {
  114. if (file == null) {
  115. throw new BuildException("import element appears without a file attribute");
  116. }
  117. ProjectHelperImpl2.AntXmlContext context;
  118. context=(ProjectHelperImpl2.AntXmlContext)project.getReference("ant.parsing.context");
  119. context.importlevel++;
  120. project.log("importlevel: "+(context.importlevel-1)+" -> "+(context.importlevel),
  121. Project.MSG_DEBUG);
  122. project.log("Importing file "+file+" from "+
  123. context.buildFile.getAbsolutePath(),
  124. Project.MSG_VERBOSE);
  125. // Paths are relative to the build file they're imported from,
  126. // *not* the current directory (same as entity includes).
  127. File importedFile = new File(file);
  128. if (!importedFile.isAbsolute()) {
  129. importedFile = new File(context.buildFileParent, file);
  130. }
  131. if (!importedFile.exists()) {
  132. throw new BuildException("Cannot find "+file+" imported from "+
  133. context.buildFile.getAbsolutePath());
  134. }
  135. // Add parent build file to the map to avoid cycles...
  136. String parentFilename = getPath(context.buildFile);
  137. if (!context.importedFiles.containsKey(parentFilename)) {
  138. context.importedFiles.put(parentFilename, context.buildFile);
  139. }
  140. // Make sure we import the file only once
  141. String importedFilename = getPath(importedFile);
  142. if (context.importedFiles.containsKey(importedFilename)) {
  143. project.log("\nSkipped already imported file:\n "+importedFilename+"\n",
  144. Project.MSG_WARN);
  145. context.importlevel--;
  146. project.log("importlevel: "+context.importlevel+" <- "+
  147. (context.importlevel+1) ,Project.MSG_DEBUG);
  148. return;
  149. } else {
  150. context.importedFiles.put(importedFilename, importedFile);
  151. }
  152. context.ignoreProjectTag=true;
  153. context.helper.parse(project, importedFile, new ProjectHelperImpl2.RootHandler(context));
  154. context.importlevel--;
  155. project.log("importlevel: "+context.importlevel+" <- "+
  156. (context.importlevel+1) ,Project.MSG_DEBUG);
  157. }
  158. private static String getPath(File file) {
  159. try {
  160. return file.getCanonicalPath();
  161. }
  162. catch (IOException e) {
  163. return file.getAbsolutePath();
  164. }
  165. }
  166. }