commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Diogo Quintela \(EF\)" <diogo.quint...@ef.pt>
Subject [jelly] [bug in ElementTag ?] WAS: [jelly] Generating xml nodes with attributes JELLY-213
Date Fri, 08 Jul 2005 11:38:53 GMT
Hello list,
	In the sequence of my patches in JELLY-213, I found some problems
that I'll describe bellow.

	Diogo Quintela


Jelly Script
############ (TestNs.jelly)
<?xml version="1.0" encoding="UTF-8"?>
<j:jelly xmlns:j="jelly:core" xmlns:x="jelly:xml">
	<one>
	    <x:element URI="http://apache/testNS" name="two">
	        <x:attribute URI="http://apache/anotherNS" 
				   name="test:abc2"
				   trim="true">
			testValue
	        </x:attribute>
	        <three>hi</three>
	    </x:element>
	</one>
</j:jelly>
##############
NOTES: Element "one" has no default namespace because j:jelly does not
define one, so nor does element "three".
Element "two" should be declared to be in namespace "http://apache/testNS"

The namespace URI set for ElementTag don't get reset if using a no
dom4j-XMLOutput, and exists before patch 1
(http://issues.apache.org/jira/browse/JELLY-213, jelly-xml.patch)
because ElementTag already allows to set a namespace.
See bellow "SAMPLE OUTPUTS"


Java Test
######### (TestNs.java)
import java.io.File; import java.io.IOException; import
java.io.OutputStreamWriter; import java.io.StringWriter; import
java.io.Writer; import java.net.URL; import
org.apache.commons.jelly.JellyContext; import
org.apache.commons.jelly.JellyException; import
org.apache.commons.jelly.XMLOutput; import org.apache.xml.serialize.Method;
import org.apache.xml.serialize.OutputFormat; import
org.apache.xml.serialize.XMLSerializer; import
org.dom4j.io.SAXContentHandler; import org.dom4j.io.SAXReader; import
org.dom4j.io.XMLWriter; import org.xml.sax.SAXException;

public class TestNs {
    public TestNs() {

    }

    public void serializeUsingSimpleXMLOutput(
		String fileName, 
		Writer writer) 
    throws JellyException, IOException {
        JellyContext context = new JellyContext();

        // cature the output
        // XMLOutput output = XMLOutput.createXMLOutput(writer);
        org.dom4j.io.OutputFormat outputFormat = 
			new org.dom4j.io.OutputFormat();
        outputFormat.setSuppressDeclaration(true);
        outputFormat.setNewlines(true);
        outputFormat.setIndent(true);
        outputFormat.setIndentSize(4);
        XMLWriter xmlWriter = new XMLWriter(writer, outputFormat);
        XMLOutput output = new XMLOutput(xmlWriter);

        URL fUrl = getClass().getResource(fileName);
        context.runScript( fUrl, output );
        //xmlWriter.close();
        output.flush();
        writer.write('\n');
        writer.flush();

    }

    public void serializeUsingSAXContentHandler(
		String fileName, 
		Writer writer) 
    throws IOException, JellyException, SAXException {
        org.dom4j.io.OutputFormat outputFormat = 
			new org.dom4j.io.OutputFormat();
        outputFormat.setSuppressDeclaration(true);
        outputFormat.setNewlines(true);
        outputFormat.setIndent(true);
        outputFormat.setIndentSize(4);

        XMLWriter xmlWriter = new XMLWriter(writer, outputFormat);
        xmlWriter.setEscapeText(false);

        SAXContentHandler saxHandler = new SAXContentHandler();
        XMLOutput output = new XMLOutput(saxHandler);

        // now run a script using a URL
        JellyContext context = new JellyContext();
        URL fUrl = getClass().getResource(fileName);

        output.startDocument();
        context.runScript(fUrl, output);
        output.endDocument();
        xmlWriter.write(saxHandler.getDocument());
        xmlWriter.flush();
    }

    public static void main(String[] args) {
        try {
            TestNs t = new TestNs();
            Writer someWriter = new OutputStreamWriter(System.out);

            System.out.println("------");
            t.serializeUsingSAXContentHandler("TestNs.jelly", someWriter);
            System.out.println("------");
            t.serializeUsingSimpleXMLOutput("TestNs.jelly", someWriter);
            System.out.println("------");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
#########
NOTES:
serializeUsingSAXContentHandler uses dom4j facilities
serializeUsingSimpleXMLOutput does not


SAMPLE OUTPUTS
##############

With PATCH 1 (http://issues.apache.org/jira/browse/JELLY-213,
jelly-xml.patch).
As Paul said, imcomplete because of lack of use of
startPrefixMapping/endPrefixMapping

### OUTPUT ###
------
<one>
    <two xmlns="http://apache/testNS" 
	   xmlns:test="http://apache/anotherNS" 
	   test:abc2="testValue">
        <three xmlns="">hi</three>
    </two>
</one>
------
<one>
    <two test:abc2="testValue">
        <three>hi</three>
    </two>
</one>
------
##############

With PATCH 2 (http://issues.apache.org/jira/browse/JELLY-213,
jelly-xml-updated.patch).
startPrefixMapping/endPrefixMapping as possible, presenting better results.
Problem. XMLOutput doesn't have state, JellyPipeline doesn't allow to when
"two" element is used, when know what was
the previous namespace to reset it's value as it's done in trunk 1 bellow
for element "three"

### OUTPUT ###
------
<one>
    <two xmlns="http://apache/testNS" 
	   xmlns:test="http://apache/anotherNS" 
	   test:abc2="testValue">
        <three xmlns="">hi</three>
    </two>
</one>
------
<one>
    <two xmlns:test="http://apache/anotherNS" 
 	   xmlns="http://apache/testNS" 
	   test:abc2="testValue">
        <three>hi</three>
    </two>
</one>
------
##############

SAXContentHandler manage state of parsing so it's able handle well with the
situation,
as seen in trunk 1 of above output.

Discussion:
Should we discard PATCH 2 and let the client XMLOutput responsible for this
handling ?
If soo, for sake of XMLOutput factory methods coeherence, shall we modify
XMLOutput.createXMLOutput(Writer[,boolean]) and
XMLOutput.createXMLOutput(OutputStream[,boolean])
to follow the aproach given in "serializeUsingSAXContentHandler method in
java sample" ?


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Mime
View raw message