Example usage for org.jdom2 Document Document

List of usage examples for org.jdom2 Document Document

Introduction

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

Prototype

public Document(List<? extends Content> content) 

Source Link

Document

This will create a new Document, with the supplied list of content, and a DocType declaration only if the content contains a DocType instance.

Usage

From source file:de.nava.informa.exporters.RSS_1_0_Exporter.java

License:Open Source License

public void write(ChannelIF channel) throws IOException {
    if (writer == null) {
        throw new RuntimeException("No writer has been initialized.");
    }/*from ww  w  . ja  va  2 s  .  c o m*/

    Element rootElem = new Element("RDF", NS_RDF);
    rootElem.addNamespaceDeclaration(NS_DEFAULT); // TODO
    rootElem.addNamespaceDeclaration(NS_DC);
    rootElem.addNamespaceDeclaration(NS_SY);
    // rootElem.setAttribute("version");
    Element channelElem = new Element("channel", NS_DEFAULT);
    if (channel.getLocation() != null) {
        channelElem.setAttribute("about", channel.getLocation().toString(), NS_RDF);
    }
    channelElem.addContent(new Element("title", NS_DEFAULT).setText(channel.getTitle()));
    if (channel.getSite() != null) {
        channelElem.addContent(new Element("link", NS_DEFAULT).setText(channel.getSite().toString()));
        channelElem.addContent(
                new Element("source", NS_DC).setAttribute("resource", channel.getSite().toString()));
    }

    channelElem.addContent(new Element("description", NS_DEFAULT).setText(channel.getDescription()));
    if (channel.getLanguage() != null) {
        channelElem.addContent(new Element("language", NS_DC).setText(channel.getLanguage()));
    }
    if (channel.getCopyright() != null) {
        channelElem.addContent(new Element("copyright", NS_DC).setText(channel.getCopyright()));
    }
    if (channel.getUpdateBase() != null) {
        channelElem.addContent(new Element("updateBase", NS_SY).setText(df.format(channel.getUpdateBase())));
    }
    if (channel.getUpdatePeriod() != null) {
        // don't put out frequency without specifying period
        channelElem.addContent(new Element("updateFrequency", NS_SY)
                .setText((new Integer(channel.getUpdateFrequency())).toString()));
        channelElem
                .addContent(new Element("updatePeriod", NS_SY).setText(channel.getUpdatePeriod().toString()));
    }
    // export channel image
    if (channel.getImage() != null) {
        Element imgElem = new Element("image", NS_DEFAULT);
        imgElem.addContent(new Element("title", NS_DEFAULT).setText(channel.getImage().getTitle()));
        imgElem.addContent(new Element("url", NS_DEFAULT).setText(channel.getImage().getLocation().toString()));
        imgElem.addContent(new Element("link", NS_DEFAULT).setText(channel.getImage().getLink().toString()));
        imgElem.addContent(new Element("height", NS_DEFAULT).setText("" + channel.getImage().getHeight()));
        imgElem.addContent(new Element("width", NS_DEFAULT).setText("" + channel.getImage().getWidth()));
        imgElem.addContent(new Element("description", NS_DEFAULT).setText(channel.getImage().getDescription()));
        channelElem.addContent(imgElem);
    }

    // TODO: add exporting textinput field
    //     if (channel.getTextInput() != null) {
    //       channelElem.addContent(channel.getTextInput().getElement());
    //     }

    // ===========================================
    Element itemsElem = new Element("items", NS_DEFAULT);
    Element seqElem = new Element("Seq", NS_RDF);
    Collection items = channel.getItems();
    Iterator it = items.iterator();
    while (it.hasNext()) {
        ItemIF item = (ItemIF) it.next();
        Element itemElem = new Element("li", NS_RDF);
        if (item.getLink() != null) {
            itemElem.setAttribute("resource", item.getLink().toString());
        }
        seqElem.addContent(itemElem);
    }
    itemsElem.addContent(seqElem);
    channelElem.addContent(itemsElem);
    rootElem.addContent(channelElem);

    // item-by-item en detail
    items = channel.getItems();
    it = items.iterator();
    while (it.hasNext()) {
        rootElem.addContent(getItemElement((ItemIF) it.next()));
    }

    // create XML outputter with indent: 2 spaces, print new lines.
    Format format = Format.getPrettyFormat();
    format.setEncoding(encoding);
    XMLOutputter outputter = new XMLOutputter(format);
    // write DOM to file
    Document doc = new Document(rootElem);
    outputter.output(doc, writer);
    writer.close();
}

From source file:de.nava.informa.exporters.RSS_2_0_Exporter.java

License:Open Source License

public void write(ChannelIF channel) throws IOException {
    if (writer == null) {
        throw new RuntimeException("No writer has been initialized.");
    }/* w  ww. j  av  a 2s.  c  om*/

    // create XML outputter with indent: 2 spaces, print new lines.
    Format format = Format.getPrettyFormat();
    format.setEncoding(encoding);
    XMLOutputter outputter = new XMLOutputter(format);

    Namespace dcNs = Namespace.getNamespace("dc", NS_DC);
    Namespace syNs = Namespace.getNamespace("sy", NS_SY);
    Namespace adminNs = Namespace.getNamespace("admin", NS_ADMIN);
    //Namespace rdfNs = Namespace.getNamespace("rdf", NS_RDF);

    Element rootElem = new Element("rss");
    rootElem.addNamespaceDeclaration(dcNs);
    rootElem.addNamespaceDeclaration(syNs);
    rootElem.addNamespaceDeclaration(adminNs);
    rootElem.setAttribute("version", RSS_VERSION);

    Element channelElem = new Element("channel");
    // rootElem.setAttribute("version");
    channelElem.addContent(new Element("title").setText(channel.getTitle()));
    if (channel.getSite() != null) {
        channelElem.addContent(new Element("link").setText(channel.getSite().toString()));
    }

    channelElem.addContent(new Element("description").setText(channel.getDescription()));
    if (channel.getLanguage() != null) {
        channelElem.addContent(new Element("language", dcNs).setText(channel.getLanguage()));
    }
    if (channel.getCopyright() != null) {
        channelElem.addContent(new Element("copyright", dcNs).setText(channel.getCopyright()));
    }
    if (channel.getPubDate() != null) {
        channelElem.addContent(new Element("pubDate").setText(ParserUtils.formatDate(channel.getPubDate())));
    }
    if (channel.getCategories() != null) {
        Collection categories = channel.getCategories();
        for (Object category : categories) {
            CategoryIF cat = (CategoryIF) category;
            channelElem = getCategoryElements(channelElem, cat, null);
        }
    }

    if (channel.getUpdateBase() != null) {
        channelElem.addContent(new Element("updateBase", syNs).setText(df.format(channel.getUpdateBase())));
    }
    if (channel.getUpdatePeriod() != null) {
        // don't put out frequency without specifying period
        channelElem.addContent(new Element("updateFrequency", syNs)
                .setText((new Integer(channel.getUpdateFrequency())).toString()));
        channelElem.addContent(new Element("updatePeriod", syNs).setText(channel.getUpdatePeriod().toString()));
    }
    // export channel image            
    if (channel.getImage() != null) {
        Element imgElem = new Element("image");
        imgElem.addContent(new Element("title").setText(channel.getImage().getTitle()));
        imgElem.addContent(new Element("url").setText(channel.getImage().getLocation().toString()));
        imgElem.addContent(new Element("link").setText(channel.getImage().getLink().toString()));
        imgElem.addContent(new Element("height").setText("" + channel.getImage().getHeight()));
        imgElem.addContent(new Element("width").setText("" + channel.getImage().getWidth()));
        imgElem.addContent(new Element("description").setText(channel.getImage().getDescription()));
        channelElem.addContent(imgElem);
    }

    // TODO: add exporting textinput field
    //     if (channel.getTextInput() != null) {
    //       channelElem.addContent(channel.getTextInput().getElement());
    //     }

    Collection items = channel.getItems();
    for (Object item : items) {
        channelElem.addContent(getItemElement((ItemIF) item));
    }

    rootElem.addContent(channelElem);

    // ---
    Document doc = new Document(rootElem);
    outputter.output(doc, writer);
    // ---
    writer.close();
}

From source file:de.openVJJ.basic.ProjectConf.java

License:Open Source License

/**
 * Saves the Project//from w  ww . j  a v  a  2  s  .  c om
 * @param fileName the file the Project is saved to
 */
public static void save(String fileName) {
    Element rootElement = new Element(ROOT_ELEMET_NAME);
    //      Element gpuElement = new Element(GPU_ELEMENT_NAME);
    //      gpuElement.setAttribute("activ", String.valueOf(useGPU));
    //      rootElement.addContent(gpuElement);
    Element baseModuleElement = new Element(ELEMET_NAME_BASE_MODULE);
    rootElement.addContent(baseModuleElement);
    baseModule.getConfig(baseModuleElement);
    try {
        FileWriter fileWriter = new FileWriter(fileName);
        new XMLOutputter().output(new Document(rootElement), fileWriter);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:de.openVJJ.InputComponents.java

License:Open Source License

public static void save(String fileName) {
    Element rootElement = new Element(ROOT_ELEMET_NAME);
    Element gpuElement = new Element(GPU_ELEMENT_NAME);
    gpuElement.setAttribute("activ", String.valueOf(useGPU));
    rootElement.addContent(gpuElement);/*from   w w w.  j av a  2  s . com*/
    Element compnetsElement = new Element(COMPONENTS_ELEMENT_NAME);
    for (ImagePublisher imagePublisher : imagePublishers) {
        compnetsElement.addContent(componetToXML(imagePublisher));
    }
    rootElement.addContent(compnetsElement);
    try {
        FileWriter fileWriter = new FileWriter(fileName);
        new XMLOutputter().output(new Document(rootElement), fileWriter);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:de.relaunch64.popelganda.database.CustomScripts.java

License:Open Source License

public CustomScripts() {
    // first of all, create the empty documents
    scriptFile = new Document(new Element(ROOT_NAME));
    // create file path to script file
    filepath = FileTools.createFilePath("relaunch64-scripts.xml");
}

From source file:de.relaunch64.popelganda.database.CustomScripts.java

License:Open Source License

/**
 * Removes the script at the index {@code index}. Use {@link #findScript(java.lang.String) findScript()}
 * to find the index of a script by name.
 *  //w  w w .ja  v a 2s  . c om
 * @param index the index of the script that should be removed
 * @return {@code true} if removal was successful
 */
public boolean removeScript(int index) {
    List<Element> children = scriptFile.getRootElement().getChildren();
    try {
        Element el = children.remove(index);
        if (el != null) {
            // reset document
            scriptFile = new Document(new Element(ROOT_NAME));
            // iterate and add remaining elements
            for (Element e : children)
                addScript(e.getAttributeValue(ATTR_NAME), e.getText());
            return true;
        }
    } catch (UnsupportedOperationException | IndexOutOfBoundsException | IllegalAddException ex) {
        return false;
    }
    return false;
}

From source file:de.relaunch64.popelganda.database.Settings.java

License:Open Source License

public Settings() {
    // first of all, create the empty documents
    settingsFile = new Document(new Element("settings"));
    // create file path to settings file
    filepath = FileTools.createFilePath("relaunch64-settings.xml");
    // now fill the initoal elements
    fillElements();//w w w. j av  a2s .  c o  m
}

From source file:de.smartics.maven.plugin.jboss.modules.xml.ModuleXmlBuilder.java

License:Apache License

/**
 * Default constructor.//from  w  ww .j a  v  a2  s  .  c  o m
 *
 * @param context the context and configuration to control the building of XML
 *          files.
 * @param module the module to build.
 * @param dependencies the dependencies to reference.
 */
public ModuleXmlBuilder(final ExecutionContext context, final ModuleDescriptor module,
        final Collection<Dependency> dependencies) {
    this.context = context;
    this.module = module;
    this.dependencies = dependencies;

    root = new Element("module", NS);
    root.setAttribute("name", module.getName());
    final String slot = calcSlot(context, module, dependencies);
    if (!SlotStrategy.MAIN_SLOT.equals(slot)) {
        root.setAttribute("slot", slot);
    }
    document = new Document(root);
}

From source file:de.tor.tribes.util.xml.JDomUtils.java

License:Apache License

/**
 * Writing part/* w w w  . ja v a 2  s .com*/
 * 
 * to generate Elements use something like this
 * 
Element extensions = new Element("extensions");
extensions.setAttribute(new Attribute("id", "2"));
extensions.addContent(amounts.toXml("amounts"));
 */

public static Document createDocument() {
    return new Document(new Element("data"));
}

From source file:de.unirostock.sems.caro.converters.CaToRo.java

License:Open Source License

/**
 * Handle meta data of a ca entry.// w ww  . ja v a2s.co m
 * 
 * @param target
 *          the path to the file in the ro
 * @param meta
 *          the meta data of the ca entry
 * @param annotationsDir
 *          the annotations dir in the ro
 * @param annotationNumber
 *          the annotation number
 * @param annotations
 *          the list of existing annotations
 * @return the annotationNumber
 */
private int handleMetaData(PathMetadata pmd, Path target, List<MetaDataObject> meta, Path annotationsDir,
        int annotationNumber, List<PathAnnotation> annotations) {
    List<Agent> authors = pmd.getAuthoredBy();

    boolean addAll = true;
    if (meta.size() == 1) {
        if (meta.get(0) instanceof OmexMetaDataObject) {
            OmexDescription omex = ((OmexMetaDataObject) meta.get(0)).getOmexDescription();
            if (omex.getCreators().size() == 1) {
                VCard creator = omex.getCreators().get(0);
                if (creator != null && creator.getOrganization() == null
                        || creator.getOrganization().length() == 0) {
                    Agent agent = vcardToAgent(creator, notifications);
                    if (agent != null) {
                        if (authors == null) {
                            authors = new ArrayList<Agent>();
                            pmd.setAuthoredBy(authors);
                        }
                        authors.add(agent);
                        addAll = false;
                    }
                }
            }
        }
    }

    if (addAll)
        for (MetaDataObject m : meta) {
            if (m instanceof OmexMetaDataObject) {
                // add COMBINE creators as RO authors
                OmexDescription omex = ((OmexMetaDataObject) m).getOmexDescription();
                for (VCard creator : omex.getCreators()) {
                    Agent agent = vcardToAgent(creator, notifications);
                    if (agent != null) {
                        if (authors == null) {
                            authors = new ArrayList<Agent>();
                            pmd.setAuthoredBy(authors);
                        }
                        authors.add(agent);
                    }
                }
            }

            // add that to the evolution?
            Element rdf = new Element("RDF", RDF_NAMESPACE);
            rdf.addContent(m.getXmlDescription().clone());
            try {
                if (!Files.exists(annotationsDir))
                    Files.createDirectories(annotationsDir);
                Path file = annotationsDir.resolve("omex-conversion-" + ++annotationNumber + ".rdf");
                Bundles.setStringValue(file, XmlTools.prettyPrintDocument(new Document(rdf)));
                PathAnnotation pa = new PathAnnotation();
                pa.setAbout(target);
                pa.setContent(file);
                pa.generateAnnotationId();
                annotations.add(pa);
                // tag this annotation as a conversion from a combine archive
                tagAnnotation(pa, annotations);
            } catch (IOException e) {
                LOGGER.error(e, "was not able to convert annotation for entry ", m.getAbout());
                notifications.add(new CaRoNotification(CaRoNotification.SERVERITY_WARN,
                        "skipping conversion of annotation for " + m.getAbout() + " -- reason: "
                                + e.getMessage()));
            }
        }

    if (authors != null && authors.size() == 1)
        pmd.setCreatedBy(authors.get(0));

    return annotationNumber;
}