You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tuscany.apache.org by ant elder <an...@gmail.com> on 2011/05/17 10:28:43 UTC

Utility to pretty print xml for the model objects

Does anyone know how to get XML from the Tuscany model objects thats nicely
formatted?

As a quick hack I came up with this but it seems like theres probably
something better and I'm not sure that org.apache.xml.serialize.OutputFormat
and org.apache.xml.serialize.XMLSerializer are going to be there by default
in all runtimes:

public class PrettyPrintTestCase {

    @Test
    public void testReadWriteComposite() throws Exception {
        DefaultExtensionPointRegistry extensionPointRegistry = new
DefaultExtensionPointRegistry();
        Composite model = new DefaultAssemblyFactory().createComposite();
        model.setName(new QName("myNs","myName"));
        model.setLocal(true);
        model.setAutowire(false);
        model.getIncludes().add(model);
        String xml = modelToXML(model, true, extensionPointRegistry);
        System.out.println(xml);
    }

    /**
     * Helper method to get the XML string for a model object.
     */
    public static String modelToXML(Base model, boolean pretty,
ExtensionPointRegistry extensionPointRegistry) {
        try {
            StAXHelper stAXHelper =
StAXHelper.getInstance(extensionPointRegistry);
            StAXArtifactProcessorExtensionPoint staxProcessors =
extensionPointRegistry.getExtensionPoint(StAXArtifactProcessorExtensionPoint.class);
            ExtensibleStAXArtifactProcessor staxProcessor = new
ExtensibleStAXArtifactProcessor(staxProcessors, null,
stAXHelper.getOutputFactory());
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            staxProcessor.write(model, bos, new
ProcessorContext(extensionPointRegistry));
            bos.close();

            if (!pretty) {
                return bos.toString();
            }

            DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
            DocumentBuilder parser = factory.newDocumentBuilder();
            Document document = parser.parse(new
ByteArrayInputStream(bos.toByteArray()));

            bos = new ByteArrayOutputStream();
            OutputFormat format = new OutputFormat(document);
            format.setLineWidth(65);
            format.setIndenting(true);
            format.setIndent(2);
            XMLSerializer serializer = new XMLSerializer(bos, format);
            serializer.serialize(document);
            return bos.toString();

        } catch (ContributionWriteException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (ParserConfigurationException e) {
            throw new RuntimeException(e);
        } catch (SAXException e) {
            throw new RuntimeException(e);
        }
    }
}

   ...ant

Re: Utility to pretty print xml for the model objects

Posted by Simon Laws <si...@googlemail.com>.
>
> It sounds like in JDK6 org.w3c.dom.ls.LSSerializer should be used, i've
> added some code to try that here:
> https://svn.apache.org/repos/asf/tuscany/sca-java-2.x/trunk/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/Utils.java
>
> It still doesn't seem perfect code, and it doesn't seem great to have to
> serialize to an xml string then read into a Document then serialize back to
> formatted xml string.
>
>    ...ant
>
>

Not sure without reading the doc in detail but is there a way to
convert the result in the XMLStreamWriter into a StAX source for a
Transformer with indenting set on.

If you already have it working it may be more trouble than it's worth
trying to answer that question.

Regards

Simon

-- 
Apache Tuscany committer: tuscany.apache.org
Co-author of a book about Tuscany and SCA: tuscanyinaction.com

Re: Utility to pretty print xml for the model objects

Posted by ant elder <an...@gmail.com>.
On Tue, May 17, 2011 at 11:15 AM, ant elder <an...@gmail.com> wrote:

>
>
> On Tue, May 17, 2011 at 11:05 AM, Simon Nash <na...@apache.org> wrote:
>
>> ant elder wrote:
>>
>>> Does anyone know how to get XML from the Tuscany model objects thats
>>> nicely formatted?
>>>
>>> As a quick hack I came up with this but it seems like theres probably
>>> something better and I'm not sure that org.apache.xml.serialize.OutputFormat
>>> and org.apache.xml.serialize.XMLSerializer are going to be there by default
>>> in all runtimes:
>>>
>>>  I did something very similar in
>> org.apache.tuscany.sca.itest.builder.TestUtils
>
>
> Ok thanks for the pointed, looks just the same. I'm not sure if we should
> use those org.apache.xml.serialize classes though in 2.x as i don't want to
> require a dependency on xerces. They are there in the JDK i'm using, guess i
> need to research if they're there on other JDKs too.
>
>    ...ant
>
>
It sounds like in JDK6 org.w3c.dom.ls.LSSerializer should be used, i've
added some code to try that here:
https://svn.apache.org/repos/asf/tuscany/sca-java-2.x/trunk/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/Utils.java

It still doesn't seem perfect code, and it doesn't seem great to have to
serialize to an xml string then read into a Document then serialize back to
formatted xml string.

   ...ant

Re: Utility to pretty print xml for the model objects

Posted by ant elder <an...@gmail.com>.
On Tue, May 17, 2011 at 11:05 AM, Simon Nash <na...@apache.org> wrote:

> ant elder wrote:
>
>> Does anyone know how to get XML from the Tuscany model objects thats
>> nicely formatted?
>>
>> As a quick hack I came up with this but it seems like theres probably
>> something better and I'm not sure that org.apache.xml.serialize.OutputFormat
>> and org.apache.xml.serialize.XMLSerializer are going to be there by default
>> in all runtimes:
>>
>>  I did something very similar in
> org.apache.tuscany.sca.itest.builder.TestUtils


Ok thanks for the pointed, looks just the same. I'm not sure if we should
use those org.apache.xml.serialize classes though in 2.x as i don't want to
require a dependency on xerces. They are there in the JDK i'm using, guess i
need to research if they're there on other JDKs too.

   ...ant

Re: Utility to pretty print xml for the model objects

Posted by Simon Nash <na...@apache.org>.
ant elder wrote:
> Does anyone know how to get XML from the Tuscany model objects thats 
> nicely formatted?
> 
> As a quick hack I came up with this but it seems like theres probably 
> something better and I'm not sure that 
> org.apache.xml.serialize.OutputFormat and 
> org.apache.xml.serialize.XMLSerializer are going to be there by default 
> in all runtimes:
> 
I did something very similar in org.apache.tuscany.sca.itest.builder.TestUtils.

   Simon

> public class PrettyPrintTestCase {
> 
>     @Test
>     public void testReadWriteComposite() throws Exception {
>         DefaultExtensionPointRegistry extensionPointRegistry = new 
> DefaultExtensionPointRegistry();
>         Composite model = new DefaultAssemblyFactory().createComposite();
>         model.setName(new QName("myNs","myName"));
>         model.setLocal(true);
>         model.setAutowire(false);
>         model.getIncludes().add(model);
>         String xml = modelToXML(model, true, extensionPointRegistry);
>         System.out.println(xml);
>     }
> 
>     /**
>      * Helper method to get the XML string for a model object.
>      */
>     public static String modelToXML(Base model, boolean pretty, 
> ExtensionPointRegistry extensionPointRegistry) {
>         try {
>             StAXHelper stAXHelper = 
> StAXHelper.getInstance(extensionPointRegistry);
>             StAXArtifactProcessorExtensionPoint staxProcessors = 
> extensionPointRegistry.getExtensionPoint(StAXArtifactProcessorExtensionPoint.class);
>             ExtensibleStAXArtifactProcessor staxProcessor = new 
> ExtensibleStAXArtifactProcessor(staxProcessors, null, 
> stAXHelper.getOutputFactory());
>             ByteArrayOutputStream bos = new ByteArrayOutputStream();
>             staxProcessor.write(model, bos, new 
> ProcessorContext(extensionPointRegistry));
>             bos.close();
> 
>             if (!pretty) {
>                 return bos.toString();
>             }
>            
>             DocumentBuilderFactory factory = 
> DocumentBuilderFactory.newInstance();
>             DocumentBuilder parser = factory.newDocumentBuilder();
>             Document document = parser.parse(new 
> ByteArrayInputStream(bos.toByteArray()));        
>            
>             bos = new ByteArrayOutputStream();
>             OutputFormat format = new OutputFormat(document);
>             format.setLineWidth(65);
>             format.setIndenting(true);
>             format.setIndent(2);
>             XMLSerializer serializer = new XMLSerializer(bos, format);
>             serializer.serialize(document);
>             return bos.toString();
>            
>         } catch (ContributionWriteException e) {
>             throw new RuntimeException(e);
>         } catch (IOException e) {
>             throw new RuntimeException(e);
>         } catch (ParserConfigurationException e) {
>             throw new RuntimeException(e);
>         } catch (SAXException e) {
>             throw new RuntimeException(e);
>         }
>     }
> }
> 
>    ...ant