Re: Strings to SAX events

2002-11-08 Thread jakob . dalsgaard
I've done that in a generator thing I've made at work, using plain 
XMLReader factory (code is off course employers property - but I can 
outline the solution)

Problem: you have a string of xml that you need to generate SAX events 
from, the string could be anything like:
   "\naklsks",
   "aslkfklslaskklfsa,
   " aasdflaskjflsadkjf "

The first string is a valid xml document, the second is missing the 
xml-declaration and does not have a single toplevel element, the third one 
is just missing the xml-declaration (does this actually make it an invalid 
xml?)

Fixing the missing xml declaration can be done by:

if (!xmlstring.startsWith("" + 
xmlstring;
}

Now if the xmlstring does contain the xml-declaration I would presume it 
is a valid xml document with one top-level element, else I add a toplevel 
element to be sure to have just one, thus fixing both the xml declaration 
and the possible multiple toplevel elements is done by:

if (!xmlstring.startsWith("" + 
xmlstring + "";
}

When streaming the SAX Events I will later filter out the toplevel element 
again ;-)

To be able to stream all but the toplevel element, you need a custom 
ContentHandler; it should proxy all events but the startElement and 
endElement of the toplevel tag; I've implemented it in an inner class and 
it goes like:

protected class FilterContentHandler
implements ContentHandler
{

   private ContentHandler destination;
   private int depth = 0;

   public FilterContentHandler (ContentHandler adestination)
   {
  destination = adestination;
   }

   public void startDocument ()
   {
  depth = 0;
   }

   public void endDocument ()
   {
   }

   public void startElement ()
   {
  if (depth > 0) {
 destination.startElement (...)
  }
 depth++;
   }

   public void endElement (...)
   {
  depth--;
  if (depth > 0) {
 destination.endElement (...);
  }
   }

   .. and more methods

}

Off course all methods throw the SAXException - methods from the 
ContentHandler interface not listed here just proxy to the destination 
ContentHandler.

Then I instantiate an XMLReader, let contenthandler be the sink of your 
SAX Events:

XMLReader reader = XMLReaderFactory.createXMLReader ();
reader.setContentHandler (new FilterContentHandler (contenthandler));

Make an inputsource:

InputSource inputsource =
   new InputSource (new StringReader (xmlstring);

then just do:

reader.parse (inputsource);


If you need a better example I could spent some of my spare time making 
it, maybe during the weekend.

Regards Jakob



Jakob Dalsgaard
Udvikler
e-mail:   [EMAIL PROTECTED]
Vesterbrogade 149
1620 København V
Tlf.:   70 25 80 30
Fax.: 70 25 80 31






"Luca Morandini" <[EMAIL PROTECTED]>
11/08/02 10:55 AM
Please respond to cocoon-users

 
To: <[EMAIL PROTECTED]>
cc: 
Subject:Strings to SAX events


Folks,

 We're in the process of writing a Transformer, which, of course, outputs
SAX
 events... but, in the midst of this stream , we need to insert an
 XML element stored in a string.

 To do this we're groping in the dark trying something like this:

  JaxpParser respParser = new JaxpParser();
  respParser.parse( new InputSource(new StringReader(str)),
  new EmbeddedXMLPipe(contentHandler));

 Which fails giving this:
 org.apache.cocoon.ProcessingException: Failed to execute pipeline.:
 java.lang.ClassCastException
 ...
 Caused by: java.lang.ClassCastException
  at

com.lucamorandini.charts.ChartTransformer.endElement(ChartTransformer.java:6
55)
  at

org.apache.cocoon.components.sax.XMLByteStreamInterpreter.parse(XMLByteStrea
mInterpreter.java:129)
  at

org.apache.cocoon.components.sax.XMLByteStreamInterpreter.deserialize(XMLByt
eStreamInterpreter.java:102)
  at
 org.apache.cocoon.components.pipeline.CachingEventPipeline.process
 (CachingEventPipeline.java:219)
  ... 44 more

 java.lang.ClassCastException
  at

com.lucamorandini.charts.ChartTransformer.endElement(ChartTransformer.java:6
55)
  at

org.apache.cocoon.components.sax.XMLByteStreamInterpreter.parse(XMLByteStrea
mInterpreter.java:129)
  at

org.apache.cocoon.components.sax.XMLByteStreamInterpreter.deserialize(XMLByt
eStreamInterpreter.java:102)
  at
 org.apache.cocoon.components.pipeline.CachingEventPipeline.process
 (CachingEventPipeline.java:219)
  at

org.apache.cocoon.components.pipeline.CachingStreamPipeline.process(CachingS
treamPipeline.java:399)

 The classes we use are:
  org.apache.avalon.excalibur.xml.JaxpParser;
  org.xml.sax.InputSource;
  java.io.StringReader;
  org.apache.cocoon.xml.EmbeddedXMLPipe;

 And the environment is:
  Solaris 5.8
  JDK 1.4.1_01
  Tomcat 4.1.12-LE-jdk14
  Cocoon 2.0.3

 May someone please help us ?

 Thanks in advance,

Piero De Nicola & Luca Morandini


 We are protected from the virus by Norton Antivirus Corporate Edition

-
Please check that your question  has not already been answere

Re: Strings to SAX events

2002-11-08 Thread Ludovic de Beaurepaire
Luca

I have the same type of need to produce SVG for barcodes. I have a String
code as parameter, and an external java module to produce graphic
informations. I will do it with an Action obtaining these informations as
java objects, a generator generating corresponding XML datas
(...) . A Stylesheet transform it.
But i'm not expert, surely there are other possibilities.

Regards,

Ludovic
- Original Message -
From: "Luca Morandini" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Friday, November 08, 2002 11:33 AM
Subject: R: Strings to SAX events


> Ludovic,
>
> we're writing a Transformer to produce SVG (and JPEG/PNG optionally)
charts.
> The idea is having the chart description and associated data in XML as
> input, transform them, ending up with a nice SVG to be serialized.
>
> We're using a charting library which doesn't produce a SAX stream, but
> outputs the SVG element as a string, hence, the need to insert this XML
> elements in the output SAX stream.
>
> I hope this clears the matter :)
>
> Luca Morandini
> Istituto Poligrafico e Zecca dello Stato
> [EMAIL PROTECTED]
> [EMAIL PROTECTED]
>
>
> > -Messaggio originale-
> > Da: Ludovic de Beaurepaire [mailto:ludovic.debeaurepaire@;axonie.com]
> > Inviato: venerdì 8 novembre 2002 11.26
> > A: [EMAIL PROTECTED]
> > Oggetto: Re: Strings to SAX events
> >
> >
> > Luca
> >
> > Sorry if it is NOK, but i didn't understand why you want to add
> > XML datas in
> > the transformer instead of in your pipeline's generator ?
> >
> > Ludovic
> > - Original Message -
> > From: "Luca Morandini" <[EMAIL PROTECTED]>
> > To: <[EMAIL PROTECTED]>
> > Sent: Friday, November 08, 2002 11:11 AM
> > Subject: R: Strings to SAX events
> >
> >
> > > Ludovic,
> > >
> > > thanks for your kind answer, but we are not in an XSP page
> > (we're writing
> > a
> > > Transformer instead), hence, your suggestion is not truly useful to
us.
> > >
> > > Best regards,
> > >
> > > Luca Morandini
> > > [EMAIL PROTECTED]
> > >
> > > > -Messaggio originale-
> > > > Da: Ludovic de Beaurepaire [mailto:ludovic.debeaurepaire@;axonie.com]
> > > > Inviato: venerdì 8 novembre 2002 11.02
> > > > A: [EMAIL PROTECTED]
> > > > Oggetto: Re: Strings to SAX events
> > > >
> > > >
> > > > Try in a XSP the following, data is your xml string :
> > > >
> > > > 
> > > > 
> > > > data
> > > > 
> > > > 
> > > >
> > > >
> > > >
> > > > - Original Message -
> > > > From: "Luca Morandini" <[EMAIL PROTECTED]>
> > > > To: <[EMAIL PROTECTED]>
> > > > Sent: Friday, November 08, 2002 10:55 AM
> > > > Subject: Strings to SAX events
> > > >
> > > >
> > > > > Folks,
> > > > >
> > > > >  We're in the process of writing a Transformer, which, of
> > > > course, outputs
> > > > > SAX
> > > > >  events... but, in the midst of this stream , we need to insert an
> > > > >  XML element stored in a string.
> > > > >
> > > > >  To do this we're groping in the dark trying something like this:
> > > > >
> > > > >   JaxpParser respParser = new JaxpParser();
> > > > >   respParser.parse( new InputSource(new StringReader(str)),
> > > > >   new EmbeddedXMLPipe(contentHandler));
> > > > >
> > > > >  Which fails giving this:
> > > > >  org.apache.cocoon.ProcessingException: Failed to execute
pipeline.:
> > > > >  java.lang.ClassCastException
> > > > >  ...
> > > > >  Caused by: java.lang.ClassCastException
> > > > >   at
> > > > >
> > > > >
> > > > com.lucamorandini.charts.ChartTransformer.endElement(ChartTransfor
> > > > mer.java:6
> > > > > 55)
> > > > >   at
> > > > >
> > > > >
> > > > org.apache.cocoon.components.sax.XMLByteStreamInterpreter.parse(XM
> > > > LByteStrea
> > > > > mInterpreter.java:129)
> > > > >   at
> > > > >
> > > > >
> > > > org.apache.cocoon.components.sax.XMLByteStreamInterpr

Re: Strings to SAX events

2002-11-08 Thread Ludovic de Beaurepaire
Luca

Sorry if it is NOK, but i didn't understand why you want to add XML datas in
the transformer instead of in your pipeline's generator ?

Ludovic
- Original Message -
From: "Luca Morandini" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Friday, November 08, 2002 11:11 AM
Subject: R: Strings to SAX events


> Ludovic,
>
> thanks for your kind answer, but we are not in an XSP page (we're writing
a
> Transformer instead), hence, your suggestion is not truly useful to us.
>
> Best regards,
>
> Luca Morandini
> [EMAIL PROTECTED]
>
> > -Messaggio originale-
> > Da: Ludovic de Beaurepaire [mailto:ludovic.debeaurepaire@;axonie.com]
> > Inviato: venerdì 8 novembre 2002 11.02
> > A: [EMAIL PROTECTED]
> > Oggetto: Re: Strings to SAX events
> >
> >
> > Try in a XSP the following, data is your xml string :
> >
> > 
> > 
> > data
> > 
> > 
> >
> >
> >
> > - Original Message -
> > From: "Luca Morandini" <[EMAIL PROTECTED]>
> > To: <[EMAIL PROTECTED]>
> > Sent: Friday, November 08, 2002 10:55 AM
> > Subject: Strings to SAX events
> >
> >
> > > Folks,
> > >
> > >  We're in the process of writing a Transformer, which, of
> > course, outputs
> > > SAX
> > >  events... but, in the midst of this stream , we need to insert an
> > >  XML element stored in a string.
> > >
> > >  To do this we're groping in the dark trying something like this:
> > >
> > >   JaxpParser respParser = new JaxpParser();
> > >   respParser.parse( new InputSource(new StringReader(str)),
> > >   new EmbeddedXMLPipe(contentHandler));
> > >
> > >  Which fails giving this:
> > >  org.apache.cocoon.ProcessingException: Failed to execute pipeline.:
> > >  java.lang.ClassCastException
> > >  ...
> > >  Caused by: java.lang.ClassCastException
> > >   at
> > >
> > >
> > com.lucamorandini.charts.ChartTransformer.endElement(ChartTransfor
> > mer.java:6
> > > 55)
> > >   at
> > >
> > >
> > org.apache.cocoon.components.sax.XMLByteStreamInterpreter.parse(XM
> > LByteStrea
> > > mInterpreter.java:129)
> > >   at
> > >
> > >
> > org.apache.cocoon.components.sax.XMLByteStreamInterpreter.deserial
> > ize(XMLByt
> > > eStreamInterpreter.java:102)
> > >   at
> > >  org.apache.cocoon.components.pipeline.CachingEventPipeline.process
> > >  (CachingEventPipeline.java:219)
> > >   ... 44 more
> > >
> > >  java.lang.ClassCastException
> > >   at
> > >
> > >
> > com.lucamorandini.charts.ChartTransformer.endElement(ChartTransfor
> > mer.java:6
> > > 55)
> > >   at
> > >
> > >
> > org.apache.cocoon.components.sax.XMLByteStreamInterpreter.parse(XM
> > LByteStrea
> > > mInterpreter.java:129)
> > >   at
> > >
> > >
> > org.apache.cocoon.components.sax.XMLByteStreamInterpreter.deserial
> > ize(XMLByt
> > > eStreamInterpreter.java:102)
> > >   at
> > >  org.apache.cocoon.components.pipeline.CachingEventPipeline.process
> > >  (CachingEventPipeline.java:219)
> > >   at
> > >
> > >
> > org.apache.cocoon.components.pipeline.CachingStreamPipeline.proces
> > s(CachingS
> > > treamPipeline.java:399)
> > >
> > >  The classes we use are:
> > >   org.apache.avalon.excalibur.xml.JaxpParser;
> > >   org.xml.sax.InputSource;
> > >   java.io.StringReader;
> > >   org.apache.cocoon.xml.EmbeddedXMLPipe;
> > >
> > >  And the environment is:
> > >   Solaris 5.8
> > >   JDK 1.4.1_01
> > >   Tomcat 4.1.12-LE-jdk14
> > >   Cocoon 2.0.3
> > >
> > >  May someone please help us ?
> > >
> > >  Thanks in advance,
> > >
> > > Piero De Nicola & Luca Morandini
> > >
> > >
> > >  We are protected from the virus by Norton Antivirus
> > Corporate Edition
> > >
> > > -
> > > Please check that your question  has not already been answered in the
> > > FAQ before posting. <http://xml.apache.org/cocoon/faq/index.html>
> > >
> > > To unsubscribe, e-mail: <[EMAIL PROTECTED]>
> > > For additional commands, e-mail:   &l

Re: Strings to SAX events

2002-11-08 Thread Ludovic de Beaurepaire
Try in a XSP the following, data is your xml string :



data





- Original Message -
From: "Luca Morandini" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Friday, November 08, 2002 10:55 AM
Subject: Strings to SAX events


> Folks,
>
>  We're in the process of writing a Transformer, which, of course, outputs
> SAX
>  events... but, in the midst of this stream , we need to insert an
>  XML element stored in a string.
>
>  To do this we're groping in the dark trying something like this:
>
>   JaxpParser respParser = new JaxpParser();
>   respParser.parse( new InputSource(new StringReader(str)),
>   new EmbeddedXMLPipe(contentHandler));
>
>  Which fails giving this:
>  org.apache.cocoon.ProcessingException: Failed to execute pipeline.:
>  java.lang.ClassCastException
>  ...
>  Caused by: java.lang.ClassCastException
>   at
>
>
com.lucamorandini.charts.ChartTransformer.endElement(ChartTransformer.java:6
> 55)
>   at
>
>
org.apache.cocoon.components.sax.XMLByteStreamInterpreter.parse(XMLByteStrea
> mInterpreter.java:129)
>   at
>
>
org.apache.cocoon.components.sax.XMLByteStreamInterpreter.deserialize(XMLByt
> eStreamInterpreter.java:102)
>   at
>  org.apache.cocoon.components.pipeline.CachingEventPipeline.process
>  (CachingEventPipeline.java:219)
>   ... 44 more
>
>  java.lang.ClassCastException
>   at
>
>
com.lucamorandini.charts.ChartTransformer.endElement(ChartTransformer.java:6
> 55)
>   at
>
>
org.apache.cocoon.components.sax.XMLByteStreamInterpreter.parse(XMLByteStrea
> mInterpreter.java:129)
>   at
>
>
org.apache.cocoon.components.sax.XMLByteStreamInterpreter.deserialize(XMLByt
> eStreamInterpreter.java:102)
>   at
>  org.apache.cocoon.components.pipeline.CachingEventPipeline.process
>  (CachingEventPipeline.java:219)
>   at
>
>
org.apache.cocoon.components.pipeline.CachingStreamPipeline.process(CachingS
> treamPipeline.java:399)
>
>  The classes we use are:
>   org.apache.avalon.excalibur.xml.JaxpParser;
>   org.xml.sax.InputSource;
>   java.io.StringReader;
>   org.apache.cocoon.xml.EmbeddedXMLPipe;
>
>  And the environment is:
>   Solaris 5.8
>   JDK 1.4.1_01
>   Tomcat 4.1.12-LE-jdk14
>   Cocoon 2.0.3
>
>  May someone please help us ?
>
>  Thanks in advance,
>
> Piero De Nicola & Luca Morandini
>
>
>  We are protected from the virus by Norton Antivirus Corporate Edition
>
> -
> Please check that your question  has not already been answered in the
> FAQ before posting. 
>
> To unsubscribe, e-mail: <[EMAIL PROTECTED]>
> For additional commands, e-mail:   <[EMAIL PROTECTED]>
>
>
>


-
Please check that your question  has not already been answered in the
FAQ before posting. 

To unsubscribe, e-mail: <[EMAIL PROTECTED]>
For additional commands, e-mail:   <[EMAIL PROTECTED]>