Example usage for org.jdom2 Document setRootElement

List of usage examples for org.jdom2 Document setRootElement

Introduction

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

Prototype

public Document setRootElement(Element rootElement) 

Source Link

Document

This sets the root Element for the Document.

Usage

From source file:ca.nrc.cadc.vos.server.RssView.java

License:Open Source License

/**
 * Write to root Element to a writer./*from   ww w  . j  a v a 2s.  co  m*/
 *
 * @param root Root Element to write.
 * @param writer Writer to write to.
 * @throws IOException if the writer fails to write.
 */
protected void write(Element root, Writer writer) throws IOException {
    XMLOutputter outputter = new XMLOutputter();
    outputter.setFormat(Format.getPrettyFormat());
    Document document = new Document();
    root.detach();
    document.setRootElement(root);
    outputter.output(document, writer);
}

From source file:ca.nrc.cadc.xml.JsonInputter.java

License:Open Source License

public Document input(final String json) throws JSONException {
    JSONObject rootJson = new JSONObject(json);
    List<String> keys = Arrays.asList(JSONObject.getNames(rootJson));
    List<Namespace> namespaces = new ArrayList<Namespace>();
    Namespace namespace = getNamespace(namespaces, rootJson, keys);

    String rootKey = null;//  ww w  . jav a  2  s. com
    List<Attribute> attributes = new ArrayList<Attribute>();
    for (String key : keys) {
        if (!key.startsWith("@xmlns")) {
            if (key.startsWith("@")) {
                String value;
                if (rootJson.isNull(key)) {
                    value = "";
                } else {
                    value = getStringValue(rootJson.get(key));
                }
                attributes.add(new Attribute(key.substring(1), value));
            } else {
                // DOM can only have one root element.
                if (rootKey != null) {
                    throw new IllegalStateException("Found multiple root entries");
                }
                rootKey = key;
            }
        }
    }

    Element rootElement = new Element(rootKey, namespace);
    for (Attribute attribute : attributes) {
        rootElement.setAttribute(attribute);
    }

    Object value = rootJson.get(rootKey);
    processObject(rootKey, value, rootElement, namespace, namespaces);

    Document document = new Document();
    document.setRootElement(rootElement);
    return document;
}

From source file:com.archimatetool.canvas.templates.wizard.SaveCanvasAsTemplateWizard.java

License:Open Source License

private String createManifest() throws IOException {
    Document doc = new Document();
    Element root = new Element(ITemplateXMLTags.XML_TEMPLATE_ELEMENT_MANIFEST);
    doc.setRootElement(root);

    // Type//ww  w  .ja  v a  2s  . c  o  m
    root.setAttribute(ITemplateXMLTags.XML_TEMPLATE_ATTRIBUTE_TYPE,
            CanvasModelTemplate.XML_CANVAS_TEMPLATE_ATTRIBUTE_TYPE_MODEL);

    // Timestamp
    root.setAttribute(ITemplateXMLTags.XML_TEMPLATE_ATTRIBUTE_TIMESTAMP,
            Long.toString(System.currentTimeMillis()));

    // Name
    Element elementName = new Element(ITemplateXMLTags.XML_TEMPLATE_ELEMENT_NAME);
    elementName.setText(fTemplateName);
    root.addContent(elementName);

    // Description
    Element elementDescription = new Element(ITemplateXMLTags.XML_TEMPLATE_ELEMENT_DESCRIPTION);
    elementDescription.setText(fTemplateDescription);
    root.addContent(elementDescription);

    // Thumbnail
    if (fIncludeThumbnail) {
        String keyThumb = TemplateManager.ZIP_ENTRY_THUMBNAILS + "1.png"; //$NON-NLS-1$
        Element elementKeyThumb = new Element(ITemplateXMLTags.XML_TEMPLATE_ELEMENT_KEY_THUMBNAIL);
        elementKeyThumb.setText(keyThumb);
        root.addContent(elementKeyThumb);
    }

    return JDOMUtils.write2XMLString(doc);
}

From source file:com.archimatetool.editor.model.impl.EditorModelManager.java

License:Open Source License

public void saveState() throws IOException {
    Document doc = new Document();
    Element rootElement = new Element("models"); //$NON-NLS-1$
    doc.setRootElement(rootElement);
    for (IArchimateModel model : getModels()) {
        File file = model.getFile(); // has been saved
        if (file != null) {
            Element modelElement = new Element("model"); //$NON-NLS-1$
            modelElement.setAttribute("file", file.getAbsolutePath()); //$NON-NLS-1$
            rootElement.addContent(modelElement);
        }//from  ww w  .  j  av a2  s.  c  o  m
    }
    JDOMUtils.write2XMLFile(doc, backingFile);
}

From source file:com.archimatetool.templates.impl.wizard.SaveArchimateModelAsTemplateWizard.java

License:Open Source License

private String createManifest() throws IOException {
    Document doc = new Document();
    Element root = new Element(ITemplateXMLTags.XML_TEMPLATE_ELEMENT_MANIFEST);
    doc.setRootElement(root);

    // Type//from  w ww  .j  av  a2  s . com
    root.setAttribute(ITemplateXMLTags.XML_TEMPLATE_ATTRIBUTE_TYPE,
            ArchimateModelTemplate.XML_TEMPLATE_ATTRIBUTE_TYPE_MODEL);

    // Timestamp
    root.setAttribute(ITemplateXMLTags.XML_TEMPLATE_ATTRIBUTE_TIMESTAMP,
            Long.toString(System.currentTimeMillis()));

    // Name
    Element elementName = new Element(ITemplateXMLTags.XML_TEMPLATE_ELEMENT_NAME);
    elementName.setText(fTemplateName);
    root.addContent(elementName);

    // Description
    Element elementDescription = new Element(ITemplateXMLTags.XML_TEMPLATE_ELEMENT_DESCRIPTION);
    elementDescription.setText(fTemplateDescription);
    root.addContent(elementDescription);

    // Thumbnails
    if (fIncludeThumbnails) {
        if (fSelectedDiagramModel != null) {
            int i = 1;
            for (IDiagramModel dm : fModel.getDiagramModels()) {
                if (dm == fSelectedDiagramModel) {
                    String keyThumb = TemplateManager.ZIP_ENTRY_THUMBNAILS + i + ".png"; //$NON-NLS-1$
                    Element elementKeyThumb = new Element(ITemplateXMLTags.XML_TEMPLATE_ELEMENT_KEY_THUMBNAIL);
                    elementKeyThumb.setText(keyThumb);
                    root.addContent(elementKeyThumb);
                    break;
                }
                i++;
            }
        }
    }

    return JDOMUtils.write2XMLString(doc);
}

From source file:com.archimatetool.templates.model.AbstractTemplate.java

License:Open Source License

@Override
public void save() throws IOException {
    if (fFile == null || !fFile.exists()) {
        return;/*from   w  ww  .jav a2s.  com*/
    }

    // Manifest
    Document doc = new Document();
    Element root = new Element(ITemplateXMLTags.XML_TEMPLATE_ELEMENT_MANIFEST);
    doc.setRootElement(root);

    Element elementName = new Element(ITemplateXMLTags.XML_TEMPLATE_ELEMENT_NAME);
    elementName.setText(getName());
    root.addContent(elementName);

    Element elementDescription = new Element(ITemplateXMLTags.XML_TEMPLATE_ELEMENT_DESCRIPTION);
    elementDescription.setText(getDescription());
    root.addContent(elementDescription);

    if (fKeyThumbnailPath != null) {
        Element elementKeyThumb = new Element(ITemplateXMLTags.XML_TEMPLATE_ELEMENT_KEY_THUMBNAIL);
        elementKeyThumb.setText(fKeyThumbnailPath);
        root.addContent(elementKeyThumb);
    }

    String manifest = JDOMUtils.write2XMLString(doc);

    // Model
    String model = ZipUtils.extractZipEntry(fFile, TemplateManager.ZIP_ENTRY_MODEL);

    // Start a zip stream
    File tmpFile = File.createTempFile("architemplate", null); //$NON-NLS-1$
    BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(tmpFile));
    ZipOutputStream zOut = new ZipOutputStream(out);

    ZipUtils.addStringToZip(manifest, TemplateManager.ZIP_ENTRY_MANIFEST, zOut);
    ZipUtils.addStringToZip(model, TemplateManager.ZIP_ENTRY_MODEL, zOut);

    // Thumbnails
    Image[] images = getThumbnails();
    int i = 1;
    for (Image image : images) {
        ZipUtils.addImageToZip(image, TemplateManager.ZIP_ENTRY_THUMBNAILS + i++ + ".png", zOut, SWT.IMAGE_PNG, //$NON-NLS-1$
                null);
    }

    zOut.flush();
    zOut.close();

    // Delete and copy
    fFile.delete();
    FileUtils.copyFile(tmpFile, fFile, false);
    tmpFile.delete();
}

From source file:com.archimatetool.templates.model.TemplateManager.java

License:Open Source License

public void saveUserTemplatesManifest() throws IOException {
    if (fUserTemplates == null || fUserTemplateGroups == null) {
        return;//  ww w .  ja va 2 s . c  o  m
    }

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

    for (ITemplate template : fUserTemplates) {
        Element templateElement = new Element(XML_TEMPLATE_ELEMENT_TEMPLATE);
        rootElement.addContent(templateElement);
        templateElement.setAttribute(XML_TEMPLATE_ATTRIBUTE_TYPE, template.getType());
        templateElement.setAttribute(XML_TEMPLATE_ATTRIBUTE_ID, template.getID());
        templateElement.setAttribute(XML_TEMPLATE_ATTRIBUTE_FILE, template.getFile().getAbsolutePath());
    }

    for (ITemplateGroup group : fUserTemplateGroups) {
        Element groupElement = new Element(XML_TEMPLATE_ELEMENT_GROUP);
        rootElement.addContent(groupElement);
        groupElement.setAttribute(XML_TEMPLATE_ATTRIBUTE_NAME, group.getName());
        for (ITemplate template : group.getTemplates()) {
            Element templateRefElement = new Element(XML_TEMPLATE_ELEMENT_TEMPLATE_REF);
            groupElement.addContent(templateRefElement);
            templateRefElement.setAttribute(XML_TEMPLATE_ATTRIBUTE_REF, template.getID());
        }
    }

    JDOMUtils.write2XMLFile(doc, getUserTemplatesManifestFile());
}

From source file:com.cats.version.VersionCfgParseAndSave.java

License:Apache License

public boolean saveVersionInfo(List<VersionInfo> infos, String fullPath) {
    try {//from   w  w  w  .ja  va  2 s  . c om
        Document doc = new Document();
        Element root = new Element("software-group");
        for (VersionInfo info : infos) {
            Element softEle = new Element("software");
            softEle.setAttribute("name", info.getAppName());
            Element versionCodeEle = new Element("latest-version-code");
            Element versionNameEle = new Element("latest-version");
            Element versionPathEle = new Element("latest-version-abspath");
            Element startupNameEle = new Element("latest-version-startup");

            versionCodeEle.setText(String.valueOf(info.getVersionCode()));
            versionNameEle.setText(info.getVersion());
            versionPathEle.setText(info.getPath());
            startupNameEle.setText(info.getStartupName());
            softEle.addContent(versionCodeEle);
            softEle.addContent(versionNameEle);
            softEle.addContent(versionPathEle);
            softEle.addContent(startupNameEle);

            List<VersionInfoDetail> details = info.getDetails();
            if (null != details) {
                Element detailEles = new Element("latest-version-detail");
                for (VersionInfoDetail verDetail : details) {
                    Element itemElem = new Element("item");
                    itemElem.setAttribute("name", verDetail.getTitle());

                    List<String> detailList = verDetail.getDetail();
                    for (String detailInfo : detailList) {
                        Element detailEle = new Element("detail");
                        detailEle.setText(detailInfo);
                        itemElem.addContent(detailEle);
                    }
                    detailEles.addContent(itemElem);
                }
                softEle.addContent(detailEles);
            }

            List<String> ignoreFiles = info.getIgnoreFiles();
            if (ignoreFiles != null) {
                Element ignoreEles = new Element("ignore-files");
                for (String ignoreInfo : ignoreFiles) {
                    Element ignoreItemEle = new Element("item");
                    ignoreItemEle.setText(ignoreInfo);
                    ignoreEles.addContent(ignoreItemEle);
                }
                softEle.addContent(ignoreEles);
            }
            root.addContent(softEle);
        }
        doc.setRootElement(root);

        //Save to xml file
        XMLOutputter xmlOut = null;
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(fullPath);
            xmlOut = new XMLOutputter(Format.getPrettyFormat());
            xmlOut.output(doc, fos);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != fos) {
                try {
                    fos.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
    return true;
}

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

License:Open Source License

/**
 * Performs the actual merge.//from  w  w  w  .  j a v  a2  s.co  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);
    }//  w  w w .  j a  v  a2  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);
    }
}