Skip to content

Commit

Permalink
Replace XMLContentWriter with XMLElementWriter
Browse files Browse the repository at this point in the history
  • Loading branch information
dmlloyd committed Jan 7, 2011
1 parent d3d6d60 commit 8b7a8b4
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/main/java/org/jboss/staxmapper/XMLContentWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
*
* @author <a href="mailto:david.lloyd@redhat.com">David M. Lloyd</a>
*/
@Deprecated
public interface XMLContentWriter {

/**
Expand Down
45 changes: 45 additions & 0 deletions src/main/java/org/jboss/staxmapper/XMLElementWriter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2011, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/

package org.jboss.staxmapper;

import javax.xml.stream.XMLStreamException;

/**
* A writer for XML elements.
*
* @param <T> the type that this writer can operate on
*
* @author <a href="mailto:david.lloyd@redhat.com">David M. Lloyd</a>
*/
public interface XMLElementWriter<T> {

/**
* Write the contents of this item.
*
* @param streamWriter the stream writer
* @param value the value passed in
* @throws XMLStreamException if an exception occurs
*/
void writeContent(XMLExtendedStreamWriter streamWriter, T value) throws XMLStreamException;

}
13 changes: 12 additions & 1 deletion src/main/java/org/jboss/staxmapper/XMLMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,25 @@ public interface XMLMapper {
* @throws XMLStreamException if an error occurs
*/
void parseDocument(Object rootObject, XMLStreamReader reader) throws XMLStreamException;


/**
* Format the element writer's output on to an XML stream writer.
*
* @param writer the element writer
* @param rootObject the root object to send in
* @param streamWriter the stream writer
* @throws XMLStreamException if an exception occurs
*/
void deparseDocument(XMLElementWriter<?> writer, Object rootObject, XMLStreamWriter streamWriter) throws XMLStreamException;

/**
* Format the content writer's output on to an XML stream writer.
*
* @param contentWriter the content writer
* @param streamWriter the stream writer
* @throws XMLStreamException if an exception occurs
*/
@Deprecated
void deparseDocument(XMLContentWriter contentWriter, XMLStreamWriter streamWriter) throws XMLStreamException;

/**
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/org/jboss/staxmapper/XMLMapperImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@ public void parseDocument(Object rootObject, XMLStreamReader reader) throws XMLS
}
}

public void deparseDocument(final XMLElementWriter<?> writer, final Object rootObject, final XMLStreamWriter streamWriter) throws XMLStreamException {
doDeparse(writer, rootObject, new FormattingXMLStreamWriter(streamWriter));
}

@SuppressWarnings( { "unchecked" })
private <T> void doDeparse(XMLElementWriter<?> writer, final T value, final XMLExtendedStreamWriter streamWriter) throws XMLStreamException {
((XMLElementWriter<T>)writer).writeContent(streamWriter, value);
}

/**
* Format the content writer's output on to an XML stream writer.
*
Expand Down
74 changes: 74 additions & 0 deletions src/test/java/org/jboss/staxmapper/SimpleWriteTest2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2011, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/

package org.jboss.staxmapper;

import java.io.StringWriter;

import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;

/**
* @author <a href="mailto:david.lloyd@redhat.com">David M. Lloyd</a>
*/
public final class SimpleWriteTest2 implements XMLElementWriter<Object> {

public static void main(String[] args) throws XMLStreamException {
final StringWriter writer = new StringWriter(512);
final XMLMapper mapper = XMLMapper.Factory.create();
mapper.deparseDocument(new SimpleWriteTest2(), new Object(), XMLOutputFactory.newInstance().createXMLStreamWriter(writer));
System.out.println("Output: " + writer.getBuffer().toString());
}

public void writeContent(final XMLExtendedStreamWriter streamWriter, final Object object) throws XMLStreamException {
streamWriter.writeStartDocument("UTF-8", "1.0");
streamWriter.writeStartElement("hello");
streamWriter.writeNamespace("foo", "http://foo");
streamWriter.writeNamespace("bar", "http://bar");
streamWriter.writeAttribute("test", "this out");
streamWriter.setUnspecifiedElementNamespace("http://foo");
streamWriter.writeStartElement("hello-two");
streamWriter.writeAttribute("test2", "this out2");
streamWriter.writeStartElement("helloblah");
streamWriter.setUnspecifiedElementNamespace("http://bar");
streamWriter.writeAttribute("test3", "this out3");
streamWriter.writeCharacters(" test ");
streamWriter.writeStartElement("helloblah2");
streamWriter.writeAttribute("test4", "this out4");
streamWriter.writeEndElement();
streamWriter.writeEndElement();
streamWriter.writeStartElement("inner");
streamWriter.writeEndElement();
streamWriter.writeEndElement();

streamWriter.setUnspecifiedElementNamespace(null);

streamWriter.writeComment("this is a comment");
streamWriter.writeComment("This is a comment\nthat spans multiple\nlines");
streamWriter.writeEmptyElement("foo");
streamWriter.writeCharacters("Some characters\n");
streamWriter.writeCharacters("Some multi-\nline\ncharacters");
streamWriter.writeEndElement();
streamWriter.writeEndDocument();
streamWriter.close();
}
}

0 comments on commit 8b7a8b4

Please sign in to comment.