Example usage for org.jdom2 Element removeAttribute

List of usage examples for org.jdom2 Element removeAttribute

Introduction

In this page you can find the example usage for org.jdom2 Element removeAttribute.

Prototype

public boolean removeAttribute(final Attribute attribute) 

Source Link

Document

This removes the supplied Attribute should it exist.

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
 *//* www  .ja  v a  2s.  c  o 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:com.rometools.rome.io.impl.RSS20Generator.java

License:Open Source License

@Override
public void populateItem(final Item item, final Element eItem, final int index) {

    super.populateItem(item, eItem, index);

    final Element description = eItem.getChild("description", getFeedNamespace());
    if (description != null) {
        description.removeAttribute("type");
    }//from  w w  w  .j  a v a2  s.com

    final String author = item.getAuthor();
    if (author != null) {
        eItem.addContent(generateSimpleElement("author", author));
    }

    final String comments = item.getComments();
    if (comments != null) {
        eItem.addContent(generateSimpleElement("comments", comments));
    }

    final Guid guid = item.getGuid();
    if (guid != null) {
        final Element eGuid = generateSimpleElement("guid", guid.getValue());
        if (!guid.isPermaLink()) {
            eGuid.setAttribute("isPermaLink", "false");
        }
        eItem.addContent(eGuid);
    }
}

From source file:com.sun.syndication.io.impl.RSS20Generator.java

License:Open Source License

public void populateItem(Item item, Element eItem, int index) {
    super.populateItem(item, eItem, index);

    Element eDescription = eItem.getChild("description", getFeedNamespace());
    if (eDescription != null)
        eDescription.removeAttribute("type");

    String author = item.getAuthor();
    if (author != null) {
        eItem.addContent(generateSimpleElement("author", author));
    }/*from w w w .  ja  va  2 s .com*/

    String comments = item.getComments();
    if (comments != null) {
        eItem.addContent(generateSimpleElement("comments", comments));
    }

    Guid guid = item.getGuid();
    if (guid != null) {
        Element eGuid = generateSimpleElement("guid", guid.getValue());
        if (!guid.isPermaLink()) {
            eGuid.setAttribute("isPermaLink", "false");
        }
        eItem.addContent(eGuid);
    }
}

From source file:Contabilidad.CodeBase64.java

public void limpia(String ruta) {
    try {//  w  w  w  .j  a  va  2 s . co  m
        org.jdom2.Document doc = new SAXBuilder().build(ruta);
        Element rootNode = doc.getRootElement();
        List list = rootNode.getContent();
        for (int i = 0; i < list.size(); i++) {
            Content elementos = (Content) list.get(i);
            if (elementos.getCType() == Content.CType.Element) {
                Element aux = (Element) elementos;
                if (aux.getName().compareToIgnoreCase("Addenda") == 0) {
                    List list2 = aux.getContent();
                    for (int j = 0; j < list2.size(); j++) {
                        Content elementos2 = (Content) list2.get(j);
                        if (elementos2.getCType() == Content.CType.Element) {
                            Element aux2 = (Element) elementos2;
                            if (aux2.getName().compareToIgnoreCase("FactDocMX") == 0) {
                                list2.remove(aux2);
                            }
                            if (aux2.getName().compareToIgnoreCase("ECFD") == 0) {
                                Namespace NP = Namespace.getNamespace("", "");
                                aux2.setNamespace(NP);
                                List list3 = aux2.getContent();
                                for (int k = 0; k < list3.size(); k++) {
                                    Content elementos3 = (Content) list3.get(k);
                                    if (elementos3.getCType() == Content.CType.Element) {
                                        Element aux3 = (Element) elementos3;
                                        aux3.setNamespace(NP);
                                        List list4 = aux3.getContent();
                                        for (int l = 0; l < list4.size(); l++) {
                                            Content elementos4 = (Content) list4.get(l);
                                            if (elementos4.getCType() == Content.CType.Element) {
                                                Element aux4 = (Element) elementos4;
                                                aux4.setNamespace(NP);
                                                List list5 = aux4.getContent();
                                                for (int m = 0; m < list5.size(); m++) {
                                                    Content elementos5 = (Content) list5.get(m);
                                                    if (elementos5.getCType() == Content.CType.Element) {
                                                        Element aux5 = (Element) elementos5;
                                                        aux5.setNamespace(NP);
                                                        List list6 = aux5.getContent();
                                                        for (int n = 0; n < list6.size(); n++) {
                                                            Content elementos6 = (Content) list6.get(n);
                                                            if (elementos6
                                                                    .getCType() == Content.CType.Element) {
                                                                Element aux6 = (Element) elementos6;
                                                                aux6.setNamespace(NP);
                                                                List list7 = aux6.getContent();
                                                                for (int p = 0; p < list7.size(); p++) {
                                                                    Content elementos7 = (Content) list7.get(p);
                                                                    if (elementos7
                                                                            .getCType() == Content.CType.Element) {
                                                                        Element aux7 = (Element) elementos7;
                                                                        aux7.setNamespace(NP);
                                                                        List list8 = aux7.getContent();
                                                                        for (int q = 0; q < list8.size(); q++) {
                                                                            Content elementos8 = (Content) list8
                                                                                    .get(q);
                                                                            if (elementos8
                                                                                    .getCType() == Content.CType.Element) {
                                                                                Element aux8 = (Element) elementos8;
                                                                                aux8.setNamespace(NP);
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                                List atributos = aux2.getAttributes();
                                for (int a = 0; a < atributos.size(); a++) {
                                    Attribute at = (Attribute) atributos.get(a);
                                    if (at.getName().compareToIgnoreCase("schemaLocation") == 0)
                                        aux2.removeAttribute(at);
                                }
                            }
                        }
                    }
                }
            }
        }
        XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
        try {
            outputter.output(doc, new FileOutputStream(ruta));
        } catch (IOException e) {
            System.out.println(e);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:devicemodel.conversions.XmlConversions.java

public static DeviceNode xmlToNode(Element e, String id) {

    String[] ids = new String[] { "" };

    if (e.getAttribute("ids") != null) {
        ids = e.getAttributeValue("ids").split(",");
        e.removeAttribute("ids");
    }//from  w w w.  j  a  va 2  s  . c o  m

    DeviceNode node = new DeviceNode(e.getName() + id);

    node.setValue(e.getTextTrim());

    for (Attribute a : e.getAttributes()) {
        node.getAttributes().put(a.getName(), a.getValue());
    }

    for (String cid : ids) {
        for (Element c : e.getChildren()) {
            try {
                node.addChild(xmlToNode(c, cid));
            } catch (Exception ex) {
            }
        }
    }

    return node;
}

From source file:eu.himeros.hocr.FlatXml.java

License:Open Source License

private void init(File inFile, File outFile) throws Exception {
    SAXBuilder builder = new SAXBuilder();
    Document doc = builder.build(inFile);
    Element root = doc.getRootElement();
    Namespace oldns = root.getNamespace();
    Element newRoot = new Element("html", "http://www.w3.org/1999/xhtml");
    Namespace xmlns = newRoot.getNamespace();
    Element head = root.getChild("head", oldns);
    head.setNamespace(xmlns);//from  w w w  .  ja  va 2s. c  o m
    for (Element child : head.getChildren())
        child.setNamespace(xmlns);
    Element title = new Element("title", xmlns);
    title.addContent("ocr");
    if (head != null)
        head.addContent(title);
    Element body = root.getChild("body", oldns);
    body.setNamespace(xmlns);
    /*Element oldPage;
    try{
    oldPage=body.getChild("div",xmlns);
    }catch(Exception ex){
    oldPage=new Element("div",xmlns);
    }*/
    Element page = new Element("div", xmlns);
    page.setAttribute("class", "ocr_page");
    page.setAttribute("id", "i" + inFile.getName().substring(1).replace(".html", ".png"));
    XPathExpression<Element> xpath = XPathFactory.instance().compile("//*[@class='ocr_carea']",
            Filters.element(), null, Namespace.getNamespace("ns", "http://www.w3.org/1999/xhtml"));
    List<Element> careaElL = xpath.evaluate(body);
    for (Element careaEl : careaElL) {
        page.addContent(new Comment("<div class=\"" + careaEl.getAttributeValue("class") + "\" title=\""
                + careaEl.getAttributeValue("title") + "\">"));
        for (Element pEl : careaEl.getChildren()) {
            page.addContent(new Comment("<p>"));
            for (Element lineEl : pEl.getChildren()) {
                lineEl.removeAttribute("id");
                lineEl.setNamespace(xmlns);
                for (Element child : lineEl.getChildren()) {
                    child.removeAttribute("id");
                    child.removeAttribute("lang");
                    child.removeAttribute("lang", xmlns);
                    child.setNamespace(xmlns);
                }
                page.addContent(lineEl.clone());
            }
            page.addContent(new Comment("</p>"));
        }
        page.addContent(new Comment("</div>"));
    }
    //oldPage.detach();
    if (body != null) {
        body.removeContent();
        body.addContent(page);
    }
    newRoot.addContent(root.removeContent());
    doc.detachRootElement();
    doc.setRootElement(newRoot);
    XMLOutputter xmlOutputter = new XMLOutputter(Format.getPrettyFormat());
    xmlOutputter.output(doc, new BufferedWriter(new FileWriter(outFile)));
}

From source file:eu.himeros.hocr.HocrInfoAggregator.java

License:Open Source License

private void makeCompliantHocr() {
    xpath = XPathFactory.instance().compile("//ns:span[@id|@idx]", Filters.element(), null,
            Namespace.getNamespace("ns", "http://www.w3.org/1999/xhtml"));
    List<Element> elements = xpath.evaluate(root);
    int spanId = 0;
    for (Element span : elements) {
        if (span.getAttribute("idx") != null) {
            try {
                span = span.getChildren().get(0);
            } catch (Exception ex) {
                //
            }/* w w w  . j ava2  s  .  c  o m*/
        }
        LinkedList<Attribute> attributeLl = new LinkedList(span.getParentElement().getAttributes());
        attributeLl.addFirst(new Attribute("id", "w_" + spanId++));
        span.getParentElement().setAttributes(attributeLl);
        String[] suggestions = null;
        String title = span.getAttributeValue("title");
        if (title != null) {
            suggestions = title.split(" ");
        }
        if (suggestions == null) {
            suggestions = new String[] { "" };
        }
        Element ins = new Element("ins", xmlns);
        ins.setAttribute("class", "alt");
        ins.setAttribute("title", makeNlp(span.getAttributeValue("class")));
        ins.setText(span.getText());
        span.removeContent();
        span.addContent(ins);
        span.setAttribute("class", "alternatives");
        span.removeAttribute("uc");
        span.removeAttribute("occ");
        span.removeAttribute("title");
        span.removeAttribute("anchor");
        span.removeAttribute("anchor-id");
        span.removeAttribute("id");
        span.getParentElement().removeAttribute("idx");
        span.removeAttribute("whole");
        span.getParentElement().removeAttribute("whole");
        if (title == null || "".equals(title)) {
            continue;
        }
        double score = 0.90;
        for (String suggestion : suggestions) {
            if (suggestion == null || "".equals(suggestion)) {
                continue;
            }
            Element del = new Element("del", xmlns);
            del.setAttribute("title", "nlp " + String.format("%.2f", score).replaceAll(",", "."));
            score = score - 0.01;
            suggestion = suggestion.replaceAll(l1PunctMarkFilter, "");
            Matcher leftMatcher = l1LeftPunctMarkPattern.matcher(ins.getText());
            if (leftMatcher.matches()) {
                suggestion = leftMatcher.group(1) + suggestion;
            }
            Matcher rightMatcher = l1RightPunctMarkPattern.matcher(ins.getText());
            if (rightMatcher.matches()) {
                String ngtSymbol = "";
                if (suggestion.endsWith("\u261a")) {
                    ngtSymbol = "\u261a";
                    suggestion = suggestion.substring(0, suggestion.length() - 1);
                }
                suggestion = suggestion + rightMatcher.group(1) + ngtSymbol;
            }
            ///!!!!
            if (suggestion.endsWith("\u261a") && ins.getParentElement().getParentElement()
                    .getAttributeValue("lang", Namespace.XML_NAMESPACE) != null) {
                String buff = suggestion.substring(0, suggestion.length() - 1);
                sa.align(buff, ins.getText());
                double sim = 1 - sa.getEditDistance()
                        / Math.max((double) buff.length(), (double) ins.getText().length());
                if (sim > 0.6) {

                    suggestion = ins.getText() + "\u261b";
                    ins.setText(buff);
                    ins.setAttribute("title", "nlp 0.70");
                }
            }
            del.addContent(suggestion);
            span.addContent(del);
        }
    }
}

From source file:no.imr.stox.functions.utils.JDOMUtils.java

public static void setAttribute(Element elm, boolean atStart, String name, String text) {
    List<Attribute> atts = new ArrayList<>(elm.getAttributes());
    if (atStart) {
        atts.stream().forEach(a -> elm.removeAttribute(a));
    }//from   w  w w.  j ava  2  s .c om
    elm.setAttribute(name, text);
    if (atStart) {
        atts.stream().forEach(a -> elm.setAttribute(a.getName(), a.getValue()));
    }
}

From source file:org.artifactory.update.md.v230.BaseRepoPathClassConverter.java

License:Open Source License

@Override
public void convert(Document doc) {
    Element rootElement = doc.getRootElement();
    Element repoPath = rootElement.getChild("repoPath");
    if (repoPath != null) {
        if (repoPath.getAttribute("class") != null) {
            repoPath.removeAttribute("class");
        }//  w w w . ja  va  2  s  .  c o m
    }
}

From source file:org.jumpmind.metl.core.runtime.component.XmlFormatter.java

License:Open Source License

private void removeAllAttributes(Element element) {
    List<Attribute> attributes = new ArrayList<Attribute>();
    attributes.addAll(element.getAttributes());
    for (Attribute attribute : attributes) {
        element.removeAttribute(attribute);
    }//  w w  w.j a va 2  s .  com
}