/**
   * load Ant properties from the source file or resource
   *
   * @exception BuildException if something goes wrong with the build
   */
  public final void execute() throws BuildException {
    // validation
    if (src == null) {
      throw new BuildException("A source resource is required.");
    }
    if (!src.isExists()) {
      if (src instanceof JavaResource) {
        // dreaded backwards compatibility
        log("Unable to find resource " + src, Project.MSG_WARN);
        return;
      }
      throw new BuildException("Source resource does not exist: " + src);
    }
    BufferedInputStream bis = null;
    Reader instream = null;
    ByteArrayInputStream tis = null;

    try {
      bis = new BufferedInputStream(src.getInputStream());
      if (encoding == null) {
        instream = new InputStreamReader(bis);
      } else {
        instream = new InputStreamReader(bis, encoding);
      }
      ChainReaderHelper crh = new ChainReaderHelper();
      crh.setPrimaryReader(instream);
      crh.setFilterChains(filterChains);
      crh.setProject(getProject());
      instream = crh.getAssembledReader();

      String text = crh.readFully(instream);

      if (text != null && text.length() != 0) {
        if (!text.endsWith("\n")) {
          text = text + "\n";
        }
        tis = new ByteArrayInputStream(text.getBytes("ISO8859_1"));
        final Properties props = new Properties();
        props.load(tis);

        Property propertyTask = new Property();
        propertyTask.bindToOwner(this);
        propertyTask.addProperties(props);
      }
    } catch (final IOException ioe) {
      throw new BuildException("Unable to load file: " + ioe, ioe, getLocation());
    } finally {
      FileUtils.close(bis);
      FileUtils.close(tis);
    }
  }
Exemplo n.º 2
0
 /**
  * Return the result of the filters on the sourcefilename.
  *
  * @param sourceFileName the filename to map
  * @return a one-element array of converted filenames, or null if the filterchain returns an empty
  *     string.
  */
 public String[] mapFileName(String sourceFileName) {
   try {
     Reader stringReader = new StringReader(sourceFileName);
     ChainReaderHelper helper = new ChainReaderHelper();
     helper.setBufferSize(8192);
     helper.setPrimaryReader(stringReader);
     helper.setProject(getProject());
     Vector filterChains = new Vector();
     filterChains.add(this);
     helper.setFilterChains(filterChains);
     String result = FileUtils.readFully(helper.getAssembledReader());
     if (result.length() == 0) {
       return null;
     } else {
       return new String[] {result};
     }
   } catch (BuildException ex) {
     throw ex;
   } catch (Exception ex) {
     throw new BuildException(ex);
   }
 }
Exemplo n.º 3
0
  /**
   * Convenience method to copy content from one Resource to another specifying whether token
   * filtering must be used, whether filter chains must be used, whether newer destination files may
   * be overwritten and whether the last modified time of <code>dest</code> file should be made
   * equal to the last modified time of <code>source</code>.
   *
   * @param source the Resource to copy from. Must not be <code>null</code>.
   * @param dest the Resource to copy to. Must not be <code>null</code>.
   * @param filters the collection of filters to apply to this copy.
   * @param filterChains filterChains to apply during the copy.
   * @param overwrite Whether or not the destination Resource should be overwritten if it already
   *     exists.
   * @param preserveLastModified Whether or not the last modified time of the destination Resource
   *     should be set to that of the source.
   * @param inputEncoding the encoding used to read the files.
   * @param outputEncoding the encoding used to write the files.
   * @param project the project instance.
   * @throws IOException if the copying fails.
   * @since Ant 1.7
   */
  public static void copyResource(
      Resource source,
      Resource dest,
      FilterSetCollection filters,
      Vector filterChains,
      boolean overwrite,
      boolean preserveLastModified,
      String inputEncoding,
      String outputEncoding,
      Project project)
      throws IOException {
    if (!overwrite) {
      long slm = source.getLastModified();
      if (dest.isExists() && slm != 0 && dest.getLastModified() > slm) {
        return;
      }
    }
    final boolean filterSetsAvailable = (filters != null && filters.hasFilters());
    final boolean filterChainsAvailable = (filterChains != null && filterChains.size() > 0);
    if (filterSetsAvailable) {
      BufferedReader in = null;
      BufferedWriter out = null;
      try {
        InputStreamReader isr = null;
        if (inputEncoding == null) {
          isr = new InputStreamReader(source.getInputStream());
        } else {
          isr = new InputStreamReader(source.getInputStream(), inputEncoding);
        }
        in = new BufferedReader(isr);
        OutputStreamWriter osw = null;
        if (outputEncoding == null) {
          osw = new OutputStreamWriter(dest.getOutputStream());
        } else {
          osw = new OutputStreamWriter(dest.getOutputStream(), outputEncoding);
        }
        out = new BufferedWriter(osw);
        if (filterChainsAvailable) {
          ChainReaderHelper crh = new ChainReaderHelper();
          crh.setBufferSize(FileUtils.BUF_SIZE);
          crh.setPrimaryReader(in);
          crh.setFilterChains(filterChains);
          crh.setProject(project);
          Reader rdr = crh.getAssembledReader();
          in = new BufferedReader(rdr);
        }
        LineTokenizer lineTokenizer = new LineTokenizer();
        lineTokenizer.setIncludeDelims(true);
        String newline = null;
        String line = lineTokenizer.getToken(in);
        while (line != null) {
          if (line.length() == 0) {
            // this should not happen, because the lines are
            // returned with the end of line delimiter
            out.newLine();
          } else {
            newline = filters.replaceTokens(line);
            out.write(newline);
          }
          line = lineTokenizer.getToken(in);
        }
      } finally {
        FileUtils.close(out);
        FileUtils.close(in);
      }
    } else if (filterChainsAvailable
        || (inputEncoding != null && !inputEncoding.equals(outputEncoding))
        || (inputEncoding == null && outputEncoding != null)) {
      BufferedReader in = null;
      BufferedWriter out = null;
      try {
        InputStreamReader isr = null;
        if (inputEncoding == null) {
          isr = new InputStreamReader(source.getInputStream());
        } else {
          isr = new InputStreamReader(source.getInputStream(), inputEncoding);
        }
        in = new BufferedReader(isr);
        OutputStreamWriter osw = null;
        if (outputEncoding == null) {
          osw = new OutputStreamWriter(dest.getOutputStream());
        } else {
          osw = new OutputStreamWriter(dest.getOutputStream(), outputEncoding);
        }
        out = new BufferedWriter(osw);
        if (filterChainsAvailable) {
          ChainReaderHelper crh = new ChainReaderHelper();
          crh.setBufferSize(FileUtils.BUF_SIZE);
          crh.setPrimaryReader(in);
          crh.setFilterChains(filterChains);
          crh.setProject(project);
          Reader rdr = crh.getAssembledReader();
          in = new BufferedReader(rdr);
        }
        char[] buffer = new char[FileUtils.BUF_SIZE];
        while (true) {
          int nRead = in.read(buffer, 0, buffer.length);
          if (nRead == -1) {
            break;
          }
          out.write(buffer, 0, nRead);
        }
      } finally {
        FileUtils.close(out);
        FileUtils.close(in);
      }
    } else {
      InputStream in = null;
      OutputStream out = null;
      try {
        in = source.getInputStream();
        out = dest.getOutputStream();

        byte[] buffer = new byte[FileUtils.BUF_SIZE];
        int count = 0;
        do {
          out.write(buffer, 0, count);
          count = in.read(buffer, 0, buffer.length);
        } while (count != -1);
      } finally {
        FileUtils.close(out);
        FileUtils.close(in);
      }
    }
    if (preserveLastModified && dest instanceof Touchable) {
      setLastModified((Touchable) dest, source.getLastModified());
    }
  }