Search in sources :

Example 1 with OutTransformWriter

use of org.apache.cxf.staxutils.transform.OutTransformWriter in project cxf by apache.

the class JAXRSBPNamespaceHandler method transformElement.

private Element transformElement(Element element) {
    final Map<String, String> transformMap = Collections.singletonMap("{" + element.getNamespaceURI() + "}*", "{http://cxf.apache.org/blueprint/jaxrs-client}*");
    W3CDOMStreamWriter domWriter = new W3CDOMStreamWriter();
    OutTransformWriter transformWriter = new OutTransformWriter(domWriter, transformMap);
    try {
        StaxUtils.copy(element, transformWriter);
    } catch (XMLStreamException e) {
        throw new RuntimeException(e);
    }
    return domWriter.getDocument().getDocumentElement();
}
Also used : W3CDOMStreamWriter(org.apache.cxf.staxutils.W3CDOMStreamWriter) OutTransformWriter(org.apache.cxf.staxutils.transform.OutTransformWriter) XMLStreamException(javax.xml.stream.XMLStreamException)

Example 2 with OutTransformWriter

use of org.apache.cxf.staxutils.transform.OutTransformWriter 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)

Example 3 with OutTransformWriter

use of org.apache.cxf.staxutils.transform.OutTransformWriter in project cxf by apache.

the class MappingBeanDefinitionParser method transformElement.

private Element transformElement(Element element) {
    W3CDOMStreamWriter domWriter = new W3CDOMStreamWriter();
    OutTransformWriter transformWriter = new OutTransformWriter(domWriter, transformMap);
    try {
        StaxUtils.copy(element, transformWriter);
    } catch (XMLStreamException e) {
        throw new RuntimeException(e);
    }
    return domWriter.getDocument().getDocumentElement();
}
Also used : W3CDOMStreamWriter(org.apache.cxf.staxutils.W3CDOMStreamWriter) OutTransformWriter(org.apache.cxf.staxutils.transform.OutTransformWriter) XMLStreamException(javax.xml.stream.XMLStreamException)

Aggregations

XMLStreamException (javax.xml.stream.XMLStreamException)3 OutTransformWriter (org.apache.cxf.staxutils.transform.OutTransformWriter)3 W3CDOMStreamWriter (org.apache.cxf.staxutils.W3CDOMStreamWriter)2 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 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)1 PrettyPrintXMLStreamWriter (org.apache.cxf.staxutils.PrettyPrintXMLStreamWriter)1