cocoon-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Frank Rocholl" <frank.roch...@gmx.li>
Subject Re: Including XML Tags in custom transformer
Date Wed, 06 Dec 2006 16:00:38 GMT
Hi Jeroen,

thanks for help.
But im not quite familar with SAX.
How I have to "Cast" the string in the example 
to be send as SAX.

Best whishes

Frank

-------- Original-Nachricht --------
Datum:  Wed, 06 Dec 2006 15:57:45 +0100
Von: Jeroen Reijn <j.reijn@hippo.nl>
An: users@cocoon.apache.org
Betreff:  Re: Including XML Tags in custom transformer

> Hi Frank,
> 
> yes this is possible.
> You need to make sure that the the XML is send as SAX and not as a String.
> 
> Your problem lies here: super.characters(externalXML.toCharArray(), 0, 
> externalXML.length()); You pass the data as a characters into the SAX, 
> so it won't be XML but text from now on.
> 
> As you might have noticed the super.startElement() creates an XML node 
> and the end element created the closing node.
> 
> Here is some nice documentation to read about SAX and writing your own 
> sitemap components.
> 
> http://wiki.apache.org/cocoon/WritingPipelineComponents
> 
> http://wiki.apache.org/cocoon/ImplementingTransformers
> 
> In your case it depends on what those external libraries return to you. 
> Do they return a DOM document you will need to convert it to SAX to be 
> able to output it.
> 
> Kind regards,
> 
> Jeroen Reijn
> 
> 
> Frank Rocholl wrote:
> > Dear all,
> > 
> > I'ld like to create my own transformer, that have to
> > do some work and have also include xml tags, that comes
> > directly from other libraries. For testing I've created 
> > a method  getExternalXMLData that returns some nodes.
> > 
> > When I use the transformer in a pipeline transforming
> > the simple xml file:
> > ### BEGIN test.xml ###
> > <testnode>
> >   <info>Hello</info>
> >   <extern/>
> > </testnode>
> > ### END  test.xml ###
> > 
> > I get the result:
> > 
> > ### BEGIN result.xml ###
> > <?xml version="1.0" encoding="UTF-8"?><testnode>
> >   <info>Hello</info>
> >   <extern><ex>  This is from extern </ex>
> >   <ex>  this too  </ex>
> >   </extern>
> > </testnode>
> > ### END  result.xml ###
> > 
> > But I'ld like to get:
> > 
> > ### BEGIN wish.xml ###
> > <?xml version="1.0" encoding="UTF-8"?><testnode>
> >   <info>Hello</info>
> >   <extern>
> >      <ex>  This is from extern </ex>
> >      <ex>  this too  </ex>
> >   </extern>
> > </testnode>
> > ### END  wish.xml ###
> > 
> > Is that possible?
> > 
> > 
> > Many thanks for help
> > 
> > 
> > Frank 
> > 
> > P.S.: Java Source Code and sitemap entries shown below:
> > 
> > ### BEGIN SaxXml.java ###
> > import java.io.IOException;
> > import java.util.Map;
> > 
> > import org.apache.avalon.framework.logger.LogEnabled;
> > import org.apache.avalon.framework.logger.Logger;
> > import org.apache.avalon.framework.parameters.Parameters;
> > import org.apache.cocoon.ProcessingException;
> > import org.apache.cocoon.environment.SourceResolver;
> > import org.apache.cocoon.transformation.AbstractTransformer;
> > import org.xml.sax.Attributes;
> > import org.xml.sax.SAXException;
> > 
> > public class SaxXml extends AbstractTransformer implements LogEnabled
> > {
> >    
> >     // The LogEnabled interface is one method: enableLogging
> >     private Logger logger;
> > 
> >     public void enableLogging(Logger logger)
> >     {
> >         this.logger = logger;
> >     }
> >    
> >     public void setup(SourceResolver sourceResolver, Map objectModel,
> >             String str, Parameters parameters) throws
> ProcessingException,
> >             SAXException, IOException
> >     {
> >         logger.info("setup");
> >     }
> > 
> >     public void startElement(String namespaceURI, String localName,
> >             String qName, Attributes attributes) throws SAXException
> >     {
> >        
> >         logger.info("startElement");
> >         
> >         if ( localName.equals("extern") )
> >         {
> >             super.startElement(namespaceURI, localName, qName,
> attributes);
> >             
> >             // assume , we got externalXML from an outside source (
> maybe a database )
> >             
> >             String externalXML = this.getExternalXMLData();
> >                             
> >             //  how do I output this here into the sax-stream ??
> >             //  ??.write(externalXML)
> >             
> >             // this does not work:
> >             super.characters(externalXML.toCharArray(), 0,
> externalXML.length());
> >         }
> >         else
> >             super.startElement(namespaceURI, localName, qName,
> attributes);
> >         
> >         return;
> >     }
> > 
> >     public void endElement(String namespaceURI, String localName, String
> qName)
> >             throws SAXException
> >     {
> >         logger.info("endElement");
> >         super.endElement(namespaceURI, localName, qName);
> >     }
> >     
> >     
> >     // This function gets some "external" xml-data on the fly !! 
> >     private String getExternalXMLData()
> >     {
> >         String data=
> >             "<ex>  This is from extern </ex>\n" +
> >             "<ex>  this too  </ex>\n";
> >         return data;
> >             
> >     }
> >     
> > }
> > ### END  SaxXml.java ###
> > 
> > ### BEGIN sitemap.xmap ###
> > <?xml version="1.0" encoding="UTF-8"?>
> > <map:sitemap xmlns:map="http://apache.org/cocoon/sitemap/1.0">
> >   <map:components>
> >     <map:generators default="file" />
> >     <map:transformers default="xslt">
> >       <map:transformer name="t2" src="SaxXml" />
> >    </map:transformers>
> >   </map:components>
> > 
> >   <map:pipelines>
> >     <map:pipeline>
> >       <map:match pattern="saxxml.html">
> >         <map:generate src="test.xml"/>
> >         <map:transform type="t2"/>
> >         <map:serialize type="xml"/>
> >       </map:match>
> >     </map:pipeline>
> >   </map:pipelines>
> > </map:sitemap>
> > ### END sitemap.xmap ###
> > 
> > 
> > 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
> For additional commands, e-mail: users-help@cocoon.apache.org

-- 
Der GMX SmartSurfer hilft bis zu 70% Ihrer Onlinekosten zu sparen! 
Ideal für Modem und ISDN: http://www.gmx.net/de/go/smartsurfer

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


Mime
View raw message