use of javax.xml.stream.XMLStreamWriter in project cxf by apache.
the class StaxUtilsTest method testDefaultPrefixInRootElementWithJDKInternalCopyTransformer.
@Test
public void testDefaultPrefixInRootElementWithJDKInternalCopyTransformer() throws Exception {
TransformerFactory trf = null;
try {
trf = TransformerFactory.newInstance("com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl", null);
trf.setFeature(javax.xml.XMLConstants.FEATURE_SECURE_PROCESSING, true);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
String xml = "<root xmlns=\"urn:org.apache.cxf:test\">Text</root>";
StringReader stringReader = new StringReader(xml);
StreamSource source = new StreamSource(stringReader);
XMLStreamReader reader = StaxUtils.createXMLStreamReader(source);
XMLStreamWriter writer = StaxUtils.createXMLStreamWriter(baos);
StaxSource staxSource = new StaxSource(reader);
Document doc = StaxUtils.read(getTestStream("./resources/copy.xsl"));
Transformer transformer = trf.newTransformer(new DOMSource(doc));
// System.out.println("Used transformer: " + transformer.getClass().getName());
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.transform(staxSource, new StreamResult(baos));
writer.flush();
baos.flush();
assertThat(new String(baos.toByteArray()), equalTo(xml));
} catch (Throwable throwable) {
// ignore on non Sun/Oracle JDK
return;
}
}
use of javax.xml.stream.XMLStreamWriter in project cxf by apache.
the class StaxUtilsTest method testCXF3193.
@Test
public void testCXF3193() throws Exception {
String testString = "<a:elem1 xmlns:a=\"test\" xmlns:b=\"test\" a:attr1=\"value\"/>";
CachingXmlEventWriter writer = new CachingXmlEventWriter();
StaxUtils.copy(StaxUtils.createXMLStreamReader(new StringReader(testString)), writer);
StringWriter swriter = new StringWriter();
XMLStreamWriter xwriter = StaxUtils.createXMLStreamWriter(swriter);
for (XMLEvent event : writer.getEvents()) {
StaxUtils.writeEvent(event, xwriter);
}
xwriter.flush();
String s = swriter.toString();
int idx = s.indexOf("xmlns:a");
idx = s.indexOf("xmlns:a", idx + 1);
assertEquals(-1, idx);
}
use of javax.xml.stream.XMLStreamWriter in project cxf by apache.
the class StaxUtilsTest method testCopy.
@Test
public void testCopy() throws Exception {
// do the stream copying
String soapMessage = "./resources/headerSoapReq.xml";
ByteArrayOutputStream baos = new ByteArrayOutputStream();
XMLStreamReader reader = StaxUtils.createXMLStreamReader(getTestStream(soapMessage));
XMLStreamWriter writer = StaxUtils.createXMLStreamWriter(baos);
StaxUtils.copy(reader, writer);
writer.flush();
baos.flush();
// write output to a string
String output = baos.toString();
baos.close();
// re-read the input xml doc to a string
InputStreamReader inputStreamReader = new InputStreamReader(getTestStream(soapMessage));
StringWriter stringWriter = new StringWriter();
char[] buffer = new char[4096];
int n = 0;
n = inputStreamReader.read(buffer);
while (n > 0) {
stringWriter.write(buffer, 0, n);
n = inputStreamReader.read(buffer);
}
String input = stringWriter.toString();
stringWriter.close();
// seach for the first begin of "<soap:Envelope" to escape the apache licenses header
int beginIndex = input.indexOf("<soap:Envelope");
input = input.substring(beginIndex);
beginIndex = output.indexOf("<soap:Envelope");
output = output.substring(beginIndex);
output = output.replaceAll("\r\n", "\n");
input = input.replaceAll("\r\n", "\n");
// compare the input and output string
assertEquals(input, output);
}
use of javax.xml.stream.XMLStreamWriter in project cxf by apache.
the class StaxUtilsTest method testDefaultPrefixInRootElementWithXalanCopyTransformer.
@Test
public void testDefaultPrefixInRootElementWithXalanCopyTransformer() throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
String xml = "<root xmlns=\"urn:org.apache.cxf:test\">Text</root>";
StringReader stringReader = new StringReader(xml);
// StreamSource source = new StreamSource(stringReader);
XMLStreamReader reader = StaxUtils.createXMLStreamReader(stringReader);
XMLStreamWriter writer = StaxUtils.createXMLStreamWriter(baos);
StaxSource staxSource = new StaxSource(reader);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
transformerFactory.setFeature(javax.xml.XMLConstants.FEATURE_SECURE_PROCESSING, true);
Document doc = StaxUtils.read(getTestStream("./resources/copy.xsl"));
Transformer transformer = transformerFactory.newTransformer(new DOMSource(doc));
// System.out.println("Used transformer: " + transformer.getClass().getName());
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.transform(staxSource, new StreamResult(baos));
writer.flush();
baos.flush();
assertThat(new String(baos.toByteArray()), equalTo(xml));
}
use of javax.xml.stream.XMLStreamWriter in project cxf by apache.
the class StaxUtilsTest method testNonNamespaceAwareParser.
@Test
public void testNonNamespaceAwareParser() throws Exception {
String xml = "<blah xmlns=\"http://blah.org/\" xmlns:snarf=\"http://snarf.org\">" + "<foo snarf:blop=\"blop\">foo</foo></blah>";
StringReader reader = new StringReader(xml);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(false);
dbf.setValidating(false);
Document doc = dbf.newDocumentBuilder().parse(new InputSource(reader));
Source source = new DOMSource(doc);
dbf.setNamespaceAware(true);
reader = new StringReader(xml);
Document docNs = dbf.newDocumentBuilder().parse(new InputSource(reader));
Source sourceNs = new DOMSource(docNs);
XMLStreamReader sreader = StaxUtils.createXMLStreamReader(source);
StringWriter sw = new StringWriter();
XMLStreamWriter swriter = StaxUtils.createXMLStreamWriter(sw);
// should not throw an exception
StaxUtils.copy(sreader, swriter);
swriter.flush();
swriter.close();
String output = sw.toString();
assertTrue(output.contains("blah"));
assertTrue(output.contains("foo"));
assertTrue(output.contains("snarf"));
assertTrue(output.contains("blop"));
sreader = StaxUtils.createXMLStreamReader(sourceNs);
sw = new StringWriter();
swriter = StaxUtils.createXMLStreamWriter(sw);
// should not throw an exception
StaxUtils.copy(sreader, swriter);
swriter.flush();
swriter.close();
output = sw.toString();
assertTrue(output.contains("blah"));
assertTrue(output.contains("foo"));
assertTrue(output.contains("snarf"));
assertTrue(output.contains("blop"));
sreader = StaxUtils.createXMLStreamReader(source);
ByteArrayOutputStream bout = new ByteArrayOutputStream();
swriter = StaxUtils.createXMLStreamWriter(bout);
StaxUtils.copy(sreader, swriter);
swriter.flush();
swriter.close();
output = bout.toString();
assertTrue(output.contains("blah"));
assertTrue(output.contains("foo"));
assertTrue(output.contains("snarf"));
assertTrue(output.contains("blop"));
}
Aggregations