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

📄 symboldefinitionsetregistry.java

📁 eclipseme的最新版本的source,欢迎j2me程序员使用
💻 JAVA
字号:
/**
 * Copyright (c) 2003-2006 Craig Setera
 * All Rights Reserved.
 * Licensed under the Eclipse Public License - v 1.0
 * For more information see http://www.eclipse.org/legal/epl-v10.html
 */
package eclipseme.core.model;

import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;

import org.eclipse.core.runtime.IPath;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

import eclipseme.core.internal.EclipseMECorePlugin;
import eclipseme.core.internal.persistence.XMLPersistenceProvider;
import eclipseme.core.internal.utils.XMLUtils;
import eclipseme.core.persistence.IPersistable;
import eclipseme.core.persistence.IPersistenceProvider;
import eclipseme.core.persistence.PersistenceException;

/**
 * Provides a registry of SymbolDefinitionSet instances for use
 * by various parts of the core system.
 * <p />
 * Copyright (c) 2003-2006 Craig Setera<br>
 * All Rights Reserved.<br>
 * Licensed under the Eclipse Public License - v 1.0<p/>
 * <br>
 * $Revision: 1.3 $
 * <br>
 * $Date: 2006/11/27 00:58:06 $
 * <br>
 * @author Craig Setera
 */
public class SymbolDefinitionSetRegistry implements IPersistable {
	/** Singleton instance of the registry */
	public static final SymbolDefinitionSetRegistry singleton = new SymbolDefinitionSetRegistry();

	// The filename used to store and retrieve the symbol definitions registry */
	private static final String FILENAME = "symbolDefinitions.xml";

	// The actual mapping of symbol definition instances
	private Map registryMap;
	
	/**
	 * Private constructor for singleton access.
	 */
	private SymbolDefinitionSetRegistry() {
		
	}
	
	/**
	 * Add the specified SymbolDefinitions object to the registry of
	 * definitions.
	 * 
	 * @param definitions
	 * @throws PersistenceException
	 * @throws IllegalStateException if the provided definition set
	 * has a <code>null</code> name.
	 */
	public void addDefinitionSet(SymbolDefinitionSet definitions) 
		throws PersistenceException 
	{
		String name = definitions.getName();
		if (name == null) {
			throw new IllegalStateException("Definition must have a name specified.");
		}
		
		getRegistryMap().put(definitions.getName(), definitions);
	}
	
	/**
	 * Create and add a new SymbolDefinitions object to the registry of
	 * definitions with the specified name.
	 * 
	 * @param name
	 * @return
	 * @throws PersistenceException
	 */
	public SymbolDefinitionSet addNewDefinitionSet(String name)
		throws PersistenceException
	{
		SymbolDefinitionSet set = new SymbolDefinitionSet();
		set.setName(name);
		addDefinitionSet(set);
		
		return set;
	}
	
	/**
	 * Clear all of the registered SymbolDefinitions objects.
	 * 
	 * @throws PersistenceException
	 */
	public void clear() 
		throws PersistenceException
	{
		getRegistryMap().clear();
	}
	
	/**
	 * Return all of the definitions registered.
	 * 
	 * @return
	 * @throws PersistenceException 
	 */
	public SymbolDefinitionSet[] getAllSetDefinitions() 
		throws PersistenceException 
	{
		Collection definitions = getRegistryMap().values();
		return (SymbolDefinitionSet[]) definitions.toArray(new SymbolDefinitionSet[definitions.size()]);
	}
	
	/**
	 * Return all of the definition names registered.
	 * 
	 * @return
	 * @throws PersistenceException
	 */
	public String[] getAllDefinitionSetNames() 
		throws PersistenceException
	{
		Set keys = registryMap.keySet();
		return (String[]) keys.toArray(new String[keys.size()]);
	}
	
	/**
	 * Return the SymbolDefinitions instance registered with the specified
	 * name or <code>null</code> if the object cannot be found.
	 * 
	 * @param name
	 * @return
	 * @throws PersistenceException
	 */
	public SymbolDefinitionSet getSymbolDefinitionSet(String name) 
		throws PersistenceException 
	{
		return (SymbolDefinitionSet) getRegistryMap().get(name);
	}
	
	/**
	 * Load the contents of the symbol definitions registry from the storage file
	 * in the plug-in state location.
	 * 
	 * @throws PersistenceException
	 */
	public void load()
		throws PersistenceException
	{
		File storeFile = getStoreFile();
		if (storeFile.exists()) {
			try {
				Document document = XMLUtils.readDocument(storeFile);
				XMLPersistenceProvider pprovider = new XMLPersistenceProvider(document);
				loadUsing(pprovider);
			} catch (ParserConfigurationException e) {
				throw new PersistenceException(e.getMessage(), e);
			} catch (SAXException e) {
				throw new PersistenceException(e.getMessage(), e);
			} catch (IOException e) {
				throw new PersistenceException(e.getMessage(), e);
			}
		}
	}

	/**
	 * @see eclipseme.core.persistence.IPersistable#loadUsing(eclipseme.core.persistence.IPersistenceProvider)
	 */
	public void loadUsing(IPersistenceProvider persistenceProvider)
		throws PersistenceException 
	{
		if (registryMap != null) {
			String keysString = persistenceProvider.loadString("eclipsemeKeys");
			if ((keysString != null) && (keysString.length() > 0)) {
				String[] keys = keysString.split(",");
				
				for (int i = 0; i < keys.length; i++) {
					String key = keys[i];
					registryMap.put(key, persistenceProvider.loadPersistable(key));
				}
			}
		}
	}
	
	/**
	 * Remove the specified definition set from the registry.
	 * Does nothing if the specified set cannot be found in the registry.
	 * 
	 * @param setName
	 */
	public void removeDefinitionSet(String setName) {
		registryMap.remove(setName);
	}

	/**
	 * Remove the specified definition set from the registry.
	 * Does nothing if the specified set cannot be found in the registry.
	 * 
	 * @param set
	 */
	public void removeDefinitionSet(SymbolDefinitionSet set) {
		removeDefinitionSet(set.getName());
	}

	/**
	 * Store out the contents of the registry into the standard
	 * device storage file in the plug-in state location.
	 *  
	 * @throws PersistenceException
	 * @throws TransformerException
	 * @throws IOException
	 */
	public void store()
		throws PersistenceException, TransformerException, IOException
	{
		XMLPersistenceProvider pprovider = new XMLPersistenceProvider("symbolDefinitionsRegistry");
		storeUsing(pprovider);
		XMLUtils.writeDocument(getStoreFile(), pprovider.getDocument());
	}

	/**
	 * @see eclipseme.core.persistence.IPersistable#storeUsing(eclipseme.core.persistence.IPersistenceProvider)
	 */
	public void storeUsing(IPersistenceProvider persistenceProvider)
		throws PersistenceException 
	{
		StringBuffer keyString = new StringBuffer();
		
		Iterator definitions = getRegistryMap().values().iterator();
		while (definitions.hasNext()) {
			SymbolDefinitionSet definition = (SymbolDefinitionSet) definitions.next();
			
			String name = definition.getStorableName();
			persistenceProvider.storePersistable(name, definition);
			keyString.append(name);
			if (definitions.hasNext()) {
				keyString.append(",");
			}
		}
		
		persistenceProvider.storeString("eclipsemeKeys", keyString.toString());
	}
	
	/**
	 * Return the registry map.  The map will be loaded from
	 * persistent storage if this is the first creation.
	 * 
	 * @return
	 * @throws PersistenceException
	 */
	private synchronized Map getRegistryMap() 
		throws PersistenceException 
	{
		if (registryMap == null) {
			registryMap = new HashMap();
			load();
		}
		
		return registryMap;
	}
	
	/**
	 * Get the file used to persist the symbol definitions.
	 * 
	 * @return
	 */
	private File getStoreFile() {
		IPath pluginStatePath = EclipseMECorePlugin.getDefault().getStateLocation();
		IPath storePath = pluginStatePath.append(FILENAME);
		
		return storePath.toFile();
	}
}

⌨️ 快捷键说明

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