Search in sources :

Example 1 with PrettyPrintXMLStreamWriter

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();
}
Also used : PrettyPrintXMLStreamWriter(org.apache.cxf.staxutils.PrettyPrintXMLStreamWriter) DOMSource(javax.xml.transform.dom.DOMSource) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) PrettyPrintXMLStreamWriter(org.apache.cxf.staxutils.PrettyPrintXMLStreamWriter)

Example 2 with PrettyPrintXMLStreamWriter

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));
    }
}
Also used : PrettyPrintXMLStreamWriter(org.apache.cxf.staxutils.PrettyPrintXMLStreamWriter) StringWriter(java.io.StringWriter) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) PrettyPrintXMLStreamWriter(org.apache.cxf.staxutils.PrettyPrintXMLStreamWriter) StreamSource(javax.xml.transform.stream.StreamSource) StringReader(java.io.StringReader)

Example 3 with PrettyPrintXMLStreamWriter

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();
}
Also used : PrettyPrintXMLStreamWriter(org.apache.cxf.staxutils.PrettyPrintXMLStreamWriter) XMLStreamReader(javax.xml.stream.XMLStreamReader) StringWriter(java.io.StringWriter) XMLStreamException(javax.xml.stream.XMLStreamException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) PrettyPrintXMLStreamWriter(org.apache.cxf.staxutils.PrettyPrintXMLStreamWriter) StringReader(java.io.StringReader)

Example 4 with PrettyPrintXMLStreamWriter

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();
}
Also used : PrettyPrintXMLStreamWriter(org.apache.cxf.staxutils.PrettyPrintXMLStreamWriter) DOMSource(javax.xml.transform.dom.DOMSource) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) PrettyPrintXMLStreamWriter(org.apache.cxf.staxutils.PrettyPrintXMLStreamWriter)

Example 5 with PrettyPrintXMLStreamWriter

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);
    }
}
Also used : Marshaller(javax.xml.bind.Marshaller) HashMap(java.util.HashMap) WSDLException(javax.wsdl.WSDLException) Method(java.lang.reflect.Method) XMLStreamException(javax.xml.stream.XMLStreamException) JAXBException(javax.xml.bind.JAXBException) WSDLException(javax.wsdl.WSDLException) PrettyPrintXMLStreamWriter(org.apache.cxf.staxutils.PrettyPrintXMLStreamWriter) OutTransformWriter(org.apache.cxf.staxutils.transform.OutTransformWriter) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) PrettyPrintXMLStreamWriter(org.apache.cxf.staxutils.PrettyPrintXMLStreamWriter) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

XMLStreamWriter (javax.xml.stream.XMLStreamWriter)6 PrettyPrintXMLStreamWriter (org.apache.cxf.staxutils.PrettyPrintXMLStreamWriter)6 StringWriter (java.io.StringWriter)3 XMLStreamException (javax.xml.stream.XMLStreamException)3 StringReader (java.io.StringReader)2 DOMSource (javax.xml.transform.dom.DOMSource)2 StreamSource (javax.xml.transform.stream.StreamSource)2 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 Method (java.lang.reflect.Method)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 WSDLException (javax.wsdl.WSDLException)1 JAXBException (javax.xml.bind.JAXBException)1 Marshaller (javax.xml.bind.Marshaller)1 XMLStreamReader (javax.xml.stream.XMLStreamReader)1 OutTransformWriter (org.apache.cxf.staxutils.transform.OutTransformWriter)1