Package pdfdb.settings

Source Code of pdfdb.settings.PluginSettingsManager

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package pdfdb.settings;

import java.util.Hashtable;
import org.jdom.Attribute;
import org.jdom.Element;
import pdfdb.indexing.plugins.Indexer;
import pdfdb.indexing.pdf.PdfIndexer;
import pdfdb.indexing.plugins.PluginDef;

/** Manages persisting and retrieving plugin definitions.
* @author ug22cmg */
public class PluginSettingsManager extends AbstractSettingsManager<PluginDef>
{

    /** Stops multiple instantiation to preserve memory. */
    private PluginSettingsManager()
    {
    }

    /** Gets the singleton instance.
     * @return The instance. */
    public static PluginSettingsManager getInstance()
    {
        PluginSettingsManager manager = (PluginSettingsManager) instances.get(
                PluginSettingsManager.class);
        if (manager == null)
        {
            PluginSettingsManager tmp = new PluginSettingsManager();
            instances.put(PluginSettingsManager.class, tmp);
            return tmp;
        }
        return manager;
    }

    /** Gets the plugin definition for the xml element.
     * @param e The xml element.
     * @return The plugin definition or null. */
    @Override
    protected PluginDef getValue(Element e)
    {
        try
        {
            Attribute ext = null;
            Element cls = null;
            String className = null;
            String extension = null;
            Class<? extends Indexer> actualCls = null;
            cls = e.getChild("class");
            ext = e.getAttribute("extension");
            if (ext == null) throw new IllegalArgumentException();
            if (cls == null) throw new IllegalArgumentException();
            className = cls.getText();
            extension = ext.getValue();
            Class genericCls = Class.forName(className);
            if (genericCls.isAnnotationPresent(Indexer.class))
            {
                actualCls = (Class<? extends Indexer>) genericCls;
            }
            else throw new IllegalArgumentException();
            return new PluginDef(extension, actualCls);
        }
        catch (ClassNotFoundException ex)
        {
            return null;
        }
    }

    /** Gets the xml element for the plugin definition.
     * @param value The plugin defintion to serialize.
     * @param out The element to return.
     * @return The out parameter. */
    @Override
    protected Element getElement(PluginDef value, Element out)
    {
        Element e = new Element("class");
        e.setText(value.getIndexerClass().getName());
        out.addContent(e);
        out.setAttribute("extension", value.getExtension());
        return out;
    }

    /** Gets the filename for the config file.
     * @return The filename. */
    @Override
    protected String getFileName()
    {
        String key = "SERVICES_LOCATION";
        return (String) UserSettingsManager.getInstance().get(key);
    }

    /** Gets the default values for the config file.
     * @return The default values indexed by extension. */
    @Override
    protected Hashtable<String, PluginDef> getDefaults()
    {
        Hashtable<String, PluginDef> table = new Hashtable<String, PluginDef>();
        Class<? extends Indexer> cls = PdfIndexer.class;
        table.put("pdf", new PluginDef("pdf", cls));
        return table;
    }
}
TOP

Related Classes of pdfdb.settings.PluginSettingsManager

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.