Java Code Examples for javax.xml.transform.dom.DOMSource#setNode()

The following examples show how to use javax.xml.transform.dom.DOMSource#setNode() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractMarshaller.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Template method for handling {@code DOMSource}s.
 * <p>This implementation delegates to {@code unmarshalDomNode}.
 * If the given source is empty, an empty source {@code Document}
 * will be created as a placeholder.
 * @param domSource the {@code DOMSource}
 * @return the object graph
 * @throws XmlMappingException if the given source cannot be mapped to an object
 * @throws IllegalArgumentException if the {@code domSource} is empty
 * @see #unmarshalDomNode(org.w3c.dom.Node)
 */
protected Object unmarshalDomSource(DOMSource domSource) throws XmlMappingException {
	if (domSource.getNode() == null) {
		domSource.setNode(buildDocument());
	}
	try {
		return unmarshalDomNode(domSource.getNode());
	}
	catch (NullPointerException ex) {
		if (!isSupportDtd()) {
			throw new UnmarshallingFailureException("NPE while unmarshalling. " +
					"This can happen on JDK 1.6 due to the presence of DTD " +
					"declarations, which are disabled.", ex);
		}
		throw ex;
	}
}
 
Example 2
Source File: AbstractMarshaller.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Template method for handling {@code DOMSource}s.
 * <p>This implementation delegates to {@code unmarshalDomNode}.
 * If the given source is empty, an empty source {@code Document}
 * will be created as a placeholder.
 * @param domSource the {@code DOMSource}
 * @return the object graph
 * @throws XmlMappingException if the given source cannot be mapped to an object
 * @throws IllegalArgumentException if the {@code domSource} is empty
 * @see #unmarshalDomNode(org.w3c.dom.Node)
 */
protected Object unmarshalDomSource(DOMSource domSource) throws XmlMappingException {
	if (domSource.getNode() == null) {
		domSource.setNode(buildDocument());
	}
	try {
		return unmarshalDomNode(domSource.getNode());
	}
	catch (NullPointerException ex) {
		if (!isSupportDtd()) {
			throw new UnmarshallingFailureException("NPE while unmarshalling. " +
					"This can happen on JDK 1.6 due to the presence of DTD " +
					"declarations, which are disabled.", ex);
		}
		throw ex;
	}
}
 
Example 3
Source File: AbstractMarshaller.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Template method for handling {@code DOMSource}s.
 * <p>This implementation delegates to {@code unmarshalDomNode}.
 * If the given source is empty, an empty source {@code Document}
 * will be created as a placeholder.
 * @param domSource the {@code DOMSource}
 * @return the object graph
 * @throws XmlMappingException if the given source cannot be mapped to an object
 * @throws IllegalArgumentException if the {@code domSource} is empty
 * @see #unmarshalDomNode(org.w3c.dom.Node)
 */
protected Object unmarshalDomSource(DOMSource domSource) throws XmlMappingException {
	if (domSource.getNode() == null) {
		domSource.setNode(buildDocument());
	}
	try {
		return unmarshalDomNode(domSource.getNode());
	}
	catch (NullPointerException ex) {
		if (!isSupportDtd()) {
			throw new UnmarshallingFailureException("NPE while unmarshalling. " +
					"This can happen on JDK 1.6 due to the presence of DTD " +
					"declarations, which are disabled.", ex);
		}
		throw ex;
	}
}
 
Example 4
Source File: WsdlHandleTask.java    From development with Apache License 2.0 6 votes vote down vote up
private void save(String fileName, Document doc)
        throws TransformerException, IOException {
    TransformerFactory transFactory = TransformerFactory.newInstance();
    Transformer transformer = transFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(
            "{http://xml.apache.org/xslt}indent-amount", "4");
    DOMSource source = new DOMSource();
    source.setNode(doc);
    StreamResult result = new StreamResult();
    OutputStream out = new FileOutputStream(fileName);
    result.setOutputStream(out);
    transformer.transform(source, result);
    out.close();
}
 
Example 5
Source File: GreeterDOMSourcePayloadProvider.java    From cxf with Apache License 2.0 6 votes vote down vote up
public DOMSource invoke(DOMSource request) {
    DOMSource response = new DOMSource();
    try {
        System.out.println("Incoming Client Request as a DOMSource data in PAYLOAD Mode");
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        transformerFactory.setFeature(javax.xml.XMLConstants.FEATURE_SECURE_PROCESSING, true);
        Transformer transformer = transformerFactory.newTransformer();
        StreamResult result = new StreamResult(System.out);
        transformer.transform(request, result);
        System.out.println("\n");

        SOAPMessage greetMeResponse = null;
        try (InputStream is = getClass().getResourceAsStream("/GreetMeDocLiteralResp3.xml")) {
            greetMeResponse = MessageFactory.newInstance().createMessage(null, is);
        }
        response.setNode(greetMeResponse.getSOAPBody().extractContentAsDocument());
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return response;
}
 
Example 6
Source File: GreeterDOMSourceMessageProvider.java    From cxf with Apache License 2.0 6 votes vote down vote up
public DOMSource invoke(DOMSource request) {
    DOMSource response = new DOMSource();
    try {
        MessageFactory factory = MessageFactory.newInstance();
        SOAPMessage soapReq = factory.createMessage();
        soapReq.getSOAPPart().setContent(request);

        System.out.println("Incoming Client Request as a DOMSource data in MESSAGE Mode");
        soapReq.writeTo(System.out);
        System.out.println("\n");

        SOAPMessage greetMeResponse = null;
        try (InputStream is = getClass().getResourceAsStream("/GreetMeDocLiteralResp2.xml")) {
            greetMeResponse = factory.createMessage(null, is);
        }
        response.setNode(greetMeResponse.getSOAPPart());
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return response;
}
 
Example 7
Source File: LogicalMessageImpl.java    From cxf with Apache License 2.0 6 votes vote down vote up
public Object getPayload(JAXBContext arg0) {
    try {
        Source s = getPayload();
        if (s instanceof DOMSource) {
            DOMSource ds = (DOMSource)s;
            ds.setNode(org.apache.cxf.helpers.DOMUtils.getDomElement(ds.getNode()));
            Node parent = ds.getNode().getParentNode();
            Node next = ds.getNode().getNextSibling();
            if (parent instanceof DocumentFragment) {
                parent.removeChild(ds.getNode());
            }
            try {
                return JAXBUtils.unmarshall(arg0, ds);
            } finally {
                if (parent instanceof DocumentFragment) {
                    parent.insertBefore(ds.getNode(), next);
                }
            }
        }
        return JAXBUtils.unmarshall(arg0, getPayload());
    } catch (JAXBException e) {
        throw new WebServiceException(e);
    }
}
 
Example 8
Source File: XmlUtil.java    From mq-http-java-sdk with MIT License 5 votes vote down vote up
public static void output(Node node, String encoding,
                          OutputStream outputStream) throws TransformerException {
    Transformer transformer = transFactory.newTransformer();
    transformer.setOutputProperty("encoding", encoding);

    DOMSource source = new DOMSource();
    source.setNode(node);

    StreamResult result = new StreamResult();
    result.setOutputStream(outputStream);

    transformer.transform(source, result);
}
 
Example 9
Source File: XmlUtil.java    From mq-http-java-sdk with MIT License 5 votes vote down vote up
public static String xmlNodeToString(Node node, String encoding)
        throws TransformerException {
    Transformer transformer = transFactory.newTransformer();
    transformer.setOutputProperty("encoding", encoding);
    StringWriter strWtr = new StringWriter();

    DOMSource source = new DOMSource();
    source.setNode(node);
    StreamResult result = new StreamResult(strWtr);
    transformer.transform(source, result);
    return strWtr.toString();

}
 
Example 10
Source File: XmlUtil.java    From mq-http-java-sdk with MIT License 5 votes vote down vote up
public static void output(Node node, String encoding,
                          OutputStream outputStream) throws TransformerException {
    Transformer transformer = transFactory.newTransformer();
    transformer.setOutputProperty("encoding", encoding);

    DOMSource source = new DOMSource();
    source.setNode(node);

    StreamResult result = new StreamResult();
    result.setOutputStream(outputStream);

    transformer.transform(source, result);
}
 
Example 11
Source File: XmlUtil.java    From mq-http-java-sdk with MIT License 5 votes vote down vote up
public static String xmlNodeToString(Node node, String encoding)
        throws TransformerException {
    Transformer transformer = transFactory.newTransformer();
    transformer.setOutputProperty("encoding", encoding);
    StringWriter strWtr = new StringWriter();

    DOMSource source = new DOMSource();
    source.setNode(node);
    StreamResult result = new StreamResult(strWtr);
    transformer.transform(source, result);
    return strWtr.toString();

}
 
Example 12
Source File: Bug4515047.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testCreateTxDoc() throws TransformerException, ParserConfigurationException {
    Transformer transformer = TransformerFactory.newInstance().newTransformer();

    StreamResult result = new StreamResult(System.out);
    DOMSource source = new DOMSource();

    /* This should not throw an Illegal Argument Exception */
    //Test empty DOMSource
    transformer.transform(source, result);

    //Test DOMSource having only an empty node
    source.setNode(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());
    transformer.transform(source, result);
}
 
Example 13
Source File: HWDOMSourceMessageProvider.java    From cxf with Apache License 2.0 5 votes vote down vote up
public DOMSource invoke(DOMSource request) {
    QName qn = (QName)ctx.getMessageContext().get(MessageContext.WSDL_OPERATION);
    if (qn == null) {
        throw new RuntimeException("No Operation Name");
    }

    //XMLUtils.writeTo(request, System.out);
    DOMSource response = new DOMSource();
    try {
        SOAPMessage msg = factory.createMessage();
        msg.getSOAPPart().setContent(request);
        SOAPBody body = msg.getSOAPBody();
        Node n = body.getFirstChild();

        while (n.getNodeType() != Node.ELEMENT_NODE) {
            n = n.getNextSibling();
        }
        if (n.getLocalName().equals(sayHi.getLocalPart())) {
            response.setNode(sayHiResponse.getSOAPPart());
        } else if (n.getLocalName().equals(greetMe.getLocalPart())) {
            response.setNode(greetMeResponse.getSOAPPart());
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return response;
}
 
Example 14
Source File: HWDOMSourcePayloadProvider.java    From cxf with Apache License 2.0 4 votes vote down vote up
public DOMSource invoke(DOMSource request) {
    QName qn = (QName)ctx.getMessageContext().get(MessageContext.WSDL_OPERATION);
    if (qn == null) {
        throw new RuntimeException("No Operation Name");
    }

    DOMSource response = new DOMSource();

    Node n = request.getNode();
    if (n instanceof Document) {
        n = ((Document)n).getDocumentElement();
    }
    if (n.getLocalName().equals(sayHi.getLocalPart())) {
        response.setNode(sayHiResponse);
    } else if (n.getLocalName().equals(greetMe.getLocalPart())) {
        Element el = DOMUtils.getFirstElement(n);
        String s = DOMUtils.getContent(el);
        if ("throwFault".equals(s.trim())) {
            try {
                SOAPFactory f = SOAPFactory.newInstance();
                SOAPFault soapFault = f.createFault();

                soapFault.setFaultString("Test Fault String ****");

                Detail detail = soapFault.addDetail();
                detail = soapFault.getDetail();

                QName qName = new QName("http://www.Hello.org/greeter", "TestFault", "ns");
                DetailEntry de = detail.addDetailEntry(qName);

                qName = new QName("http://www.Hello.org/greeter", "ErrorCode", "ns");
                SOAPElement errorElement = de.addChildElement(qName);
                errorElement.setTextContent("errorcode");

                throw new SOAPFaultException(soapFault);
            } catch (SOAPException e) {
                e.printStackTrace();
            }
        }

        response.setNode(greetMeResponse);
    }
    return response;
}