本文整理汇总了Java中javax.xml.xpath.XPath.setNamespaceContext方法的典型用法代码示例。如果您正苦于以下问题:Java XPath.setNamespaceContext方法的具体用法?Java XPath.setNamespaceContext怎么用?Java XPath.setNamespaceContext使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.xml.xpath.XPath
的用法示例。
在下文中一共展示了XPath.setNamespaceContext方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testAddLibraries
import javax.xml.xpath.XPath; //导入方法依赖的package包/类
public void testAddLibraries() throws Exception {
SuiteProject suite = TestBase.generateSuite(getWorkDir(), "suite");
TestBase.generateSuiteComponent(suite, "lib");
TestBase.generateSuiteComponent(suite, "testlib");
NbModuleProject clientprj = TestBase.generateSuiteComponent(suite, "client");
Library lib = LibraryFactory.createLibrary(new LibImpl("lib"));
FileObject src = clientprj.getSourceDirectory();
assertTrue(ProjectClassPathModifier.addLibraries(new Library[] {lib}, src, ClassPath.COMPILE));
assertFalse(ProjectClassPathModifier.addLibraries(new Library[] {lib}, src, ClassPath.COMPILE));
Library testlib = LibraryFactory.createLibrary(new LibImpl("testlib"));
FileObject testsrc = clientprj.getTestSourceDirectory("unit");
assertTrue(ProjectClassPathModifier.addLibraries(new Library[] {testlib}, testsrc, ClassPath.COMPILE));
assertFalse(ProjectClassPathModifier.addLibraries(new Library[] {testlib}, testsrc, ClassPath.COMPILE));
InputSource input = new InputSource(clientprj.getProjectDirectory().getFileObject("nbproject/project.xml").toURL().toString());
XPath xpath = XPathFactory.newInstance().newXPath();
xpath.setNamespaceContext(nbmNamespaceContext());
assertEquals("org.example.client", xpath.evaluate("//nbm:data/nbm:code-name-base", input)); // control
assertEquals("org.example.lib", xpath.evaluate("//nbm:module-dependencies/*/nbm:code-name-base", input));
assertEquals("org.example.testlib", xpath.evaluate("//nbm:test-dependencies/*/*/nbm:code-name-base", input));
}
示例2: testAddRoots
import javax.xml.xpath.XPath; //导入方法依赖的package包/类
public void testAddRoots() throws Exception {
NbModuleProject prj = TestBase.generateStandaloneModule(getWorkDir(), "module");
FileObject src = prj.getSourceDirectory();
FileObject jar = TestFileUtils.writeZipFile(FileUtil.toFileObject(getWorkDir()), "a.jar", "entry:contents");
URL root = FileUtil.getArchiveRoot(jar.toURL());
assertTrue(ProjectClassPathModifier.addRoots(new URL[] {root}, src, ClassPath.COMPILE));
assertFalse(ProjectClassPathModifier.addRoots(new URL[] {root}, src, ClassPath.COMPILE));
FileObject releaseModulesExt = prj.getProjectDirectory().getFileObject("release/modules/ext");
assertNotNull(releaseModulesExt);
assertNotNull(releaseModulesExt.getFileObject("a.jar"));
jar = TestFileUtils.writeZipFile(releaseModulesExt, "b.jar", "entry2:contents");
root = FileUtil.getArchiveRoot(jar.toURL());
assertTrue(ProjectClassPathModifier.addRoots(new URL[] {root}, src, ClassPath.COMPILE));
assertFalse(ProjectClassPathModifier.addRoots(new URL[] {root}, src, ClassPath.COMPILE));
assertEquals(2, releaseModulesExt.getChildren().length);
String projectXml = prj.getProjectDirectory().getFileObject("nbproject/project.xml").toURL().toString();
InputSource input = new InputSource(projectXml);
XPath xpath = XPathFactory.newInstance().newXPath();
xpath.setNamespaceContext(nbmNamespaceContext());
assertEquals(projectXml, "ext/a.jar", xpath.evaluate("//nbm:class-path-extension[1]/nbm:runtime-relative-path", input));
assertEquals(projectXml, "release/modules/ext/a.jar", xpath.evaluate("//nbm:class-path-extension[1]/nbm:binary-origin", input));
assertEquals(projectXml, "ext/b.jar", xpath.evaluate("//nbm:class-path-extension[2]/nbm:runtime-relative-path", input));
assertEquals(projectXml, "release/modules/ext/b.jar", xpath.evaluate("//nbm:class-path-extension[2]/nbm:binary-origin", input));
}
示例3: unwrapSoapMessage
import javax.xml.xpath.XPath; //导入方法依赖的package包/类
public Element unwrapSoapMessage(Element soapElement) {
XPath xpath = XPathFactory.newInstance().newXPath();
NamespaceContextImpl context = new NamespaceContextImpl();
context.startPrefixMapping("soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
context.startPrefixMapping("samlp", "urn:oasis:names:tc:SAML:2.0:protocol");
context.startPrefixMapping("saml", "urn:oasis:names:tc:SAML:2.0:assertion");
context.startPrefixMapping("ds", "http://www.w3.org/2000/09/xmldsig#");
xpath.setNamespaceContext(context);
try {
final String expression = "/soapenv:Envelope/soapenv:Body/samlp:Response";
Element element = (Element) xpath.evaluate(expression, soapElement, XPathConstants.NODE);
if (element == null) {
String errorMessage = format("Document{0}{1}{0}does not have element {2} inside it.", NEW_LINE,
writeToString(soapElement), expression);
LOG.error(errorMessage);
throw new IllegalArgumentException(errorMessage);
}
return element;
} catch (XPathExpressionException e) {
throw propagate(e);
}
}
示例4: shouldBuildDocumentFromSetOfXPaths
import javax.xml.xpath.XPath; //导入方法依赖的package包/类
@Test
public void shouldBuildDocumentFromSetOfXPaths()
throws XPathExpressionException, TransformerException, IOException {
Map<String, Object> xmlProperties = fixtureAccessor.getXmlProperties();
Document document = documentBuilder.newDocument();
document.setXmlStandalone(true);
Document builtDocument = new XmlBuilder(namespaceContext)
.putAll(xmlProperties.keySet())
.build(document);
for (Entry<String, Object> xpathToValuePair : xmlProperties.entrySet()) {
XPath xpath = xpathFactory.newXPath();
if (null != namespaceContext) {
xpath.setNamespaceContext(namespaceContext);
}
assertThat(xpath.evaluate(xpathToValuePair.getKey(), builtDocument)).isNotNull();
}
assertThat(xmlToString(builtDocument)).isEqualTo(fixtureAccessor.getPutXml());
}
示例5: shouldBuildDocumentFromSetOfXPathsAndSetValues
import javax.xml.xpath.XPath; //导入方法依赖的package包/类
@Test
public void shouldBuildDocumentFromSetOfXPathsAndSetValues()
throws XPathExpressionException, TransformerException, IOException {
Map<String, Object> xmlProperties = fixtureAccessor.getXmlProperties();
Document document = documentBuilder.newDocument();
document.setXmlStandalone(true);
Document builtDocument = new XmlBuilder(namespaceContext)
.putAll(xmlProperties)
.build(document);
for (Entry<String, Object> xpathToValuePair : xmlProperties.entrySet()) {
XPath xpath = xpathFactory.newXPath();
if (null != namespaceContext) {
xpath.setNamespaceContext(namespaceContext);
}
assertThat(xpath.evaluate(xpathToValuePair.getKey(), builtDocument, XPathConstants.STRING))
.isEqualTo(xpathToValuePair.getValue());
}
assertThat(xmlToString(builtDocument)).isEqualTo(fixtureAccessor.getPutValueXml());
}
示例6: unwrapSoapMessage
import javax.xml.xpath.XPath; //导入方法依赖的package包/类
private Element unwrapSoapMessage(Element soapElement, SamlElementType samlElementType) {
XPath xpath = XPathFactory.newInstance().newXPath();
NamespaceContextImpl context = new NamespaceContextImpl();
context.startPrefixMapping("soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
context.startPrefixMapping("samlp", "urn:oasis:names:tc:SAML:2.0:protocol");
context.startPrefixMapping("saml", "urn:oasis:names:tc:SAML:2.0:assertion");
context.startPrefixMapping("ds", "http://www.w3.org/2000/09/xmldsig#");
xpath.setNamespaceContext(context);
try {
String expression = "//samlp:" + samlElementType.getElementName();
Element element = (Element) xpath.evaluate(expression, soapElement, XPathConstants.NODE);
if (element == null) {
String errorMessage = format("Document{0}{1}{0}does not have element {2} inside it.", NEW_LINE,
XmlUtils.writeToString(soapElement), expression);
LOG.error(errorMessage);
throw new SoapUnwrappingException(errorMessage);
}
return element;
} catch (XPathExpressionException e) {
throw propagate(e);
}
}
示例7: runXPathQuery
import javax.xml.xpath.XPath; //导入方法依赖的package包/类
public static List<String> runXPathQuery(File parsedFile, String xpathExpr) throws Exception{
List<String> result = new ArrayList<String>();
XPath xpath = XPathFactory.newInstance().newXPath();
xpath.setNamespaceContext(getNamespaceContext());
InputSource inputSource = new InputSource(new FileInputStream(parsedFile));
NodeList nodes = (NodeList) xpath.evaluate(xpathExpr, inputSource, XPathConstants.NODESET);
if((nodes != null) && (nodes.getLength() > 0)){
for(int i=0; i<nodes.getLength();i++){
Node node = nodes.item(i);
result.add(node.getNodeValue());
}
}
return result;
}
示例8: runXPathQuery
import javax.xml.xpath.XPath; //导入方法依赖的package包/类
public static List runXPathQuery(File parsedFile, String xpathExpr) throws Exception{
List result = new ArrayList();
XPath xpath = XPathFactory.newInstance().newXPath();
xpath.setNamespaceContext(getNamespaceContext());
InputSource inputSource = new InputSource(new FileInputStream(parsedFile));
NodeList nodes = (NodeList) xpath.evaluate(xpathExpr, inputSource, XPathConstants.NODESET);
if((nodes != null) && (nodes.getLength() > 0)){
for(int i=0; i<nodes.getLength();i++){
org.w3c.dom.Node node = nodes.item(i);
result.add(node.getNodeValue());
}
}
return result;
}
示例9: getAttributeQuery
import javax.xml.xpath.XPath; //导入方法依赖的package包/类
private Element getAttributeQuery(Document document) throws XPathExpressionException {
XPath xpath = XPathFactory.newInstance().newXPath();
NamespaceContextImpl context = new NamespaceContextImpl();
context.startPrefixMapping("soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
context.startPrefixMapping("samlp", "urn:oasis:names:tc:SAML:2.0:protocol");
xpath.setNamespaceContext(context);
return (Element) xpath.evaluate("//samlp:Response", document, XPathConstants.NODE);
}
示例10: getDubboInterfaces
import javax.xml.xpath.XPath; //导入方法依赖的package包/类
protected LinkedList<String> getDubboInterfaces() {
LinkedList<String> result = new LinkedList<String>();
try {
String filePath = ((AbstractConfiguration) this.wrDoc
.getConfiguration()).dubboconfigpath;
if(!StringUtils.isEmpty(filePath)) {
Document dubboConfig = readXMLConfig(((AbstractConfiguration) this.wrDoc
.getConfiguration()).dubboconfigpath);
XPath xPath = XPathFactory.newInstance().newXPath();
xPath.setNamespaceContext(new UniversalNamespaceCache(dubboConfig,
false));
NodeList serviceNodes = (NodeList) xPath.evaluate(
"//:beans/dubbo:service", dubboConfig,
XPathConstants.NODESET);
for (int i = 0; i < serviceNodes.getLength(); i++) {
Node node = serviceNodes.item(i);
String ifc = getAttributeValue(node, "interface");
if (ifc != null)
result.add(ifc);
}
}
} catch (Exception e) {
this.logger.error(e);
}
this.logger.debug("dubbo interface list:");
for (String s : result) {
this.logger.debug("interface: " + s);
}
return result;
}
示例11: getDubboProtocols
import javax.xml.xpath.XPath; //导入方法依赖的package包/类
/**
* @param dubboConfigFilePath
* @return HashMap, key protocol id, value "dubbo" or "http"
*/
protected static HashMap<String, String> getDubboProtocols(String dubboConfigFilePath) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException {
HashMap<String, String> result = new HashMap<>();
if(!StringUtils.isEmpty(dubboConfigFilePath)) {
Document dubboConfig = readXMLConfig(dubboConfigFilePath);
XPath xPath = XPathFactory.newInstance().newXPath();
xPath.setNamespaceContext(new UniversalNamespaceCache(dubboConfig,
false));
NodeList serviceNodes = (NodeList) xPath.evaluate(
"//:beans/dubbo:protocol", dubboConfig,
XPathConstants.NODESET);
for (int i = 0; i < serviceNodes.getLength(); i++) {
Node node = serviceNodes.item(i);
String id = getAttributeValue(node, "id");
String name = getAttributeValue(node, "name");
String server = getAttributeValue(node, "server");
if(StringUtils.isEmpty(id)) {
id = name;
}
if("servlet".equalsIgnoreCase(server) || "jetty".equalsIgnoreCase(server)) {
result.put(id, "http");
} else {
result.put(id, "dubbo");
}
}
}
return result;
}
示例12: evaluate
import javax.xml.xpath.XPath; //导入方法依赖的package包/类
void evaluate(boolean enableExt) throws XPathFactoryConfigurationException, XPathExpressionException {
Document document = getDocument();
XPathFactory xPathFactory = XPathFactory.newInstance();
/**
* Use of the extension function 'http://exslt.org/strings:tokenize' is
* not allowed when the secure processing feature is set to true.
* Attempt to use the new property to enable extension function
*/
if (enableExt) {
boolean isExtensionSupported = enableExtensionFunction(xPathFactory);
}
xPathFactory.setXPathFunctionResolver(new MyXPathFunctionResolver());
if (System.getSecurityManager() == null) {
xPathFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, false);
}
XPath xPath = xPathFactory.newXPath();
xPath.setNamespaceContext(new MyNamespaceContext());
String xPathResult = xPath.evaluate(XPATH_EXPRESSION, document);
System.out.println(
"XPath result (enableExtensionFunction == " + enableExt + ") = \""
+ xPathResult
+ "\"");
}
示例13: configure
import javax.xml.xpath.XPath; //导入方法依赖的package包/类
protected void configure(XPath xPath, NamespaceContext namespaceContext)
{
xPath.setNamespaceContext(namespaceContext);
}
示例14: newXPath
import javax.xml.xpath.XPath; //导入方法依赖的package包/类
/**
* Creates a new XPath object, specifying which prefix in the query is used for the
* android namespace.
* @param androidPrefix The namespace prefix.
*/
public static XPath newXPath(String androidPrefix) {
XPath xpath = sFactory.newXPath();
xpath.setNamespaceContext(new AndroidNamespaceContext(androidPrefix));
return xpath;
}
示例15: query
import javax.xml.xpath.XPath; //导入方法依赖的package包/类
public static NodeList query(Node node, String searchString, XPathContext xpathcontext)
throws XPathExpressionException {
XPath xpath = XPathFactory.newInstance().newXPath();
xpath.setNamespaceContext(xpathcontext);
return (NodeList) xpath.evaluate(searchString, node, XPathConstants.NODESET);
}