下面列出了javax.xml.transform.dom.DOMSource#getNode ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test
public void testDOMSourcePAYLOAD() throws Exception {
URL wsdl = getClass().getResource("/wsdl/hello_world_xml_wrapped.wsdl");
assertNotNull(wsdl);
XMLService service = new XMLService(wsdl, SERVICE_NAME);
assertNotNull(service);
InputStream is = getClass().getResourceAsStream("/messages/XML_GreetMeDocLiteralReq.xml");
Document doc = StaxUtils.read(is);
DOMSource reqMsg = new DOMSource(doc);
assertNotNull(reqMsg);
Dispatch<DOMSource> disp = service.createDispatch(PORT_NAME, DOMSource.class,
Service.Mode.PAYLOAD);
disp.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
"http://localhost:"
+ port
+ "/XMLService/XMLDispatchPort");
DOMSource result = disp.invoke(reqMsg);
assertNotNull(result);
Node respDoc = result.getNode();
assertEquals("greetMeResponse", respDoc.getFirstChild().getLocalName());
assertEquals("Hello tli", respDoc.getFirstChild().getTextContent());
}
@Test
public void readDOMSource() throws Exception {
MockHttpInputMessage inputMessage = new MockHttpInputMessage(BODY.getBytes("UTF-8"));
inputMessage.getHeaders().setContentType(new MediaType("application", "xml"));
DOMSource result = (DOMSource) converter.read(DOMSource.class, inputMessage);
Document document = (Document) result.getNode();
assertEquals("Invalid result", "root", document.getDocumentElement().getLocalName());
}
@Test
public void readDOMSourceExternal() throws Exception {
MockHttpInputMessage inputMessage = new MockHttpInputMessage(bodyExternal.getBytes("UTF-8"));
inputMessage.getHeaders().setContentType(new MediaType("application", "xml"));
converter.setSupportDtd(true);
DOMSource result = (DOMSource) converter.read(DOMSource.class, inputMessage);
Document document = (Document) result.getNode();
assertEquals("Invalid result", "root", document.getDocumentElement().getLocalName());
assertNotEquals("Invalid result", "Foo Bar", document.getDocumentElement().getTextContent());
}
/**
* Sets up handler for <code>DOMResult</code>.
*/
private void setupDOMResultHandler(DOMSource source, DOMResult result) throws SAXException {
// If there's no DOMResult, unset the validator handler
if (result == null) {
fDOMValidatorHandler = null;
fSchemaValidator.setDocumentHandler(null);
return;
}
final Node nodeResult = result.getNode();
// If the source node and result node are the same use the DOMResultAugmentor.
// Otherwise use the DOMResultBuilder.
if (source.getNode() == nodeResult) {
fDOMValidatorHandler = fDOMResultAugmentor;
fDOMResultAugmentor.setDOMResult(result);
fSchemaValidator.setDocumentHandler(fDOMResultAugmentor);
return;
}
if (result.getNode() == null) {
try {
DocumentBuilderFactory factory = JdkXmlUtils.getDOMFactory(
fComponentManager.getFeature(JdkXmlUtils.OVERRIDE_PARSER));
DocumentBuilder builder = factory.newDocumentBuilder();
result.setNode(builder.newDocument());
}
catch (ParserConfigurationException e) {
throw new SAXException(e);
}
}
fDOMValidatorHandler = fDOMResultBuilder;
fDOMResultBuilder.setDOMResult(result);
fSchemaValidator.setDocumentHandler(fDOMResultBuilder);
}
/**
* Construct a SAXImpl object using the given block size.
*/
public SAXImpl(XSLTCDTMManager mgr, Source source,
int dtmIdentity, DTMWSFilter whiteSpaceFilter,
XMLStringFactory xstringfactory,
boolean doIndexing, int blocksize,
boolean buildIdIndex,
boolean newNameTable)
{
super(mgr, source, dtmIdentity, whiteSpaceFilter, xstringfactory,
doIndexing, blocksize, false, buildIdIndex, newNameTable);
_dtmManager = mgr;
_size = blocksize;
// Use a smaller size for the space stack if the blocksize is small
_xmlSpaceStack = new int[blocksize <= 64 ? 4 : 64];
/* From DOMBuilder */
_xmlSpaceStack[0] = DTMDefaultBase.ROOTNODE;
// If the input source is DOMSource, set the _document field and
// create the node2Ids table.
if (source instanceof DOMSource) {
_hasDOMSource = true;
DOMSource domsrc = (DOMSource)source;
Node node = domsrc.getNode();
if (node instanceof Document) {
_document = (Document)node;
}
else {
_document = node.getOwnerDocument();
}
_node2Ids = new HashMap<>();
}
}
private static Node tryExtractNodeFromDOMSource(Source s) {
if (s instanceof DOMSource) {
@SuppressWarnings("unchecked") DOMSource ds = (DOMSource) s;
return ds.getNode();
}
return null;
}
/**
* Construct a SAXImpl object using the given block size.
*/
public SAXImpl(XSLTCDTMManager mgr, Source source,
int dtmIdentity, DTMWSFilter whiteSpaceFilter,
XMLStringFactory xstringfactory,
boolean doIndexing, int blocksize,
boolean buildIdIndex,
boolean newNameTable)
{
super(mgr, source, dtmIdentity, whiteSpaceFilter, xstringfactory,
doIndexing, blocksize, false, buildIdIndex, newNameTable);
_dtmManager = mgr;
_size = blocksize;
// Use a smaller size for the space stack if the blocksize is small
_xmlSpaceStack = new int[blocksize <= 64 ? 4 : 64];
/* From DOMBuilder */
_xmlSpaceStack[0] = DTMDefaultBase.ROOTNODE;
// If the input source is DOMSource, set the _document field and
// create the node2Ids table.
if (source instanceof DOMSource) {
_hasDOMSource = true;
DOMSource domsrc = (DOMSource)source;
Node node = domsrc.getNode();
if (node instanceof Document) {
_document = (Document)node;
}
else {
_document = node.getOwnerDocument();
}
_node2Ids = new HashMap<>();
}
}
/**
* Sets up handler for <code>DOMResult</code>.
*/
private void setupDOMResultHandler(DOMSource source, DOMResult result) throws SAXException {
// If there's no DOMResult, unset the validator handler
if (result == null) {
fDOMValidatorHandler = null;
fSchemaValidator.setDocumentHandler(null);
return;
}
final Node nodeResult = result.getNode();
// If the source node and result node are the same use the DOMResultAugmentor.
// Otherwise use the DOMResultBuilder.
if (source.getNode() == nodeResult) {
fDOMValidatorHandler = fDOMResultAugmentor;
fDOMResultAugmentor.setDOMResult(result);
fSchemaValidator.setDocumentHandler(fDOMResultAugmentor);
return;
}
if (result.getNode() == null) {
try {
DocumentBuilderFactory factory = JdkXmlUtils.getDOMFactory(
fComponentManager.getFeature(JdkXmlUtils.OVERRIDE_PARSER));
DocumentBuilder builder = factory.newDocumentBuilder();
result.setNode(builder.newDocument());
}
catch (ParserConfigurationException e) {
throw new SAXException(e);
}
}
fDOMValidatorHandler = fDOMResultBuilder;
fDOMResultBuilder.setDOMResult(result);
fSchemaValidator.setDocumentHandler(fDOMResultBuilder);
}
protected Element getDocumentElement(DOMSource ds) {
Node nd = ds.getNode();
if (nd instanceof Document) {
nd = ((Document)nd).getDocumentElement();
}
return (Element)nd;
}
@Test
public void readDOMSource() throws Exception {
MockHttpInputMessage inputMessage = new MockHttpInputMessage(BODY.getBytes("UTF-8"));
inputMessage.getHeaders().setContentType(new MediaType("application", "xml"));
DOMSource result = (DOMSource) converter.read(DOMSource.class, inputMessage);
Document document = (Document) result.getNode();
assertEquals("Invalid result", "root", document.getDocumentElement().getLocalName());
}
@Test
public void readDOMSourceExternal() throws Exception {
MockHttpInputMessage inputMessage = new MockHttpInputMessage(bodyExternal.getBytes("UTF-8"));
inputMessage.getHeaders().setContentType(new MediaType("application", "xml"));
converter.setSupportDtd(true);
DOMSource result = (DOMSource) converter.read(DOMSource.class, inputMessage);
Document document = (Document) result.getNode();
assertEquals("Invalid result", "root", document.getDocumentElement().getLocalName());
assertNotEquals("Invalid result", "Foo Bar", document.getDocumentElement().getTextContent());
}
/**
* Construct a SAXImpl object using the given block size.
*/
public SAXImpl(XSLTCDTMManager mgr, Source source,
int dtmIdentity, DTMWSFilter whiteSpaceFilter,
XMLStringFactory xstringfactory,
boolean doIndexing, int blocksize,
boolean buildIdIndex,
boolean newNameTable)
{
super(mgr, source, dtmIdentity, whiteSpaceFilter, xstringfactory,
doIndexing, blocksize, false, buildIdIndex, newNameTable);
_dtmManager = mgr;
_size = blocksize;
// Use a smaller size for the space stack if the blocksize is small
_xmlSpaceStack = new int[blocksize <= 64 ? 4 : 64];
/* From DOMBuilder */
_xmlSpaceStack[0] = DTMDefaultBase.ROOTNODE;
// If the input source is DOMSource, set the _document field and
// create the node2Ids table.
if (source instanceof DOMSource) {
_hasDOMSource = true;
DOMSource domsrc = (DOMSource)source;
Node node = domsrc.getNode();
if (node instanceof Document) {
_document = (Document)node;
}
else {
_document = node.getOwnerDocument();
}
_node2Ids = new HashMap<>();
}
}
protected Element getDocumentElement(DOMSource ds) {
Node nd = ds.getNode();
if (nd instanceof Document) {
nd = ((Document)nd).getDocumentElement();
}
return (Element)nd;
}
/**
* Sets up handler for <code>DOMResult</code>.
*/
private void setupDOMResultHandler(DOMSource source, DOMResult result) throws SAXException {
// If there's no DOMResult, unset the validator handler
if (result == null) {
fDOMValidatorHandler = null;
fSchemaValidator.setDocumentHandler(null);
return;
}
final Node nodeResult = result.getNode();
// If the source node and result node are the same use the DOMResultAugmentor.
// Otherwise use the DOMResultBuilder.
if (source.getNode() == nodeResult) {
fDOMValidatorHandler = fDOMResultAugmentor;
fDOMResultAugmentor.setDOMResult(result);
fSchemaValidator.setDocumentHandler(fDOMResultAugmentor);
return;
}
if (result.getNode() == null) {
try {
DocumentBuilderFactory factory = fComponentManager.getFeature(Constants.ORACLE_FEATURE_SERVICE_MECHANISM) ?
DocumentBuilderFactory.newInstance() : new DocumentBuilderFactoryImpl();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
result.setNode(builder.newDocument());
}
catch (ParserConfigurationException e) {
throw new SAXException(e);
}
}
fDOMValidatorHandler = fDOMResultBuilder;
fDOMResultBuilder.setDOMResult(result);
fSchemaValidator.setDocumentHandler(fDOMResultBuilder);
}
private static Document toDocument(DOMSource d) {
Node n = d.getNode();
return n instanceof Document ? (Document) n : n.getOwnerDocument();
}
private String getTargetNamespace(DOMSource src) {
Element elem = (Element)src.getNode();
return elem.getAttribute("targetNamespace");
}
private String getTargetNamespace(DOMSource src) {
Element elem = (Element)src.getNode();
return elem.getAttribute("targetNamespace");
}
/**
* Construct a DOM2DTM object from a DOM node.
*
* @param mgr The DTMManager who owns this DTM.
* @param domSource the DOM source that this DTM will wrap.
* @param dtmIdentity The DTM identity ID for this DTM.
* @param whiteSpaceFilter The white space filter for this DTM, which may
* be null.
* @param xstringfactory XMLString factory for creating character content.
* @param doIndexing true if the caller considers it worth it to use
* indexing schemes.
*/
public DOM2DTM(DTMManager mgr, DOMSource domSource,
int dtmIdentity, DTMWSFilter whiteSpaceFilter,
XMLStringFactory xstringfactory,
boolean doIndexing)
{
super(mgr, domSource, dtmIdentity, whiteSpaceFilter,
xstringfactory, doIndexing);
// Initialize DOM navigation
m_pos=m_root = domSource.getNode();
// Initialize DTM navigation
m_last_parent=m_last_kid=NULL;
m_last_kid=addNode(m_root, m_last_parent,m_last_kid, NULL);
// Apparently the domSource root may not actually be the
// Document node. If it's an Element node, we need to immediately
// add its attributes. Adapted from nextNode().
// %REVIEW% Move this logic into addNode and recurse? Cleaner!
//
// (If it's an EntityReference node, we're probably scrod. For now
// I'm just hoping nobody is ever quite that foolish... %REVIEW%)
//
// %ISSUE% What about inherited namespaces in this case?
// Do we need to special-case initialize them into the DTM model?
if(ELEMENT_NODE == m_root.getNodeType())
{
NamedNodeMap attrs=m_root.getAttributes();
int attrsize=(attrs==null) ? 0 : attrs.getLength();
if(attrsize>0)
{
int attrIndex=NULL; // start with no previous sib
for(int i=0;i<attrsize;++i)
{
// No need to force nodetype in this case;
// addNode() will take care of switching it from
// Attr to Namespace if necessary.
attrIndex=addNode(attrs.item(i),0,attrIndex,NULL);
m_firstch.setElementAt(DTM.NULL,attrIndex);
}
// Terminate list of attrs, and make sure they aren't
// considered children of the element
m_nextsib.setElementAt(DTM.NULL,attrIndex);
// IMPORTANT: This does NOT change m_last_parent or m_last_kid!
} // if attrs exist
} //if(ELEMENT_NODE)
// Initialize DTM-completed status
m_nodesAreProcessed = false;
}
private String getTargetNamespace(DOMSource src) {
Element elem = (Element)src.getNode();
return elem.getAttribute("targetNamespace");
}
/**
* Construct a DOM2DTM object from a DOM node.
*
* @param mgr The DTMManager who owns this DTM.
* @param domSource the DOM source that this DTM will wrap.
* @param dtmIdentity The DTM identity ID for this DTM.
* @param whiteSpaceFilter The white space filter for this DTM, which may
* be null.
* @param xstringfactory XMLString factory for creating character content.
* @param doIndexing true if the caller considers it worth it to use
* indexing schemes.
*/
public DOM2DTM(DTMManager mgr, DOMSource domSource,
int dtmIdentity, DTMWSFilter whiteSpaceFilter,
XMLStringFactory xstringfactory,
boolean doIndexing)
{
super(mgr, domSource, dtmIdentity, whiteSpaceFilter,
xstringfactory, doIndexing);
// Initialize DOM navigation
m_pos=m_root = domSource.getNode();
// Initialize DTM navigation
m_last_parent=m_last_kid=NULL;
m_last_kid=addNode(m_root, m_last_parent,m_last_kid, NULL);
// Apparently the domSource root may not actually be the
// Document node. If it's an Element node, we need to immediately
// add its attributes. Adapted from nextNode().
// %REVIEW% Move this logic into addNode and recurse? Cleaner!
//
// (If it's an EntityReference node, we're probably scrod. For now
// I'm just hoping nobody is ever quite that foolish... %REVIEW%)
//
// %ISSUE% What about inherited namespaces in this case?
// Do we need to special-case initialize them into the DTM model?
if(ELEMENT_NODE == m_root.getNodeType())
{
NamedNodeMap attrs=m_root.getAttributes();
int attrsize=(attrs==null) ? 0 : attrs.getLength();
if(attrsize>0)
{
int attrIndex=NULL; // start with no previous sib
for(int i=0;i<attrsize;++i)
{
// No need to force nodetype in this case;
// addNode() will take care of switching it from
// Attr to Namespace if necessary.
attrIndex=addNode(attrs.item(i),0,attrIndex,NULL);
m_firstch.setElementAt(DTM.NULL,attrIndex);
}
// Terminate list of attrs, and make sure they aren't
// considered children of the element
m_nextsib.setElementAt(DTM.NULL,attrIndex);
// IMPORTANT: This does NOT change m_last_parent or m_last_kid!
} // if attrs exist
} //if(ELEMENT_NODE)
// Initialize DTM-completed status
m_nodesAreProcessed = false;
}