ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Upayavira ...@upaya.co.uk>
Subject Re: Questions: A Cocoon Ant task
Date Thu, 09 Oct 2003 09:23:59 GMT
Dominique et al,

Thanks for that. That's really helpful. I've got a version coded, and 
I'm almost there...

I've got it reading my XML and parsing it successfully.

And I've managed to create my own class loader, and have that load my 
Cocoon classes, directly from the cocoon/WEB-INF/lib folder. Great too.

However, when it starts to dig deeper into Cocoon, I find that it is 
using the Ant classloader in preference to mine - i.e. when asked for 
org.apache.log.Hierarchy, it presents the one from within 
velocity-dep-1.3-dev.jar, which is an Ant jar, not the one provided by 
Cocoon.

Any ideas how I can force all classloading between 
loader.setThreadContextLoader() and        
loader.resetThreadContextLoader() to use _my_ classloader, and ignore 
completely Ant's one?

Thanks for your help - I never thought I'd get this far with it - 
*complete* reuse of my configuration code!

Regards, Upayavira

Dominique Devienne wrote:

>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
>
>
>  
>




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


Mime
View raw message