public PositionTester(String uri, int line, int col) throws IOException, SAXException, XPathExpressionException { InputStream is = new FileInputStream(new File(uri)); Document document = PositionalXMLReader.readXML(is); XPathFactory xFactory = XPathFactory.newInstance(); XPath xpath = xFactory.newXPath(); XPathExpression expr = xpath.compile("//*"); Object result = expr.evaluate(document, XPathConstants.NODESET); NodeList allNodes = (NodeList) result; int i = 0; Node node; while (allNodes.item(i) != null) { node = allNodes.item(i); i++; int ln = (Integer) node.getUserData("lineNumber"); int cn = (Integer) node.getUserData("columnNumber"); if (line == ln && col == cn) { element = node; break; } } }
@JRubyMethod public IRubyObject root(ThreadContext context) { Node rootNode = getDocument().getDocumentElement(); try { Boolean isValid = (Boolean) rootNode.getUserData(NokogiriHelpers.VALID_ROOT_NODE); if (!isValid) return context.getRuntime().getNil(); } catch (NullPointerException e) { // does nothing since nil wasn't set to the root node before. } if (rootNode == null) return context.getRuntime().getNil(); else return getCachedNodeOrCreate(context.getRuntime(), rootNode); }
/** 将一个DOM结点封装成XmlNode, 对同一个DOM结点, 当调用此方法时每次 都返回同一个XmlNode实例. */ public static XmlNode valueOf(Node dom) { if (dom == null) { return null; } else { XmlNode node = (XmlNode) dom.getUserData(XML_NODE_KEY); if (node == null) { node = new RealNode(dom); dom.setUserData(XML_NODE_KEY, node, null); } return node; } }
private FailureLocation getFailureLocation(List<Document> docs, XNode fNode) { if (fNode == null) { return null; } XPathUtils xpather = new XPathUtils(fNode.getNSMap()); for (Document doc : docs) { Node node = (Node) xpather.getValue(fNode.toString(), doc, XPathConstants.NODE); if (null != node) { try { return new FailureLocation((Location) node.getUserData("location"), doc.getDocumentURI()); } catch (Exception ex) { // ignore, probably not DOM level 3 } } } return null; }
private static XmlNode getUserData(Node node) { if (DOM_LEVEL_3) { return (XmlNode) node.getUserData(USER_DATA_XMLNODE_KEY); } return null; }