use of javax.xml.stream.XMLStreamWriter in project cxf by apache.
the class OutTransformWriterTest method testReadWithComplexTransformationNamespace2.
@Test
public void testReadWithComplexTransformationNamespace2() throws Exception {
Map<String, String> map = new HashMap<>();
map.put("{http://testbeans.com/double}*", "{http://testbeans.com/double/v2}*");
map.put("{http://testbeans.com}*", "{http://testbeans.com/v3}*");
// the namespaces are prefixed in the input
XMLStreamReader reader = TransformTestUtils.createOutTransformedStreamReader("../resources/doubleBeanIn1.xml", map, null, null, null, false, null);
XMLStreamReader reader2 = StaxUtils.createXMLStreamReader(InTransformReader.class.getResourceAsStream("../resources/doubleBean.xml"));
TransformTestUtils.verifyReaders(reader2, reader, true, false);
// the child elements with the default namespace that is declared in the elements
reader = TransformTestUtils.createOutTransformedStreamReader("../resources/doubleBeanIn2.xml", map, null, null, null, false, null);
reader2 = StaxUtils.createXMLStreamReader(InTransformReader.class.getResourceAsStream("../resources/doubleBean.xml"));
TransformTestUtils.verifyReaders(reader2, reader, true, false);
// the child elements with the default namespace that is declared in their parent element
reader = TransformTestUtils.createOutTransformedStreamReader("../resources/doubleBeanIn3.xml", map, null, null, null, false, null);
reader2 = StaxUtils.createXMLStreamReader(InTransformReader.class.getResourceAsStream("../resources/doubleBean.xml"));
TransformTestUtils.verifyReaders(reader2, reader, true, false);
// writing each child separately (as the soap header children are serialized)
ByteArrayOutputStream os = new ByteArrayOutputStream();
XMLStreamWriter writer = new OutTransformWriter(StaxUtils.createXMLStreamWriter(os, StandardCharsets.UTF_8.name()), map, null, null, null, false, null);
boolean nsset = "ns3".equals(writer.getNamespaceContext().getPrefix("http://testbeans.com/double"));
writer.writeStartElement("ns3", "testDoubleBean", "http://testbeans.com/double");
if (!nsset) {
writer.writeNamespace("ns3", "http://testbeans.com/double");
}
nsset = "".equals(writer.getNamespaceContext().getPrefix("http://testbeans.com"));
writer.writeStartElement("", "bean", "http://testbeans.com");
if (!nsset) {
writer.writeNamespace("", "http://testbeans.com");
}
writer.writeEndElement();
nsset = "".equals(writer.getNamespaceContext().getPrefix("http://testbeans.com"));
writer.writeStartElement("", "beanNext", "http://testbeans.com");
if (!nsset) {
writer.writeNamespace("", "http://testbeans.com");
}
writer.writeEndElement();
writer.writeEndElement();
writer.flush();
reader = StaxUtils.createXMLStreamReader(new ByteArrayInputStream(os.toByteArray()));
reader2 = StaxUtils.createXMLStreamReader(InTransformReader.class.getResourceAsStream("../resources/doubleBean.xml"));
TransformTestUtils.verifyReaders(reader2, reader, true, false);
}
use of javax.xml.stream.XMLStreamWriter in project cxf by apache.
the class OutTransformWriterTest method testDefaultNamespace.
@Test
public void testDefaultNamespace() throws Exception {
ByteArrayOutputStream os = new ByteArrayOutputStream();
XMLStreamWriter writer = StaxUtils.createXMLStreamWriter(os, StandardCharsets.UTF_8.name());
Map<String, String> outMap = new HashMap<>();
outMap.put("{http://testbeans.com}*", "{http://testbeans.com/v2}*");
OutTransformWriter transformWriter = new OutTransformWriter(writer, outMap, Collections.<String, String>emptyMap(), Collections.<String>emptyList(), false, "http://testbeans.com/v2");
JAXBContext context = JAXBContext.newInstance(TestBean.class);
Marshaller m = context.createMarshaller();
m.marshal(new TestBean(), transformWriter);
String expected = "<?xml version='1.0' encoding='UTF-8'?>" + "<testBean xmlns=\"http://testbeans.com/v2\"><bean/></testBean>";
assertEquals(expected, os.toString());
}
use of javax.xml.stream.XMLStreamWriter in project cxf by apache.
the class OutTransformWriterTest method testReplaceSimpleElement.
// additional test cases
@Test
public void testReplaceSimpleElement() throws Exception {
InputStream is = new ByteArrayInputStream("<ns:test xmlns:ns=\"http://bar\"><ns:a>1</ns:a></ns:test>".getBytes());
ByteArrayOutputStream os = new ByteArrayOutputStream();
XMLStreamWriter writer = new OutTransformWriter(StaxUtils.createXMLStreamWriter(os, StandardCharsets.UTF_8.name()), null, Collections.singletonMap("{http://bar}a", "{http://bar}a=1 2 3"), null, null, false, null);
StaxUtils.copy(new StreamSource(is), writer);
writer.flush();
XMLStreamReader reader = StaxUtils.createXMLStreamReader(new ByteArrayInputStream(os.toByteArray()));
ByteArrayOutputStream bos = new ByteArrayOutputStream();
StaxUtils.copy(reader, bos);
String value = bos.toString();
assertEquals("<ns:test xmlns:ns=\"http://bar\"><ns:a>1 2 3</ns:a></ns:test>", value);
}
use of javax.xml.stream.XMLStreamWriter in project cxf by apache.
the class AbstractLoggingInterceptor method writePrettyPayload.
protected void writePrettyPayload(StringBuilder builder, StringWriter stringWriter, String contentType) throws Exception {
// Just transform the XML message when the cos has content
StringWriter swriter = new StringWriter();
XMLStreamWriter xwriter = StaxUtils.createXMLStreamWriter(swriter);
xwriter = new PrettyPrintXMLStreamWriter(xwriter, 2);
StaxUtils.copy(new StreamSource(new StringReader(stringWriter.getBuffer().toString())), xwriter);
xwriter.close();
String result = swriter.toString();
if (result.length() < limit || limit == -1) {
builder.append(swriter.toString());
} else {
builder.append(swriter.toString().substring(0, limit));
}
}
use of javax.xml.stream.XMLStreamWriter in project cxf by apache.
the class AbstractOutDatabindingInterceptor method writeParts.
protected void writeParts(Message message, Exchange exchange, BindingOperationInfo operation, MessageContentsList objs, List<MessagePartInfo> parts) {
OutputStream out = message.getContent(OutputStream.class);
XMLStreamWriter origXmlWriter = message.getContent(XMLStreamWriter.class);
Service service = exchange.getService();
XMLStreamWriter xmlWriter = origXmlWriter;
CachingXmlEventWriter cache = null;
// configure endpoint and operation level schema validation
setOperationSchemaValidation(message);
// need to cache the events in case validation fails or buffering is enabled
if (shouldBuffer(message)) {
if (!(xmlWriter instanceof CachingXmlEventWriter)) {
cache = new CachingXmlEventWriter();
try {
cache.setNamespaceContext(origXmlWriter.getNamespaceContext());
} catch (XMLStreamException e) {
// ignorable, will just get extra namespace decls
}
xmlWriter = cache;
}
out = null;
}
if (out != null && writeToOutputStream(message, operation.getBinding(), service) && !MessageUtils.getContextualBoolean(message, DISABLE_OUTPUTSTREAM_OPTIMIZATION, false)) {
if (xmlWriter != null) {
try {
xmlWriter.writeCharacters("");
xmlWriter.flush();
} catch (XMLStreamException e) {
throw new Fault(e);
}
}
DataWriter<OutputStream> osWriter = getDataWriter(message, service, OutputStream.class);
for (MessagePartInfo part : parts) {
if (objs.hasValue(part)) {
Object o = objs.get(part);
osWriter.write(o, part, out);
}
}
} else {
DataWriter<XMLStreamWriter> dataWriter = getDataWriter(message, service, XMLStreamWriter.class);
for (MessagePartInfo part : parts) {
if (objs.hasValue(part)) {
NamespaceContext c = null;
if (!part.isElement() && xmlWriter instanceof CachingXmlEventWriter) {
try {
c = xmlWriter.getNamespaceContext();
xmlWriter.setNamespaceContext(new CachingXmlEventWriter.NSContext(null));
} catch (XMLStreamException e) {
// ignore
}
}
Object o = objs.get(part);
dataWriter.write(o, part, xmlWriter);
if (c != null) {
try {
xmlWriter.setNamespaceContext(c);
} catch (XMLStreamException e) {
// ignore
}
}
}
}
}
if (cache != null) {
try {
for (XMLEvent event : cache.getEvents()) {
StaxUtils.writeEvent(event, origXmlWriter);
}
} catch (XMLStreamException e) {
throw new Fault(e);
}
}
}
Aggregations