Example usage for org.jdom2 Element detach

List of usage examples for org.jdom2 Element detach

Introduction

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

Prototype

@Override
    public Element detach() 

Source Link

Usage

From source file:nl.nn.adapterframework.util.XmlBuilder.java

License:Apache License

private Element buildElement(String value) throws JDOMException, IOException {
    StringReader stringReader = new StringReader(value);
    SAXBuilder saxBuilder = new SAXBuilder();
    Document document;/*from  ww  w  . ja  v  a2  s .c  om*/
    document = saxBuilder.build(stringReader);
    Element element = document.getRootElement();
    return element.detach();
}

From source file:org.artifactory.version.converter.v1412.IndexerCronExpPropertyConverter.java

License:Open Source License

@Override
public void convert(Document doc) {
    log.info("Converting indexer indexingIntervalHours property to a cron expression based "
            + "configuration descriptor.");

    Element rootElement = doc.getRootElement();
    Namespace namespace = rootElement.getNamespace();

    Element indexerElement = rootElement.getChild("indexer", namespace);
    if (indexerElement != null) {
        // Remove indexingIntervalHours property
        Element indexingIntervalHours = indexerElement.getChild("indexingIntervalHours", namespace);
        int intervalElementIndex = indexerElement.indexOf(indexingIntervalHours);
        if (indexingIntervalHours != null) {
            indexingIntervalHours.detach();

            // Add cron expression property
            Element cronExpElement = new Element("cronExp", namespace);
            cronExpElement.setText("0 23 5 * * ?");
            indexerElement.addContent(intervalElementIndex, cronExpElement);
        }/*from w w  w.  j a  va2s.  com*/
    }

    log.info("Finished converting the indexer indexingIntervalHours property.");
}

From source file:org.artifactory.version.converter.v1414.ArchiveBrowsingConverter.java

License:Open Source License

@Override
public void convert(Document doc) {
    Element rootElement = doc.getRootElement();
    Namespace namespace = rootElement.getNamespace();
    Element securityElement = rootElement.getChild("security", namespace);
    if (securityElement != null) {
        Element archiveBrowsingEnabledElement = securityElement.getChild("archiveBrowsingEnabled", namespace);
        if (archiveBrowsingEnabledElement != null) {
            archiveBrowsingEnabledElement.detach();
        }/*from w  w  w. j  a  v a2  s .com*/
    }
}

From source file:org.artifactory.version.converter.v144.ServerIdXmlConverter.java

License:Open Source License

@Override
public void convert(Document doc) {
    Element rootElement = doc.getRootElement();
    Namespace namespace = rootElement.getNamespace();
    Element addons = rootElement.getChild("addons", namespace);
    if (addons != null) {
        Element serverId = addons.getChild("serverId", namespace);
        if (serverId != null) {
            serverId.detach();
        }//from w  w w. j  ava 2s .  c  om
    }
}

From source file:org.dvlyyon.net.netconf.transport.ssh.AsyncSshConnection.java

License:Open Source License

/**
 * Called when a regular (non-hello) message is received. The message can be one of three types:<ol>
 * <li>rpc-reply - This would be a response to the <b>create-subscription</b> message</li>
 * <li>replayComplete - This is an indicator that says old notifications have been replayed - ignored</li>
 * <li>notification - This is an actual notification - call the registered listener</li>
 * </ol>/*w  w w. j  a va  2  s .c  om*/
 */
@Override
@SuppressWarnings("unchecked")
protected void handleResponse(Element response) {
    try {
        // We are interested in three types of responses
        // RPC-OK (in response to create-subscription); verify using the message ID and OK response
        String responseType = response.getName();
        if (responseType.equals("rpc-reply")) {
            // This must be a response to the create-subscription message; verify using the message ID
            Namespace ns = Namespace.getNamespace(BASE_NAMESPACE);
            String messageId = response.getAttributeValue("message-id");
            if (m_createSubscriptionMessageId.equals(messageId)) {
                Element ok = response.getChild("ok", ns);
                if (ok != null) {
                    s_logger.debug("Got an OK response to create-subscription.");
                    m_readyToReceiveNotifications = true;
                } else {
                    s_logger.warn("Expected an OK response to create-subscription; did not get one");
                    s_logger.warn("Actual response: " + XmlUtils.toXmlString(response));
                }
            } else {
                //s_logger.info(XmlUtils.toXmlString(response));
                s_logger.info("Reply received to keep-alive ping on async. channel");
            }
        } else if (responseType.equals("replayComplete")) {
            // We don't really care about this one
            s_logger.info("Received ReplayComplete indicator from device");
        } else if (responseType.equals("notification")) {
            Element root = null;
            if (s_logger.isDebugEnabled()) {
                s_logger.debug("Top-level notification XML: " + XmlUtils.toXmlString(response));
            }
            // Notifications (call the listener)
            s_logger.debug("Got a notification from device: " + responseType);
            Timestamp eventTime = null;
            ArrayList<Element> dataNodes = new ArrayList<Element>();
            List<Element> kids = (List<Element>) response.getChildren();
            for (Element kid : kids) {
                if (kid.getName().equals("eventTime") && kid.getNamespaceURI().equals(NOTIF_NAMESPACE)) {
                    try {
                        eventTime = new RFC3399Timestamp(kid.getText()).getSqlTimestamp();
                    } catch (final Exception ex) {
                        s_logger.warn("Error parsing event date: " + kid.getText() + " using current time");
                        ;
                        eventTime = new Timestamp(System.currentTimeMillis());
                    }
                } else {
                    dataNodes.add(kid);
                }
            }
            if (dataNodes.size() > 0) {
                root = new Element("data");
                for (Element dataNode : dataNodes) {
                    dataNode.detach();
                    root.addContent(dataNode);
                }
                if (s_logger.isDebugEnabled())
                    s_logger.debug("Notification data: " + XmlUtils.toXmlString(root));
            }
            m_lastReceivedNotificationTime = eventTime;
            m_listener.notify(eventTime, root);
        } else {
            s_logger.warn("Unexpected XML message received from device: " + responseType);
        }
    } catch (final Exception fex) {
        // Any exception in event processing should just ignore the event, instead of throwing it to the
        // caller (since the thread it is being called in will bag out otherwise)
        s_logger.error("Error processing notification: " + XmlUtils.toXmlString(response, true));
        if (s_logger.isDebugEnabled()) {
            s_logger.error(fex, fex);
        }
    }
}

From source file:org.goobi.production.export.ExportXmlLog.java

License:Open Source License

/**
 * This method exports the production metadata for al list of processes as a single file to a given stream.
 * /*www.  java  2  s .  c o m*/
 * @param processList
 * @param outputStream
 * @param xslt
 */

public void startExport(List<Process> processList, OutputStream outputStream, String xslt) {
    Document answer = new Document();
    Element root = new Element("processes");
    answer.setRootElement(root);
    Namespace xmlns = Namespace.getNamespace("http://www.goobi.io/logfile");

    Namespace xsi = Namespace.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
    root.addNamespaceDeclaration(xsi);
    root.setNamespace(xmlns);
    Attribute attSchema = new Attribute("schemaLocation", "http://www.goobi.io/logfile" + " XML-logfile.xsd",
            xsi);
    root.setAttribute(attSchema);
    for (Process p : processList) {
        Document doc = createDocument(p, false);
        Element processRoot = doc.getRootElement();
        processRoot.detach();
        root.addContent(processRoot);
    }

    XMLOutputter outp = new XMLOutputter();
    outp.setFormat(Format.getPrettyFormat());

    try {

        outp.output(answer, outputStream);
    } catch (IOException e) {

    } finally {
        if (outputStream != null) {
            try {
                outputStream.close();
            } catch (IOException e) {
                outputStream = null;
            }
        }
    }

}

From source file:org.isima.carsharing.launcher.Launcher.java

public static void addConfigComment(SettingsDelegate settingsDelegate, File out) {
    try {//from   w w  w.j a v a  2 s . c  om
        SAXBuilder builder = new SAXBuilder();

        Document doc = (Document) builder.build(out);
        Element rootNode = doc.getRootElement();
        Element rootNodeCopy = rootNode.clone();

        doc.removeContent(rootNode);
        rootNodeCopy.detach();

        Comment comment = new Comment(settingsDelegate.usedConfigsToXMLComment());
        doc.addContent(comment);
        doc.addContent(rootNodeCopy);

        XMLOutputter xmlOutput = new XMLOutputter();
        xmlOutput.setFormat(Format.getPrettyFormat());
        xmlOutput.output(doc, new FileWriter(out));
    } catch (JDOMException | IOException ex) {
        Logger.getLogger(Launcher.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:org.kitodo.docket.ExportXmlLog.java

License:Open Source License

/**
 * This method exports the production metadata for al list of processes as a
 * single file to a given stream./*  w w w. j a v a2 s  . co m*/
 *
 * @param docketDataList
 *            a list of Docket data
 * @param outputStream
 *            The output stream, to write the docket to.
 */

void startMultipleExport(Iterable<DocketData> docketDataList, OutputStream outputStream) {
    Document answer = new Document();
    Element root = new Element("processes");
    answer.setRootElement(root);
    Namespace xmlns = Namespace.getNamespace(NAMESPACE);

    Namespace xsi = Namespace.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
    root.addNamespaceDeclaration(xsi);
    root.setNamespace(xmlns);
    Attribute attSchema = new Attribute("schemaLocation", NAMESPACE + " XML-logfile.xsd", xsi);
    root.setAttribute(attSchema);
    for (DocketData docketData : docketDataList) {
        Document doc = createDocument(docketData, false);
        Element processRoot = doc.getRootElement();
        processRoot.detach();
        root.addContent(processRoot);
    }

    XMLOutputter outp = new XMLOutputter(Format.getPrettyFormat());

    try {
        outp.output(answer, outputStream);
    } catch (IOException e) {
        logger.error("Generating XML Output failed.", e);
    } finally {
        if (outputStream != null) {
            try {
                outputStream.close();
            } catch (IOException e) {
                logger.error("Closing the output stream failed.", e);
            }
        }
    }

}

From source file:org.knoxcraft.database.xml.XmlDatabase.java

License:Open Source License

/**
 * Removes fields from the given element that are not contained in the given layout
 *
 * @param element//from  ww  w  .ja v  a2s  .co  m
 * @param layout
 */
private void removeFields(Element element, HashSet<Column> layout) {
    for (Element child : element.getChildren()) {
        boolean found = false;

        for (Column column : layout) {
            if (child.getName().equals(column.columnName())) {
                found = true;
            }
        }
        if (!found) {
            child.detach();
        }
    }
}

From source file:org.knoxcraft.database.xml.XmlDatabase.java

License:Open Source License

private void removeData(File file, Document table, Map<String, Object> filters, boolean removeAll)
        throws IOException {
    ArrayList<Element> toremove = new ArrayList<Element>();
    String[] fields = new String[filters.size()];
    filters.keySet().toArray(fields); // We know those are strings
    for (Element element : table.getRootElement().getChildren()) {
        int equalFields = 0;

        for (String field : fields) {
            Element child = element.getChild(field);

            if (child != null) {
                if (child.getText().equals(String.valueOf(filters.get(field)))) {
                    equalFields++;//w w  w . ja  v  a 2  s  . c  om
                }
            }
        }
        if (equalFields != fields.length) {
            continue; // Not the entry we're looking for
        }
        // table.getRootElement().removeContent(element);
        toremove.add(element);
        if (!removeAll) {
            // Just remove one row
            break;
        }
    }
    for (Element e : toremove) {
        e.detach();
    }
    write(file, table);
}