net.sf.fikin.ant
Class XPathTask

java.lang.Object
  extended by org.apache.tools.ant.ProjectComponent
      extended by org.apache.tools.ant.Task
          extended by net.sf.fikin.ant.XPathTask

public class XPathTask
extends org.apache.tools.ant.Task

Execute XPath expressions against input XML/HTML data and return result.

Support XML or HTML inputs

Support writing to Project properties or a file

Support for multiple xpath expressions per single input

Example:

      <xpath>
          <input file="bin/test/ant/test1/in1.xml" />
          <output property="p1" />
          <forEach select="/root/id" >
              <valueOf select="local-name(..)" />
              <valueOf select="'/'" />
              <valueOf select="local-name()" />
          </forEach>
          <forEach select="/root" >
              <valueOf select="local-name()" />
              <valueOf select="' count nested tags : '" />
              <valueOf select="count(.)" />
          </forEach>
      </xpath>
      <echo>${p1}</echo>
 

where for input file:

      <root><id/></root>
 

would set property p1 to: [echo] root/id root count nested tags : 1

Example:

      <xpath>
          <input>
              <![CDATA[ <root><tag id="1"/><tag id="2"/></root> ]]>
          </input>
          <forEach select="/root/tag" >
              <valueOf select="@id" />
          </forEach>
          <sequential>
              <echo>@{node}</echo>
          </sequential>
      </xpath>
 

would print out: [echo] 1 [echo] 2

created on Jan 3, 2007

Since:
Version:
$Revision: 1.7 $
Author:
fiykov
Ant Task Name:
xpath

Nested Class Summary
 class XPathTask.ForEach
          Represents a for-each nested tag created on Jan 3, 2007
 class XPathTask.Function
          Represent a function tag created on Jan 3, 2007
 class XPathTask.ListOfFunctions
          Functions context handler used by XPath to resolve custom namespaces
 class XPathTask.ListOfNS
          Namespace context handler used by XPath to resolve custom namespaces
 class XPathTask.Select
          Represents a value-of nested tag created on Jan 3, 2007
 
Field Summary
protected  FactoryType factory
          factory to use, if defined
protected  boolean failOnError
          abort build when execution error, by default true
protected  java.util.Vector forEachs
          list of for-each nested tags
protected  java.util.Vector functions
          hold custom defined functions
protected  XHTMLInput input
          input document
protected  org.apache.tools.ant.taskdefs.MacroDef macroDef
          internal instance of MacroDef, corresponds to nested sequential tag
protected  org.apache.tools.ant.taskdefs.MacroInstance macroInstance
          macro instance used to execute sequential if defined, internal member
protected  java.util.Vector namespaces
          hold all defined namespaces
protected  DelimitedPrintWriter out
          output writer, internal member
protected  Output output
          output document
protected  int recordCnt
          count how many records were discovered by forEach tags, internal member
protected  javax.xml.xpath.XPath xpath
          xpath object to use for expression evaluations, internal member
protected  javax.xml.xpath.XPathFactory xpathFactory
          xpathFactory to use, internal member
 
Fields inherited from class org.apache.tools.ant.Task
description, location, target, taskName, taskType, wrapper
 
Fields inherited from class org.apache.tools.ant.ProjectComponent
project
 
Constructor Summary
XPathTask()
           
 
Method Summary
 void addNamespace(Namespace ns)
          add new namespace tag
 void check(org.apache.tools.ant.Task task)
          test for input has been set and is consistent test output or sequential has been set and is consistent test namespaces are consistent test functions are consistent test for at least one for-each and it is consistent test factory if defined
protected  void closeOut()
          close the output system : in this case closing out stream if such has been used (and not macroInstance)
 FactoryType createFactory()
          create factory tag
 XPathTask.ForEach createForEach()
          create new for-each tag
 XPathTask.Function createFunction()
          create new function tag
 XHTMLInput createInput()
          create xml/html/xhtml input tag
 Output createOutput()
          create output tag
 java.lang.Object createSequential()
          Create sequential task
 void execute()
          execute the task
protected  void execute(org.w3c.dom.Node node, java.util.List selects, java.lang.StringBuffer appendTo)
          execute xpath for each of Select elements in the vector
protected  void execute(org.w3c.dom.Node node, XPathTask.ForEach fe)
          execute xpath expression of this for-each
protected  void execute(org.w3c.dom.Node node, XPathTask.Select select, java.lang.StringBuffer appendTo)
          execute the xpath expression and append it to the string buffer
protected  void execute(java.lang.String rowOfData)
          either print the data to the output or execute the sequential with it
protected  void initOut()
          Initialize the output system : this being out stream or macroInstance
protected  javax.xml.xpath.XPathFactory newXPathFactory()
          instantiate new xpath factory
 void setFailOnError(boolean failOnError)
          abort build whenever execution error
protected  void setFunctions()
          set functions the the xpath object
protected  void setNamespaces()
          set namespaces the the xpath object
 
Methods inherited from class org.apache.tools.ant.Task
getDescription, getLocation, getOwningTarget, getRuntimeConfigurableWrapper, getTaskName, getTaskType, getWrapper, handleErrorFlush, handleErrorOutput, handleFlush, handleInput, handleOutput, init, isInvalid, log, log, maybeConfigure, perform, reconfigure, setDescription, setLocation, setOwningTarget, setRuntimeConfigurableWrapper, setTaskName, setTaskType
 
Methods inherited from class org.apache.tools.ant.ProjectComponent
getProject, setProject
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

failOnError

protected boolean failOnError
abort build when execution error, by default true


namespaces

protected java.util.Vector namespaces
hold all defined namespaces


functions

protected java.util.Vector functions
hold custom defined functions


macroDef

protected org.apache.tools.ant.taskdefs.MacroDef macroDef
internal instance of MacroDef, corresponds to nested sequential tag


input

protected XHTMLInput input
input document


output

protected Output output
output document


forEachs

protected java.util.Vector forEachs
list of for-each nested tags


factory

protected FactoryType factory
factory to use, if defined


xpathFactory

protected javax.xml.xpath.XPathFactory xpathFactory
xpathFactory to use, internal member


xpath

protected javax.xml.xpath.XPath xpath
xpath object to use for expression evaluations, internal member


out

protected DelimitedPrintWriter out
output writer, internal member


macroInstance

protected org.apache.tools.ant.taskdefs.MacroInstance macroInstance
macro instance used to execute sequential if defined, internal member


recordCnt

protected int recordCnt
count how many records were discovered by forEach tags, internal member

Constructor Detail

XPathTask

public XPathTask()
Method Detail

setFailOnError

public void setFailOnError(boolean failOnError)
abort build whenever execution error

Parameters:
failOnError - aborts build if set to true,

by default true

Ant Task Param Required:
false

addNamespace

public void addNamespace(Namespace ns)
add new namespace tag

Parameters:
ns - is a new namespace tag
Ant Task Param Required:
false

createFunction

public XPathTask.Function createFunction()
create new function tag

Returns:
new function
Ant Task Param Required:
false

createInput

public XHTMLInput createInput()
create xml/html/xhtml input tag

Returns:
XHTMLInput is the input for the DOM tree creation
Ant Task Param Required:
true

createSequential

public java.lang.Object createSequential()
Create sequential task

Its code will be executed for each Node found in by for-each tags

One can refer to the combined value-of string (computed for that Node) via the macro attribute @{node}

Returns:
a MacroDef#NestedSequential object to be configured
Ant Task Param Required:
this or a sequential tag is expected

createOutput

public Output createOutput()
create output tag

Returns:
Output is the output of the XPath expression evaluation, a file or property
Ant Task Param Required:
this or a sequential tag is expected

createFactory

public FactoryType createFactory()
create factory tag

Returns:
XPathFactory to use
Ant Task Param Required:
false

createForEach

public XPathTask.ForEach createForEach()
create new for-each tag

Returns:
foreach is a new for-each tag
Ant Task Param Required:
true

check

public void check(org.apache.tools.ant.Task task)
           throws org.apache.tools.ant.BuildException
test for input has been set and is consistent test output or sequential has been set and is consistent test namespaces are consistent test functions are consistent test for at least one for-each and it is consistent test factory if defined

Throws:
org.apache.tools.ant.BuildException

execute

public void execute()
             throws org.apache.tools.ant.BuildException
execute the task

Overrides:
execute in class org.apache.tools.ant.Task
Throws:
org.apache.tools.ant.BuildException

execute

protected void execute(org.w3c.dom.Node node,
                       XPathTask.ForEach fe)
                throws org.apache.tools.ant.BuildException
execute xpath expression of this for-each

Parameters:
node - to use as root
fe - is for-each xpath expression
Throws:
org.apache.tools.ant.BuildException

execute

protected void execute(org.w3c.dom.Node node,
                       java.util.List selects,
                       java.lang.StringBuffer appendTo)
                throws javax.xml.xpath.XPathExpressionException
execute xpath for each of Select elements in the vector

Parameters:
node - to use as base
selects - is a list of
appendTo - is a string buffer to append the evaluation result to
Throws:
javax.xml.xpath.XPathExpressionException

execute

protected void execute(org.w3c.dom.Node node,
                       XPathTask.Select select,
                       java.lang.StringBuffer appendTo)
                throws javax.xml.xpath.XPathExpressionException
execute the xpath expression and append it to the string buffer

Parameters:
node - to use as base
select - is xpath to evaluate
appendTo - is a string buffer to append the evaluation result to
Throws:
javax.xml.xpath.XPathExpressionException

execute

protected void execute(java.lang.String rowOfData)
either print the data to the output or execute the sequential with it

Parameters:
rowOfData -

initOut

protected void initOut()
Initialize the output system : this being out stream or macroInstance

To be called once before calling execute(String) for first time


closeOut

protected void closeOut()
close the output system : in this case closing out stream if such has been used (and not macroInstance)

To be called at the end of the task execution i.e. execute()


newXPathFactory

protected javax.xml.xpath.XPathFactory newXPathFactory()
                                                throws org.apache.tools.ant.BuildException
instantiate new xpath factory

Returns:
XPathFactory.newInstance()
Throws:
org.apache.tools.ant.BuildException

setNamespaces

protected void setNamespaces()
set namespaces the the xpath object


setFunctions

protected void setFunctions()
set functions the the xpath object



Copyright © 2003-2007 Nikolay Fiykov. All Rights Reserved.