Menu

Output to Ant property

2016-12-19
2016-12-19
  • Gintas Grigelionis

    I am missing a posibility to output the result of HtmlCleaner to an Ant property which would make htmlcleaner into an HTML parser for Ant. Here's an attempt to use the dist attribute using a property pseudo-URL (rather than inventing a new Ant-specific attribute and adding getter and setter for it):

    ===================================================================
    --- HtmlCleanerForAnt.java      (revision 474)
    +++ HtmlCleanerForAnt.java      (working copy)
    @@ -38,18 +38,21 @@
     package org.htmlcleaner;
    
     import org.apache.tools.ant.BuildException;
    +import org.apache.tools.ant.Task;
    
    -import java.net.URL;
    +import java.io.ByteArrayOutputStream;
     import java.io.File;
    +import java.io.FileOutputStream;
     import java.io.IOException;
     import java.io.OutputStream;
    -import java.io.FileOutputStream;
    -import java.util.*;
    +import java.net.URL;
    +import java.util.Map;
    +import java.util.TreeMap;
    
     /**
      * <p>Support for ANT.</p>
      */
    -public class HtmlCleanerForAnt extends org.apache.tools.ant.Task {
    +public class HtmlCleanerForAnt extends Task {
    
         private String text;
         private String src;
    @@ -293,8 +296,13 @@
                 }
    
                 OutputStream out;
    +            String antPropertyName = "";
                 if ( dest == null || "".equals(dest.trim()) ) {
                     out = System.out;
    +            } else if ( dest.startsWith("property:") ) {
    +                out = new ByteArrayOutputStream();
    +                antPropertyName = dest.substring(dest.indexOf(':') + 1);
    +                getProject().log("Setting property " + antPropertyName);
                 } else {
                     out = new FileOutputStream(dest);
                 }
    @@ -308,6 +316,10 @@
                 } else {
                     new SimpleXmlSerializer(props).writeToStream(node, out, outcharset);
                 }
    +
    +            if ( antPropertyName != null && antPropertyName.length() > 0 ) {
    +                getProject().setNewProperty(antPropertyName, out.toString());
    +            }
             } catch (IOException e) {
                  throw new BuildException(e);
             } catch (XPatherException e) {
    
     
  • Scott Wilson

    Scott Wilson - 2017-05-02

    Added in v 2.20!

     

Log in to post a comment.

MongoDB Logo MongoDB