Eric,
Here is a snippet of the code I use for sending all XML to the standard input of a
process, and converting its standard output to XML and send it to the next pipeline
component.
public class TransfListado extends AbstractTransformer {
private SAXTransformerFactory fabrica;
private ByteArrayOutputStream buf = new ByteArrayOutputStream(1024);
/**Formato de entrada*/
private Properties formato = new Properties();
public TransfListado() {
fabrica = (SAXTransformerFactory)TransformerFactory.newInstance();
formato.put(OutputKeys.METHOD, "xml");
formato.put(OutputKeys.ENCODING, "ISO-8859-1");
//formato.put(OutputKeys.INDENT, "yes");
}
/** @see
org.apache.cocoon.transformation.AbstractTransformer#setup(org.apache.cocoon.environment.SourceResolver,
java.util.Map, java.lang.String, org.apache.avalon.framework.parameters.Parameters) */
public void setup(SourceResolver resolver, Map objectModel, String src,
Parameters params) throws ProcessingException, SAXException, IOException {
}
protected void capturaXml() throws TransformerConfigurationException {
buf.reset();
TransformerHandler handler = fabrica.newTransformerHandler();
handler.getTransformer().setOutputProperties(formato);
handler.setResult(new StreamResult(buf));
setContentHandler(handler);
setLexicalHandler(handler);
}
private InputStream getSalidaProceso(Process proceso) throws IOException {
proceso.getErrorStream().close();
proceso.getOutputStream().write(buf.toByteArray());
proceso.getOutputStream().close();
return proceso.getInputStream();
}
/**Arranca el proceso y le env�a todos los eventos SAX
* @see org.xml.sax.ContentHandler#startDocument() */
public void startDocument() throws SAXException {
try {
capturaXml();
} catch (TransformerConfigurationException e) {
throw new SAXException(e);
}
super.startDocument();
}
/**Despu�s de haber enviado toda la informaci�n a listado4,
* se lee su salida est�ndar y se analiza con un [EMAIL PROTECTED] XMLReader}
* convencional para generar nuevos eventos SAX*/
public void endDocument() throws SAXException {
super.endDocument();
try {
//###Arrancar proceso
StringBuffer cmd = new StringBuffer("my command line");
Process proceso = Runtime.getRuntime().exec(cmd.toString(),
VarsEntorno.i.getArray());
//###Procesar resultado
getLogger().debug("Leyendo resultado de listado4");
InputStreamReader stream new
InputStreamReader(getSalidaProceso(proceso));
stream.reset();
XMLReader analizador = XMLReaderFactory.createXMLReader();
analizador.setContentHandler(xmlConsumer);
analizador.parse(new InputSource(stream));
} catch (SAXException ex) {
throw ex;
} catch (Exception e) {
throw new SAXException(e);
}
}
}
> -----Mensaje original-----
> De: JACOB, ERIC [mailto:[EMAIL PROTECTED]
> Enviado el: lunes, 16 de agosto de 2004 15:29
> Para: Perez Carmona, David
> Asunto: Re: Custom transformer
>
>
>
>
> Sorry, maybe I used the wrong term...
>
> Here an example:
>
> public class CustomTransformer extends AbstractSAXTransformer {
>
>
> protected Properties format;
>
>
> public SOAPTransformer() {
> //format = XMLUtils.createPropertiesForXML( true );
> format = new Properties();
> format.put( OutputKeys.METHOD, "xml" );
> format.put( OutputKeys.OMIT_XML_DECLARATION, "yes" );
> }
>
>
> public void setup( SourceResolver sourceResolver,
> Map map,
> String str,
> Parameters parameters )
> throws ProcessingException, SAXException, IOException {
> // get some parameters here...
> }
>
>
> public void startDocument() throws SAXException {
> this.startSerializedXMLRecording( format );
> }
>
>
> public void endDocument() throws SAXException {
> try {
> String message = this.endSerializedXMLRecording();
> execute ( message );
> } catch ( ProcessingException pe ) {
> throw new SAXException( pe.getMessage() );
> }
> }
>
>
> protected void execute ( String message )
> throws ProcessingException, SAXException {
> try {
> // I use the XMLReader just to see what is hapenning...
> // This method would rather do something with the XML
> message, maybe send it
> // to a remote server and return back the response to the pipeline
> XMLReader xmlReader = XMLReaderFactory.createXMLReader();
> xmlReader.setFeature(
> "http://xml.org/sax/features/namespaces", true );
> xmlReader.setFeature(
>
> "http://xml.org/sax/features/namespace-prefixes", false );
> xmlReader.setContentHandler( this.contentHandler );
> StringReader strRdr = new StringReader( message );
> InputSource inSrc = new InputSource( strRdr );
> xmlReader.parse( inSrc );
> } catch ( Exception e ) {
> throw new ProcessingException( e.getMessage() );
> }
> }
>
>
> }
>
>
> This gives me the entire XML file minus the XML Namespaces. So I got a
> prefix not bound error when I execute it. Any ideas? What
> happends if I have
> more than one namespace?
>
> Thanks for your help.
>
> Eric
>
>
> -----Original Message-----
> From: Perez Carmona, David [mailto:[EMAIL PROTECTED]
>
> Sent: Thursday, August 12, 2004 3:00 AM
> To: [EMAIL PROTECTED]; [EMAIL PROTECTED]
> Subject: RE: Custom transformer
>
>
> Can you put an example? I don't fully understand your problem.
> The XML file is already parsed after the generation phase.
> Parse = convert
> text to SAX events.
>
> > -----Mensaje original-----
> > De: JACOB, ERIC [mailto:[EMAIL PROTECTED]
> > Enviado el: mi�rcoles, 11 de agosto de 2004 14:06
> > Para: [EMAIL PROTECTED]
> > Asunto: RE: Custom transformer
> >
>
> >
>
> >
>
> > The problem is not really the error... I just need some
>
> > suggestions on how I
> > could parse an entire xml file (generate by the xml
>
> > generator) before doing
> > some action with it (I don't care about what is inside the
>
> > xml document, I
> > just want to sent it).
> >
>
> > I'm not very familiar with the SAX event and cocoon
>
> > transformer, and the
> > information I've found on the cocoon's site (and wiki),
>
> > didn't help me much
> > in what I'm trying to do.
> >
>
> > The startSerializedXMLRecording and endSerializedXMLRecording
>
> > functions
> > didn't act the way I expected... I thought overriding the
>
> > setConsumer and
> > write a custom contentHandler, but maybe it exists a better way...
> >
>
> > Thanks for your help,
> >
>
> > Eric
> >
>
> > -----Original Message-----
> > From: Perez Carmona, David [mailto:[EMAIL PROTECTED]
>
> > Sent: Wednesday, August 11, 2004 2:36 AM
> > To: [EMAIL PROTECTED]; [EMAIL PROTECTED]
> > Subject: RE: Custom transformer
> >
>
> >
>
> > Could you please provide the full call stack?
> >
>
> > > -----Mensaje original-----
> > > De: JACOB, ERIC [mailto:[EMAIL PROTECTED]
> > > Enviado el: martes, 10 de agosto de 2004 21:47
> > > Para: [EMAIL PROTECTED]
> > > Asunto: Custom transformer
> > >
> >
>
> > >
> >
>
> > >
> >
>
> > > Hi all,
> > >
> >
>
> > > I'm writing a custom transformer that need to parse an
>
> > entire xml file
> > > before doing something with it. The sitemap should look like that:
> > >
> >
>
> > > <map:match pattern="">
> > > <map:generate src="foo.xml"/>
> > > <map:transform type="custom-transformer"/>
> > > <map:serialize type="xml"/>
> > > </map:match>
> > >
> >
>
> > > So, I want to send foo.xml to a remote server and return the
> >
>
> > > response back
> > > to the pipeline to serialize it in xml. I extended the
> > > AbstractSAXTransformer since it provides various useful
>
> > methods and I
> > > thought doing something like this:
> > >
> >
>
> > > public void startDocument() throws SAXException {
> > > this.startSerializedXMLRecording( format );
> > > }
> > >
> >
>
> > > public void endDocument() throws SAXException {
> > > try {
> > > String value = this.endSerializedXMLRecording();
> > > callSomeFunction( value );
> > > } catch ( ProcessingException pe ) {
> > > throw new SAXException( pe.getMessage() );
> > > }
> > > }
> > >
> >
>
> > > But I got an error when I encounter text node:
> > >
> >
>
> > > org.apache.cocoon.ProcessingException: Failed to execute
> pipeline.:
> > > org.xml.sax.SAXException: no protocol
> > >
> >
>
> > > Any ideas on how I could manage it?
> > >
> >
>
> > > Thanks,
> > >
> >
>
> > > Eric
> > >
> >
>
> > >
> >
>
> > >
> >
>
> > >
> >
>
> > >
>
> >
> ---------------------------------------------------------------------
> > > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > > For additional commands, e-mail: [EMAIL PROTECTED]
> > >
> >
>
> > >
> >
>
> > > *************************************************************
> > > Este correo ha sido procesado por el Antivirus del Grupo FCC.
> > > *************************************************************
> > >
> >
>
> >
>
> > *************************************************************
> > Este correo ha sido procesado por el antivirus del Grupo FCC.
> > *************************************************************
> >
>
> >
>
> >
>
> >
> ---------------------------------------------------------------------
> > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]
> >
>
> >
>
> > *************************************************************
> > Este correo ha sido procesado por el Antivirus del Grupo FCC.
> > *************************************************************
> >
>
>
> *************************************************************
> Este correo ha sido procesado por el antivirus del Grupo FCC.
> *************************************************************
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>
>
>
> *************************************************************
> Este correo ha sido procesado por el Antivirus del Grupo FCC.
> *************************************************************
>
*************************************************************
Este correo ha sido procesado por el antivirus del Grupo FCC.
*************************************************************
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]