cocoon-docs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Apache Wiki <wikidi...@apache.org>
Subject [Cocoon Wiki] Update of "UseCocoonXMLSerializerCode" by AlexanderKlimetschek
Date Thu, 10 May 2007 10:34:36 GMT
Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Cocoon Wiki" for change notification.

The following page has been changed by AlexanderKlimetschek:
http://wiki.apache.org/cocoon/UseCocoonXMLSerializerCode

New page:
#FORMAT Java
    public static InputStream getInputStream(...my params...)
            throws IOException, SourceNotFoundException {

        Serializer serializer = createSerializer();

        ByteArrayOutputStream outputStream = null;
        try {
            outputStream = new ByteArrayOutputStream();
            serializer.setOutputStream(outputStream);

            // fill the serializer with SAX Events (it's a ContentHandler)
            MyBrilliantClass.toSAX(serializer, ...my params...);

            return new ByteArrayInputStream(outputStream.toByteArray());
        } catch (SAXException e) {
            throw new CascadingIOException(e);
        } finally {
            if (null != outputStream)
                outputStream.close();
        }
    }
    
    private static Serializer createSerializer() {
        // we use Cocoons XMLSerializer here which is configurable in terms
        // of the Transformer Implementation to use (some are buggy, so we
        // want to ensure which one is used)
        
        DefaultConfiguration transformerConfig = 
            new DefaultConfiguration("transformer-factory", null);
        transformerConfig.setValue(
                "org.apache.xalan.xsltc.trax.TransformerFactoryImpl");
        
        DefaultConfiguration serializerConfig = 
            new DefaultConfiguration("serializer", null);
        serializerConfig.setAttribute("class", "org.apache.cocoon.serialization.XMLSerializer");
        serializerConfig.addChild(transformerConfig);
        
        
        XMLSerializer serializer = new XMLSerializer();
        serializer.enableLogging(new NullLogger());
        try {
            serializer.configure(serializerConfig);
        } catch (ConfigurationException e) {
            throw new InitializationException(
                    "could not confiure XMLSerializer", e);
        }
        
        return serializer;
    }

Mime
View raw message