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:com.izforge.izpack.util.xmlmerge.action.InsertAction.java

License:Open Source License

@Override
public void perform(Element originalElement, Element patchElement, Element outputParentElement) {

    if (patchElement == null && originalElement != null) {
        outputParentElement.addContent((Element) originalElement.clone());

    } else {//from ww w . ja  v a  2 s.c  o m
        List<Content> outputContent = outputParentElement.getContent();

        Iterator<Content> it = outputContent.iterator();

        int lastIndex = outputContent.size();

        while (it.hasNext()) {
            Content content = it.next();

            if (content instanceof Element) {
                Element element = (Element) content;

                if (element.getQualifiedName().equals(patchElement.getQualifiedName())) {
                    lastIndex = outputParentElement.indexOf(element);
                }
            }
        }

        List<Content> toAdd = new ArrayList<Content>();
        toAdd.add(patchElement);
        outputContent.addAll(Math.min(lastIndex + 1, outputContent.size()), toAdd);
    }
}

From source file:com.izforge.izpack.util.xmlmerge.action.KeepAction.java

License:Open Source License

@Override
public void perform(Element originalElement, Element patchElement, Element outputParentElement) {
    if (originalElement != null && patchElement != null) {
        outputParentElement.addContent((Element) patchElement.clone());
    }//  ww  w  .  ja  v  a 2  s  .c  o m
}

From source file:com.izforge.izpack.util.xmlmerge.action.OrderedMergeAction.java

License:Open Source License

@Override
public void perform(Element originalElement, Element patchElement, Element outputParentElement)
        throws AbstractXmlMergeException {

    logger.fine("Merging: " + originalElement + " (original) and " + patchElement + "(patch)");

    Mapper mapper = (Mapper) m_mapperFactory.getOperation(originalElement, patchElement);

    if (originalElement == null) {
        outputParentElement.addContent(mapper.map(patchElement));
    } else if (patchElement == null) {
        outputParentElement.addContent((Content) originalElement.clone());
    } else {/*from  w  w w.  j  av a 2 s.c o m*/

        Element workingElement = new Element(originalElement.getName(), originalElement.getNamespacePrefix(),
                originalElement.getNamespaceURI());
        addAttributes(workingElement, originalElement);

        logger.fine("Adding " + workingElement);
        outputParentElement.addContent(workingElement);

        doIt(workingElement, originalElement, patchElement);
    }

}

From source file:com.izforge.izpack.util.xmlmerge.action.OrderedMergeAction.java

License:Open Source License

/**
 * Performs the actual merge between two source elements.
 *
 * @param parentOut The merged element//  w ww . j  ava2s. c o  m
 * @param parentIn1 The first source element
 * @param parentIn2 The second source element
 * @throws AbstractXmlMergeException If an error occurred during the merge
 */
private void doIt(Element parentOut, Element parentIn1, Element parentIn2) throws AbstractXmlMergeException {

    addAttributes(parentOut, parentIn2);

    Content[] list1 = (Content[]) parentIn1.getContent().toArray(new Content[] {});
    Content[] list2 = (Content[]) parentIn2.getContent().toArray(new Content[] {});

    int offsetTreated1 = 0;
    int offsetTreated2 = 0;

    for (Content content1 : list1) {

        logger.fine("List 1: " + content1);

        if (content1 instanceof Comment || content1 instanceof Text) {
            parentOut.addContent((Content) content1.clone());
            offsetTreated1++;
        } else if (!(content1 instanceof Element)) {
            throw new DocumentException(content1.getDocument(),
                    "Contents of type " + content1.getClass().getName() + " not supported");
        } else {
            Element e1 = (Element) content1;

            // does e1 exist on list2 and has not yet been treated
            int posInList2 = -1;
            for (int j = offsetTreated2; j < list2.length; j++) {

                logger.fine("List 2: " + list2[j]);

                if (list2[j] instanceof Element) {

                    if (((Matcher) m_matcherFactory.getOperation(e1, (Element) list2[j])).matches(e1,
                            (Element) list2[j])) {
                        logger.fine("Match found: " + e1 + " and " + list2[j]);
                        posInList2 = j;
                        break;
                    }
                } else if (list2[j] instanceof Comment || list2[j] instanceof Text) {
                    // skip
                } else {
                    throw new DocumentException(list2[j].getDocument(),
                            "Contents of type " + list2[j].getClass().getName() + " not supported");
                }
            }

            // element found in second list, but there is some elements to
            // be treated before in second list
            while (posInList2 != -1 && offsetTreated2 < posInList2) {
                Content contentToAdd;
                if (list2[offsetTreated2] instanceof Element) {
                    applyAction(parentOut, null, (Element) list2[offsetTreated2]);
                } else {
                    // FIXME prevent double comments in output by enhancing applyAction() to
                    // Content type instead of Element
                    // Workaround: Add only comments from original document (List1)
                    if (!(list2[offsetTreated2] instanceof Comment)) {
                        contentToAdd = (Content) list2[offsetTreated2].clone();
                        parentOut.addContent(contentToAdd);
                    }
                }

                offsetTreated2++;
            }

            // element found in all lists
            if (posInList2 != -1) {

                applyAction(parentOut, (Element) list1[offsetTreated1], (Element) list2[offsetTreated2]);

                offsetTreated1++;
                offsetTreated2++;
            } else {
                // element not found in second list
                applyAction(parentOut, (Element) list1[offsetTreated1], null);
                offsetTreated1++;
            }
        }
    }

    // at the end of list1, are there some elements in list2 which must be still treated?
    while (offsetTreated2 < list2.length) {
        Content contentToAdd;
        if (list2[offsetTreated2] instanceof Element) {
            applyAction(parentOut, null, (Element) list2[offsetTreated2]);
        } else {
            // FIXME prevent double comments in output by enhancing applyAction() to Content
            // type instead of Element
            // Workaround: Add only comments from original document (List1)
            if (!(list2[offsetTreated2] instanceof Comment)) {
                contentToAdd = (Content) list2[offsetTreated2].clone();
                parentOut.addContent(contentToAdd);
            }
        }

        offsetTreated2++;
    }

}

From source file:com.izforge.izpack.util.xmlmerge.action.OverrideAction.java

License:Open Source License

@Override
public void perform(Element originalElement, Element patchElement, Element outputParentElement) {
    if (originalElement != null && patchElement != null) {
        outputParentElement.addContent((Element) patchElement.clone());
    } else if (originalElement != null) {
        outputParentElement.addContent((Element) originalElement.clone());
    }/* w  ww .j a  va 2 s  .  co m*/
}

From source file:com.izforge.izpack.util.xmlmerge.action.PreserveAction.java

License:Open Source License

@Override
public void perform(Element originalElement, Element patchElement, Element outputParentElement) {
    if (originalElement != null) {
        outputParentElement.addContent((Element) originalElement.clone());
    }/* www .j a v a 2 s. co  m*/
}

From source file:com.izforge.izpack.util.xmlmerge.action.ReplaceAction.java

License:Open Source License

@Override
public void perform(Element originalElement, Element patchElement, Element outputParentElement) {
    if (patchElement != null) {
        outputParentElement.addContent((Element) patchElement.clone());
    } else {/* w  w  w  .ja v a  2 s.com*/
        outputParentElement.addContent((Element) originalElement.clone());
    }
}

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);
    }//from  ww w .  j  a va  2 s .c  om

    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 childName, String[] val) {
    Element rootElement = new Element(name);
    for (String v : val) {
        Element childElement = new Element(childName);
        childElement.setText(v);/* w  ww. j  a  va  2s.c  o  m*/
        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  va2  s  .  c  o m*/

    // 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();
}