javax.xml.transform.dom.DOMSource#setNode ( )源码实例Demo

下面列出了javax.xml.transform.dom.DOMSource#setNode ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: spring-analysis-note   文件: AbstractMarshaller.java
/**
 * 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;
	}
}
 
/**
 * 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;
	}
}
 
/**
 * 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;
	}
}
 
源代码4 项目: development   文件: WsdlHandleTask.java
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();
}
 
源代码5 项目: cxf   文件: GreeterDOMSourcePayloadProvider.java
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;
}
 
源代码6 项目: cxf   文件: GreeterDOMSourceMessageProvider.java
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;
}
 
源代码7 项目: cxf   文件: LogicalMessageImpl.java
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);
    }
}
 
源代码8 项目: mq-http-java-sdk   文件: XmlUtil.java
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);
}
 
源代码9 项目: mq-http-java-sdk   文件: XmlUtil.java
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();

}
 
源代码10 项目: mq-http-java-sdk   文件: XmlUtil.java
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);
}
 
源代码11 项目: mq-http-java-sdk   文件: XmlUtil.java
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();

}
 
源代码12 项目: openjdk-jdk9   文件: Bug4515047.java
@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);
}
 
源代码13 项目: cxf   文件: HWDOMSourceMessageProvider.java
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;
}
 
源代码14 项目: cxf   文件: HWDOMSourcePayloadProvider.java
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;
}