public void outputPretty(PrintWriter out, Annotation annotation) throws ServletException { try { Document input = XMLOutputter.annotationToDoc(annotation, pipeline); Nodes output = corenlpTransformer.transform(input); for (int i = 0; i < output.size(); i++) { out.print(output.get(i).toXML()); } } catch (RuntimeException e) { throw e; } catch (Exception e) { throw new ServletException(e); } }
/** * @return list of peak or peakGroup children (not descendants) */ public List<PeakOrGroup> getPeakOrGroupChildren() { Nodes nodes = this.query("./cml:peak | ./cml:peakGroup", CMLConstants.CML_XPATH); List<PeakOrGroup> peaks = new ArrayList<PeakOrGroup>(); for (int i = 0; i < nodes.size(); i++) { peaks.add((PeakOrGroup) nodes.get(i)); } return peaks; }
XMLReader tagsoup = XMLReaderFactory.createXMLReader("org.ccil.cowan.tagsoup.Parser"); Builder bob = new Builder(tagsoup); Document html = bob.build("http://www.yahoo.com"); Nodes images = html.query("//img"); for (int index = 0; index < images.size(); index++) { Element image = (Element) images.get(index); String src = image.getAttribute("src").getValue(); // do something with it... }
/** process all instances of arg in element. *TODO currently doesn't do anything * @param element */ public static void processArgs(CMLElement element) { Nodes parameterNameArgs = element.query(CMLArg.NS+"[@parameterName]", CMLConstants.CML_XPATH); for (int i = 0; i < parameterNameArgs.size(); i++) { @SuppressWarnings("unused") CMLArg arg = (CMLArg) parameterNameArgs.get(i); } }
public Element[] getDescendantElements(String name) { List<Element> descendants = new ArrayList<Element>(); Nodes nodes = xomElement.query(xpathForElementName(name), namespaceMappings); for (int i = 0; i < nodes.size(); i++) { descendants.add(new Element((nu.xom.Element)nodes.get(i))); } descendants.remove(this); return descendants.toArray(new Element[0]); }
public Element[] getDescendantElements(String name) { List<Element> descendants = new ArrayList<Element>(); Nodes nodes = xomElement.query(xpathForElementName(name), namespaceMappings); for (int i = 0; i < nodes.size(); i++) { descendants.add(new Element((nu.xom.Element)nodes.get(i))); } descendants.remove(this); return descendants.toArray(new Element[0]); }
protected static Iterator<Element> getElementsFromXPath(final String xPath, final Document document) { Nodes regNodes = document.query(xPath); List<Element> elements = new ArrayList<>(); for (int i = 0; i < regNodes.size(); i++) { try { elements.add((Element) regNodes.get(i)); } catch (ClassCastException ex) { ExceptionPrinter.printHistory(new CouldNotPerformException("Invalide data from XPath[" + xPath + "]!", ex), LOGGER); } } return elements.iterator(); }
public static Node getNode(Document doc, String entityType, String entityName) { String query = createQueryStringByName(entityType, entityName); Nodes nodes = doc.query(query); if (nodes != null && nodes.size() > 0) { return nodes.get(0); } return null; }
/** Wraps each item in the result sequence into a decorated element wrapper. */ private static Document wrapSequence(Nodes nodes) { // make a copy of the template for sequences: Element items = (Element) TEMPLATES.get(Nodes.class.getName()); items = new Element(items); int size = nodes.size(); for (int i=0; i < size; i++) { items.appendChild(wrap(nodes.get(i))); } return new Document(items); }
protected Element getElementFromXPath(final String xPath, final Document document) throws VerificationFailedException { Nodes regNodes = document.query(xPath); if (regNodes.size() != 1) { throw new VerificationFailedException("Invalide data from XPath[" + xPath + "]!"); } return (Element) regNodes.get(0); }
@Override protected void appendChildrenToNewParent(Element oldParent, Element newParent) throws SAXException { try { Nodes children = oldParent.removeChildren(); for (int i = 0; i < children.size(); i++) { newParent.appendChild(children.get(i)); } } catch (XMLException e) { fatal(e); } }
@Override protected void appendChildrenToNewParent(Element oldParent, Element newParent) throws SAXException { try { Nodes children = oldParent.removeChildren(); for (int i = 0; i < children.size(); i++) { newParent.appendChild(children.get(i)); } } catch (XMLException e) { fatal(e); } }
public static Nodes normalizeFormatting(final Nodes docs) { Nodes n = new Nodes(); for (int i = 0; i < docs.size(); i++) { n.append(normalizeFormatting(docs.get(i))); } return n; }
public static Element normalizeWhitespaceInTextNodes(Element element) { Nodes texts = element.query(".//text()"); for (int i = 0; i < texts.size(); i++) { Text text = (Text) texts.get(i); text.setValue(normalizeSpace(text.getValue())); } return element; }
@Override public ISource transform(ISource source) throws SourceException { Document d = source.getDocument(); Nodes tables = d.query("//table"); for (int i = 0; i < tables.size(); i++) { Element table = (Element) tables.get(i); table.addAttribute(new Attribute("class", getValue())); } return source; }
private void removeIrrelevantFooter(Element rootElement) { Element body = rootElement.getFirstChildElement("body"); body.removeChild(rootElement.query("//div[@class='footer']").get(0)); } }
private String categorize(Element row) { String cssClass = row.getAttributeValue("class"); if (cssClass == null) { Element cell = (Element) row.query("td").get(0); cssClass = cell.getAttributeValue("class"); } Check.notNull(cssClass, "cssClass is null"); return cssClass.toUpperCase(); }
private void removeIrrelevantFooter(Element rootElement) { Element body = rootElement.getFirstChildElement("body"); body.removeChild(rootElement.query("//div[@class='footer']").get(0)); } }
public String getOutputFragment(String inputFragment) { return new TestRig() .withFixture(this) .processFragment(inputFragment) .getXOMDocument() .query("//table").get(0) .toXML() .replaceAll("\u00A0", " "); }
public String process(String inputFragment) { return new TestRig() .withFixture(this) .processFragment(inputFragment) .getXOMDocument() .query("//table").get(0) .toXML() .replaceAll("\u00A0", " "); }}