You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Maciek Kolesnik <mk...@gmail.com> on 2005/10/14 20:11:10 UTC

[betwixt] serialization to DOM rather than string

Submitting this just for consideration ....

This version of BeanWriter will serialize an object graph as DOM as oppose
to string. Might be useful in some cases.



package COM.soundbite.xml.serialization;

import java.io.IOException;
import java.util.Stack;

import org.apache.commons.betwixt.XMLUtils;
import org.apache.commons.betwixt.io.WriteContext;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

/**
* A class that overrides standard behaviour of the betwixt by serializing
into DOM rather than string
*
* @author mkolesnik
* @version $Id: $
*
*/
public class DOMBeanWriter extends BeanWriter {

private Element rootElement;
private Document rootDocument;
private Stack elements = new Stack();

/**
* creates new instance of the writer that will create DOM rooted at
* the element passed in
* @param root root element to append the rest to.
*/
public DOMBeanWriter(Element root) {
rootDocument = root.getOwnerDocument();
rootElement = root;
}

/**
* creates new instance of the writer that will create DOM starting from the
root node passed in
* @param root root element to append the rest to.
*/
public DOMBeanWriter(Document root) {
rootDocument = root;
}

public void start() throws IOException, SAXException {
if (rootElement != null) {
elements.push(rootElement);
} else {
elements.push(rootDocument);
}
}

public void end() throws IOException, SAXException {
elements.pop();
}

protected void startElement(WriteContext context, String uri, String
localName, String qualifiedName, Attributes attr)
throws IOException, SAXException {

Node parent = (Node) elements.peek();
Element currentElement = rootDocument.createElement(localName);

parent.appendChild(currentElement);
elements.push(currentElement);

for (int i = 0; i < attr.getLength(); i++) {
currentElement.setAttribute(attr.getQName(i), XMLUtils.escapeAttributeValue(
attr.getValue(i)));
}
}

protected void endElement(WriteContext context, String uri, String
localName, String qualifiedName)
throws IOException, SAXException {

elements.pop();
}


protected void bodyText(WriteContext context, String text) throws
IOException {

Node currentElement = (Node) elements.peek();

Node newElement = rootDocument.createTextNode(
getMixedContentEncodingStrategy().encode(
text,
context.getCurrentDescriptor()));

currentElement.appendChild(newElement);

}

}

Re: [betwixt] serialization to DOM rather than string

Posted by robert burrell donkin <ro...@blueyonder.co.uk>.
On Tue, 2005-11-01 at 09:12 -0500, Maciek Kolesnik wrote:
> Hi Robert,
> 
> I'm going to do it - 

great :)

> it's going to be be a while tho before i can get to it.

not a problem: most of the committers here day jobs to hold so we can
all empathise. 

- robert


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


Re: [betwixt] serialization to DOM rather than string

Posted by Maciek Kolesnik <mk...@gmail.com>.
Hi Robert,

I'm going to do it - it's going to be be a while tho before i can get to it.

Thanks,
Maciek

On 10/22/05, robert burrell donkin <ro...@blueyonder.co.uk> wrote:
> On Fri, 2005-10-14 at 14:11 -0400, Maciek Kolesnik wrote:
> > Submitting this just for consideration ....
> >
> > This version of BeanWriter will serialize an object graph as DOM as oppose
> > to string. Might be useful in some cases.
>
> hi Maciek
>
> i'd be interested in including this within betwixt. unfortunately, this
> isn't possible as it stands.
>
> contributions need to include the ASL2.0 license boilerplate and should
> be packaged in an appropriate apache package. if you'd like to
> contribute this implementation to betwixt, please open a bug report and
> attach a new version of the file. unit tests and a documentation patch
> would be great.
>
> TIA
>
> - robert
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-dev-help@jakarta.apache.org
>
>

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


Re: [betwixt] serialization to DOM rather than string

Posted by robert burrell donkin <ro...@blueyonder.co.uk>.
On Fri, 2005-10-14 at 14:11 -0400, Maciek Kolesnik wrote:
> Submitting this just for consideration ....
> 
> This version of BeanWriter will serialize an object graph as DOM as oppose
> to string. Might be useful in some cases.

hi Maciek

i'd be interested in including this within betwixt. unfortunately, this
isn't possible as it stands. 

contributions need to include the ASL2.0 license boilerplate and should
be packaged in an appropriate apache package. if you'd like to
contribute this implementation to betwixt, please open a bug report and
attach a new version of the file. unit tests and a documentation patch
would be great.

TIA

- robert


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