protected Document getDocument(boolean namespaceAware, boolean validate) throws Exception { // TODO: optimize this final InputSource source = getTestXMLInputSource(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setValidating(validate); dbf.setNamespaceAware(namespaceAware); dbf.setAttribute(JAXPConstants.JAXP_SCHEMA_LANGUAGE, JAXPConstants.W3C_XML_SCHEMA); DocumentBuilder db = dbf.newDocumentBuilder(); LOG.info("Setting entityresolver"); db.setEntityResolver( Util.getNotificationEntityResolver(services.getNotificationContentTypeService())); db.setErrorHandler(new SimpleErrorHandler(LOG)); return db.parse(source); }
@Test public void testXPathWithValidatedDOMDocNamespace() throws Exception { LOG.debug("TEST"); Document doc = getDocument(true, true); LOG.info("Default namespace: " + doc.lookupNamespaceURI(null)); LOG.info("default prefix: " + doc.lookupPrefix(doc.lookupNamespaceURI(null))); XPath xpath = XPathFactory.newInstance().newXPath(); xpath.setNamespaceContext(Util.getNotificationNamespaceContext(doc)); String channelName = (String) xpath.evaluate( "/nreq:notification/nreq:channel", doc.getDocumentElement(), XPathConstants.STRING); assertEquals("Test Channel #1", channelName); }