📄 applicationdescriptor.java
字号:
/**
* Copyright (c) 2004 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.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;
import org.eclipse.core.runtime.CoreException;
import eclipseme.core.internal.utils.ColonDelimitedProperties;
/**
* This class is a representation of a Java Application Descriptor (jad)
* for a Midlet Suite.
* <p>
* <b>Note:</b> This class/interface is part of an interim API that is still
* under development and expected to change before reaching stability. It is
* being made available at this early stage to solicit feedback from pioneering
* adopters on the understanding that any code that uses this API will almost
* certainly be broken as the API evolves.
* </p>
* Copyright (c) 2004 Craig Setera<br>
* All Rights Reserved.<br>
* Licensed under the Eclipse Public License - v 1.0<p/>
* <br>
* $Revision: 1.7 $
* <br>
* $Date: 2006/02/11 21:26:57 $
* <br>
* @author Craig Setera
*/
public class ApplicationDescriptor {
/** The prefix of all midlet definition properties */
public static final String MIDLET_PREFIX = "MIDlet-";
/**
* The definition of a Midlet within the application descriptor.
*/
public static class MidletDefinition {
private String name;
private String icon;
private String className;
MidletDefinition(String definitionString) {
int fieldCount = 0;
name = "";
icon = "";
className = "";
StringTokenizer st = new StringTokenizer(definitionString, ",", true);
while (st.hasMoreTokens()) {
String token = st.nextToken();
if (token.equals(",")) {
fieldCount++;
} else {
switch (fieldCount) {
case 0: name = token; break;
case 1: icon = token; break;
case 2: className = token; break;
}
}
}
}
/**
* Construct a new midlet definition with the specified
* information.
*
* @param name
* @param icon
* @param className
*/
public MidletDefinition(String name, String icon, String className) {
super();
this.name = name;
this.icon = icon;
this.className = className;
}
/**
* @return Returns the className.
*/
public String getClassName() {
return className;
}
/**
* @param className The className to set.
*/
public void setClassName(String className) {
this.className = className;
}
/**
* @return Returns the icon.
*/
public String getIcon() {
return icon;
}
/**
* @param icon The icon to set.
*/
public void setIcon(String icon) {
this.icon = icon;
}
/**
* @return Returns the name.
*/
public String getName() {
return name;
}
/**
* @param name The name to set.
*/
public void setName(String name) {
this.name = name;
}
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append(name).append(',');
sb.append(icon).append(',');
sb.append(className);
return sb.toString();
}
}
private File sourceFile;
private List midletDefinitions;
private ColonDelimitedProperties manifestProperties;
/**
* Construct a new ApplicationDescriptor instance
* based on the data in the specified file.
*
* @param jadFile The file in which the descriptor is being held.
* @throws IOException when an error occurs reading the file
*/
public ApplicationDescriptor(File jadFile)
throws IOException
{
sourceFile = jadFile;
midletDefinitions = new ArrayList();
parseDescriptor(jadFile);
}
/**
* Add a new MidletDefinition instance.
*
* @param midletDefinition the midlet definition to be added
*/
public void addMidletDefinition(MidletDefinition midletDefinition) {
midletDefinitions.add(midletDefinition);
}
/**
* Return the configuration specification version associated with
* this JAD file.
*
* @return
* @throws CoreException
*/
public Version getConfigurationSpecificationVersion()
throws CoreException
{
Version version = null;
String configIdentifier =
manifestProperties.getProperty(IJADConstants.JAD_MICROEDITION_CONFIG);
// Attempt to parse the value
if (configIdentifier != null) {
String[] components = configIdentifier.split("-");
if (components.length == 2) {
version = new Version(components[1]);
}
}
// Make sure we fall back to something reasonable
if (version == null) {
version = new Version("1.0");
}
return version;
}
/**
* Return the list of MidletDefinition instances currently
* managed by the ApplicationDescriptor.
*
* @return a list of MidletDefinition instances
*/
public List getMidletDefinitions() {
return midletDefinitions;
}
/**
* Return the JAD URL specified in the application descriptor
* or <code>null</code> if it has not been specified. This
* method does not check the validity of the value.
*
* @return
*/
public String getMidletJarURL() {
String url = null;
if (manifestProperties != null) {
url = manifestProperties.getProperty(IJADConstants.JAD_MIDLET_JAR_URL);
}
return url;
}
/**
* Return the overall manifest properties.
*
* @return the manifest properties.
*/
public ColonDelimitedProperties getManifestProperties() {
return manifestProperties;
}
/**
* Return the current count of MidletDefinition instances within
* this application descriptor.
*
* @return the number of MidletDefinition instances
*/
public int getMidletCount() {
return midletDefinitions.size();
}
/**
* Store the ApplicationDescriptor instance into the same File
* from which it was originally read.
*
* @throws IOException when an error occurs while storing the descriptor
*/
public void store()
throws IOException
{
store(sourceFile);
}
/**
* Store the ApplicationDescriptor instance into the specified
* file.
*
* @param jadFile the file into which the descriptor will be written
* @throws IOException when an error occurs while storing the descriptor
*/
public void store(File jadFile)
throws IOException
{
// Copy the current properties and add the midlets
int index = 1;
ColonDelimitedProperties copy = copyProperties();
Iterator iter = midletDefinitions.iterator();
while (iter.hasNext()) {
MidletDefinition def = (MidletDefinition) iter.next();
String key = MIDLET_PREFIX + (index++);
String value = def.toString();
copy.setProperty(key, value);
}
// Write out the resulting manifest properties
FileOutputStream fos = new FileOutputStream(jadFile);
try {
copy.store(fos, "Midlet Property Definitions");
} finally {
try { fos.close(); } catch (IOException e) {}
}
}
/**
* Copy the manifest properties.
*
* @return the copy of the properties
*/
private ColonDelimitedProperties copyProperties() {
ColonDelimitedProperties copy = new ColonDelimitedProperties();
Iterator keys = manifestProperties.keySet().iterator();
while (keys.hasNext()) {
String key = (String) keys.next();
String value = manifestProperties.getProperty(key);
copy.setProperty(key, value);
}
return copy;
}
/**
* Parse the application descriptor file and store the information
* into this instance.
*
* @param jadFile the file to be parsed
* @throws IOException
*/
private void parseDescriptor(File jadFile)
throws IOException
{
if (jadFile.exists()) {
// Parse as a set of properties
FileInputStream fis = new FileInputStream(jadFile);
manifestProperties = new ColonDelimitedProperties();
manifestProperties.load(fis);
// Pull out the midlet definitions
parseMidletDefinitions();
}
}
/**
* Parse out the midlet definitions from the manifest properties.
*/
private void parseMidletDefinitions() {
ArrayList keysToRemove = new ArrayList(manifestProperties.size());
Iterator iter = manifestProperties.keySet().iterator();
while (iter.hasNext()) {
String key = (String) iter.next();
if (key.startsWith(MIDLET_PREFIX)) {
int midletNumber = -1;
try {
midletNumber = Integer.parseInt(key.substring(MIDLET_PREFIX.length()));
} catch (NumberFormatException e) {}
if (midletNumber != -1) {
String value = manifestProperties.getProperty(key);
MidletDefinition def = new MidletDefinition(value);
if (midletNumber >= midletDefinitions.size()) {
midletDefinitions.add(def);
} else {
midletDefinitions.add(midletNumber, def);
}
// Mark for removal from the properties
keysToRemove.add(key);
}
}
}
// Remove the midlets
Iterator keyIter = keysToRemove.iterator();
while (keyIter.hasNext()) {
String key = (String) keyIter.next();
manifestProperties.remove(key);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -