You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cocoon.apache.org by Craig Christophersen <cr...@synesis7.com> on 2004/05/19 01:08:55 UTC

InputStream as source or generator

Can anyone point me in a direction of how to use an InputStream as either  a Source or Generator in the pipeline? The StreamGenerator is just for request input from a post. I would like to use an inputstream from a DTO, bean, or ejb.
I have created an action to instantiate a bean and can use the parameters retrieved as strings. I would like to get the inputstream(of xml data) into the pipeline.

Craig Christophersen
(406)496-6421
craigch@synesis7.com

Re: InputStream as source or generator

Posted by Ralph Goers <Ra...@dslextreme.com>.
I used Betwixt's SAXBeanWriter to convert my bean to a generator. I
believe this is more efficient since no "real" xml is ever created.


public class BetwixtGenerator extends AbstractGenerator
{
    protected ServiceManager manager;
    private Object           bean;

    /**
     * Sets up the BetwixtGenerator.
     *
     * @param resolver the source resolver
     * @param objectModel the Cocoon object model
     * @param src ignored by the BetwixtGenerator
     * @param par ignored by the BetwixtGenerator
     * @throws ProcessingException
     * @throws SAXException
     * @throws IOException
     */
    public final void setup(SourceResolver resolver, Map objectModel,
String src,
                      Parameters par)
        throws ProcessingException, SAXException, IOException
    {
        super.setup(resolver, objectModel, src, par);

        Request request =
(Request)objectModel.get(ObjectModelHelper.REQUEST_OBJECT);
        this.bean =
request.getAttribute(BusinessDelegate.DATA_TRANSFER_OBJECT);
        if (this.bean == null)
        {
            throw new ProcessingException("No object was provided to
serialize");
        }
    }

    /**
     * Generate XML data.
     * @throws SAXException
     */
    public final void generate()
        throws SAXException
    {
        try
        {
            SAXBeanWriter beanWriter = new
SAXBeanWriter(this.contentHandler);
            beanWriter.setWriteEmptyElements(false);
            beanWriter.setWriteIDs(false);
            beanWriter.getXMLIntrospector().setElementNameMapper(new
CapitalizeNameMapper());
            beanWriter.getXMLIntrospector().setAttributesForPrimitives(true);
            beanWriter.write(this.bean);
        }
        catch (Exception e)
        {
            getLogger().error("Cannot generate XML for " + bean, e);
            SAXException saxException = new SAXException(e.getMessage());
            throw saxException;
        }
    }


Craig Christophersen said:
> Can anyone point me in a direction of how to use an InputStream as either
> a Source or Generator in the pipeline? The StreamGenerator is just for
> request input from a post. I would like to use an inputstream from a DTO,
> bean, or ejb.
> I have created an action to instantiate a bean and can use the parameters
> retrieved as strings. I would like to get the inputstream(of xml data)
> into the pipeline.
>
> Craig Christophersen
> (406)496-6421
> craigch@synesis7.com


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org