Example usage for org.jdom2 Element Element

List of usage examples for org.jdom2 Element Element

Introduction

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

Prototype

public Element(final String name) 

Source Link

Document

Create a new element with the supplied (local) name and no namespace.

Usage

From source file:com.izforge.izpack.util.xmlmerge.merge.DefaultXmlMerge.java

License:Open Source License

/**
 * Performs the actual merge./* w  w w . j av a2  s  .c  o  m*/
 *
 * @param docs The documents to merge. The first doc is assumed to be the original one to apply patches against.
 * @return The merged result document
 * @throws AbstractXmlMergeException If an error occurred during the merge
 */
private Document doMerge(Document[] docs) throws AbstractXmlMergeException {
    Document originalDoc = docs[0];
    Element origRootElement = originalDoc.getRootElement();

    for (int i = 1; i < docs.length; i++) {
        Element comparedRootElement = docs[i].getRootElement();

        Document output = new Document();
        if (originalDoc.getDocType() != null) {
            output.setDocType((DocType) originalDoc.getDocType().clone());
        }
        output.setRootElement(new Element("root"));
        Element outputRootElement = output.getRootElement();

        m_rootMergeAction.perform(origRootElement, comparedRootElement, outputRootElement);

        Element root = (Element) outputRootElement.getChildren().get(0);
        root.detach();

        sortRootChildrenRecursive(root);

        originalDoc.setRootElement(root);
    }

    return originalDoc;
}

From source file:com.js.quickestquail.server.ViewAllMoviesHandler.java

private void buildXML() {
    Element rootElement = new Element("movies");

    for (Entry<File, String> en : DriveManager.get().getSelected().entrySet()) {
        Movie mov = CachedMovieProvider.get().getMovieByID(en.getValue());

        Element movieElement = new Element("movie");

        movieElement.addContent(makeElement("imdbid", mov.getImdbID()));
        movieElement.addContent(makeElement("imdbrating", mov.getImdbRating() + ""));
        movieElement.addContent(makeElement("imdbvotes", mov.getImdbVotes() + ""));

        movieElement.addContent(makeElement("title", mov.getTitle()));
        movieElement.addContent(makeElement("year", mov.getYear() + ""));
        movieElement.addContent(makeElement("countries", "country", mov.getCountry()));
        movieElement.addContent(makeElement("genres", "genre", mov.getGenre()));

        movieElement.addContent(makeElement("writers", "writer", mov.getWriter()));
        movieElement.addContent(makeElement("directors", "director", mov.getDirector()));
        movieElement.addContent(makeElement("actors", "actor", mov.getActors()));

        movieElement.addContent(makeElement("poster", mov.getPoster()));

        movieElement.addContent(makeElement("plot", mov.getPlot()));

        movieElement.addContent(makeElement("file", en.getKey().getAbsolutePath()));

        rootElement.addContent(movieElement);
    }// ww  w  .  j av  a 2 s .co m

    Document doc = new Document();
    doc.setRootElement(rootElement);

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

    try {
        if (!rootDir.exists())
            rootDir.mkdirs();
        xmlOutput.output(doc, new FileWriter(xmlSourceFile));
    } catch (IOException ex) {
        Logger.getLogger(ViewAllMoviesHandler.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:com.js.quickestquail.server.ViewAllMoviesHandler.java

private Element makeElement(String name, String val) {
    Element rootElement = new Element(name);
    rootElement.setText(val);
    return rootElement;
}

From source file:com.js.quickestquail.server.ViewAllMoviesHandler.java

private Element makeElement(String name, String childName, String[] val) {
    Element rootElement = new Element(name);
    for (String v : val) {
        Element childElement = new Element(childName);
        childElement.setText(v);//  www  .  ja va2s  .c om
        rootElement.addContent(childElement);
    }
    return rootElement;
}

From source file:com.js.quickestquail.ui.actions.io.ExportToXMLAction.java

private void writeAll(File outputFile) {

    // progress dialog
    JProgressDialog dialog = new JProgressDialog(UI.get(), false);
    dialog.setMaximum(DriveManager.get().getSelected().size());
    dialog.setTitle(java.util.ResourceBundle.getBundle("i18n/i18n").getString("export.xml"));
    dialog.setVisible(true);//from  w  w w  . ja v  a2s  .c om

    // process in new thread
    new Thread() {
        @Override
        public void run() {
            try {
                Element rootElement = new Element("movies");

                List<Entry<File, String>> entries = new ArrayList<>(
                        DriveManager.get().getSelected().entrySet());
                java.util.Collections.sort(entries, new Comparator<Entry<File, String>>() {
                    @Override
                    public int compare(Entry<File, String> o1, Entry<File, String> o2) {
                        Movie mov1 = CachedMovieProvider.get().getMovieByID(o1.getValue());
                        Movie mov2 = CachedMovieProvider.get().getMovieByID(o2.getValue());
                        return mov1.getTitle().compareTo(mov2.getTitle());
                    }
                });

                int nofMovies = 0;
                for (Entry<File, String> en : entries) {
                    Movie mov = CachedMovieProvider.get().getMovieByID(en.getValue());

                    // update progress dialog
                    dialog.setText(mov.getTitle());
                    dialog.setProgress(nofMovies);

                    Element movieElement = new Element("movie");

                    movieElement.addContent(makeElement("imdbid", mov.getImdbID()));
                    movieElement.addContent(makeElement("imdbrating", mov.getImdbRating() + ""));
                    movieElement.addContent(makeElement("imdbvotes", mov.getImdbVotes() + ""));

                    movieElement.addContent(makeElement("title", mov.getTitle()));
                    movieElement.addContent(makeElement("year", mov.getYear() + ""));
                    movieElement.addContent(makeElement("countries", "country", mov.getCountry()));
                    movieElement.addContent(makeElement("genres", "genre", mov.getGenre()));

                    movieElement.addContent(makeElement("writers", "writer", mov.getWriter()));
                    movieElement.addContent(makeElement("directors", "director", mov.getDirector()));
                    movieElement.addContent(makeElement("actors", "actor", mov.getActors()));

                    movieElement.addContent(makeElement("poster", mov.getPoster()));

                    movieElement.addContent(makeElement("plot", mov.getPlot()));

                    movieElement.addContent(makeElement("file", en.getKey().getAbsolutePath()));

                    rootElement.addContent(movieElement);

                    nofMovies++;
                }

                Document doc = new Document();
                doc.setRootElement(rootElement);

                // close IO
                XMLOutputter xmlOutput = new XMLOutputter();
                xmlOutput.setFormat(Format.getPrettyFormat());
                xmlOutput.output(doc, new FileWriter(outputFile));

                // close dialog
                dialog.setVisible(false);

            } catch (Exception ex) {
            }
        }
    }.start();
}

From source file:com.khodev.oradiff.io.XmlSchemaWriter.java

License:Open Source License

private static <T extends DBObject> Element convertToXml(String containerTagName, String tagName,
        Collection<T> dbObjects) {
    Element elementList = new Element(containerTagName);
    for (DBObject object : dbObjects) {
        elementList.addContent(getXml(tagName, object));
    }//from w  w  w  .j a  v a 2 s.  c o  m
    return elementList;
}

From source file:com.khodev.oradiff.io.XmlSchemaWriter.java

License:Open Source License

private static Element getXml(String tagName, DBObject object) {
    Element element = new Element(tagName);
    element.setAttribute("name", object.getName());
    if (object instanceof Constraint) {
        Constraint constraint = (Constraint) object;
        element.setAttribute("constraintType", constraint.getConstraintType());
        element.setAttribute("deferrable", constraint.getDeferrable());
        element.setAttribute("deferred", constraint.getDeferred());
        element.setAttribute("deleteRule", constraint.getDeleteRule());
        element.setAttribute("generated", constraint.getGenerated());
        element.setAttribute("refConstraintName", constraint.getRefConstraintName());
        element.setAttribute("refUserName", constraint.getRefUserName());
        element.setAttribute("searchCondition", constraint.getSearchCondition());
        element.setAttribute("status", constraint.getStatus());
        element.setAttribute("validated", constraint.getValidated());
        Element xmlColumns = new Element("columns");
        for (IndexColumn column : constraint.getColumns())
            xmlColumns.addContent(getXml("column", column));
        element.addContent(xmlColumns);//from w w  w. j  av  a2s .  c  om
    }
    if (object instanceof TablespaceObject) {
        element.setAttribute("tablespace", ((TablespaceObject) object).getTablespace());
    }
    if (object instanceof Source) {
        Source source = (Source) object;
        Element xmlBody = new Element("body");
        String bodyStr = "";
        for (String line : source.getBody())
            bodyStr += line;
        xmlBody.setText(bodyStr);
        element.addContent(xmlBody);
    }
    if (object instanceof DBPackage) {
        DBPackage pkg = (DBPackage) object;
        Element xmlDeclaration = new Element("declaration");
        String bodyStr = "";
        for (String line : pkg.getDeclaration())
            bodyStr += line;
        xmlDeclaration.setText(bodyStr);
        element.addContent(xmlDeclaration);
    }
    if (object instanceof Sequence) {
        Sequence sequence = (Sequence) object;
        element.setAttribute("cacheSize", Integer.toString(sequence.getCacheSize()));
        element.setAttribute("cycleFlag", Boolean.toString(sequence.isCycleFlag()));
        element.setAttribute("incrementBy", sequence.getIncrementBy());
        element.setAttribute("lastNumber", sequence.getLastNumber());
        element.setAttribute("maxValue", sequence.getMaxValue());
        element.setAttribute("minValue", sequence.getMinValue());
        element.setAttribute("orderFlag", Boolean.toString(sequence.isOrderFlag()));
    }
    if (object instanceof Table) {
        Table table = (Table) object;
        element.setAttribute("comments", table.getComments());
        element.addContent(XmlSchemaWriter.convertToXml("columns", "column", table.getColumns()));
        element.addContent(XmlSchemaWriter.convertToXml("indexes", "index", table.getIndexes()));
        element.addContent(XmlSchemaWriter.convertToXml("constraints", "constraint", table.getConstraints()));
        element.addContent(XmlSchemaWriter.convertToXml("grants", "grant", table.getGrants()));
        element.addContent(
                XmlSchemaWriter.convertToXml("publicSynonyms", "publicSynonym", table.getPublicSynonyms()));
        return element;

    }
    if (object instanceof Trigger) {
        Trigger trigger = (Trigger) object;
        element.setAttribute("description", trigger.getDescription());
        element.setAttribute("event", trigger.getEvent());
        element.setAttribute("status", trigger.getStatus());
        element.setAttribute("table", trigger.getTable());
        element.setAttribute("type", trigger.getType());
        element.addContent(new Element("when").setText(trigger.getWhen()));
        element.addContent(new Element("body").setText(trigger.getBody()));
    }
    if (object instanceof View) {
        View view = (View) object;
        Element xmlColumns = new Element("columns");
        for (String column : view.getColumns())
            xmlColumns.addContent(new Element("column").setAttribute("name", column));
        element.addContent(xmlColumns);
        element.addContent(new Element("source").setText(view.getSource()));
    }
    if (object instanceof Column) {
        Column column = (Column) object;
        element.setAttribute("id", Integer.toString(column.getId()));
        element.setAttribute("type", column.getType());
        element.setAttribute("length", Integer.toString(column.getLength()));
        element.setAttribute("precision", Integer.toString(column.getPrecision()));
        element.setAttribute("scale", Integer.toString(column.getScale()));
        element.setAttribute("nullable", Boolean.toString(column.isNullable()));
        element.setAttribute("comments", column.getComment());
        element.setAttribute("defaultValue", column.getDefaultValue());
    }
    if (object instanceof Index) {
        Index index = (Index) object;
        element.setAttribute("type", index.getType());
        element.setAttribute("isUnique", Boolean.toString(index.isUnique()));
        element.setAttribute("compression", index.getCompression());
        Element xmlColumns = new Element("columns");
        for (IndexColumn column : index.getColumns())
            xmlColumns.addContent(getXml("column", column));
        element.addContent(xmlColumns);
    }
    if (object instanceof Grant) {
        Grant grant = (Grant) object;
        element.setAttribute("selectPriv", Boolean.toString(grant.isSelectPriv()));
        element.setAttribute("insertPriv", Boolean.toString(grant.isInsertPriv()));
        element.setAttribute("deletePriv", Boolean.toString(grant.isDeletePriv()));
        element.setAttribute("updatePriv", Boolean.toString(grant.isUpdatePriv()));
        element.setAttribute("referencesPriv", Boolean.toString(grant.isReferencesPriv()));
        element.setAttribute("alterPriv", Boolean.toString(grant.isAlterPriv()));
        element.setAttribute("indexPriv", Boolean.toString(grant.isIndexPriv()));
    }

    if (object instanceof IndexColumn) {
        IndexColumn indexColumn = (IndexColumn) object;
        element.setAttribute("position", Integer.toString(indexColumn.getPosition()));
    }
    return element;
}

From source file:com.khodev.oradiff.io.XmlSchemaWriter.java

License:Open Source License

private Element getXml(Schema schema) throws IOException {
    Element database = new Element("database");
    database.addContent(convertToXml("tables", "table", schema.getDbTables()));
    database.addContent(convertToXml("packages", "package", schema.getDbPackages()));
    database.addContent(convertToXml("functions", "function", schema.getDbFunctions()));
    database.addContent(convertToXml("procedures", "procedure", schema.getDbProcedures()));
    database.addContent(convertToXml("views", "view", schema.getDbViews()));
    database.addContent(convertToXml("sequences", "sequence", schema.getDbSequences()));
    database.addContent(convertToXml("triggers", "trigger", schema.getDbTriggers()));
    return database;
}

From source file:com.khodev.sc2.quiz.Quiz.java

License:Open Source License

private void writeUserData(OutputStream s) throws IOException {
    Element root = new Element("Catalog");
    Document d = new Document(root);
    Element user = new Element("CUser").setAttribute("id", "QuizDictionary");
    root.addContent(user);/*from   ww w.jav a  2 s  .  c  om*/
    user.addContent(new Element("Fields").setAttribute("EditorColumn", "1").setAttribute("Id", "Level")
            .setAttribute("Type", "Int"));
    user.addContent(new Element("Fields").setAttribute("EditorColumn", "2").setAttribute("Id", "Question")
            .setAttribute("Type", "Text"));
    user.addContent(new Element("Fields").setAttribute("EditorColumn", "3").setAttribute("Id", "Answer")
            .setAttribute("Type", "Text"));
    user.addContent(new Element("Instances").setAttribute("Id", "[Default]"));

    int i = 0;
    for (Question question : questions) {
        i++;
        Element instance = new Element("Instances").setAttribute("Id", "" + i);
        user.addContent(instance);
        Element q = new Element("Text").setAttribute("Text", "UserData/QuizDictionary/" + i + "_Question");
        instance.addContent(q);
        q.addContent(new Element("Field").setAttribute("Id", "Question"));
        Element r = new Element("Text").setAttribute("Text", "UserData/QuizDictionary/" + i + "_Answer");
        instance.addContent(r);
        r.addContent(new Element("Field").setAttribute("Id", "Answer"));
    }
    XMLOutputter xmlOutput = new XMLOutputter();
    xmlOutput.setFormat(Format.getPrettyFormat());
    xmlOutput.output(d, s);
}

From source file:com.kixeye.kixmpp.client.module.chat.MessageKixmppClientModule.java

License:Apache License

/**
 * Sends a private message.//www.j a v a  2s.co m
 * 
 * @param toJid
 * @param body
 */
public void sendMessage(KixmppJid toJid, String body) {
    Element messageElement = new Element("message");
    messageElement.setAttribute("type", "chat");
    messageElement.setAttribute("from", client.getJid().getFullJid());
    messageElement.setAttribute("to", toJid.getBaseJid());

    Element bodyElement = new Element("body");
    bodyElement.setText(body);

    messageElement.addContent(bodyElement);

    client.sendStanza(messageElement);
}