⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 versionupdatetask.java

📁 java插件系统源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            try {
                info.processVersion(versions, builder, timestampVersion);
            } catch (Exception e) {
                throw new BuildException("failed processing manifest " //$NON-NLS-1$
                        + info.getManifestFile(), e, getLocation());
            }
        }
        if (alterReferences) {
            if (getVerbose()) {
                log("Processing version references"); //$NON-NLS-1$
            }
            for (Iterator it = infos.values().iterator(); it.hasNext();) {
                PluginInfo info = (PluginInfo) it.next();
                try {
                    info.processVersionReferences(versions, builder);
                } catch (Exception e) {
                    throw new BuildException("failed processing manifest " //$NON-NLS-1$
                            + info.getManifestFile(), e, getLocation());
                }
            }
        }
        Transformer transformer;
        try {
            transformer = TransformerFactory.newInstance().newTransformer();
        } catch (TransformerConfigurationException tce) {
            throw new BuildException("can't obtain XML document transformer ", //$NON-NLS-1$
                    tce, getLocation());
        } catch (TransformerFactoryConfigurationError tfce) {
            throw new BuildException(
                    "can't obtain XML document transformer factory ", //$NON-NLS-1$
                    tfce, getLocation());
        }
        transformer.setOutputProperty(OutputKeys.METHOD, "xml"); //$NON-NLS-1$
        transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); //$NON-NLS-1$
        transformer.setOutputProperty(OutputKeys.STANDALONE, "no"); //$NON-NLS-1$
        transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC,
                "-//JPF//Java Plug-in Manifest 1.0"); //$NON-NLS-1$
        transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,
                "http://jpf.sourceforge.net/plugin_1_0.dtd"); //$NON-NLS-1$
        if (getVerbose()) {
            log("Saving manifests"); //$NON-NLS-1$
        }
        for (Iterator it = infos.values().iterator(); it.hasNext();) {
            PluginInfo info = (PluginInfo) it.next();
            try {
                info.save(versions, transformer);
            } catch (Exception e) {
                throw new BuildException("failed saving manifest " //$NON-NLS-1$
                        + info.getManifestFile(), e, getLocation());
            }
        }
        // saving versions
        if (getVerbose()) {
            log("Saving versions file " + versionsFile); //$NON-NLS-1$
        }
        try {
            OutputStream out =
                new BufferedOutputStream(new FileOutputStream(versionsFile));
            try {
                versions.store(out, "Plug-in and plug-in fragment versions file"); //$NON-NLS-1$
            } finally {
                out.close();
            }
        } catch (IOException ioe) {
            throw new BuildException("failed writing versions file " //$NON-NLS-1$
                    + versionsFile, ioe, getLocation());
        }
        log("... finished JPF version updater"); //$NON-NLS-1$
    }
    
    static final class PluginInfo {
        private static Date getTimestamp(final File file,
                final Date parentTimestamp) {
            Date result;
            if (parentTimestamp == null) {
                result = new Date(file.lastModified());
            } else {
                result = new Date(Math.max(parentTimestamp.getTime(),
                        file.lastModified()));
            }
            File[] files = file.listFiles();
            if (files != null) {
                for (int i = 0; i < files.length; i++) {
                    result = getTimestamp(files[i], result);
                }
            }
            return result;
        }
        
        private static Version upgradeVersion(final Version ver) {
            if (ver == null) {
                return new Version(0, 0, 1, null);
            }
            return new Version(ver.getMajor(), ver.getMinor(),
                    ver.getBuild() + 1, ver.getName());
        }

        private static Version upgradeVersion(final Version ver,
                final Date timestamp) {
            DateFormat dtf =
                new SimpleDateFormat("yyyyMMddHHmmss", Locale.ENGLISH); //$NON-NLS-1$
            if (ver == null) {
                return new Version(0, 0, 1, dtf.format(timestamp));
            }
            return new Version(ver.getMajor(), ver.getMinor(),
                    ver.getBuild() + 1, dtf.format(timestamp));
        }

        private final String id;
        private final File manifestFile;
        private final Version oldVersion;
        private final Date oldTimestamp;
        private final DateFormat dtf =
            new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH); //$NON-NLS-1$
        private Document doc;
        private Version newVersion;
        private Date currentTimestamp;
        
        PluginInfo(final String anId, final Properties versions,
                final File aMnifestFile, final File homeFile,
                final Version currentVersion) throws ParseException {
            id = anId;
            String versionStr = versions.getProperty(anId, null);
            String timestampStr = versions.getProperty(anId + ".timestamp", null); //$NON-NLS-1$
            if (versionStr != null) {
                oldVersion = Version.parse(versionStr);
            } else {
                oldVersion = currentVersion;
            }
            if (timestampStr != null) {
                oldTimestamp = dtf.parse(timestampStr);
            } else {
                oldTimestamp = null;
            }
            manifestFile = aMnifestFile;
            currentTimestamp = getTimestamp(homeFile, null);
            versions.setProperty(id + ".timestamp", //$NON-NLS-1$
                    dtf.format(currentTimestamp));
        }
        
        File getManifestFile() {
            return manifestFile;
        }
        
        void processVersion(final Properties versions,
                final DocumentBuilder builder, final boolean timestampVersion)
                throws Exception {
            if (IoUtil.compareFileDates(oldTimestamp, currentTimestamp)) {
                newVersion = oldVersion;
            } else {
                newVersion = !timestampVersion ? upgradeVersion(oldVersion)
                        : upgradeVersion(oldVersion, currentTimestamp);
            }
            versions.setProperty(id, newVersion.toString());
            if (oldVersion.equals(newVersion)) {
                return;
            }
            doc = builder.parse(manifestFile);
            Element root = doc.getDocumentElement();
            if (root.hasAttribute("version")) { //$NON-NLS-1$
                root.setAttribute("version", newVersion.toString()); //$NON-NLS-1$
            }
        }
        
        void processVersionReferences(final Properties versions,
                final DocumentBuilder builder) throws Exception {
            if (doc == null) {
                doc = builder.parse(manifestFile);
            }
            Element root = doc.getDocumentElement();
            if ("plugin-fragment".equals(root.getNodeName())) { //$NON-NLS-1$
                processVersionReference(versions, root);
            }
            NodeList imports = root.getElementsByTagName("import"); //$NON-NLS-1$
            for (int i = 0; i < imports.getLength(); i++) {
                processVersionReference(versions, (Element) imports.item(i));
            }
        }
        
        private void processVersionReference(final Properties versions,
                final Element elm) {
            if (!elm.hasAttribute("plugin-version")) { //$NON-NLS-1$
                return;
            }
            String version = versions.getProperty(
                    elm.getAttribute("plugin-id"), id); //$NON-NLS-1$
            if (version != null) {
                elm.setAttribute("plugin-version", version); //$NON-NLS-1$
            }
        }

        void save(final Properties versions, final Transformer transformer)
                throws Exception {
            if (doc == null) {
                return;
            }
            long modified = manifestFile.lastModified();
            transformer.transform(new DOMSource(doc),
                    new StreamResult(manifestFile));
            manifestFile.setLastModified(modified);
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -