Example usage for org.jdom2 Document detachRootElement

List of usage examples for org.jdom2 Document detachRootElement

Introduction

In this page you can find the example usage for org.jdom2 Document detachRootElement.

Prototype

public Element detachRootElement() 

Source Link

Document

Detach the root Element from this document.

Usage

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  ww  .j  ava  2 s. 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:net.psexton.libuti.UtiDb.java

License:Open Source License

/**
 * Adds UTI mappings to the DB from an XML source
 * @param in InputStream containing XML data
 * @throws IOException if there was a problem reading the InputStream
 * @throws JDOMException if there was a problem parsing the XML
 *//*from  w ww.  ja  v  a  2 s .  com*/
public final void importXmlData(InputStream in) throws IOException, JDOMException {
    // Parse the input stream and rip out a usable Element from the Document
    Document doc = new SAXBuilder().build(in);
    Element root = doc.detachRootElement();

    // root element is <uti-list>
    // Iterate over all <uti> children
    for (Object o : root.getChildren("uti")) {
        Element uti = (Element) o;
        // UTI's name is in a <name> child
        String name = uti.getChildText("name");
        conformances.addVertex(name); // Add UTI to graph
        // File suffixes are in <suffix> children
        // Iterate over them
        for (Object o2 : uti.getChildren("suffix")) {
            Element suffix = (Element) o2;
            if (suffix.getAttribute("preferred") != null
                    && suffix.getAttribute("preferred").getBooleanValue()) {
                reverseSuffixTable.put(name, suffix.getText()); // Add UTI->suffix to reverseSuffixTable
            }
            suffixTable.put(suffix.getText(), name); // Add suffix->UTI to suffixTable
        }
        // Conformances are in <conforms-to> children
        // Iterate over them
        for (Object o2 : uti.getChildren("conforms-to")) {
            Element conformsTo = (Element) o2;
            String parentUtiName = conformsTo.getText();
            String edgeName = name + "->" + parentUtiName;
            conformances.addEdge(edgeName, name, parentUtiName, EdgeType.DIRECTED);
        }
    }
}

From source file:org.artifactory.logging.version.v1.LogbackConfigSwapper.java

License:Open Source License

/**
 * Replaces the content of the given logback configuration with the content of the latest
 *
 * @param doc Logback configuration/*from   ww w .  j a  va 2  s . c  om*/
 */
@Override
public void convert(Document doc) {
    //Get the updated config
    InputStream newConfigFile = getClass()
            .getResourceAsStream("/META-INF/default/" + ArtifactoryHome.LOGBACK_CONFIG_FILE_NAME);
    if (newConfigFile == null) {
        log.error("Replacement logback configuration file was not found in '/META-INF/default/'.");
        return;
    }

    doc.detachRootElement();
    doc.setRootElement(XmlUtils.parse(newConfigFile).detachRootElement());
}