You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@cocoon.apache.org by Miguel Carvalho <ma...@rnl.ist.utl.pt> on 2003/08/05 13:34:56 UTC

RE: problem applying a generator and transformer

Hi, thanks for your contributions, the <util:include-expr/> tip worked
great.. :)

And now i'm having a little doubt,

i want to create a Transformer that takes the XML from my generator and
changes some attributes of the tags existing in it, and

the package org.xml.sax.Attributes only has methods like getValue,
getQname... and i was wondering how could i get the sax events from one
Generator and then change or introduce attributes to the nodes...

it could be like...

public void startElement(String namespaceURI, String localName, String
qName,
            Attributes attributes) throws SAXException
    {
		Attributes newAttribute;

		//fill in the attribute, accessing a MySql Data Base

            super.startElement(NAMESPACE, , , newAttribute);
    }


If anyone has any ideas it would be great  :)

thanks in advance
Miguel Carvalho

-----Original Message-----
From: adrian.geissel@ecb.int [mailto:adrian.geissel@ecb.int]
Sent: quinta-feira, 31 de Julho de 2003 13:25
To: dev@cocoon.apache.org
Subject: RE: problem applying a generator and transformer


Hi Joerg,

<xsp:expr/> will take its content and add it to the output as a string -
that's your problem.

Try one of the xsp-util functions to include you data as XML (I think it's
<util:include-expr/>, but please check)

/Adrian

> -----Original Message-----
> From: Joerg Heinicke [mailto:joerg.heinicke@gmx.de]
> Sent: Thursday 31 July 2003 14:21
> To: dev@cocoon.apache.org
> Subject: Re: problem applying a generator and transformer
>
>
> Miguel Carvalho wrote:
>
> > Hi, im havinf some trouble creating a Generator in XSP and
> after that
> > aplying a transformer in XSL, the problem is, that i am not
> able to parse
> > the XML returned by the generator with the transformer.
> >
> >
> --------------------------------------------------------------
> --------------
> > ----------
> > Generator code
> >
> > <xsp:page language="java" xmlns:xsp="http://apache.org/xsp">
> >
> >     <xsp:structure>
> >
> <xsp:include>pt.laseeb.dae.xmlDbApi.daeXmlDbApi</xsp:include>
> >
> <xsp:include>org.xmldb.api.base.ResourceIterator</xsp:include>
> >         <xsp:include>org.xmldb.api.base.Resource</xsp:include>
> >         <xsp:include>org.xmldb.api.base.XMLDBException</xsp:include>
> >     </xsp:structure>
> >
> >  <document>
> >         <xsp:logic>
> >             try
> >             {
> >                 Resource res = null;
> >                 String resStr = null;
> >                 daeXmlDbApi daeApi = new daeXmlDbApi();
> >                 ResourceIterator results;
> >                 results =
> daeApi.getArticleSection("1").getIterator();
> >                 while (results.hasMoreResources())
> >                 {
> >                     res = results.nextResource();
> >                     <contents><xsp:expr>
> > (String)res.getContent()</xsp:expr></contents>
> >                 }
> >             }
> >             catch(Exception e)
> >             {
> >             }
> >         </xsp:logic>
> >
> >  </document>
> > </xsp:page>
>
> Here you have <document/> as root element.
>
> >
> --------------------------------------------------------------
> --------------
> > ----------
> >
> > daeAPI is a API created by me to access to an XINDICE
> database that contains
> > XML following the current syntax:
> >
> > <article>
> >   <title></title>
> >   <text></text>
> >   <image></image>
> > </article>
> >
> >
> --------------------------------------------------------------
> --------------
> > ----------
> > Transformer code
> >
> > <?xml version="1.0" encoding="iso-8859-1"?>
> > <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
> > version="1.0">
> >
> > <xsl:output indent="no" method="html" omit-xml-declaration
> = "yes" />
> >
> >
> > <xsl:template match="/">
> >     <xsl:apply-templates />
> > </xsl:template>
> >
> > <xsl:template match="/contents">
> >
> >     <xsl:for-each select = "/article" >
> >
> >                     <titulo><xsl:value-of
> disable-output-escaping = "yes"
> > select = "/title" /></titulo>
> >                     <imagem><xsl:value-of
> disable-output-escaping = "yes"
> > select = "/image" /></imagem>
> >
> >    </xsl:for-each>
> >
> > </xsl:template>
> >
> > </xsl:stylesheet>
>
> Here you match on <contents/> as root element. Try to remove the line
> <xsl:output/> or at least make it correct: you don't want
> html as result
> of this transformation. Furthermore does the XSP code return XML or a
> string of the database content, that should be XML. Fix it
> there. Don't
> use disable-output-escaping.
>
> Joerg
>
> >
> --------------------------------------------------------------
> --------------
> > ------
> >
> > and the problem is that what i get in the view source of
> the browser is
> > something like,
> >
> > &lt;?xml version="1.0"?&gt;
> > &lt;article id="1" rating="2" sectionid="1"
> > xmlns:src="http://xml.apache.org/xindice/Query"
> src:col="/db/daeDocuments"
> > src:key="1"&gt;
> > 	&lt;title&gt;Titulo com rating 2&lt;/title&gt;
> >     &lt;text&gt;Texto&lt;/text&gt;
> > &lt;/article&gt;&lt;?xml version="1.0"?&gt;
> > &lt;article id="2" rating="1" sectionid="1"
> > xmlns:src="http://xml.apache.org/xindice/Query"
> src:col="/db/daeDocuments"
> > src:key="2"&gt;
> > 				&lt;title&gt;Titulo do artigo
> com rating igual a 1&lt;/title&gt;
> > 				&lt;text&gt;texto do artigo com
> rating igual a 1&lt;/text&gt;
> > 				&lt;image&gt;img1.jpg&lt;/image&gt;
> > &lt;/article&gt;&lt;?xml version="1.0"?&gt;
> > &lt;article id="3" rating="2" sectionid="1"
> > xmlns:src="http://xml.apache.org/xindice/Query"
> src:col="/db/daeDocuments"
> > src:key="3"&gt;
> > 				&lt;title&gt;Titulo do artigo
> com rating igual a 2&lt;/title&gt;
> > 				&lt;text&gt;texto do artigo com
> rating igual a 2&lt;/text&gt;
> > 				&lt;image&gt;img1.jpg&lt;/image&gt;
> > 			&lt;/article&gt;&lt;?xml version="1.0"?&gt;
> > &lt;article id="4" rating="2" sectionid="1"
> > xmlns:src="http://xml.apache.org/xindice/Query"
> src:col="/db/daeDocuments"
> > src:key="4"&gt;
> > 				&lt;title&gt;Titulo do artigo
> com rating igual a 2&lt;/title&gt;
> > 				&lt;text&gt;texto do artigo com
> rating igual a 2&lt;/text&gt;
> > 				&lt;image&gt;img1.jpg&lt;/image&gt;
> > 			&lt;/article&gt;
> >
> > and i con't parse it in my transformer.
> >
> > If anyone could give a look at the code and see what i am
> doing wrong i
> > would apreciate it :)
> >
> > Thanks id advance
> > Miguel Carvalho
>

Any e-mail message from the European Central Bank (ECB) is sent in good
faith but shall neither be binding nor construed as constituting a
commitment by the ECB except where provided for in a written agreement.
This e-mail is intended only for the use of the recipient(s) named above.
Any unauthorised disclosure, use or dissemination, either in whole or in
part, is prohibited.
If you have received this e-mail in error, please notify the sender
immediately via e-mail and delete this e-mail from your system.




Re: problem creating a new transformer

Posted by Bruno Dumon <br...@outerthought.org>.
Miguel,

this type of question is better suited for the user mailing list.

Anyhow, in order to help you, it would be useful if you mentioned the
Cocoon version you are using.

On Tue, 2003-08-05 at 21:55, Miguel Carvalho wrote:
> Hi, i have sent a previous mail on this subject, but now i'm trying an
> approach that i'm not being able to get it right... :(
> 
> And it follows like this,
> 
> i want to create a new transformer that gets sax events from a XSP Generator
> created by me (with your precious help ).
> 
> bellow i will post the XSP code
<snipped rest of message/>

-- 
Bruno Dumon                             http://outerthought.org/
Outerthought - Open Source, Java & XML Competence Support Center
bruno@outerthought.org                          bruno@apache.org


problem creating a new transformer

Posted by Miguel Carvalho <mi...@mail.telepac.pt>.
Hi, i have sent a previous mail on this subject, but now i'm trying an
approach that i'm not being able to get it right... :(

And it follows like this,

i want to create a new transformer that gets sax events from a XSP Generator
created by me (with your precious help ).

bellow i will post the XSP code

<!--Begin of
start.xsp------------------------------------------------------------>

<xsp:page language="java" xmlns:xsp="http://apache.org/xsp"
                          xmlns:util="http://apache.org/xsp/util/2.0">

    <xsp:structure>
        <xsp:include>pt.laseeb.dae.xmlDbApi.daeXmlDbApi</xsp:include>
        <xsp:include>org.xmldb.api.base.ResourceIterator</xsp:include>
        <xsp:include>org.xmldb.api.base.Resource</xsp:include>
        <xsp:include>org.xmldb.api.base.XMLDBException</xsp:include>

<xsp:include>org.apache.cocoon.components.language.markup.xsp.XSPUtil.*</xsp
:include>
    </xsp:structure>

<document xmlns:dae="http://laseeb/dae/sgDae">
        <xsp:logic>
            try
            {
                Resource res = null;
                String resStr = null;
                daeXmlDbApi daeApi = new daeXmlDbApi();
                ResourceIterator results;
                results = daeApi.getArticleSection("1").getIterator();

                while (results.hasMoreResources())
                {
                    res = results.nextResource();
                    resStr = (String) res.getContent();
<util:include-expr><util:expr><xsp:expr>resStr</xsp:expr></util:expr></util:
include-expr>
                }
            }
            catch(Exception e)
            {
            }
        </xsp:logic>

</document>
</xsp:page>

<!--End of
start.xsp------------------------------------------------------------>

when i create a pipeline with only this genrator and a html serializer like,

<map:pipeline>
    <map:match pattern="daestart">
        <map:generate type="serverpages" src="daexsps/start.xsp"/>
        <map:serialize type="html"/>
     </map:match>
  </map:pipeline>

i get the following result,

<!--------------------------------------------------------------------------
---->

<document xmlns:xml="http://www.w3.org/XML/1998/namespace"
xmlns:util="http://apache.org/xsp/util/2.0"
xmlns:xsp="http://apache.org/xsp" xmlns:dae="http://laseeb/dae/sgDae">
        <dae:article id="1" rating="2" sectionid="1"
xmlns:dae="http://laseeb/dae/sgDae"
xmlns:src="http://xml.apache.org/xindice/Query" src:col="/db/daeDocuments"
src:key="1">
<dae:title>Titulo com rating 2</dae:title>
<dae:text>Texto</dae:text>
</dae:article>
<dae:article id="2" rating="1" sectionid="1"
xmlns:dae="http://laseeb/dae/sgDae"
xmlns:src="http://xml.apache.org/xindice/Query" src:col="/db/daeDocuments"
src:key="2">
<dae:title>Titulo do artigo com rating igual a 1</dae:title>
<dae:text>texto do artigo com rating igual a 1</dae:text>
<dae:image>img1.jpg</dae:image>
</dae:article>
<dae:article id="3" rating="2" sectionid="1"
xmlns:dae="http://laseeb/dae/sgDae"
xmlns:src="http://xml.apache.org/xindice/Query" src:col="/db/daeDocuments"
src:key="3">
<dae:title>Titulo do artigo com rating igual a 2</dae:title>
<dae:text>texto do artigo com rating igual a 2</dae:text>
<dae:image>img1.jpg</dae:image>
</dae:article>
<dae:article id="4" rating="2" sectionid="1"
xmlns:dae="http://laseeb/dae/sgDae"
xmlns:src="http://xml.apache.org/xindice/Query" src:col="/db/daeDocuments"
src:key="4">
<dae:title>Titulo do artigo com rating igual a 2</dae:title>
<dae:text>texto do artigo com rating igual a 2</dae:text>
<dae:image>img1.jpg</dae:image>
</dae:article>


</document>

<!--------------------------------------------------------------------------
>

wich is the right one. But when i try to apply one transformer created by
me, deaTransformer.java (bellow are the methods setup, startElement and
endElement that i had to implement,


<!--Begin of
daeTransformer.java---------------------------------------------------------
--->

 public void setup(SourceResolver resolver, Map objectModel, String src,
Parameters par)
            throws ProcessingException, SAXException, IOException
    {
         this.mode = MODE_NONE;
    }

public void startElement(String namespaceURI, String localName, String
qName,
            Attributes attributes) throws SAXException
    {
        if (namespaceURI != null && namespaceURI.equals(NAMESPACE) )
        {
            AttributesImpl newAttr = new AttributesImpl();
            if (localName.equals(ARTICLE_ELEMENT) == true)
            {
                newAttr.addAttribute(namespaceURI, ARTICLE_ELEMENT, qName,
"color", "red");
                super.startElement(namespaceURI, ARTICLE_ELEMENT, qName,
newAttr);
            }
            else if (localName.equals(ARTICLE_TITLE_ELEMENT) == true)
            {
                this.mode = MODE_TITLE;
                super.startElement(namespaceURI, localName, qName,
attributes);
            }
            else if (localName.equals(ARTICLE_TEXT_ELEMENT) == true)
            {
                this.mode = MODE_TEXT;
                super.startElement(namespaceURI, localName, qName,
attributes);
            }
            else if (localName.equals(ARTICLE_IMAGE_ELEMENT) == true)
            {
                this.mode = MODE_IMAGE;
                super.startElement(namespaceURI, localName, qName,
attributes);
            }
            else
            {
                throw new SAXException("Unknown element " + localName);
            }
        }
        else
        {
        // Not for us
            super.startElement(namespaceURI, localName, qName, attributes);
        }

    }

public void characters(char[] buffer, int start, int length)
            throws SAXException
    {
        switch (this.mode)
        {
            case MODE_NONE : super.characters(buffer, start, length);
            break;
            case MODE_TITLE : this.articleTitle.append(buffer, start,
length);
            break;
            case MODE_TEXT : this.articleText.append(buffer, start, length);
            break;
            case MODE_IMAGE : this.articleImage.append(buffer, start,
length);
            break;
        }
    }

    public void endElement(String namespaceURI, String localName, String
qName)
            throws SAXException {

        if (namespaceURI != null && namespaceURI.equals(NAMESPACE))
        {
            if (localName.equals(ARTICLE_ELEMENT) == true)
            {
                String text;

                try
                {
                    //text = "Sending mail to " + this.toAddress + " was
successful.";
                } catch (Exception any)
                {
                    this.getLogger().error("Exception during sending of
mail", any);
    // failure message
                    //text = "Sending mail to " + this.toAddress + "
failed!";
                }
// create SAX events for success/failure
                super.endElement(NAMESPACE, ARTICLE_ELEMENT, "");

            } else if (localName.equals(ARTICLE_TITLE_ELEMENT) == true)
            {
    // mailto received
                super.characters(this.articleTitle.toString().toCharArray(),
0, this.articleTitle.length());
                super.endElement(NAMESPACE, ARTICLE_TITLE_ELEMENT, "");
                this.mode = MODE_NONE;

            } else if (localName.equals(ARTICLE_TEXT_ELEMENT) == true)
            {
                super.characters(this.articleText.toString().toCharArray(),
0, this.articleText.length());
                super.endElement(NAMESPACE, ARTICLE_TEXT_ELEMENT, "");
                this.mode = MODE_NONE;

            } else if (localName.equals(ARTICLE_IMAGE_ELEMENT) == true)
            {
                super.characters(this.articleImage.toString().toCharArray(),
0, this.articleImage.length());
                super.endElement(NAMESPACE, ARTICLE_IMAGE_ELEMENT, "");
                this.mode = MODE_NONE;

            } else
            {
                throw new SAXException("Unknown element " + localName);
            }
        } else
        {
// not for us
        super.endElement(namespaceURI, localName, qName);
    }
}

<!--End of
daeTransformer.java---------------------------------------------------------
--->

and using a pipeline like,

<map:pipeline>
    <map:match pattern="daestart">
        <map:generate type="serverpages" src="daexsps/start.xsp"/>
        <map:transform type="dae"/>
        <map:serialize type="html"/>
     </map:match>
  </map:pipeline>

i get,

Cocoon 2 - Internal server error

----------------------------------------------------------------------------
----

type internal-server-error

message Exception in ServerPagesGenerator.generate()

description org.apache.cocoon.ProcessingException: Exception in
ServerPagesGenerator.generate(): java.lang.NullPointerException

sender org.apache.cocoon.servlet.CocoonServlet

source Cocoon servlet

exception

org.apache.cocoon.ProcessingException: Exception in
ServerPagesGenerator.generate(): java.lang.NullPointerException

request-uri

/dae/daestart

original exception

Original exception : java.lang.NullPointerException
	at
org.apache.cocoon.serialization.AbstractTextSerializer.startElement(Abstract
TextSerializer.java:271)
	at
org.apache.cocoon.xml.AbstractXMLPipe.startElement(AbstractXMLPipe.java:87)
	at
pt.laseeb.dae.transformer.daeTransformer.startElement(daeTransformer.java:11
6)
.....
and it goes on... :(

Im sorry for the size of the mail, but if you have read this far and have a
clue of what i am doing wrong, i would be thakfull if you give me a hint. :)

thanks in advance
Miguel Carvalho