ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Dhaval Chokshi" <dhavalchok...@elitecore.com>
Subject New ANT Task : ChangeXML
Date Thu, 16 Oct 2003 12:23:30 GMT
hi all,

I have made one ANT Task that can be used to change XML files using ANT. This can be used
as :

<!-- ============================================== -->
 <changexml filename="readme.xml"
                    xpath="/upgrade"
                    mode="attribute"
                    node="version"
                    value="5.0.0.1"/>

will set version attribute in the /upgrade node to 5.0.0.1
output :

<upgrade version="5.0.0.1">
    ...
</upgrade>

<!-- ============================================== -->

 <changexml filename="readme.xml"
      xpath="/upgrade/contents"
      mode="element"
      node="added-files"
      value="/war/operations-manager/mllnlogin.jsp"/>

will add one new child element to /upgrade/contents named <added-files> and set its
txt to /war/operations-manager/mllnlogin.jsp.
output
<upgrade>
...
    <contents>
            ...
            <added-files>
                    /war/operations-manager/mllnlogin.jsp
            </added-files>
    </contents>
...
</upgrade>
<!-- ============================================== -->


/**
 * Author : Dhaval Chokshi
 * Elitecore Technologies Ltd.
 * Created on Oct 13, 2003
 * dhavalchokshi@elitecore.com
 */

package com.elitecore.ant.taskdefs;

import org.apache.tools.ant.Task;
import org.apache.tools.ant.taskdefs.*;
import org.apache.tools.ant.types.*;
import com.elitecore.ant.types.SafeFileSet;
import org.apache.tools.ant.BuildException;
import java.util.*;
import java.io.*;
import java.util.regex.*;
import org.jdom.Element;
import org.jdom.Document;
import org.jdom.output.XMLOutputter;
import org.jdom.input.SAXBuilder;
import java.io.FileInputStream;
import org.jdom.xpath.XPath;
import org.jdom.JDOMException;

public class ChangeXML extends Task {
 private String filename;
 private String xpath;
 private String mode;
 private String node;
 private String value;
 private String srcFilename;
 
    public void setFilename(String filename) {
  this.filename = filename;
 }
 
 public void setXpath(String xpath) {
  this.xpath = xpath;
 }
 
 public void setMode(String mode) {
  this.mode = mode;
 }
 
 public void setNode(String node) {
  this.node = node;
 }
 
 public void setValue(String value) {
  this.value = value;
 }
 
 public void setSrcfile(String filename) {
  this.srcFilename = filename;
 }
 
 public void execute() throws BuildException {
  
  if(filename == null) {
   throw new BuildException("Filename attribute is mandatory.");
  }
  
  if(xpath == null) {
   throw new BuildException("xpath attribute is mandatory.");
  }
  
  if(mode == null) {
   throw new BuildException("mode attribute is mandatory.");
  }
  
  if(node == null) {
   throw new BuildException("node attribute is mandatory.");
  }
  
  SAXBuilder builder = new SAXBuilder();
  try {
   Document doc = builder.build(new FileInputStream(filename));
   XPath jdomxpath = XPath.newInstance(xpath);
   Element element = (Element) jdomxpath.selectSingleNode(doc);
   
   if(mode.equals("attribute")) {
    element.setAttribute(node,value);
   }
  
   if(mode.equals("element")) {
    if(srcFilename == null) {
     Element child = new Element(node);
     child.setText(value);
     element.addContent("\n ");
     element.addContent(child);
    } else {
     FileInputStream srcFileIS = new FileInputStream(srcFilename);
     BufferedReader br = new BufferedReader(new InputStreamReader(srcFileIS));
     String str;
     while((str = br.readLine()) != null) {
      Element child = new Element(node);
      child.setText(value);
      element.addContent("\n ");
      element.addContent(child);
     }
     br.close();
     srcFileIS.close();
    }
   }
   
   XMLOutputter outputter = new XMLOutputter();
   FileOutputStream fout = new FileOutputStream(filename);
   outputter.output(doc, fout);
   
   fout.close();
  } catch(FileNotFoundException e) {
   log("File " + filename + " Not found.");
   throw new BuildException(e.getMessage());
  } catch(JDOMException e) {
   log("JDOM Exception : ");
   throw new BuildException(e.getMessage());
  } catch(IOException e) {
   log("IO Exception ");
   throw new BuildException(e.getMessage());
  }
 }
}


Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message