You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cocoon.apache.org by Alessandro Vincelli <a....@w4b.it> on 2004/10/21 18:19:21 UTC
Transformer: DocumentFragment and SAX events
Hi all,
I am trying to write a custom Transformer which should do the following:
ignore everything except <row> elements;
when an <row> element is encountered, generate a <tr> element,
containing a <td> element for every child of the source <row> element;
example:
source:
...<anything>...
<row>
<field1>A</field1>
<field2>B</field2>
</row>
...</anything>...
should produce:
<tr>
<td>A</td>
<td>B</td>
</tr>
My approach is, when I find a <row> element, to use startRecording(),
with endRecording() when the end of the <row> element is reached. Thus I
get a DocumentFragment from which I can extract what I need.
The problem is that the resulting DocumentFragment gets populated with
the nodes I need *only if I call super.startElement() for every
element*. But, this way, I get in the result also elements I wish to
suppress.
Example:
...<anything>...
<row>
<field1>A</field1>
<field2>B</field2>
</row>
...</anything>...
actually produces:
...<anything>...
<tr>
<td>A</td>
<td>B</td>
</tr>
...</anything>...
How can I overcome this? I am sure that I am doing something deeply
wrong, as you can see from the attached source.
Thanks in advance,
Alessandro
package com.w4b.formProcessor;
import org.apache.cocoon.transformation.*;
import org.w3c.dom.DocumentFragment;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.Attributes;
import org.xml.sax.helpers.AttributesImpl;
import org.xml.sax.SAXException;
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 java.util.Map;
public class ExcelOutputTransformer extends AbstractSAXTransformer
{
boolean firstRow;
AttributesImpl attrs = new AttributesImpl();
public void startDocument() throws SAXException{
firstRow = true;
getLogger().debug("inizio documento...");
super.startElement("","table","table",attrs);
}
public void endDocument() throws SAXException{
getLogger().debug("fine documento.");
super.endElement("","table","table");
}
public void startElement(String nsURI, String localName, String
qualName, Attributes attr) throws SAXException{
getLogger().debug(localName);
if (!(localName.equals("row"))) {
super.contentHandler.startElement(nsURI,localName,qualName,attr);
return;
}
getLogger().debug("inizio riga...");
this.startRecording();
}
public void endElement(String nsURI, String localName, String
qualName) throws SAXException{
if (!(localName.equals("row"))) {
super.contentHandler.endElement(nsURI,localName,qualName);
return;
}
getLogger().debug("fine riga.");
DocumentFragment df = this.endRecording();
if (firstRow) {
super.startElement("","tr","tr",attrs);
Node n = df.getFirstChild();
int i = 0;
while (n!=null) {
i++;
if (n!=null) {
super.startElement("","td","td",attrs);
String val = n.getNodeName();
super.characters(val.toCharArray(),0,val.length());
super.endElement("","td","td");
} else {
getLogger().debug("nodo " + i + "nullo.");
}
n = n.getNextSibling();
}
super.endElement("","tr","tr");
firstRow = false;
}
Node n = df.getFirstChild();
int i = 0;
super.startElement("","tr","tr",attrs);
while (n!=null) {
i++;
if (n.getFirstChild()!=null) {
super.startElement("","td","td",attrs);
String val = n.getFirstChild().getNodeValue();
super.characters(val.toCharArray(),0,val.length());
super.endElement("","td","td");
} else {
getLogger().debug("nodo " + i + "nullo.");
}
n = n.getNextSibling();
}
super.endElement("","tr","tr");
}
}
--
-------------------------------
Alessandro Vincelli
W4B - web for business s.r.l.
Firenze
via Pellicceria 10 - 50123
E-mail: a.vincelli@w4b.it
tel: 055-2654270
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org