public static List<CMLMolecule> extractTopLevelMolecules(Node node) { String xpath = "//*[not(local-name()='molecule')]" + "/*[local-name()='molecule'] | self::*[local-name()='molecule']"; return CMLUtil.convertNodesToMoleculeList(node.query(xpath)); }
/** * convenience routine to get query nodes (iterating through get(i) is * fragile if nodes are removed) * * @param node * @param xpath * @return list of nodes (immutable) - empty if none or null node */ public static List<Node> getQueryNodes(Node node, String xpath) { List<Node> nodeList = new ArrayList<Node>(); if (node != null) { Nodes nodes = node.query(xpath); for (int i = 0; i < nodes.size(); i++) { nodeList.add(nodes.get(i)); } } return nodeList; }
/** * convenience routine to get query nodes (iterating thorugh get(i) is * fragile if nodes are removed) * * @param node * (can be null) * @param xpath * xpath relative to node * @param context * @return list of nodes (immutable) - empty if none */ public static List<Node> getQueryNodes(Node node, String xpath, XPathContext context) { List<Node> nodeList = new ArrayList<Node>(); if (node != null) { Nodes nodes = node.query(xpath, context); for (int i = 0; i < nodes.size(); i++) { nodeList.add(nodes.get(i)); } } return nodeList; }
public static Nodes extractNodesByXpath(final String xpath, final Node node, final int expectedNumberOfNodes, final boolean throwException) throws XMLParsingException { Nodes nodes = node.query(xpath); if (nodes.size() == expectedNumberOfNodes) { return nodes; } if (throwException) { throw new XMLParsingException("Expected " + expectedNumberOfNodes + " to be found with xPath " + xpath + ", found " + nodes.size()); } return nodes; }
public static Nodes extractNodesByXpath(final String xpath, final Node node, final NumberOfNodes expectedNumberOfNodes) throws MissingNodeException, OverissueNodeException, NotOneNodeException { Nodes nodes = node.query(xpath); switch (expectedNumberOfNodes) { case ARBITRARY: { break; } case AT_LEAST_ONE: { if (nodes.size() < 1) { throw new MissingNodeException(xpath, node); } break; } case AT_MOST_ONE: { if (nodes.size() > 1) { throw new OverissueNodeException(xpath, nodes, node); } break; } case EXACT_ONE: { if (nodes.size() != 1) { throw new NotOneNodeException(xpath, nodes, node); } break; } default: throw new AssertionError("Found not handled value[" + expectedNumberOfNodes.name() + "]!"); } return nodes; }
/** * <p> * Returns the first Attribute with name attrName from Document doc. Uses xPath "// * * @throws org.openbase.jul.extension.xml.exception.XMLParsingException * @param throwException flag set throw exception if no such Attribute can be found. <br> * @param attrName * @param doc * @return */ public static String extractAttributeValue(final String attrName, final Node doc, final boolean throwException) throws XMLParsingException { String xpath = "descendant-or-self::*/@" + attrName; Nodes nodes = doc.query(xpath); for (int i = 0; i < nodes.size(); i++) { if (nodes.get(i) instanceof Attribute) { return nodes.get(i).getValue(); } } if (throwException) { throw new XMLParsingException("No Attribute " + attrName + " in document:\n" + doc.toXML()); } else { return null; } }
schema.setName(nSchema.getAttribute(ATTR_NAME)).setAlias(nSchema.getAttribute(ATTR_ALIAS, schema.getName())); Nodes nTables = nSchema.getNode().query("child::table"); for (int i = 0; i < nTables.size(); i++) { INodeHolder nTable = holderFactory.newHolder(nTables.get(i)); schema.add(table); Nodes nColumns = nTable.getNode().query("child::column"); for (int j = 0; j < nColumns.size(); j++) { INodeHolder nColumn = holderFactory.newHolder(nColumns.get(j));