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

📄 guessingapiimporthelper.java

📁 eclipseme的最新版本的source,欢迎j2me程序员使用
💻 JAVA
字号:
/**
 * Copyright (c) 2003-2005 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.importer;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.zip.ZipFile;

import org.eclipse.core.runtime.IStatus;

import eclipseme.core.internal.EclipseMECorePlugin;
import eclipseme.core.model.API;
import eclipseme.core.model.APIType;
import eclipseme.core.model.Version;

/**
 * A library import helper that looks for particular classes
 * in the library file to guess at the appropriate API's.
 * <p />
 * Copyright (c) 2003-2005 Craig Setera<br>
 * All Rights Reserved.<br>
 * Licensed under the Eclipse Public License - v 1.0<p/>
 * <br>
 * $Revision: 1.2 $
 * <br>
 * $Date: 2006/02/13 01:16:52 $
 * <br>
 * @author Craig Setera
 */
public class GuessingAPIImportHelper implements ILibraryAPIImportHelper {
	// The file in which the optional API definitions can be found
	private static final String OPTIONAL_API_FILE = "opt_apis.txt";
	
	// Basic configuration and profile classes used for recognition
	private static final String MIDP_10_CLASS = "javax/microedition/midlet/MIDlet.class";
	private static final String MIDP_10_UI_CLASS = "javax/microedition/lcdui/Item.class";
	private static final String MIDP_20_CLASS = "javax/microedition/io/SecurityInfo.class";
	
	private static final String CLDC_10_CLASS = "javax/microedition/io/Connection.class";
	private static final String CLDC_11_CLASS = "java/lang/Float.class";

	// A holder class for optional library information
	private static class OptionalLibraryInfo {
		String classEntry;
		String identifier;
		String description;
		Version version;
		
		OptionalLibraryInfo(
				String classEntry, 
				String identifier, 
				String description, 
				String versionString)
		{
			this.classEntry = classEntry;
			this.identifier = identifier;
			this.description = description;
			this.version = new Version(versionString);
		}
	}
	
	private static OptionalLibraryInfo[] optionalLibraryInfo;
	
	/**
	 * Return the optional library information.  This information
	 * is read from the file opt_apis.txt.
	 * 
	 * @return
	 */
	private static OptionalLibraryInfo[] getOptionalLibraryInfo() {
		if (optionalLibraryInfo == null) {
			optionalLibraryInfo = readInfo();
		}
		
		return optionalLibraryInfo;
	}
	
	/**
	 * Read and return the optional library information.  This method
	 * should not return <code>null</code>, but may return an empty list
	 * in the case of a problem.
	 * 
	 * @return
	 */
	private static OptionalLibraryInfo[] readInfo() {
		ArrayList infoList = new ArrayList();

		InputStream apiStream =
			GuessingAPIImportHelper.class.getResourceAsStream(OPTIONAL_API_FILE);
		if (apiStream != null) {
			String line = null;
			
			InputStreamReader isr = new InputStreamReader(apiStream);
			BufferedReader reader = new BufferedReader(isr);
			
			try {
				while ((line = reader.readLine()) != null) {
					String[] components = line.split("\\|");
					
					OptionalLibraryInfo info = new OptionalLibraryInfo(
						components[0],
						components[1],
						components[2],
						components[3]);
					infoList.add(info);
				}
			} catch (IOException e) {
				EclipseMECorePlugin.log(IStatus.WARNING, "Error reading optional API information", e);
			} finally {
				try { reader.close(); } catch (IOException e) {}
			}
		}
		
		return (OptionalLibraryInfo[]) infoList.toArray(new OptionalLibraryInfo[infoList.size()]);
	}

	/**
	 * @see eclipseme.core.importer.ILibraryAPIImportHelper#getAPIs(java.io.File)
	 */
	public API[] getAPIs(File libraryFile) {
		ArrayList apis = new ArrayList();
		
		ZipFile zipFile = null;
		try {
			zipFile = new ZipFile(libraryFile);
			
			// NOTE: Some older libraries contain a combination of 
			// configurations and profiles in one archive.  The
			// order of these tests should remain the same as listed.
			guessConfiguration(zipFile, apis);
			guessProfile(zipFile, apis);
			guessOptional(zipFile, apis);
		} catch (IOException e) {
			EclipseMECorePlugin.log(IStatus.WARNING, "Error opening zip file " + libraryFile, e);
		} finally {
			if (zipFile != null) {
				try { zipFile.close(); } catch (IOException e) {}
			}
		}
		
		return (API[]) apis.toArray(new API[apis.size()]);
	}

	/**
	 * Attempt to guess if the library has a configuration
	 * and if so, what type and version.
	 * 
	 * @param zipfile
	 * @param apis
	 */
	private void guessConfiguration(ZipFile zipfile, ArrayList apis) {
		boolean isCLDC = zipContainsEntry(zipfile, CLDC_10_CLASS);
		boolean isCLDC11 = zipContainsEntry(zipfile, CLDC_11_CLASS);
		
		if (isCLDC) {
			API api = new API();
			apis.add(api);

			api.setIdentifier("CLDC");
			api.setName("Connected Limited UEIDevice Configuration");
			api.setType(APIType.CONFIGURATION);
			
			Version version = isCLDC11 ? new Version("1.1") : new Version("1.0");
			api.setVersion(version);
		}
	}

	/**
	 * Guess at the optional information for the specified library file.
	 * 
	 * @param zipfile
	 * @param apis
	 */
	private void guessOptional(ZipFile zipfile, ArrayList apis) {
		OptionalLibraryInfo[] infoArray = getOptionalLibraryInfo();
		for (int i = 0; i < infoArray.length; i++) {
			OptionalLibraryInfo info = infoArray[i];
			if (zipContainsEntry(zipfile, info.classEntry)) {
				API api = new API();
				apis.add(api);
				
				api.setIdentifier(info.identifier);
				api.setName(info.description);
				api.setType(APIType.OPTIONAL);
				api.setVersion(info.version);
			}
		}
	}

	/**
	 * Attempt to guess if the library has a profile
	 * and if so, what type and version.
	 * 
	 * @param libraryFile
	 * @param apis
	 */
	private void guessProfile(ZipFile libraryFile, ArrayList apis) {
		boolean isMIDP = zipContainsEntry(libraryFile, MIDP_10_CLASS);
		boolean isImp = isMIDP && !(zipContainsEntry(libraryFile, MIDP_10_UI_CLASS));
		boolean is2_0 = isMIDP && zipContainsEntry(libraryFile, MIDP_20_CLASS);
		Version version = is2_0 ? new Version("2.0") : new Version("1.0");
		
		if (isMIDP) {
			API api = new API();
			apis.add(api);
			
			if (isImp) {
				api.setIdentifier("IMP");
				api.setName("Information Module Profile");
			} else {
				api.setIdentifier("MIDP");
				api.setName("Mobile Information UEIDevice Profile");
			}
			
			api.setType(APIType.PROFILE);
			api.setVersion(version);
		}
	}

	/**
	 * Return a boolean indicating whether the specified zip file contains
	 * the specified entry.
	 * 
	 * @param zipFile
	 * @param entryName
	 * @return
	 */
	private boolean zipContainsEntry(ZipFile zipFile, String entryName) {
		return zipFile.getEntry(entryName) != null;
	}
}

⌨️ 快捷键说明

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