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

📄 versionupdatetask.java

📁 java插件系统源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*****************************************************************************
 * Java Plug-in Framework (JPF)
 * Copyright (C) 2006-2007 Dmitry Olshansky
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *****************************************************************************/
package org.java.plugin.tools.ant;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.apache.tools.ant.BuildException;
import org.java.plugin.registry.PluginDescriptor;
import org.java.plugin.registry.PluginFragment;
import org.java.plugin.registry.Version;
import org.java.plugin.util.IoUtil;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

/**
 * <p>
 * This class can upgrade all version and plugin-version tags in all plugin
 * manifest files, to the latest version specified in a text file (in Java
 * properties format). This class also handles updating the build number in the
 * specified file.
 * <p>
 * This class will only upgrade 'version' and 'plugin-version' tags that already
 * exist in the manifest files, so it won't add any to the manifest files.
 * <p>
 * This class tracks plug-in modification timestamp's and keep them together
 * with versions info in the given text file. The actual plug-in version will be
 * upgraded only if plug-in timestamp changes.
 * 
 * @author Jonathan Giles
 * @author Dmitry Olshansky
 */
public class VersionUpdateTask extends BaseJpfTask {
    private File versionsFile;
    private boolean alterReferences = false;
    private boolean timestampVersion = false;
    
    /**
     * @param value <code>true</code> if version references should be upgraded
     */
    public final void setAlterReferences(final boolean value) {
        alterReferences = value;
    }

    /**
     * @param value file where to store versioning related info
     */
    public void setVersionsFile(final File value) {
        versionsFile = value;
    }
    
    /**
     * @param value if <code>true</code>, the plug-in timestamp will be included
     *              into version {@link Version#getName() name} attribute
     */
    public void setTimestampVersion(final boolean value) {
        timestampVersion = value;
    }

    /**
     * @see org.apache.tools.ant.Task#execute()
     */
    public void execute() throws BuildException {
        log("Starting JPF version updater..."); //$NON-NLS-1$
        if (versionsFile == null) {
            throw new BuildException("versionsfile attribute must be set!", //$NON-NLS-1$
                    getLocation());
        }
        initRegistry(true);
        // reading contents of versions file
        if (getVerbose()) {
            log("Loading versions file " + versionsFile); //$NON-NLS-1$
        }
        Properties versions = new Properties();
        if (versionsFile.exists()) {
            try {
                InputStream in =
                    new BufferedInputStream(new FileInputStream(versionsFile));
                try {
                    versions.load(in);
                } finally {
                    in.close();
                }
            } catch (IOException ioe) {
                throw new BuildException("failed reading versions file " //$NON-NLS-1$
                        + versionsFile, ioe, getLocation());
            }
        } else {
            log("Versions file " + versionsFile //$NON-NLS-1$
                    + " not found, new one will be created."); //$NON-NLS-1$
        }
        Map infos = new HashMap();// <plug-in or fragment id, PluginInfo>
        // collecting manifests
        for (Iterator it = getRegistry().getPluginDescriptors().iterator();
                it.hasNext();) {
            PluginDescriptor descr = (PluginDescriptor) it.next();
            File manifestFile = IoUtil.url2file(descr.getLocation());
            if (manifestFile == null) {
                throw new BuildException(
                        "non-local plug-in manifest URL given " //$NON-NLS-1$
                        + descr.getLocation());
            }
            URL homeUrl = getPathResolver().resolvePath(descr, "/"); //$NON-NLS-1$
            File homeFile = IoUtil.url2file(homeUrl);
            if (homeFile == null) {
                throw new BuildException(
                        "non-local plug-in home URL given " //$NON-NLS-1$
                        + homeUrl);
            }
            try {
                infos.put(descr.getId(), new PluginInfo(descr.getId(),
                        versions, manifestFile, homeFile, descr.getVersion()));
            } catch (ParseException pe) {
                throw new BuildException("failed parsing versions data " //$NON-NLS-1$
                        + " for manifest " + manifestFile, pe, getLocation()); //$NON-NLS-1$
            }
            if (getVerbose()) {
                log("Collected manifest file " + manifestFile); //$NON-NLS-1$
            }
        }
        for (Iterator it = getRegistry().getPluginFragments().iterator();
                it.hasNext();) {
            PluginFragment descr = (PluginFragment) it.next();
            File manifestFile = IoUtil.url2file(descr.getLocation());
            if (manifestFile == null) {
                throw new BuildException(
                        "non-local plug-in fragment manifest URL given " //$NON-NLS-1$
                        + descr.getLocation());
            }
            URL homeUrl = getPathResolver().resolvePath(descr, "/"); //$NON-NLS-1$
            File homeFile = IoUtil.url2file(homeUrl);
            if (homeFile == null) {
                throw new BuildException(
                        "non-local plug-in fragment home URL given " //$NON-NLS-1$
                        + homeUrl);
            }
            try {
                infos.put(descr.getId(), new PluginInfo(descr.getId(),
                        versions, manifestFile, homeFile, descr.getVersion()));
            } catch (ParseException pe) {
                throw new BuildException("failed parsing versions data " //$NON-NLS-1$
                        + " for manifest " + manifestFile, pe, getLocation()); //$NON-NLS-1$
            }
            if (getVerbose()) {
                log("Populated manifest file " + manifestFile); //$NON-NLS-1$
            }
        }
        // processing manifest versions
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
        builderFactory.setValidating(false);
        DocumentBuilder builder;
        try {
            builder = builderFactory.newDocumentBuilder();
        } catch (ParserConfigurationException pce) {
            throw new BuildException("can't obtain XML document builder ", //$NON-NLS-1$
                    pce, getLocation());
        }
        if (getVerbose()) {
            log("Processing versions"); //$NON-NLS-1$
        }
        for (Iterator it = infos.values().iterator(); it.hasNext();) {
            PluginInfo info = (PluginInfo) it.next();

⌨️ 快捷键说明

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