下面列出了javax.xml.transform.dom.DOMSource#setNode ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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;
}
}
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();
}
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;
}
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;
}
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);
}
}
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);
}
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();
}
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);
}
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();
}
@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);
}
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;
}
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;
}