use of org.apache.cxf.staxutils.PrettyPrintXMLStreamWriter in project cxf by apache.
the class ImportRepairTest method dumpSchema.
private void dumpSchema(Document document) throws Exception {
if (!dumpSchemas) {
return;
}
XMLStreamWriter xwriter = StaxUtils.createXMLStreamWriter(System.err);
xwriter = new PrettyPrintXMLStreamWriter(xwriter, 2);
StaxUtils.copy(new DOMSource(document), xwriter);
xwriter.close();
}
use of org.apache.cxf.staxutils.PrettyPrintXMLStreamWriter 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 org.apache.cxf.staxutils.PrettyPrintXMLStreamWriter in project cxf by apache.
the class PrettyLoggingFilter method getPrettyMessage.
/**
* Pretty-print {@linkplain LogEvent} XML payload.
*
* @param event the log event containing an XML payload which is to be pretty-printed.
* @return pretty-printed XML or original payload in case of an unexpected exception.
*/
private String getPrettyMessage(LogEvent event) {
String payload = event.getPayload();
StringWriter swriter = new StringWriter(estimatePrettySize(payload));
XMLStreamWriter xwriter = new PrettyPrintXMLStreamWriter(StaxUtils.createXMLStreamWriter(swriter), 2);
XMLStreamReader xreader = StaxUtils.createXMLStreamReader(new StringReader(payload));
try {
StaxUtils.copy(xreader, xwriter);
xwriter.flush();
} catch (XMLStreamException xse) {
if (!event.isTruncated()) {
LOG.debug("Error while pretty printing cxf message, returning raw message.", xse);
return payload;
}
// Expected behavior for truncated payloads - keep what is already written.
// This might effectively result in additional truncation,
// as the truncated XML document might result in partially parsed XML nodes,
// for example an open start tag. As long as a truncated payload is not
// mistaken for a non-truncated payload, we're good.
flush(xwriter);
return swriter.toString();
} finally {
close(xwriter);
close(xreader);
}
return swriter.toString();
}
use of org.apache.cxf.staxutils.PrettyPrintXMLStreamWriter in project cxf by apache.
the class SchemaSerializer method writeXml.
private void writeXml(Node n, PrintWriter pw) throws XMLStreamException {
XMLStreamWriter writer = StaxUtils.createXMLStreamWriter(pw);
writer = new PrettyPrintXMLStreamWriter(writer, 2);
StaxUtils.copy(new DOMSource(n), writer);
writer.close();
}
use of org.apache.cxf.staxutils.PrettyPrintXMLStreamWriter in project cxf by apache.
the class JAXBExtensionHelper method marshall.
/* (non-Javadoc)
* @see javax.wsdl.extensions.ExtensionSerializer#marshall(java.lang.Class,
* javax.xml.namespace.QName, javax.wsdl.extensions.ExtensibilityElement,
* java.io.PrintWriter, javax.wsdl.Definition, javax.wsdl.extensions.ExtensionRegistry)
*/
public void marshall(@SuppressWarnings("rawtypes") Class parent, QName qname, ExtensibilityElement obj, PrintWriter pw, final Definition wsdl, ExtensionRegistry registry) throws WSDLException {
try {
Marshaller u = createMarshaller();
u.setProperty("jaxb.encoding", StandardCharsets.UTF_8.name());
u.setProperty("jaxb.fragment", Boolean.TRUE);
u.setProperty("jaxb.formatted.output", Boolean.TRUE);
Object mObj = obj;
Class<?> objectFactory = Class.forName(PackageUtils.getPackageName(typeClass) + ".ObjectFactory", true, obj.getClass().getClassLoader());
Method[] methods = objectFactory.getDeclaredMethods();
for (Method method : methods) {
if (method.getParameterTypes().length == 1 && method.getParameterTypes()[0].equals(typeClass)) {
mObj = method.invoke(objectFactory.newInstance(), new Object[] { obj });
}
}
javax.xml.stream.XMLOutputFactory fact = javax.xml.stream.XMLOutputFactory.newInstance();
XMLStreamWriter writer = new PrettyPrintXMLStreamWriter(fact.createXMLStreamWriter(pw), 2, getIndentLevel(parent));
if (namespace != null && !namespace.equals(jaxbNamespace)) {
Map<String, String> outMap = new HashMap<>();
outMap.put("{" + jaxbNamespace + "}*", "{" + namespace + "}*");
writer = new OutTransformWriter(writer, outMap, Collections.<String, String>emptyMap(), Collections.<String>emptyList(), false, "");
}
Map<String, String> nspref = new HashMap<>();
for (Object ent : wsdl.getNamespaces().entrySet()) {
Map.Entry<?, ?> entry = (Map.Entry<?, ?>) ent;
nspref.put((String) entry.getValue(), (String) entry.getKey());
}
JAXBUtils.setNamespaceMapper(nspref, u);
u.marshal(mObj, writer);
writer.flush();
} catch (Exception ex) {
throw new WSDLException(WSDLException.PARSER_ERROR, "", ex);
}
}
Aggregations