Example usage for org.jdom2.filter Filters textOnly

List of usage examples for org.jdom2.filter Filters textOnly

Introduction

In this page you can find the example usage for org.jdom2.filter Filters textOnly.

Prototype

public static final Filter<Text> textOnly() 

Source Link

Document

Return a Filter that matches any Text data (excludes CDATA instances).

Usage

From source file:at.ac.tuwien.ims.latex2mobiformulaconv.converter.mathml2html.FormulaConverter.java

License:Open Source License

/**
 * Replaces all formulas with the html representation of the mapped formula objects
 *
 * @param doc        JDOM Document where to replace the formulas
 * @param formulaMap Map of the indexed Formula Objects
 * @return JDOM Document with replaced formulas
 *///  ww w.  ja  va2 s .  co  m
public Document replaceFormulas(Document doc, Map<Integer, Formula> formulaMap) {
    List<Element> foundFormulas = xpath.evaluate(doc);

    if (foundFormulas.size() > 0) {
        Map<String, Element> formulaMarkupMap = new HashMap<>();

        // Initialize markup map
        for (Element element : foundFormulas) {
            formulaMarkupMap.put(element.getAttribute("id").getValue(), element);
        }

        // Replace all found formulas
        Iterator<Integer> formulaIterator = formulaMap.keySet().iterator();
        while (formulaIterator.hasNext()) {
            Integer id = formulaIterator.next();

            Element formulaMarkupRoot = formulaMarkupMap.get(FORMULA_ID_PREFIX + id);
            Formula formula = formulaMap.get(id);

            formulaMarkupRoot.removeAttribute("class");
            formulaMarkupRoot.removeContent();
            formulaMarkupRoot.setName("div");

            Element div = (Element) formulaMarkupRoot.getParent();
            div.setName("div");
            div.setAttribute("class", "formula");

            // Potentially there's text inside the paragraph...
            List<Text> texts = div.getContent(Filters.textOnly());
            if (texts.isEmpty() == false) {
                String textString = "";
                for (Text text : texts) {
                    textString += text.getText();
                }
                Element textSpan = new Element("span");
                textSpan.setAttribute("class", "text");
                textSpan.setText(textString);
                div.addContent(textSpan);

                List<Content> content = div.getContent();
                content.removeAll(texts);
            }

            if (generateDebugMarkup) {
                div.setAttribute("style", "border: 1px solid black;");

                // Header
                Element h4 = new Element("h4");
                h4.setText("DEBUG - Formula #" + formula.getId());
                div.addContent(h4);

                // Render LaTeX source
                Element latexPre = new Element("pre");
                latexPre.setAttribute("class", "debug-latex");
                latexPre.setText(formula.getLatexCode());
                div.addContent(latexPre);

                // Render MathML markup
                Element mathmlPre = new Element("pre");
                mathmlPre.setAttribute("class", "debug-mathml");
                mathmlPre.setText(formula.getMathMl());
                div.addContent(mathmlPre);

                // Render HTML Markup
                Element htmlPre = new Element("pre");
                htmlPre.setAttribute("class", "debug-html");
                XMLOutputter xmlOutputter = new XMLOutputter();
                xmlOutputter.setFormat(Format.getRawFormat());
                htmlPre.setText(xmlOutputter.outputString(formula.getHtml()));

                div.addContent(htmlPre);

            }

            // Set formula into
            formulaMarkupRoot.addContent(formula.getHtml());
        }
    }
    return doc;
}

From source file:ec.edu.cedia.redi.ldclient.provider.ScopusAuthorProvider.java

License:Apache License

/**
 * Maps each author from XML to RDF using default implementation of
 * {@link AbstractXMLDataProvider#parseResponse}.
 *
 * @see//w ww .  j av a2s  .  c  o m
 * <a href="http://api.elsevier.com/documentation/AUTHORSearchAPI.wadl">Authors
 * search API.</a>
 *
 * @param input
 * @param resource
 * @param requestUrl
 * @param triples
 * @param contentType
 * @return list of resources of authors found.
 * @throws DataRetrievalException
 */
private List<String> parseResponseAuthorsSearch(InputStream input, String resource, String requestUrl,
        Model triples, String contentType) throws DataRetrievalException {
    try {

        // List of authors to extract perfil information such as publications, affiliations, etc.
        List<String> authorsFound = new ArrayList();
        ValueFactory vf = ValueFactoryImpl.getInstance();
        // Keep stream for various reads.
        byte[] response = IOUtils.toByteArray(input);
        final Document doc = new SAXBuilder(XMLReaders.NONVALIDATING).build(new ByteArrayInputStream(response));
        // get only URI of authors
        XPathExpression<Text> path = XPathFactory.instance().compile(
                "/atom:search-results/atom:entry/prism:url/text()", Filters.textOnly(), null,
                Namespace.getNamespace("atom", "http://www.w3.org/2005/Atom"),
                Namespace.getNamespace("prism", "http://prismstandard.org/namespaces/basic/2.0/"));
        // Map each author XML to RDF using default implementationf parseResponse method from AbstractXMLDataProvider.
        List<Text> auhtorsFound = path.evaluate(doc);
        for (int i = 0; i < auhtorsFound.size(); i++) {
            setAuthorXPathMappings(i);
            String authorsResource = auhtorsFound.get(i).getValue();
            super.parseResponse(authorsResource, requestUrl, triples, new ByteArrayInputStream(response),
                    contentType);
            authorsFound.add(
                    authorsResource + "?apiKey=" + apiKey + "&httpAccept=application/rdf%2Bxml&view=ENHANCED");
            triples.add(vf.createURI(authorsResource), OWL.ONEOF, vf.createURI(resource));
        }
        return authorsFound;
    } catch (JDOMException | IOException | DataRetrievalException ex) {
        throw new DataRetrievalException(ex);
    }
}