Example usage for org.jdom2 Element addContent

List of usage examples for org.jdom2 Element addContent

Introduction

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

Prototype

@Override
public Element addContent(final Collection<? extends Content> newContent) 

Source Link

Document

Appends all children in the given collection to the end of the content list.

Usage

From source file:cz.lbenda.dataman.db.ExportTableData.java

License:Apache License

/** Write rows from sql query to output stream
 * @param sqlQueryRows rows/*from  w  w  w . j av a  2 s. c  om*/
 * @param outputStream stream to which are data write */
public static void writeSqlQueryRowsToXMLv2(SQLQueryRows sqlQueryRows, OutputStream outputStream)
        throws IOException {
    Element root = new Element("export");
    root.setAttribute("sql", sqlQueryRows.getSQL());
    root.setAttribute("version", "2");
    List<Element> rows = new ArrayList<>(sqlQueryRows.getRows().size());

    sqlQueryRows.getRows().forEach(rowDesc -> {
        Element row = new Element("row");
        List<Element> cols = new ArrayList<>(sqlQueryRows.getMetaData().getColumns().size());
        sqlQueryRows.getMetaData().getColumns().forEach(columnDesc -> {
            Element element = new Element(columnDesc.getName());
            if (rowDesc.isColumnNull(columnDesc)) {
                element.setAttribute("null", "true");
            } else {
                element.setText(rowDesc.getColumnValueStr(columnDesc));
            }
            cols.add(element);
        });
        row.addContent(cols);
        rows.add(row);
    });
    root.addContent(rows);
    XMLOutputter xmlOutputter = new XMLOutputter();
    xmlOutputter.output(new Document(root), outputStream);
}

From source file:cz.lbenda.dbapp.rc.db.JDBCConfiguration.java

License:Apache License

public final Element storeToElement() {
    Element jdbc = new Element("jdbc");
    jdbc.addContent(new Element("driverClass").setText(driverClass));
    jdbc.addContent(new Element("user").setText(getUsername()));
    jdbc.addContent(new Element("password").setText(getPassword()));
    jdbc.addContent(new Element("url").setText(getUrl()));
    Element libraries = new Element("libraries");
    jdbc.addContent(libraries);// ww w  .j av  a  2 s  .co m
    if (importedLibreries != null) {
        for (String library : importedLibreries) {
            libraries.addContent(new Element("librarie").setText(library));
        }
    }
    return jdbc;
}

From source file:cz.lbenda.dbapp.rc.SessionConfiguration.java

License:Apache License

public static Element storeAllToElement() {
    Element root = new Element("DBApp");
    Element sessions = new Element("sessions");
    root.addContent(sessions);
    for (SessionConfiguration sc : getConfigurations()) {
        sessions.addContent(sc.storeToElement());
    }/*from   w ww .  j a v a2  s  .  co m*/
    return root;
}

From source file:cz.lbenda.dbapp.rc.SessionConfiguration.java

License:Apache License

public final Element storeToElement() {
    Element ses = new Element("session");
    ses.setAttribute("id", getId());
    if (this.connectionTimeout > 0) {
        ses.addContent(new Element("connectionTimeout").setText(Integer.toString(this.connectionTimeout)));
    }/*from   w  w  w . j a va  2 s  .co m*/
    if (jdbcConfiguration != null) {
        ses.addContent(jdbcConfiguration.storeToElement());
    }
    Element ed = new Element("extendedDescription");
    ed.setAttribute("type", this.getExtendedConfigurationType().name());
    ed.setText(this.getExtendedConfigurationPath());
    ses.addContent(ed);
    if (!librariesPaths.isEmpty()) {
        Element libs = new Element("libraries");
        for (String path : getLibrariesPaths()) {
            Element lib = new Element("library");
            lib.setText(path);
            libs.addContent(lib);
        }
        ses.addContent(libs);
    }
    return ses;
}

From source file:cz.muni.fi.mir.mathmlcanonicalization.modules.AbstractModule.java

License:Apache License

protected void replaceElement(final Element toReplace, final String replacementName) {
    assert toReplace != null && replacementName != null;
    assert !replacementName.isEmpty();
    final Element parent = toReplace.getParentElement();
    assert parent != null;
    final Element replacement = new Element(replacementName);
    replacement.addContent(toReplace.removeContent());
    final List<Attribute> attributes = toReplace.getAttributes();
    for (Attribute attribute : attributes) {
        replacement.setAttribute(attribute.detach());
    }//ww  w  .  j  a v a2  s  .co  m
    final int parentIndex = parent.indexOf(toReplace);
    parent.removeContent(parentIndex);
    parent.addContent(parentIndex, replacement);
    LOGGER.log(Level.FINE, "{0} replaced with {1}", new Object[] { toReplace, replacementName });
}

From source file:cz.muni.fi.mir.mathmlcanonicalization.modules.MfencedReplacer.java

License:Apache License

private void replaceMfenced(final Element mfencedElement) {
    assert mfencedElement != null;
    final char[] separators = getSeparators(mfencedElement);
    final Namespace ns = mfencedElement.getNamespace();
    final List<Element> children = mfencedElement.getChildren();
    final int nChildren = children.size();
    final int last = Math.min(separators.length - 1, nChildren - 2);

    Element insideFence = null;
    if (nChildren == 1 && children.get(0).getName().equals(ROW)) {
        // we do not want to add another mrow
        insideFence = children.get(0).detach();
    } else if (nChildren != 0) {
        insideFence = new Element(ROW, ns);
        for (int i = 0; i < nChildren; i++) {
            // add separator
            if (i > 0 && last >= 0) { // not before first or when blank separators
                char separatorChar = separators[(i - 1 > last) ? last : i - 1];
                String separatorStr = Character.toString(separatorChar);
                insideFence.addContent(new Element(OPERATOR, ns).setText(separatorStr));
            }//from   ww  w. ja v a2  s . c o m
            // add original child
            insideFence.addContent(children.get(0).detach());
        }
    }
    replaceMfenced(mfencedElement, insideFence);
}

From source file:cz.muni.fi.mir.mathmlcanonicalization.modules.MfencedReplacer.java

License:Apache License

private void replaceMfenced(final Element mfencedElement, final Element insideContent) {
    assert mfencedElement != null; // but insideContent can be null
    final Namespace ns = mfencedElement.getNamespace();
    Element replacement = new Element(ROW, ns);
    String openStr = getProperty(DEFAULT_OPEN);
    String closeStr = getProperty(DEFAULT_CLOSE);
    if (openStr.isEmpty() || closeStr.isEmpty()) {
        LOGGER.warning("Default open or close fence not set");
    }//from  w  w w  .  ja v a  2  s .c  o  m

    if (!isEnabled(FORCE_DEFAULT_OPEN)) {
        openStr = mfencedElement.getAttributeValue(OPEN_FENCE, openStr);
    }
    if (!isEnabled(FORCE_DEFAULT_CLOSE)) {
        closeStr = mfencedElement.getAttributeValue(CLOSE_FENCE, closeStr);
    }

    replacement.addContent(new Element(OPERATOR, ns).setText(openStr));
    if (insideContent != null) {
        if (isEnabled(ADD_INNER_ROW)) {
            replacement.addContent(insideContent);
        } else {
            replacement.addContent(insideContent.removeContent());
        }
    }
    replacement.addContent(new Element(OPERATOR, ns).setText(closeStr));

    final Element parent = mfencedElement.getParentElement();
    final int index = parent.indexOf(mfencedElement);
    parent.removeContent(index);
    if (isEnabled(ADD_OUTER_ROW)) {
        parent.addContent(index, replacement);
    } else {
        parent.addContent(index, replacement.removeContent());
    }
    LOGGER.fine("Mfenced element converted");
}

From source file:cz.muni.fi.mir.mathmlcanonicalization.modules.MrowNormalizer.java

License:Apache License

/**
 * Wrap previously detected fenced expressions in mrow to be same as output
 * of MfencedReplacer//from w w  w.  j a v  a2s.  c  om
 *
 * @param siblings children of parent element
 * @param fenced list of elements inside parentheses, children of parent
 * element
 * @param opening opening parenthesis, child of parent element
 * @param closing closing parenthesis, child of parent element
 */
private void wrapFenced(final List<Element> siblings, final List<Element> fenced, final Element opening,
        final Element closing) {
    assert siblings != null && fenced != null && opening != null;
    final Element parent = opening.getParentElement();
    assert closing != null && closing.getParentElement().equals(parent);
    for (Element e : fenced) {
        e.detach();
    }
    final int openingIndex = parent.indexOf(opening);

    // Element to be placed inside parentheses.
    // If null, the original 'fenced' list will be used.
    final Element innerElement;
    if (fenced.isEmpty() || !isEnabled(WRAP_ISIDE)) {
        innerElement = null; // will not wrap inside in mrow
    } else if (fenced.size() == 1) {
        innerElement = fenced.get(0); // no need to wrap, just one element
    } else {
        innerElement = new Element(ROW);
        innerElement.addContent(fenced);
        LOGGER.fine("Inner mrow added");
    }

    if (((parent.getName().equals(ROW) && siblings.get(0) == opening
            && siblings.get(siblings.size() - 1) == closing)) || !isEnabled(WRAP_OUTSIDE)) {
        // will not wrap outside in mrow
        if (innerElement == null) {
            parent.addContent(openingIndex + 1, fenced);
        } else {
            parent.addContent(openingIndex + 1, innerElement);
        }
        return;
    }
    // wrap outside in mrow
    opening.detach();
    closing.detach();
    final Element outerMrowElement = new Element(ROW);
    outerMrowElement.addContent(opening);
    if (innerElement != null) {
        outerMrowElement.addContent(innerElement);
    } else {
        outerMrowElement.addContent(fenced);
    }
    outerMrowElement.addContent(closing);
    parent.addContent(openingIndex, outerMrowElement);
    LOGGER.fine("Outer mrow added");
}

From source file:cz.muni.fi.mir.mathmlcanonicalization.modules.ScriptNormalizer.java

License:Apache License

private void normalizeSupInSub(final Element element) {
    assert element != null;
    final List<Element> children = element.getChildren();
    for (int i = 0; i < children.size(); i++) {
        final Element actual = children.get(i);
        normalizeSupInSub(actual);//from   w  w  w .  j  av a 2s . c o  m
        if (!actual.getName().equals(SUBSCRIPT)) {
            continue;
        }
        List<Element> subscriptChildren = actual.getChildren();
        if (subscriptChildren.size() != 2) {
            LOGGER.info("Invalid msub, skipped");
            continue;
        }
        if (!subscriptChildren.get(0).getName().equals(SUPERSCRIPT)) {
            continue;
        }
        final List<Element> superscriptChildren = subscriptChildren.get(0).getChildren();
        if (superscriptChildren.size() != 2) {
            LOGGER.info("Invalid msup, skipped");
            continue;
        }
        final Element newMsub = new Element(SUBSCRIPT);
        newMsub.addContent(superscriptChildren.get(0).detach());
        newMsub.addContent(subscriptChildren.get(1).detach());
        final Element newMsup = new Element(SUPERSCRIPT);
        newMsup.addContent(newMsub);
        newMsup.addContent(superscriptChildren.get(0).detach());
        children.set(i, newMsup);
        LOGGER.fine("Sub/sup scripts swapped");
    }
}

From source file:cz.muni.fi.mir.mathmlcanonicalization.modules.ScriptNormalizer.java

License:Apache License

private void normalizeMsubsup(final Element element, Collection<String> firstChildren) {
    assert element != null && firstChildren != null;
    final List<Element> children = element.getChildren();
    for (int i = 0; i < children.size(); i++) {
        final Element actual = children.get(i);
        if (actual.getName().equals(SUBSUP)) {
            final List<Element> actualChildren = actual.getChildren();
            if (actualChildren.size() != 3) {
                LOGGER.info("Invalid msubsup, skipped");
                continue;
            }//from www .j  a v  a  2 s . com
            if (!firstChildren.contains(actualChildren.get(0).getName())) {
                continue;
            }
            final Element newMsub = new Element(SUBSCRIPT);
            newMsub.addContent(actualChildren.get(0).detach());
            newMsub.addContent(actualChildren.get(0).detach());
            final Element newMsup = new Element(SUPERSCRIPT);
            newMsup.addContent(newMsub);
            newMsup.addContent(actualChildren.get(0).detach());
            children.set(i, newMsup);
            i--; // move back to check the children of the new transformation
            LOGGER.fine("Msubsup converted to nested msub and msup");
        } else {
            normalizeMsubsup(actual, firstChildren);
        }
    }
}