/** * Select all attributes matching a given expression within the given object. * * @param root * The object from which the expression is to begin its application. * @param expression * The XPath expression to be evaluated. * @return A {@link List} of {@link Attribute} objects representing all XML element attributes that match the * given XPath expression. * @throws JDOMException * If any errors occur during the analysis. */ private List<Attribute> selectAttributeInternal(final Object root, final String expression) throws JDOMException { return XPathFactory.instance().compile(expression, Filters.attribute()).evaluate(root); }
private void filterLang(org.jdom2.Document j2Doc) { XPathFactory xpfac = XPathFactory.instance(); XPathExpression<Attribute> xp = xpfac.compile("//*/@lang", Filters.attribute()); for (Attribute att : xp.evaluate(j2Doc)) { if(!att.getValue().equals(lang)) { att.getParent().detach(); } } }
/** * update the disks in the domain XML. It is assumed that the the size of the volumes is the same as the number of * disk elements and that the order is the same. */ public static void updateDisks(Document domainXml, List<StorageVol> volumes) throws LibvirtException { XPathFactory xpf = XPathFactory.instance(); XPathExpression<Element> diskExpr = xpf.compile(XPATH_DISK, Filters.element()); XPathExpression<Attribute> fileExpr = xpf.compile(XPATH_DISK_FILE, Filters.attribute()); List<Element> disks = diskExpr.evaluate(domainXml); Iterator<StorageVol> cloneDiskIter = volumes.iterator(); for (Element disk : disks) { Attribute file = fileExpr.evaluateFirst(disk); StorageVol cloneDisk = cloneDiskIter.next(); file.setValue(cloneDisk.getPath()); } }
private void filterLang(org.jdom2.Document j2Doc) { XPathFactory xpfac = XPathFactory.instance(); XPathExpression<Attribute> xp = xpfac.compile("//*/@classifier", Filters.attribute()); for (Attribute att : xp.evaluate(j2Doc)) { boolean remove = true; for(String classifier : classifiers) { if(att.getValue().equals(classifier)) { remove = false; } } if(remove){att.getParent().detach();} } } }
/** Get the disks connected to the domain. */ public static List<Disk> getDisks(Connect connect, Document domainXml) { try { List<Disk> ret = new ArrayList<>(); XPathFactory xpf = XPathFactory.instance(); XPathExpression<Element> diskExpr = xpf.compile(XPATH_DISK, Filters.element()); XPathExpression<Attribute> typeExpr = xpf.compile(XPATH_DISK_TYPE, Filters.attribute()); XPathExpression<Attribute> fileExpr = xpf.compile(XPATH_DISK_FILE, Filters.attribute()); XPathExpression<Attribute> devExpr = xpf.compile(XPATH_DISK_DEV, Filters.attribute()); List<Element> disks = diskExpr.evaluate(domainXml); for (Element disk : disks) { Attribute type = typeExpr.evaluateFirst(disk); Attribute file = fileExpr.evaluateFirst(disk); Attribute dev = devExpr.evaluateFirst(disk); StorageVol volume = LibvirtUtil.findVolume(connect, file.getValue()); ret.add(new Disk(dev.getValue(), file.getValue(), volume, type.getValue())); } return ret; } catch (LibvirtException e) { throw new LibvirtRuntimeException(e); } }
XPathExpression<Attribute> hintPath = XPathFactory.instance().compile("password/@hint", Filters.attribute()); Attribute hintAttr = hintPath.evaluateFirst(u); String hint = hintAttr == null ? null : hintAttr.getValue();