Пример #1
0
  /** Executes the task. */
  public void execute() throws BuildException {
    checkParameters();

    final FileUtils fUtils = FileUtils.getFileUtils();
    final Project project = getProject();
    for (FileSet fileset : filesets) {
      final DirectoryScanner scanner = fileset.getDirectoryScanner(project);

      final File fromDir = fileset.getDir(project);
      final String[] srcFiles = scanner.getIncludedFiles();

      for (int fIndex = 0; fIndex < srcFiles.length; fIndex++) {
        final File file = fUtils.resolveFile(fromDir, srcFiles[fIndex]);

        if (!file.isFile()) {
          continue;
        }

        if (false == forceAllFiles && false == file.getName().endsWith(".java")) {
          throw new BuildException(
              "File does not end with '.java'. Use 'force' attribute to override.");
        }

        try {
          checkLicense(file);
        } catch (IOException e) {
          throw new BuildException("Could not process file: " + file.getAbsolutePath(), e);
        }
      }
    }
  }
Пример #2
0
 /**
  * Sets an optional super grammar file. Use setGlib(File superGrammar) instead.
  *
  * @param superGrammar the super grammar filename
  * @deprecated since ant 1.6
  */
 public void setGlib(String superGrammar) {
   String sg = null;
   if (Os.isFamily("dos")) {
     sg = superGrammar.replace('\\', '/');
   } else {
     sg = superGrammar;
   }
   setGlib(FILE_UTILS.resolveFile(getProject().getBaseDir(), sg));
 }
Пример #3
0
 /**
  * Check to see if the target is up to date with respect to input files.
  *
  * @param files the list of files to check.
  * @return true if the cab file is newer than its dependents.
  */
 protected boolean isUpToDate(Vector files) {
   boolean upToDate = true;
   final int size = files.size();
   for (int i = 0; i < size && upToDate; i++) {
     String file = files.elementAt(i).toString();
     if (FILE_UTILS.resolveFile(baseDir, file).lastModified() > cabFile.lastModified()) {
       upToDate = false;
     }
   }
   return upToDate;
 }
  /**
   * transformation
   *
   * @throws BuildException exception if something goes wrong with the transformation.
   */
  public void transform() throws BuildException {
    checkOptions();
    Project project = task.getProject();

    TempFile tempFileTask = new TempFile();
    tempFileTask.bindToOwner(task);

    XSLTProcess xsltTask = new XSLTProcess();
    xsltTask.bindToOwner(task);

    xsltTask.setXslResource(getStylesheet());

    // acrobatic cast.
    xsltTask.setIn(((XMLResultAggregator) task).getDestinationFile());
    File outputFile = null;
    if (format.equals(FRAMES)) {
      String tempFileProperty = getClass().getName() + String.valueOf(counter++);
      File tmp =
          FILE_UTILS.resolveFile(project.getBaseDir(), project.getProperty("java.io.tmpdir"));
      tempFileTask.setDestDir(tmp);
      tempFileTask.setProperty(tempFileProperty);
      tempFileTask.execute();
      outputFile = new File(project.getProperty(tempFileProperty));
    } else {
      outputFile = new File(toDir, "junit-noframes.html");
    }
    xsltTask.setOut(outputFile);
    for (Iterator i = params.iterator(); i.hasNext(); ) {
      XSLTProcess.Param param = (XSLTProcess.Param) i.next();
      XSLTProcess.Param newParam = xsltTask.createParam();
      newParam.setProject(task.getProject());
      newParam.setName(param.getName());
      newParam.setExpression(param.getExpression());
    }
    XSLTProcess.Param paramx = xsltTask.createParam();
    paramx.setProject(task.getProject());
    paramx.setName("output.dir");
    paramx.setExpression(toDir.getAbsolutePath());
    final long t0 = System.currentTimeMillis();
    try {
      xsltTask.execute();
    } catch (Exception e) {
      throw new BuildException("Errors while applying transformations: " + e.getMessage(), e);
    }
    final long dt = System.currentTimeMillis() - t0;
    task.log("Transform time: " + dt + "ms");
    if (format.equals(FRAMES)) {
      Delete delete = new Delete();
      delete.bindToOwner(task);
      delete.setFile(outputFile);
      delete.execute();
    }
  }
    /**
     * Resolves file: URIs relative to the build file.
     *
     * @param publicId The public identifier, or <code>null</code> if none is available. Ignored in
     *     this implementation.
     * @param systemId The system identifier provided in the XML document. Will not be <code>null
     *     </code>.
     * @return an inputsource for this identifier
     */
    public InputSource resolveEntity(String publicId, String systemId) {

      context.getProject().log("resolving systemId: " + systemId, Project.MSG_VERBOSE);

      if (systemId.startsWith("file:")) {
        String path = FILE_UTILS.fromURI(systemId);

        File file = new File(path);
        if (!file.isAbsolute()) {
          file = FILE_UTILS.resolveFile(context.getBuildFileParent(), path);
          context
              .getProject()
              .log(
                  "Warning: '"
                      + systemId
                      + "' in "
                      + context.getBuildFile()
                      + " should be expressed simply as '"
                      + path.replace('\\', '/')
                      + "' for compliance with other XML tools",
                  Project.MSG_WARN);
        }
        context.getProject().log("file=" + file, Project.MSG_DEBUG);
        try {
          InputSource inputSource = new InputSource(new FileInputStream(file));
          inputSource.setSystemId(FILE_UTILS.toURI(file.getAbsolutePath()));
          return inputSource;
        } catch (FileNotFoundException fne) {
          context
              .getProject()
              .log(file.getAbsolutePath() + " could not be found", Project.MSG_WARN);
        }
      }
      // use default if not file or file not found
      context.getProject().log("could not resolve systemId", Project.MSG_DEBUG);
      return null;
    }
    /**
     * Initialisation routine called after handler creation with the element name and attributes.
     * The attributes which this handler can deal with are: <code>"default"</code>, <code>"name"
     * </code>, <code>"id"</code> and <code>"basedir"</code>.
     *
     * @param uri The namespace URI for this element.
     * @param tag Name of the element which caused this handler to be created. Should not be <code>
     *     null</code>. Ignored in this implementation.
     * @param qname The qualified name for this element.
     * @param attrs Attributes of the element which caused this handler to be created. Must not be
     *     <code>null</code>.
     * @param context The current context.
     * @exception SAXParseException if an unexpected attribute is encountered or if the <code>
     *     "default"</code> attribute is missing.
     */
    public void onStartElement(
        String uri, String tag, String qname, Attributes attrs, AntXMLContext context)
        throws SAXParseException {
      String baseDir = null;
      boolean nameAttributeSet = false;

      Project project = context.getProject();
      // Set the location of the implicit target associated with the project tag
      context.getImplicitTarget().setLocation(new Location(context.getLocator()));

      /**
       * XXX I really don't like this - the XML processor is still too 'involved' in the processing.
       * A better solution (IMO) would be to create UE for Project and Target too, and then process
       * the tree and have Project/Target deal with its attributes ( similar with Description ).
       *
       * <p>If we eventually switch to ( or add support for ) DOM, things will work smoothly - UE
       * can be avoided almost completely ( it could still be created on demand, for backward
       * compatibility )
       */
      for (int i = 0; i < attrs.getLength(); i++) {
        String attrUri = attrs.getURI(i);
        if (attrUri != null && !attrUri.equals("") && !attrUri.equals(uri)) {
          continue; // Ignore attributes from unknown uris
        }
        String key = attrs.getLocalName(i);
        String value = attrs.getValue(i);

        if (key.equals("default")) {
          if (value != null && !value.equals("")) {
            if (!context.isIgnoringProjectTag()) {
              project.setDefault(value);
            }
          }
        } else if (key.equals("name")) {
          if (value != null) {
            context.setCurrentProjectName(value);
            nameAttributeSet = true;
            if (!context.isIgnoringProjectTag()) {
              project.setName(value);
              project.addReference(value, project);
            } else if (isInIncludeMode()) {
              if (!"".equals(value)
                  && (getCurrentTargetPrefix() == null || getCurrentTargetPrefix().length() == 0)) {
                // help nested include tasks
                setCurrentTargetPrefix(value);
              }
            }
          }
        } else if (key.equals("id")) {
          if (value != null) {
            // What's the difference between id and name ?
            if (!context.isIgnoringProjectTag()) {
              project.addReference(value, project);
            }
          }
        } else if (key.equals("basedir")) {
          if (!context.isIgnoringProjectTag()) {
            baseDir = value;
          }
        } else {
          // XXX ignore attributes in a different NS ( maybe store them ? )
          throw new SAXParseException(
              "Unexpected attribute \"" + attrs.getQName(i) + "\"", context.getLocator());
        }
      }

      // XXX Move to Project ( so it is shared by all helpers )
      String antFileProp = MagicNames.ANT_FILE + "." + context.getCurrentProjectName();
      String dup = project.getProperty(antFileProp);
      String typeProp = MagicNames.ANT_FILE_TYPE + "." + context.getCurrentProjectName();
      String dupType = project.getProperty(typeProp);
      if (dup != null && nameAttributeSet) {
        Object dupFile = null;
        Object contextFile = null;
        if (MagicNames.ANT_FILE_TYPE_URL.equals(dupType)) {
          try {
            dupFile = new URL(dup);
          } catch (java.net.MalformedURLException mue) {
            throw new BuildException(
                "failed to parse "
                    + dup
                    + " as URL while looking"
                    + " at a duplicate project"
                    + " name.",
                mue);
          }
          contextFile = context.getBuildFileURL();
        } else {
          dupFile = new File(dup);
          contextFile = context.getBuildFile();
        }

        if (context.isIgnoringProjectTag() && !dupFile.equals(contextFile)) {
          project.log(
              "Duplicated project name in import. Project "
                  + context.getCurrentProjectName()
                  + " defined first in "
                  + dup
                  + " and again in "
                  + contextFile,
              Project.MSG_WARN);
        }
      }
      if (nameAttributeSet) {
        if (context.getBuildFile() != null) {
          project.setUserProperty(antFileProp, context.getBuildFile().toString());
          project.setUserProperty(typeProp, MagicNames.ANT_FILE_TYPE_FILE);
        } else if (context.getBuildFileURL() != null) {
          project.setUserProperty(antFileProp, context.getBuildFileURL().toString());
          project.setUserProperty(typeProp, MagicNames.ANT_FILE_TYPE_URL);
        }
      }
      if (context.isIgnoringProjectTag()) {
        // no further processing
        return;
      }
      // set explicitly before starting ?
      if (project.getProperty("basedir") != null) {
        project.setBasedir(project.getProperty("basedir"));
      } else {
        // Default for baseDir is the location of the build file.
        if (baseDir == null) {
          project.setBasedir(context.getBuildFileParent().getAbsolutePath());
        } else {
          // check whether the user has specified an absolute path
          if ((new File(baseDir)).isAbsolute()) {
            project.setBasedir(baseDir);
          } else {
            project.setBaseDir(FILE_UTILS.resolveFile(context.getBuildFileParent(), baseDir));
          }
        }
      }
      project.addTarget("", context.getImplicitTarget());
      context.setCurrentTarget(context.getImplicitTarget());
    }