RESOLVED - issue BATCH-1430: StaxEventItemWriter: Declare additional namespaces at the top-level element

This commit is contained in:
dsyer
2010-01-05 14:26:18 +00:00
parent d0f94cb0ee
commit 0d8cb85c10
17 changed files with 2400 additions and 36 deletions

View File

@@ -232,15 +232,17 @@ public class StaxEventItemWriter<T> extends ExecutionContextUserSupport implemen
/**
* Set the tag name of the root element. If not set, default name is used
* ("root"). Namespace URI and prefix can also be set optionally using the
* ("root"). Namespace URI and prefix can also be set optionally using the
* notation:
*
* <pre>
* {uri}prefix:root
* </pre>
*
* The prefix is optional (defaults to empty), but if it is specified then the
* uri must be provided.
* The prefix is optional (defaults to empty), but if it is specified then
* the uri must be provided. In addition you might want to declare other
* namespaces using the {@link #setRootElementAttributes(Map) root
* attributes}.
*
* @param rootTagName the tag name to be used for the root element
*/
@@ -276,7 +278,8 @@ public class StaxEventItemWriter<T> extends ExecutionContextUserSupport implemen
}
/**
* Set the root element attributes to be written.
* Set the root element attributes to be written. If any of the key names
* begin with "xmlns:" then they are treated as namespace declarations.
*
* @param rootElementAttributes attributes of the root element
*/
@@ -307,8 +310,7 @@ public class StaxEventItemWriter<T> extends ExecutionContextUserSupport implemen
if (rootTagName.contains("{")) {
rootTagNamespace = rootTagName.replaceAll("\\{(.*)\\}.*", "$1");
rootTagName = rootTagName.replaceAll("\\{.*\\}(.*)", "$1");
if (rootTagName.contains(":"))
{
if (rootTagName.contains(":")) {
rootTagNamespacePrefix = rootTagName.replaceAll("(.*):.*", "$1");
rootTagName = rootTagName.replaceAll(".*:(.*)", "$1");
}
@@ -430,14 +432,29 @@ public class StaxEventItemWriter<T> extends ExecutionContextUserSupport implemen
// write root tag
writer.add(factory.createStartElement(getRootTagNamespacePrefix(), getRootTagNamespace(), getRootTagName()));
if (StringUtils.hasText(getRootTagNamespace())) {
writer.add(factory.createNamespace(getRootTagNamespacePrefix(), getRootTagNamespace()));
if (StringUtils.hasText(getRootTagNamespacePrefix())) {
writer.add(factory.createNamespace(getRootTagNamespacePrefix(), getRootTagNamespace()));
}
else {
writer.add(factory.createNamespace(getRootTagNamespace()));
}
}
// write root tag attributes
if (!CollectionUtils.isEmpty(getRootElementAttributes())) {
for (Map.Entry<String, String> entry : getRootElementAttributes().entrySet()) {
writer.add(factory.createAttribute(entry.getKey(), entry.getValue()));
String key = entry.getKey();
if (key.startsWith("xmlns")) {
String prefix = "";
if (key.contains(":")) {
prefix = key.substring(key.indexOf(":") + 1);
}
writer.add(factory.createNamespace(prefix, entry.getValue()));
}
else {
writer.add(factory.createAttribute(key, entry.getValue()));
}
}
}

View File

@@ -62,9 +62,19 @@ public class StaxEventItemWriterTests {
private static final String TEST_STRING = "<" + ClassUtils.getShortName(StaxEventItemWriter.class)
+ "-testString/>";
private static final String NS_TEST_STRING = "<ns:" + ClassUtils.getShortName(StaxEventItemWriter.class)
+ "-testString/>";
private static final String FOO_TEST_STRING = "<foo:" + ClassUtils.getShortName(StaxEventItemWriter.class)
+ "-testString/>";
private SimpleMarshaller marshaller;
@Before
public void setUp() throws Exception {
resource = new FileSystemResource(File.createTempFile("StaxEventWriterOutputSourceTests", ".xml"));
File directory = new File("target/data");
directory.mkdirs();
resource = new FileSystemResource(File.createTempFile("StaxEventWriterOutputSourceTests", ".xml", directory));
writer = createItemWriter();
executionContext = new ExecutionContext();
}
@@ -287,29 +297,68 @@ public class StaxEventItemWriterTests {
public void testWriteRootTagWithNamespaceAndPrefix() throws Exception {
writer.setRootTagName("{http://www.springframework.org/test}ns:root");
writer.afterPropertiesSet();
marshaller.setNamespace(writer.getRootTagNamespace());
marshaller.setNamespacePrefix(writer.getRootTagNamespacePrefix());
writer.open(executionContext);
writer.write(items);
writer.close();
String content = getOutputFileContent();
assertTrue("Wrong content: " + content, content
.contains(("<ns:root xmlns:ns=\"http://www.springframework.org/test\">")));
assertTrue("Wrong content: " + content, content.contains(TEST_STRING));
assertTrue("Wrong content: " + content, content.contains(NS_TEST_STRING));
assertTrue("Wrong content: " + content, content.contains(("</ns:root>")));
assertTrue("Wrong content: " + content, content.contains(("<ns:root")));
}
/**
* Item is written to the output file with additional namespaces and prefix.
*/
@Test
public void testWriteRootTagWithAdditionalNamespace() throws Exception {
writer.setRootTagName("{http://www.springframework.org/test}ns:root");
marshaller.setNamespace("urn:org.test.foo");
marshaller.setNamespacePrefix("foo");
writer.setRootElementAttributes(Collections.singletonMap("xmlns:foo", "urn:org.test.foo"));
writer.afterPropertiesSet();
writer.open(executionContext);
writer.write(items);
writer.close();
String content = getOutputFileContent();
System.err.println(content);
assertTrue("Wrong content: " + content, content
.contains(("<ns:root xmlns:ns=\"http://www.springframework.org/test\" "
+ "xmlns:foo=\"urn:org.test.foo\">")));
assertTrue("Wrong content: " + content, content.contains(FOO_TEST_STRING));
assertTrue("Wrong content: " + content, content.contains(("</ns:root>")));
assertTrue("Wrong content: " + content, content.contains(("<ns:root")));
}
/**
* Writes object's toString representation as XML comment.
*/
private static class SimpleMarshaller implements Marshaller {
private String namespacePrefix = "";
private String namespace = "";
public void setNamespace(String namespace) {
this.namespace = namespace;
}
public void setNamespacePrefix(String namespacePrefix) {
this.namespacePrefix = namespacePrefix;
}
public void marshal(Object graph, Result result) throws XmlMappingException, IOException {
Assert.isInstanceOf(StaxResult.class, result);
StaxResult staxResult = (StaxResult) result;
try {
staxResult.getXMLEventWriter().add(
XMLEventFactory.newInstance().createStartElement("", "", graph.toString()));
XMLEventFactory.newInstance().createStartElement(namespacePrefix, namespace, graph.toString()));
staxResult.getXMLEventWriter().add(
XMLEventFactory.newInstance().createEndElement("", "", graph.toString()));
XMLEventFactory.newInstance().createEndElement(namespacePrefix, namespace, graph.toString()));
}
catch (XMLStreamException e) {
throw new RuntimeException("Exception while writing to output file", e);
@@ -338,7 +387,7 @@ public class StaxEventItemWriterTests {
StaxEventItemWriter<Object> source = new StaxEventItemWriter<Object>();
source.setResource(resource);
Marshaller marshaller = new SimpleMarshaller();
marshaller = new SimpleMarshaller();
source.setMarshaller(marshaller);
source.setEncoding("UTF-8");
@@ -351,5 +400,5 @@ public class StaxEventItemWriterTests {
return source;
}
}