ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jan.Mate...@rzf.fin-nrw.de
Subject RE: New ANT Task : ChangeXML
Date Fri, 17 Oct 2003 07:07:17 GMT
Before writing new tasks - a look on Ant´s external page would be a good
action. You should do that directly after looking into the manual :-)

I wrote a task some days ago and got a naming collision with another one 
(but that task wasn´t listed in the doc...) so I had to rename my new task.

A quick search on sourceforge could help also.


big exception: you really WANT to write a task :-) (maybe for learning...)


Jan




> -----Original Message-----
> From: Dhaval Chokshi [mailto:dhavalchokshi@elitecore.com]
> Sent: Thursday, October 16, 2003 4:57 PM
> To: Ant Users List
> Subject: Re: New ANT Task : ChangeXML
> 
> 
> No I didn't know about the xmltask.
> 
> Thank you for that
> 
> Dhaval
> 
> > You know the xml tasks?
> > http://www.oopsconsultancy.com/software/xmltask.html
> > 
> > 
> > Jan
> > 
> > > -----Original Message-----
> > > From: Dhaval Chokshi [mailto:dhavalchokshi@elitecore.com]
> > > Sent: Thursday, October 16, 2003 2:24 PM
> > > To: Ant Users List
> > > Subject: New ANT Task : ChangeXML
> > > 
> > > 
> > > 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());
> > >   }
> > >  }
> > > }
> > > 
> > > 
> > 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> For additional commands, e-mail: user-help@ant.apache.org
> 

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