List of usage examples for org.dom4j XPath setNamespaceContext
void setNamespaceContext(NamespaceContext namespaceContext);
From source file:org.orbeon.oxf.xml.XPathUtils.java
License:Open Source License
/** * Apply the given XPath expression to the given node. * * @param prefixes mapping of prefixes to namespace URIs for prefixes used in expr *//* w w w .jav a 2 s . co m*/ public static org.dom4j.Node selectSingleNode(org.dom4j.Node node, String expr, Map prefixes) { try { org.dom4j.XPath path = node.createXPath(expr); path.setNamespaceContext(new SimpleNamespaceContext(prefixes)); return path.selectSingleNode(node); } catch (InvalidXPathException e) { throw new OXFException(e); } }
From source file:org.orbeon.oxf.xml.XPathUtils.java
License:Open Source License
/** * Select a value as a String./* ww w .j a v a 2s . co m*/ * * If the XPath expressions select an empty node set, return null. */ public static String selectStringValue(Node node, String expr, Map prefixes) { try { XPath path = new DOMXPath(expr); path.setNamespaceContext(new SimpleNamespaceContext(prefixes)); Object result = path.selectSingleNode(node); return (result == null || result instanceof String) ? (String) result : new DOMXPath(".").stringValueOf(result); } catch (JaxenException e) { throw new OXFException(e); } }
From source file:org.orbeon.oxf.xml.XPathUtils.java
License:Open Source License
private static void hookupPath(org.dom4j.XPath path, Map prefixes, VariableContext variableContext, FunctionContext functionContext) { if (prefixes != null) path.setNamespaceContext(new SimpleNamespaceContext(prefixes)); if (variableContext != null) path.setVariableContext(variableContext); if (functionContext != null) { final FunctionContext _functionContext = functionContext; path.setFunctionContext(new FunctionContext() { public Function getFunction(String namespaceURI, String prefix, String localName) throws UnresolvableException { Function f = _functionContext.getFunction(namespaceURI, prefix, localName); if (f != null) return f; else return XPathFunctionContext.getInstance().getFunction(namespaceURI, prefix, localName); }//from w w w . j av a 2 s .c om }); } }
From source file:org.suren.autotest.web.framework.code.DefaultXmlDataSourceGenerator.java
License:Apache License
/** * @param document/*from w w w. j a v a 2s. c o m*/ */ private void parse(Document doc) { SimpleNamespaceContext simpleNamespaceContext = new SimpleNamespaceContext(); simpleNamespaceContext.addNamespace("ns", "http://surenpi.com"); XPath xpath = new DefaultXPath("/ns:autotest/ns:includePage"); xpath.setNamespaceContext(simpleNamespaceContext); @SuppressWarnings("unchecked") List<Element> includePageList = xpath.selectNodes(doc); if (includePageList != null && includePageList.size() > 0) { for (Element includePage : includePageList) { String pageConfig = includePage.attributeValue("pageConfig"); generate(pageConfig, outputDir); } } xpath = new DefaultXPath("/ns:autotest/ns:pages"); xpath.setNamespaceContext(simpleNamespaceContext); Element pagesEle = (Element) xpath.selectSingleNode(doc); String pagePackage = pagesEle.attributeValue("pagePackage", ""); if (StringUtils.isNotBlank(pagePackage)) { pagePackage = pagePackage.trim(); } // pages parse progress xpath = new DefaultXPath("/ns:autotest/ns:pages/ns:page"); xpath.setNamespaceContext(simpleNamespaceContext); @SuppressWarnings("unchecked") List<Element> pageNodes = xpath.selectNodes(doc); if (pageNodes != null) { for (Element ele : pageNodes) { String pageClsStr = ele.attributeValue("class"); if (pageClsStr == null) { logger.warn("can not found class attribute."); continue; } String dataSrc = ele.attributeValue("dataSource"); try { parse(doc, pagePackage, pageClsStr, dataSrc, ele); } catch (NoSuchBeanDefinitionException e) { logger.error("Page element [{}] parse error, in document [{}].", "pageClsStr", doc); throw e; } catch (Exception e) { logger.error("Page element parse error.", e); } } } }
From source file:org.suren.autotest.web.framework.code.DefaultXmlDataSourceGenerator.java
License:Apache License
/** * ??Page/*from w w w . j a v a 2 s . c om*/ * * @param pageClsStr * @param dataSrc * @param dataSrc2 * @param ele */ private void parse(Document doc, String pagePackage, final String pageClsStr, String dataSrc, Element ele) throws Exception { SimpleNamespaceContext simpleNamespaceContext = new SimpleNamespaceContext(); simpleNamespaceContext.addNamespace("ns", "http://surenpi.com"); XPath xpath = new DefaultXPath("/ns:autotest/ns:dataSources/ns:dataSource[@name='" + dataSrc + "']"); xpath.setNamespaceContext(simpleNamespaceContext); Element dataSourceEle = (Element) xpath.selectSingleNode(doc); if (dataSourceEle == null) { logger.error("Can not found dataSource element by {}.", dataSrc); return; } String dsType = dataSourceEle.attributeValue("type"); String dsResource = dataSourceEle.attributeValue("resource"); logger.debug("DataSource type is {}, resource is {}.", dsType, dsResource); updateXmlDataSourceByEle(ele, dsResource, pagePackage, pageClsStr); }
From source file:org.suren.autotest.web.framework.code.DefaultXmlDataSourceGenerator.java
License:Apache License
/** * @param ele//w ww . jav a 2s. c o m * @param pageClsStr2 */ private void updateXmlDataSourceByEle(Element ele, String dsResource, String pagePackage, String pageClsStr) { Document doc = docMap.get(dsResource); if (doc == null) { doc = prepareDoc(dsResource); docMap.put(dsResource, doc); } SimpleNamespaceContext simpleNamespaceContext = new SimpleNamespaceContext(); simpleNamespaceContext.addNamespace("ns", "http://datasource.surenpi.com"); XPath xpath = new DefaultXPath("/ns:dataSources"); xpath.setNamespaceContext(simpleNamespaceContext); Element dataSourcesEle = doc.getRootElement(); dataSourcesEle.addAttribute("pagePackage", pagePackage); //? if (StringUtils.isNotBlank(pagePackage)) { pagePackage = (pagePackage.trim()); xpath = new DefaultXPath("/ns:dataSources[@pagePackage='" + pagePackage + "']/ns:dataSource[@pageClass='" + pageClsStr + "']"); xpath.setNamespaceContext(simpleNamespaceContext); } else { xpath = new DefaultXPath("/ns:dataSources/ns:dataSource[@pageClass='" + pageClsStr + "']"); xpath.setNamespaceContext(simpleNamespaceContext); } Element dataSourceEle = (Element) xpath.selectSingleNode(doc); if (dataSourceEle == null) { String prefix = dataSourcesEle.getNamespacePrefix(); if (StringUtils.isBlank("")) { String parentName = dataSourcesEle.getName(); if (parentName.contains(":")) { prefix = parentName.split(":")[0]; } } if (StringUtils.isNotBlank(prefix)) { prefix = prefix + ":"; } dataSourceEle = dataSourcesEle.addElement(prefix + "dataSource"); dataSourceEle.addAttribute("pageClass", pageClsStr); } //?? if (StringUtils.isNotBlank(pagePackage)) { pagePackage = (pagePackage.trim()); xpath = new DefaultXPath("/ns:dataSources[@pagePackage='" + pagePackage + "']/ns:dataSource[@pageClass='" + pageClsStr + "']/ns:page[1]"); xpath.setNamespaceContext(simpleNamespaceContext); } else { xpath = new DefaultXPath("/ns:dataSources/ns:dataSource[@pageClass='" + pageClsStr + "']/ns:page[1]"); xpath.setNamespaceContext(simpleNamespaceContext); } Element pageEle = (Element) xpath.selectSingleNode(dataSourceEle); if (pageEle == null) { String prefix = dataSourceEle.getNamespacePrefix(); if (StringUtils.isNotBlank(prefix)) { prefix = prefix + ":"; } pageEle = dataSourceEle.addElement(prefix + "page"); } ele.accept(new PageFieldVisitor(pageEle, pagePackage, pageClsStr)); }
From source file:org.suren.autotest.web.framework.code.DefaultXmlDataSourceGenerator.java
License:Apache License
/** * ?xmldocument/* w ww . jav a2 s.c o m*/ * @param resPath * @return */ private Document prepareDoc(final String resPath) { ClassLoader clsLoader = this.getClass().getClassLoader(); URL url = clsLoader.getResource(resPath); InputStream dsInput = null; try { if (url != null) { dsInput = url.openStream(); } } catch (IOException e1) { e1.printStackTrace(); } Document doc = null; if (dsInput != null) { try { doc = new SAXReader().read(dsInput); } catch (DocumentException e) { e.printStackTrace(); } } else { doc = DocumentHelper.createDocument(); } SimpleDateFormat dateFormat = new SimpleDateFormat(); doc.addComment("Auto created by AutoTest, " + dateFormat.format(new Date())); SimpleNamespaceContext simpleNamespaceContext = new SimpleNamespaceContext(); simpleNamespaceContext.addNamespace("ns", "http://datasource.surenpi.com"); XPath xpath = new DefaultXPath("/ns:dataSources"); xpath.setNamespaceContext(simpleNamespaceContext); //? Element dataSourcesEle = (Element) xpath.selectSingleNode(doc); if (dataSourcesEle == null) { String prefix = "suren"; dataSourcesEle = doc.addElement(prefix + ":dataSources"); dataSourcesEle.addNamespace(prefix, "http://datasource.surenpi.com"); dataSourcesEle.addAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"); dataSourcesEle.addAttribute("xsi:schemaLocation", "http://datasource.surenpi.com " + "http://surenpi.com/schema/datasource/autotest.web.framework.datasource.xsd "); } return doc; }
From source file:org.suren.autotest.web.framework.code.DefaultXmlGenerator.java
License:Apache License
@Override public void generate(String srcCoding, final String outputDir) { //???Page// w w w. j ava2 s . c om Generator generator = new DefaultXmlCodeGenerator() { private String springConfigPath = "applicationContext.xml"; private Set<String> allPackage = new HashSet<String>(); @Override protected void create(AutoPage autoPage) { super.create(autoPage); allPackage.add(autoPage.getPackageName()); } @Override protected void done() { super.done(); if (allPackage.size() == 0) { return; } Document springConfigDoc = null; try (InputStream inputStream = getClassLoader().getResourceAsStream(springConfigPath)) { if (inputStream != null) { springConfigDoc = new SAXReader().read(inputStream); } } catch (IOException e) { e.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); } if (springConfigDoc == null) { springConfigDoc = DocumentHelper.createDocument(); String prefix = "p"; Element root = springConfigDoc.addElement(prefix + ":beans"); root.addNamespace(prefix, "http://www.springframework.org/schema/beans"); root.addNamespace("context", "http://www.springframework.org/schema/context"); root.addAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"); root.addAttribute("xsi:schemaLocation", "http://www.springframework.org/schema/beans " + "http://www.springframework.org/schema/beans/spring-beans-3.0.xsd " + "http://www.springframework.org/schema/context " + "http://www.springframework.org/schema/context/spring-context-3.0.xsd "); } SimpleNamespaceContext simpleNamespaceContext = new SimpleNamespaceContext(); simpleNamespaceContext.addNamespace("p", "http://www.springframework.org/schema/beans"); simpleNamespaceContext.addNamespace("context", "http://www.springframework.org/schema/context"); XPath xpath = new DefaultXPath("/p:beans/context:component-scan"); xpath.setNamespaceContext(simpleNamespaceContext); //? Element componentScanEle = (Element) xpath.selectSingleNode(springConfigDoc); if (componentScanEle == null) { componentScanEle = springConfigDoc.getRootElement().addElement("context:component-scan"); } //pagepackage? String basePackage = componentScanEle.attributeValue("base-package", ""); basePackage = basePackage.trim(); if (!"".equals(basePackage) && !basePackage.endsWith(",")) { basePackage += ","; } basePackage.replaceAll(",\\s*", ","); for (String newPackage : allPackage) { if (!basePackage.contains(newPackage + ",")) { basePackage += (newPackage + ","); } } componentScanEle.addAttribute("base-package", basePackage); //??? File outputDirFile = new File(outputDir); if (!outputDirFile.isDirectory()) { outputDirFile.mkdirs(); } try (OutputStream dsOutput = new FileOutputStream(new File(outputDirFile, springConfigPath))) { OutputFormat outputFormat = OutputFormat.createPrettyPrint(); outputFormat.setIndentSize(4); XMLWriter xmlWriter = new XMLWriter(dsOutput, outputFormat); xmlWriter.write(springConfigDoc); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }; generator.generate(srcCoding, outputDir); //? generator = new DefaultXmlDataSourceGenerator(); generator.generate(srcCoding, outputDir); //? generator = new DefaultXmlSuiteRunnerGenerator(); generator.generate(srcCoding, outputDir); //?} }
From source file:org.suren.autotest.web.framework.code.DefaultXmlSuiteRunnerGenerator.java
License:Apache License
/** * @param document/*from w w w. j ava2 s . co m*/ */ private void parse(Document doc) { SimpleNamespaceContext simpleNamespaceContext = new SimpleNamespaceContext(); simpleNamespaceContext.addNamespace("ns", "http://surenpi.com"); XPath xpath = new DefaultXPath("/ns:autotest/ns:includePage"); xpath.setNamespaceContext(simpleNamespaceContext); @SuppressWarnings("unchecked") List<Element> includePageList = xpath.selectNodes(doc); if (includePageList != null && includePageList.size() > 0) { for (Element includePage : includePageList) { String pageConfig = includePage.attributeValue("pageConfig"); generate(pageConfig, outputDir); } } xpath = new DefaultXPath("/ns:autotest/ns:pages"); xpath.setNamespaceContext(simpleNamespaceContext); Element pagesEle = (Element) xpath.selectSingleNode(doc); String pagePackage = pagesEle.attributeValue("pagePackage", ""); if (StringUtils.isNotBlank(pagePackage)) { pagePackage = pagePackage.trim(); } // pages parse progress xpath = new DefaultXPath("/ns:autotest/ns:pages/ns:page"); xpath.setNamespaceContext(simpleNamespaceContext); @SuppressWarnings("unchecked") List<Element> pageNodes = xpath.selectNodes(doc); if (pageNodes != null) { for (Element ele : pageNodes) { String pageClsStr = ele.attributeValue("class"); if (pageClsStr == null) { logger.warn("can not found class attribute."); continue; } try { parse(doc, pagePackage, pageClsStr, ele); } catch (NoSuchBeanDefinitionException e) { logger.error("Page element [{}] parse error, in document [{}].", "pageClsStr", doc); throw e; } catch (Exception e) { logger.error("Page element parse error.", e); } } } }
From source file:org.suren.autotest.web.framework.code.DefaultXmlSuiteRunnerGenerator.java
License:Apache License
/** * @param ele//from w w w . j a v a2 s. c o m */ private void updateXmlDataSourceByEle(Element ele, String pagePackage, String pageClsStr) { SimpleDateFormat dateFormat = new SimpleDateFormat(); suiteRunnerDoc.addComment("Auto created by AutoTest, " + dateFormat.format(new Date())); SimpleNamespaceContext simpleNamespaceContext = new SimpleNamespaceContext(); simpleNamespaceContext.addNamespace("ns", "http://suite.surenpi.com"); XPath xpath = new DefaultXPath("/ns:suite"); xpath.setNamespaceContext(simpleNamespaceContext); //? Element dataSourcesEle = suiteRunnerDoc.getRootElement();//(Element) xpath.selectSingleNode(suiteRunnerDoc); if (dataSourcesEle == null) { String prefix = "suren"; dataSourcesEle = suiteRunnerDoc.addElement(prefix + ":suite"); dataSourcesEle.addNamespace(prefix, "http://suite.surenpi.com"); dataSourcesEle.addAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"); dataSourcesEle.addAttribute("xsi:schemaLocation", "http://suite.surenpi.com " + "http://surenpi.com/schema/suite/autotest.web.framework.suite.xsd "); dataSourcesEle.addAttribute("pageConfig", this.srcCoding); dataSourcesEle.addAttribute("pagePackage", pagePackage); } xpath = new DefaultXPath("/ns:suite/ns:page[@class='" + pageClsStr + "']"); xpath.setNamespaceContext(simpleNamespaceContext); Element dataSourceEle = (Element) xpath.selectSingleNode(suiteRunnerDoc); if (dataSourceEle == null) { String prefix = dataSourcesEle.getNamespacePrefix(); if (StringUtils.isBlank("")) { String parentName = dataSourcesEle.getName(); if (parentName.contains(":")) { prefix = parentName.split(":")[0]; } } if (StringUtils.isNotBlank(prefix)) { prefix = prefix + ":"; } dataSourceEle = dataSourcesEle.addElement(prefix + "page"); dataSourceEle.addAttribute("class", pageClsStr); } //?? xpath = new DefaultXPath("/ns:suite/ns:page[@class='" + pageClsStr + "']/ns:actions[1]"); xpath.setNamespaceContext(simpleNamespaceContext); Element pageEle = (Element) xpath.selectSingleNode(dataSourceEle); if (pageEle == null) { String prefix = dataSourceEle.getNamespacePrefix(); if (StringUtils.isNotBlank(prefix)) { prefix = prefix + ":"; } pageEle = dataSourceEle.addElement(prefix + "actions"); } ele.accept(new PageFieldVisitor(pageEle)); }