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() 

Source Link

Document

Creates a new empty document.

Usage

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);/*from w ww.j av  a  2  s. c o m*/
    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);
        }
    }
    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);/*from   w w  w . j a  v a  2s .c  om*/

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

    // 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;//from   w ww.j a v a 2s. com
    }

    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 {//  w  w  w  .  java2s.  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.cybernostics.jsp2thymeleaf.JSP2Thymeleaf.java

private void writeTree(JspTree jspTree, OutputStream outputStream) {
    try {/*  ww  w.  j  a  v  a  2 s.c  om*/
        Document doc = new Document();
        final List<Content> content = rootContentFor(jspTree);

        doc.addContent(content);

        XMLOutputter out = new XMLOutputter(Format.getPrettyFormat().setTextMode(Format.TextMode.NORMALIZE)
                .setLineSeparator(NEWLINE).setOmitDeclaration(true));

        out.output(doc, outputStream);
    } catch (IOException ex) {
        Logger.getLogger(JSP2Thymeleaf.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:com.example.android.customnotifications.MainActivity.java

License:Apache License

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sample_main);

    Document doc = new Document();
    Gson gson = new Gson();

}

From source file:com.example.android.fingerprintdialog.MainActivity.java

License:Apache License

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toast.makeText(this, "shove it up your bot bot init", Toast.LENGTH_SHORT).show();

    try {// w  w  w  .  j ava2  s .  c  om
        mKeyStore = KeyStore.getInstance("AndroidKeyStore");
    } catch (KeyStoreException e) {
        Intent intent = new Intent("flap");
        throw new RuntimeException("Failed to get an instance of KeyStore", e);
    }
    try {
        mKeyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
    } catch (NoSuchAlgorithmException | NoSuchProviderException e) {
        org.w3c.dom.Document anotherdoc;
        throw new RuntimeException("Failed to get an instance of KeyGenerator", e);
    }
    Cipher defaultCipher;
    Cipher cipherNotInvalidated;
    try {
        defaultCipher = Cipher.getInstance(KeyProperties.KEY_ALGORITHM_AES + "/" + KeyProperties.BLOCK_MODE_CBC
                + "/" + KeyProperties.ENCRYPTION_PADDING_PKCS7);
        cipherNotInvalidated = Cipher.getInstance(KeyProperties.KEY_ALGORITHM_AES + "/"
                + KeyProperties.BLOCK_MODE_CBC + "/" + KeyProperties.ENCRYPTION_PADDING_PKCS7);
    } catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
        Document doc;
        doc = new Document();
        throw new RuntimeException("Failed to get an instance of Cipher", e);

    }
    mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

    KeyguardManager keyguardManager = getSystemService(KeyguardManager.class);
    FingerprintManager fingerprintManager = getSystemService(FingerprintManager.class);
    Button purchaseButton = (Button) findViewById(R.id.purchase_button);
    Button purchaseButtonNotInvalidated = (Button) findViewById(R.id.purchase_button_not_invalidated);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        purchaseButtonNotInvalidated.setEnabled(true);
        purchaseButtonNotInvalidated.setOnClickListener(
                new PurchaseButtonClickListener(cipherNotInvalidated, KEY_NAME_NOT_INVALIDATED));
    } else {
        // Hide the purchase button which uses a non-invalidated key
        // if the app doesn't work on Android N preview
        purchaseButtonNotInvalidated.setVisibility(View.GONE);
        findViewById(R.id.purchase_button_not_invalidated_description).setVisibility(View.GONE);
    }

    if (!keyguardManager.isKeyguardSecure()) {
        // Show a message that the user hasn't set up a fingerprint or lock screen.
        Toast.makeText(this,
                "Secure lock screen hasn't set up.\n"
                        + "Go to 'Settings -> Security -> Fingerprint' to set up a fingerprint",
                Toast.LENGTH_LONG).show();
        purchaseButton.setEnabled(false);
        purchaseButtonNotInvalidated.setEnabled(false);
        return;
    }

    // Now the protection level of USE_FINGERPRINT permission is normal instead of dangerous.
    // See http://developer.android.com/reference/android/Manifest.permission.html#USE_FINGERPRINT
    // The line below prevents the false positive inspection from Android Studio
    // noinspection ResourceType
    if (!fingerprintManager.hasEnrolledFingerprints()) {
        purchaseButton.setEnabled(false);
        // This happens when no fingerprints are registered.
        Toast.makeText(this,
                "Go to 'Settings -> Security -> Fingerprint' and register at least one fingerprint",
                Toast.LENGTH_LONG).show();
        return;
    }
    createKey(DEFAULT_KEY_NAME, true);
    createKey(KEY_NAME_NOT_INVALIDATED, false);
    purchaseButton.setEnabled(true);
    purchaseButton.setOnClickListener(new PurchaseButtonClickListener(defaultCipher, DEFAULT_KEY_NAME));
    // ATTENTION: This was auto-generated to implement the App Indexing API.
    // See https://g.co/AppIndexing/AndroidStudio for more information.
    client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}

From source file:com.github.cat.yum.store.util.YumUtil.java

private static void createRepoMd(String rootPath, RepoModule... repos) throws IOException {
    Document doc = new Document();
    Element root = new Element("repomd", REPONAMESPACE);
    doc.addContent(root);// w  ww  .java2 s  .com
    root.addNamespaceDeclaration(RPMNAMESPACE);
    long now = System.currentTimeMillis();

    for (RepoModule repo : repos) {
        Element data = new Element("data", REPONAMESPACE);
        data.setAttribute("type", repo.getModule());
        Element location = new Element("location", REPONAMESPACE);
        File xmlGzFie = getXmlGzFile(repo, repo.getXmlGzCode());
        location.setAttribute("href", replacePath(FileUtils.getFileRelativePath(repo.getRootPath(), xmlGzFie)));
        data.addContent(location);
        Element checksum = new Element("checksum", REPONAMESPACE);
        checksum.setAttribute("type", ALGORITHM);
        checksum.setAttribute("pkgid", "YES");
        checksum.setText(repo.getXmlGzCode());
        data.addContent(checksum);
        Element size = new Element("size", REPONAMESPACE);
        size.setText(repo.getXmlGzSize() + "");
        data.addContent(size);
        Element timestamp = new Element("timestamp", REPONAMESPACE);
        timestamp.setText(now + "");
        data.addContent(timestamp);
        Element openCheckSum = new Element("open-checksum", REPONAMESPACE);
        openCheckSum.setAttribute("type", ALGORITHM);
        openCheckSum.setAttribute("pkgid", "YES");
        openCheckSum.setText(repo.getXmlCode());
        data.addContent(openCheckSum);
        Element openSize = new Element("open-size", REPONAMESPACE);
        openSize.setText(repo.getXmlSize() + "");
        data.addContent(openSize);
        Element revision = new Element("revision", REPONAMESPACE);
        data.addContent(revision);
        root.addContent(data);
    }
    File repoMd = new File(rootPath + File.separator + REPOPATH + File.separator + "repomd" + ".xml");
    xmlToFile(doc, repoMd);
}

From source file:com.github.cat.yum.store.util.YumUtil.java

private static RepoModule createOther(RpmData[] rpmdatas, String rootPath)
        throws IOException, NoSuchAlgorithmException {
    RepoModule repo = new RepoModule(rootPath, "other");
    Document doc = new Document();
    Element root = new Element("otherdata", OTHERNAMESPACE);
    doc.addContent(root);/*from  ww  w  . j a  v a2  s.c  o m*/
    root.setAttribute("packages", rpmdatas.length + "");

    for (RpmData rpmdata : rpmdatas) {
        RpmMetadata rpmMetadata = rpmdata.rpmMetadata;

        Element packAge = new Element("package", OTHERNAMESPACE);
        packAge.setAttribute("pkgid", HashFile.getsum(rpmdata.rpm, ALGORITHM));
        packAge.setAttribute("name", rpmMetadata.name);
        packAge.setAttribute("arch", rpmMetadata.architecture);

        root.addContent(packAge);

        Element version = new Element("version", OTHERNAMESPACE);
        version.setAttribute("epoch", rpmMetadata.epoch + "");
        version.setAttribute("ver", rpmMetadata.version);
        version.setAttribute("rel", rpmMetadata.release);
        packAge.setContent(version);

        for (ChangeLog log : rpmMetadata.changeLogs) {
            Element fileElement = new Element("changelog", OTHERNAMESPACE);
            fileElement.setAttribute("author", log.author);
            fileElement.setAttribute("date", log.date + "");
            fileElement.setText(log.text);
            packAge.addContent(fileElement);
        }
    }
    yumXmlSave(doc, repo);
    return repo;
}