ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Dominique Devienne <DDevie...@lgc.com>
Subject RE: Questions: A Cocoon Ant task
Date Wed, 08 Oct 2003 19:04:21 GMT
You can use DynamicConfigurator to intercept attribute/elements, and build
the DOM tree yourself, which you can then feed to Cocoon's CLI I guess.

The code below should get you a leg up. Provided with no warranties
whatsoever ;-) I wrote this a long time ago, on a weekend, with little Ant
experience. --DD

package com.lgc.buildmagic;

import java.io.IOException;
import java.io.ByteArrayOutputStream;

import org.w3c.dom.Node;
import org.w3c.dom.Element;
import org.w3c.dom.Document;
import org.w3c.dom.DocumentFragment;
import org.w3c.dom.NodeList;
import org.w3c.dom.DOMException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.tools.ant.Project;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DynamicConfigurator;

import org.apache.tools.ant.types.DataType;
//import org.apache.tools.ant.util.DOMElementWriter;

/**
 * Base class for those classes that can appear inside the build file
 * as stand alone data types.  
 *
 * <p><em>My very first data type ;-)</em></p>
 *
 * @author <a href="mailto:ddevienne@lgc.com">Dominique Devienne</a> 
 */
public class XmlFragment
             extends DataType
             implements DynamicConfigurator {

    /**
     * A dyna'tor for each element.
     */
    private static class ElementWrapper
                         implements DynamicConfigurator {

        /** The XML DOM node */
        private Node _node;

        /** Instantiate a root wrapper */
        private ElementWrapper(Node node) {
            _node = node; // Could be a Document or DocumentFragment
        }

        /** Instantiate a child wrapper */
        private ElementWrapper(Node parent, String childName) {
            Document document = parent.getOwnerDocument();
            if (document == null) {
              document = (Document)parent; // Node is the document!
            }
            _node = document.createElement(childName);
            parent.appendChild(_node);
        }

        public String toString() {
            ByteArrayOutputStream ostream = new ByteArrayOutputStream();
            // This will fail, since the DocFragment is not an Element!
            // DOMElementWriter needs to be fixed to take a node, not an
Element.
            try {
                new DOMElementWriter().write(_node, ostream);
                return ostream.toString();
            }
            catch (IOException e) {
                throw new BuildException(e);
            }
        }


        //
        // interface DynamicConfigurator
        //

        public void setDynamicAttribute(String name, String value)
                    throws BuildException {
            // Never called for anything by Element wrappers
            Element element = (Element)_node;
            element.setAttribute(name, value);
        }

        public Object createDynamicElement(String name)
                      throws BuildException {
            return new ElementWrapper(_node, name);
        }

    } // END class XmlFragment.ElementWrapper


    private DocumentFragment _fragment;
    private ElementWrapper _wrapper;

    /**
     * Instanciate a new DOM document fragment wrapped in an Ant data type.
     */
    public XmlFragment() {
        try {
            DocumentBuilder builder =
DocumentBuilderFactory.newInstance().newDocumentBuilder();
            _fragment = builder.newDocument().createDocumentFragment();
            _wrapper = new ElementWrapper(_fragment);
        }
        catch (ParserConfigurationException e) {
            throw new BuildException(e);
        }
    }

    /**
     * Sets the name of the top-level DOM node, the xml fragment, to use.
     *
     * @param  name the xml fragment node name. Defaults to "xmlfragment".
    public void setName(String name) {
        _wrapper._tagname = name;
    }
     */

    public String toString() {
        return _wrapper.toString();
    }


    //
    // interface DynamicConfigurator
    //

    public void setDynamicAttribute(String name, String value)
                throws BuildException {
        // The root only supports explicit 'normal' attributes!
        throw new BuildException("attribute "+name+" not supported!");
    }

    public Object createDynamicElement(String name)
                  throws BuildException {
        return _wrapper.createDynamicElement(name);
    }

} // END class XmlFragment

> -----Original Message-----
> From: Upayavira [mailto:uv@upaya.co.uk]
> Sent: Wednesday, October 08, 2003 1:49 PM
> To: dev@ant.apache.org
> Subject: Questions: A Cocoon Ant task
> 
> I am attempting to craft an Ant task for Apache Cocoon's command line
> interface.
> 
> Cocoon's CLI can be configured with an XML xconf file. I want to move
> this configuration information into the Ant build script itself.
> 
> Is there any way I can share the code to interpret this XML
> configuration information between Cocoon and Ant? In Cocoon's CLI I use
> a DOM to parse the XML, but in Ant I seem to have to create objects for
> nested elements, and have Ant handle it using introspection.
> 
> Is there any other way, or do I just have to accept that I've got to
> maintain two sets of code - one for use from the command line, and one
> from Ant?
> 
> I hope I'm clear enough.
> 
> Thanks for any help.
> 
> Regards,
> 
> Upayavira

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org


Mime
View raw message