use of javax.xml.stream.XMLOutputFactory in project sulky by huxi.
the class PropertyListEncoder method encode.
public void encode(PropertyList obj, OutputStream into) throws IOException {
PropertyListWriter propertyListWriter = new PropertyListWriter();
XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
try {
XMLStreamWriter writer = outputFactory.createXMLStreamWriter(new OutputStreamWriter(into, StandardCharsets.UTF_8));
propertyListWriter.write(writer, obj, true);
writer.close();
} catch (XMLStreamException e) {
throw new IOException("Exception while writing XML!", e);
}
}
use of javax.xml.stream.XMLOutputFactory in project cxf by apache.
the class BadgerFishProvider method writeTo.
public void writeTo(Object obj, Class<?> clazz, Type genericType, Annotation[] annotations, MediaType m, MultivaluedMap<String, Object> headers, OutputStream os) {
try {
if (!new Locale("badgerFishLanguage").equals(requestHeaders.getLanguage())) {
throw new RuntimeException();
}
JAXBContext context = getJAXBContext(obj.getClass());
Marshaller marshaller = context.createMarshaller();
XMLOutputFactory factory = new BadgerFishXMLOutputFactory();
XMLStreamWriter xsw = factory.createXMLStreamWriter(os);
marshaller.marshal(obj, xsw);
xsw.close();
} catch (JAXBException e) {
e.printStackTrace();
} catch (XMLStreamException e) {
e.printStackTrace();
}
}
use of javax.xml.stream.XMLOutputFactory in project cxf by apache.
the class StaxUtils method createXMLStreamWriter.
public static XMLStreamWriter createXMLStreamWriter(Result r) {
if (r instanceof DOMResult) {
// use our own DOM writer to avoid issues with Sun's
// version that doesn't support getNamespaceContext
DOMResult dr = (DOMResult) r;
Node nd = dr.getNode();
if (nd instanceof Document) {
return new W3CDOMStreamWriter((Document) nd);
} else if (nd instanceof Element) {
return new W3CDOMStreamWriter((Element) nd);
} else if (nd instanceof DocumentFragment) {
return new W3CDOMStreamWriter((DocumentFragment) nd);
}
}
XMLOutputFactory factory = getXMLOutputFactory();
try {
return factory.createXMLStreamWriter(r);
} catch (XMLStreamException e) {
throw new RuntimeException("Cant' create XMLStreamWriter", e);
} finally {
returnXMLOutputFactory(factory);
}
}
use of javax.xml.stream.XMLOutputFactory in project cxf by apache.
the class JAXBElementProvider method getStreamWriter.
protected XMLStreamWriter getStreamWriter(Object obj, OutputStream os, MediaType mt) {
XMLStreamWriter writer = null;
MessageContext mc = getContext();
if (mc != null) {
writer = mc.getContent(XMLStreamWriter.class);
if (writer == null) {
XMLOutputFactory factory = (XMLOutputFactory) mc.get(XMLOutputFactory.class.getName());
if (factory != null) {
try {
writer = factory.createXMLStreamWriter(os);
} catch (XMLStreamException e) {
throw ExceptionUtils.toInternalServerErrorException(new RuntimeException("Cant' create XMLStreamWriter", e), null);
}
}
}
if (writer == null && getEnableStreaming()) {
writer = StaxUtils.createXMLStreamWriter(os);
}
}
if (writer == null && os == null) {
writer = getStreamHandlerFromCurrentMessage(XMLStreamWriter.class);
}
return createTransformWriterIfNeeded(writer, os, true);
}
use of javax.xml.stream.XMLOutputFactory in project cxf by apache.
the class JAXBEncoderDecoderTest method testCustomNamespaces.
@Test
public void testCustomNamespaces() throws Exception {
Map<String, String> mapper = new HashMap<>();
mapper.put("http://apache.org/hello_world_soap_http/types", "Omnia");
mapper.put("http://cxf.apache.org/jaxb_form", "Gallia");
ObjectWithQualifiedElementElement testObject = new ObjectWithQualifiedElementElement();
testObject.setString1("twine");
testObject.setString2("cord");
QName elName = new QName(wrapperAnnotation.targetNamespace(), wrapperAnnotation.localName());
MessagePartInfo part = new MessagePartInfo(elName, null);
part.setElement(true);
part.setElementQName(elName);
StringWriter stringWriter = new StringWriter();
XMLOutputFactory opFactory = XMLOutputFactory.newInstance();
opFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.TRUE);
XMLEventWriter writer = opFactory.createXMLEventWriter(stringWriter);
Marshaller m = context.createMarshaller();
JAXBUtils.setNamespaceMapper(mapper, m);
JAXBEncoderDecoder.marshall(m, testObject, part, writer);
writer.flush();
writer.close();
String xmlResult = stringWriter.toString();
// the following is a bit of a crock, but, to tell the truth, this test case most exists
// so that it could be examined inside the debugger to see how JAXB works.
assertTrue(xmlResult.contains("Gallia:string2"));
}
Aggregations