/** * Extracts the value of the supplied attribute * * @param element * element with attribute in question * @param attributeName * name of the attribute in question * @param failIfNotFound * determines if exception should be thrown if attribute is not * found * @return value of the attribute in question, null if not found and * failIfNotFound is set to false * @throws GenericArtifactParsingException * exception thrown is attribute is missing and failIfNotFound * is set to true */ public static String getAttributeValue(Element element, String attributeName, boolean failIfNotFound) throws GenericArtifactParsingException { XPath xpath = new DefaultXPath("@" + attributeName); xpath.setNamespaceURIs(ccfNamespaceMap); Node attributeNode = xpath.selectSingleNode(element); if (attributeNode == null) { if (failIfNotFound) { throw new GenericArtifactParsingException("Missing attribute: " + attributeName + " in element " + element.getName()); } else { return null; } } else { return attributeNode.getText(); } }
/** * 解析测试套件配置文件 * @param suiteInputStream 配置文件输入流 * @return 测试套件对象 * @throws DocumentException */ public Suite parse(InputStream suiteInputStream) throws DocumentException { SAXReader reader = new SAXReader(); reader.setEncoding("utf-8"); Document document = reader.read(suiteInputStream); simpleNamespaceContext.addNamespace("ns", NS_URI); XPath xpath = new DefaultXPath("/ns:suite"); xpath.setNamespaceContext(simpleNamespaceContext); Element suiteEle = (Element) xpath.selectSingleNode(document); if (suiteEle == null) { suiteEle = document.getRootElement(); // throw new RuntimeException("Can not found suite config."); } Suite suite = new Suite(); String xmlConfPath = suiteEle.attributeValue("pageConfig"); String pagePackage = suiteEle.attributeValue("pagePackage", ""); String rows = suiteEle.attributeValue("rows", "1"); String lackLines = suiteEle.attributeValue("lackLines", "nearby"); String errorLines = suiteEle.attributeValue("errorLines", "stop"); String afterSleep = suiteEle.attributeValue("afterSleep", "0"); suite.setXmlConfPath(xmlConfPath); suite.setPagePackage(pagePackage); suite.setRows(rows); suite.setLackLines(lackLines); suite.setErrorLines(errorLines); suite.setAfterSleep(Long.parseLong(afterSleep)); pagesParse(document, suite); return suite; }
/** * 解析具体的action元素 * @param actionList * @param actionsEle action组元素 * @param afterSleep action所在组的休眠时间 * @param beforeSleep action所在组的休眠时间 */ private void parse(List<SuiteAction> actionList, final Element actionsEle, String beforeSleep, String afterSleep) { DefaultXPath xpath = new DefaultXPath("ns:action"); xpath.setNamespaceContext(simpleNamespaceContext); @SuppressWarnings("unchecked") List<Element> actionEleList = xpath.selectNodes(actionsEle); for(Element actionEle : actionEleList) { String field = actionEle.attributeValue("field"); String name = actionEle.attributeValue("name", "click"); String invoker = actionEle.attributeValue("invoker"); String actionBeforeSleep = actionEle.attributeValue("beforeSleep", beforeSleep); String actionAfterSleep = actionEle.attributeValue("afterSleep", afterSleep); String repeat = actionEle.attributeValue("repeat", "1"); String disable = actionEle.attributeValue("disable", "false"); if(Boolean.parseBoolean(disable)) { continue; } SuiteAction suiteAction = new SuiteAction(field, name); suiteAction.setInvoker(invoker); suiteAction.setBeforeSleep(Long.parseLong(actionBeforeSleep)); suiteAction.setAfterSleep(Long.parseLong(actionAfterSleep)); suiteAction.setRepeat(Integer.parseInt(repeat)); parseActionParam(actionEle, suiteAction); actionList.add(suiteAction); } }
/** * Extracts the value of the supplied attribute * * @param element * element with attribute in question * @param attributeName * name of the attribute in question * @return value of the attribute in question * @throws GenericArtifactParsingException * exception s thrown is attribute is missing */ private static String getAttributeValue(Element element, String attributeName) throws GenericArtifactParsingException { // TODO Cash constructed XPath objects? // XPath xpath = new DefaultXPath("@" + CCF_NAMESPACE_PREFIX + ":" + // attributeName); XPath xpath = new DefaultXPath("@" + attributeName); xpath.setNamespaceURIs(ccfNamespaceMap); Node attributeNode = xpath.selectSingleNode(element); if (attributeNode == null) throw new GenericArtifactParsingException("Missing attribute: " + attributeName + " in element " + element.getName()); else return attributeNode.getText(); }
/** * Extracts the value of the supplied attribute without throwing an * exception if missing * * @param element * element with attribute in question * @param attributeName * name of the attribute in question * @return value of the attribute in question, null if attribute is missing * */ private static String getAttributeValueWithoutException(Element element, String attributeName) { // TODO Cash constructed XPath objects? // XPath xpath = new DefaultXPath("@" + CCF_NAMESPACE_PREFIX + ":" + // attributeName); XPath xpath = new DefaultXPath("@" + attributeName); xpath.setNamespaceURIs(ccfNamespaceMap); Node attributeNode = xpath.selectSingleNode(element); if (attributeNode == null) return null; else return attributeNode.getText(); }
private int[] extractBoxConstraint(Element root) { int[] result = new int[4]; String nodeName = currentToken.getNode().getName(); XPath xPath = new DefaultXPath("//node[@name='" + nodeName + "']"); Element node = (Element) xPath.selectSingleNode(root); result[0] = Integer.valueOf(node.attribute("x").getValue()).intValue(); result[1] = Integer.valueOf(node.attribute("y").getValue()).intValue(); result[2] = Integer.valueOf(node.attribute("width").getValue()).intValue(); result[3] = Integer.valueOf(node.attribute("height").getValue()).intValue(); return result; }
private int[] extractBoxConstraint(Element root, Token token) { int[] result = new int[4]; String nodeName = token.getNode().getName(); XPath xPath = new DefaultXPath("//node[@name='" + nodeName + "']"); Element node = (Element) xPath.selectSingleNode(root); result[0] = Integer.valueOf(node.attribute("x").getValue()).intValue(); result[1] = Integer.valueOf(node.attribute("y").getValue()).intValue(); result[2] = Integer.valueOf(node.attribute("width").getValue()).intValue(); result[3] = Integer.valueOf(node.attribute("height").getValue()).intValue(); return result; }
public void render(final InputStream in, final OutputStream out, final Map<String , String> config) throws Exception { SAXReader reader = new SAXReader(); Document document = reader.read(in); XPath xpath = new DefaultXPath("/l:LexML/l:Metadado/l:Identificacao"); Map<String,String> ns = new HashMap<String,String>(); ns.put("l","http://www.lexml.gov.br/1.0"); xpath.setNamespaceURIs(ns); Node n = xpath.selectSingleNode(document); //System.out.println("n:" + n); //System.out.println("attributes: " + ((Element) n).attributes()); String urn = ((Element)n).attributeValue("URN"); //System.out.println("urn:" + urn); String[] urnComps = urn.split(":"); String autoridade = urnComps[3]; String tipoNorma = urnComps[4]; RendererPDFContext ctx = new RendererPDFContext(autoridade,tipoNorma); ctx.addConfig(config); Element root = document.getRootElement(); ctx.setOutputStream(out); new PDFBuilder(ctx, root).build(); }
/** * @param browser * @param ver * @param os 操作系统名称 * @param arch CUP架构(32或者64位) * @return 找不到返回null */ public String getUrl(String browser, String ver, String os, String arch) { String xpathStr = String.format("//drivers/driver[@type='%s']/supports/browser[@version='%s']", browser, ver); XPath xpath = new DefaultXPath(xpathStr); String path = null; @SuppressWarnings("unchecked") List<Element> nodes = xpath.selectNodes(document); String driverVer = null; for(Element ele : nodes) { @SuppressWarnings("unchecked") List<Element> itemList = ele.getParent().getParent().element("items").elements("item"); for(Element item : itemList) { if(os.equals(item.attributeValue("os")) && arch.equals(item.attributeValue("arch"))) { path = item.attributeValue("path"); break; } } driverVer = ele.getParent().getParent().attributeValue("version"); break; } if(driverVer != null && !driverVer.trim().equals("")) { String base = document.getRootElement().attributeValue("base"); String subPath = ""; for(Browser bro : browserList()) { if(browser.equals(bro.getName())) { subPath = bro.getPath(); break; } } if("win32".equals(os)) { arch = ""; } path = base + subPath + "/" + driverVer + "/" + browser + "driver_" + os + arch + ".zip"; } else { path = null; } return path; }
/** * @return 支持的浏览器以及版本列表 */ public Map<String, Set<String>> supportBrowser() { Map<String, Set<String>> browserMap = new HashMap<String, Set<String>>(); String xpathStr = String.format("//drivers/driver"); XPath xpath = new DefaultXPath(xpathStr); @SuppressWarnings("unchecked") List<Element> nodes = xpath.selectNodes(document); for(Element ele : nodes) { String type = ele.attributeValue("type"); Set<String> verList = new TreeSet<String>(new Comparator<String>(){ @Override public int compare(String o1, String o2) { return o2.compareTo(o1); } }); @SuppressWarnings("unchecked") List<Element> verEleList = ele.element("supports").elements("browser"); for(Element verEle : verEleList) { String ver = verEle.attributeValue("version"); verList.add(ver); } Set<String> oldVerList = browserMap.get(type); if(oldVerList == null) { browserMap.put(type, verList); } else { oldVerList.addAll(verList); } } return browserMap; }
@Override protected XPath initialValue() { return new DefaultXPath(this.xpath); }
@Override protected XPath initialValue() { return new DefaultXPath("descendant-or-self::text() | descendant-or-self::*/@*"); }
public XPath createObject(String key) { return new DefaultXPath(key); }