use of javax.xml.stream.XMLOutputFactory in project cxf by apache.
the class JAXBEncoderDecoderTest method testMarshallWithoutQNameInfo.
@Test
public void testMarshallWithoutQNameInfo() throws Exception {
GreetMe obj = new GreetMe();
obj.setRequestType("Hello");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
XMLOutputFactory opFactory = XMLOutputFactory.newInstance();
opFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.TRUE);
XMLEventWriter writer = opFactory.createXMLEventWriter(baos);
// STARTDOCUMENT/ENDDOCUMENT is not required
// writer.add(eFactory.createStartDocument("utf-8", "1.0"));
JAXBEncoderDecoder.marshall(context.createMarshaller(), obj, null, writer);
// writer.add(eFactory.createEndDocument());
writer.flush();
writer.close();
// System.out.println(baos.toString());
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
XMLInputFactory ipFactory = XMLInputFactory.newInstance();
XMLEventReader reader = ipFactory.createXMLEventReader(bais);
Unmarshaller um = context.createUnmarshaller();
Object val = um.unmarshal(reader, GreetMe.class);
assertTrue(val instanceof JAXBElement);
val = ((JAXBElement<?>) val).getValue();
assertTrue(val instanceof GreetMe);
assertEquals(obj.getRequestType(), ((GreetMe) val).getRequestType());
}
use of javax.xml.stream.XMLOutputFactory in project cxf by apache.
the class XMLStreamDataWriterTest method setUp.
@Before
public void setUp() throws Exception {
baos = new ByteArrayOutputStream();
XMLOutputFactory factory = XMLOutputFactory.newInstance();
streamWriter = factory.createXMLStreamWriter(baos);
assertNotNull(streamWriter);
inFactory = XMLInputFactory.newInstance();
}
use of javax.xml.stream.XMLOutputFactory in project Payara by payara.
the class ConfigPersistence method test.
@Test
public void test() throws TransactionFailure {
final DomDocument document = getDocument(getHabitat());
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos.reset();
final ConfigurationPersistence testPersistence = new ConfigurationPersistence() {
public void save(DomDocument doc) throws IOException, XMLStreamException {
XMLOutputFactory factory = XMLOutputFactory.newInstance();
XMLStreamWriter writer = factory.createXMLStreamWriter(baos);
doc.writeTo(new IndentingXMLStreamWriter(writer));
writer.close();
}
};
TransactionListener testListener = new TransactionListener() {
public void transactionCommited(List<PropertyChangeEvent> changes) {
try {
testPersistence.save(document);
} catch (IOException e) {
// To change body of catch statement use File | Settings | File Templates.
e.printStackTrace();
} catch (XMLStreamException e) {
// To change body of catch statement use File | Settings | File Templates.
e.printStackTrace();
}
}
public void unprocessedTransactedEvents(List<UnprocessedChangeEvents> changes) {
}
};
Transactions transactions = getHabitat().getService(Transactions.class);
try {
transactions.addTransactionsListener(testListener);
doTest();
} catch (TransactionFailure f) {
f.printStackTrace();
throw f;
} finally {
transactions.waitForDrain();
transactions.removeTransactionsListener(testListener);
}
// now check if we persisted correctly...
final String resultingXml = baos.toString();
logger.fine(resultingXml);
assertTrue("assertResult from " + getClass().getName() + " was false from " + resultingXml, assertResult(resultingXml));
}
use of javax.xml.stream.XMLOutputFactory in project Payara by payara.
the class DeepCopyTest method save.
public OutputStream save(DomDocument doc) throws IOException, XMLStreamException {
final ByteArrayOutputStream outStream = new ByteArrayOutputStream();
outStream.reset();
XMLOutputFactory factory = XMLOutputFactory.newInstance();
XMLStreamWriter writer = factory.createXMLStreamWriter(outStream);
doc.writeTo(new IndentingXMLStreamWriter(writer));
writer.close();
return outStream;
}
use of javax.xml.stream.XMLOutputFactory in project Payara by payara.
the class ConfigModularityUtils method serializeConfigBean.
public String serializeConfigBean(ConfigBeanProxy configBean) {
if (configBean == null)
return null;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
XMLOutputFactory xmlFactory = XMLOutputFactory.newInstance();
XMLStreamWriter writer = null;
IndentingXMLStreamWriter indentingXMLStreamWriter = null;
String s = null;
try {
writer = xmlFactory.createXMLStreamWriter(new BufferedOutputStream(bos));
indentingXMLStreamWriter = new IndentingXMLStreamWriter(writer);
Dom configBeanDom = Dom.unwrap(configBean);
configBeanDom.writeTo(configBeanDom.model.getTagName(), indentingXMLStreamWriter);
indentingXMLStreamWriter.flush();
s = bos.toString();
} catch (XMLStreamException e) {
if (LOG.isLoggable(Level.FINE)) {
LOG.log(Level.FINE, "Cannot serialize the configbean: " + configBean.toString(), e);
}
return null;
} finally {
try {
if (bos != null)
bos.close();
if (writer != null)
writer.close();
if (indentingXMLStreamWriter != null)
indentingXMLStreamWriter.close();
} catch (IOException e) {
if (LOG.isLoggable(Level.FINE)) {
LOG.log(Level.FINE, "Cannot serialize the configbean: " + configBean.toString(), e);
}
} catch (XMLStreamException e) {
if (LOG.isLoggable(Level.FINE)) {
LOG.log(Level.FINE, "Cannot serialize the configbean: " + configBean.toString(), e);
}
}
}
return s;
}
Aggregations