1     public InputStream getInputStream(...my params...)
   2             throws IOException, SourceNotFoundException {
   3 
   4         Serializer serializer = createSerializer();
   5 
   6         ByteArrayOutputStream outputStream = null;
   7         try {
   8             outputStream = new ByteArrayOutputStream();
   9             serializer.setOutputStream(outputStream);
  10 
  11             // fill the serializer with SAX Events (it's a ContentHandler)
  12             MyBrilliantClass.toSAX(serializer, ...my params...);
  13 
  14             return new ByteArrayInputStream(outputStream.toByteArray());
  15         } catch (SAXException e) {
  16             throw new CascadingIOException(e);
  17         } finally {
  18             if (null != outputStream)
  19                 outputStream.close();
  20         }
  21     }
  22     
  23     private Serializer createSerializer() {
  24         // we use Cocoons XMLSerializer here which is configurable in terms
  25         // of the Transformer Implementation to use (some are buggy, so we
  26         // want to ensure which one is used)
  27         
  28         DefaultConfiguration transformerConfig = 
  29             new DefaultConfiguration("transformer-factory", null);
  30         transformerConfig.setValue(
  31                 "org.apache.xalan.xsltc.trax.TransformerFactoryImpl");
  32         
  33         DefaultConfiguration serializerConfig = 
  34             new DefaultConfiguration("serializer", null);
  35 
  36         // here we use cocoon's serializer
  37         serializerConfig.setAttribute("class",
  38             "org.apache.cocoon.serialization.XMLSerializer");
  39 
  40         serializerConfig.addChild(transformerConfig);        
  41         
  42         XMLSerializer serializer = new XMLSerializer();
  43         serializer.enableLogging(new NullLogger());
  44         try {
  45             serializer.configure(serializerConfig);
  46         } catch (ConfigurationException e) {
  47             throw new InitializationException(
  48                     "could not confiure XMLSerializer", e);
  49         }
  50         
  51         return serializer;
  52     }

UseCocoonXMLSerializerCode (last edited 2009-09-20 23:42:24 by localhost)