cocoon-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Miguel Carvalho" <mig.c...@mail.telepac.pt>
Subject cocoon sax events and transformer doubts
Date Tue, 12 Aug 2003 15:02:10 GMT
Hi, i'm having some doubts about the way the sax events are created in a
transformer in java...

I'm asking this question because i wrote a transformer in java, but i want
to apply another transformer in xsl after that and i can't access the
attributes in the nodes...

this is the code of the transformer i wrote..

package pt.laseeb.dae.transformer;

/*
 * File: daeTransformer.java
 *
 * Project: Design Automatico e Evolutivo
 *
 * Authors: Paulo Sérgio Silva Paixao        - pssp@mega.ist.utl.pt
 *          Miguel Angelo Rodrigues Carvalho - marc@mega.ist.utl.pt
 *
 * Date: 4/Ago/2003
 *
 * Trabalho de Fim de Curso - Design Automático e Evolutivo
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA  02111-1307, USA.
 *
 */

import org.apache.cocoon.transformation.AbstractTransformer;
import org.apache.cocoon.environment.SourceResolver;
import org.apache.cocoon.ProcessingException;
import org.apache.avalon.framework.parameters.Parameters;
import org.xml.sax.SAXException;
import org.xml.sax.Attributes;
import org.xml.sax.helpers.AttributesImpl;
import java.util.Map;
import java.io.IOException;

public class daeTransformer extends AbstractTransformer
{
    public static final String DOCUMENT_ELEMENT = "document";
    public static final String CONTENT_ELEMENT = "contents";
    public static final String ARTICLE_ELEMENT = "article";
    public static final String ARTICLE_TITLE_ELEMENT = "title";
    public static final String ARTICLE_TEXT_ELEMENT = "text";
    public static final String ARTICLE_IMAGE_ELEMENT = "image";

    protected static final int MODE_NONE = 0;
    protected static final int MODE_ARTICLE = 1;
    protected static final int MODE_TITLE = 2;
    protected static final int MODE_TEXT = 3;
    protected static final int MODE_IMAGE = 4;

    protected int mode;
    protected StringBuffer articleText;
    protected StringBuffer articleTitle;
    protected StringBuffer articleImage;

    protected Attributes attr;


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


    public void startElement(String namespaceURI, String localName, String
qName,
            Attributes attributes) throws SAXException
    {
            AttributesImpl newAttr = new AttributesImpl();

            if (localName.equals(DOCUMENT_ELEMENT) == true)
            {
                super.startElement("", localName , qName, newAttr);
            }
            else if (localName.equals(CONTENT_ELEMENT) == true)

            {
                super.startElement("", localName , qName, newAttr);

            }
            else if (localName.equals(ARTICLE_ELEMENT) == true)
            {
                newAttr.addAttribute("", localName, "layouttype", "", "2");
                newAttr.addAttribute("", localName, "xpos", "", "5");
                newAttr.addAttribute("", localName, "ypos", "", "7");
                newAttr.addAttribute("", localName, "border", "", "dotted");

                super.startElement("", localName, qName, newAttr);

            }
            else if (localName.equals(ARTICLE_TITLE_ELEMENT) == true)
            {
                this.mode = MODE_TITLE;

                super.startElement("", localName, qName, newAttr);
            }
            else if (localName.equals(ARTICLE_TEXT_ELEMENT) == true)
            {
                this.mode = MODE_TEXT;
                super.startElement("", localName, qName, attributes);
            }
            else if (localName.equals(ARTICLE_IMAGE_ELEMENT) == true)
            {
                this.mode = MODE_IMAGE;
                super.startElement("", localName, qName, attributes);
            }
            else
            {
                throw new SAXException("Unknown start element " +
localName);
            }
    }


    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 (localName.equals(DOCUMENT_ELEMENT) == true)
            {
                super.endElement("", localName , qName);
            }
            else if (localName.equals(CONTENT_ELEMENT) == true)
            {
                super.endElement("", localName, qName);
            }
            else if (localName.equals(ARTICLE_ELEMENT) == true)
            {
                super.endElement("", localName, qName);

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

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

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

            } else
            {
                throw new SAXException("Unknown end element " + localName);
            }
    }

}


and the result of this transformer is something like ...


<document>

<contents>

<article layouttype="2" xpos="5" ypos="7" border="dotted">
<title>Titulo com rating 2</title>
<text>Texto</text>
</article>
<article layouttype="2" xpos="5" ypos="7" border="dotted">
<title>Titulo do artigo com rating igual a 1</title>
<text>texto do artigo com rating igual a 1</text>
<image>img1.jpg</image>
</article>
<article layouttype="2" xpos="5" ypos="7" border="dotted">
<title>Titulo do artigo com rating igual a 2</title>
<text>texto do artigo com rating igual a 2</text>
<image>img1.jpg</image>
</article>
<article layouttype="2" xpos="5" ypos="7" border="dotted">
<title>Titulo do artigo com rating igual a 2</title>
<text>texto do artigo com rating igual a 2</text>
<image>img1.jpg</image>
</article>

</contents>

</document>


and after that i would like to apply a xsl stylesheet that can access some
of the properties available in the nodes, this is the code of the xsl


<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
    <xsl:output type="html"/>

    <xsl:template match="/">
        <xsl:apply-templates select="*"/>
    </xsl:template>

    <xsl:template match="contents">
        contents
        <HTML>
            <HEAD>
            </HEAD>
            <BODY>
                <xsl:apply-templates select="article[@layouttype='2']"/>
            </BODY>
        </HTML>
    </xsl:template>

    <xsl:template match="border">
        border
    </xsl:template>

    <xsl:template match="article">
        article
        <xsl:value-of select="title"/>
        <xsl:value-of select="text"/>

        <img>
            <xsl:attribute name="src">
                <xsl:value-of select="image"/>
            </xsl:attribute>
        </img>

    </xsl:template>

</xsl:stylesheet>

but i can't access the attributes of the nodes...


If anyone has some ideas of why this might be hapenning it would be a very
good help..


thanks in advance
Miguel Carvalho



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


Mime
View raw message