📄 jadfilemigrator.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.internal.migration;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.jar.Manifest;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import eclipseme.core.internal.EclipseMECorePlugin;
import eclipseme.core.internal.utils.ColonDelimitedProperties;
import eclipseme.core.model.IMidletSuiteProject;
import eclipseme.core.model.MidletSuiteFactory;
import eclipseme.core.nature.J2MENature;
/**
* Handle the migration of the incorrectly formatted JAD files created
* in earlier versions of the plugin.
* <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.7 $
* <br>
* $Date: 2006/11/12 01:11:05 $
* <br>
* @author Craig Setera
*/
public class JADFileMigrator {
/**
*
* @uml.property name="workspaceRoot"
* @uml.associationEnd
* @uml.property name="workspaceRoot" multiplicity="(1 1)"
*/
// The root of the workspace that allows us to look through projects
private IWorkspaceRoot workspaceRoot;
/**
* Default constructor
*/
public JADFileMigrator(IWorkspaceRoot workspaceRoot) {
super();
this.workspaceRoot = workspaceRoot;
}
/**
* Do the work of this migration.
*
* @throws CoreException
*/
public void doMigration()
throws CoreException
{
IProject[] projects = workspaceRoot.getProjects();
for (int i = 0; i < projects.length; i++) {
if (projects[i].isOpen() && J2MENature.hasJ2MENature(projects[i])) {
try {
migrateJadFileAsNecessary(projects[i]);
} catch (IOException e) {
EclipseMECorePlugin.throwCoreException(IStatus.WARNING, 999, e);
}
}
}
}
/**
* Copy the source properties to the target properties.
*
* @param source
* @param target
*/
private void copyProperties(Properties source, ColonDelimitedProperties target) {
Iterator iterator = source.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry entry = (Map.Entry) iterator.next();
target.setProperty((String) entry.getKey(), (String) entry.getValue());
}
}
/**
* Return the midlet suite associated with the specified project
* or <code>null</code> if there is no midlet suite associated
* with the specified project.
*
* @param project
* @return
*/
private IMidletSuiteProject getMidletSuite(IProject project) {
IMidletSuiteProject midletSuite = null;
IJavaProject javaProject = JavaCore.create(project);
if (javaProject != null) {
midletSuite =
MidletSuiteFactory.getMidletSuiteProject(javaProject);
}
return midletSuite;
}
/**
* Do the actual JAD file migration.
*
* @param jadFile
* @throws IOException
* @throws CoreException
*/
private void migrateJadFile(IFile jadFile)
throws IOException, CoreException
{
// Rewrite the file
Properties properties = new Properties();
properties.load(jadFile.getContents(true));
ColonDelimitedProperties newProperties = new ColonDelimitedProperties();
copyProperties(properties, newProperties);
FileOutputStream fos =
new FileOutputStream(jadFile.getLocation().toFile());
newProperties.store(fos, "");
fos.close();
// Refresh the metadata
jadFile.refreshLocal(IResource.DEPTH_ZERO, new NullProgressMonitor());
}
/**
* Migrate the JAD file in the specified project.
*
* @param project
* @throws CoreException
* @throws IOException
*/
private void migrateJadFileAsNecessary(IProject project)
throws CoreException, IOException
{
IMidletSuiteProject midletSuite = getMidletSuite(project);
if (midletSuite != null) {
IFile jadFile = midletSuite.getJadFile();
if (jadFile.exists() && !jadFile.isReadOnly()) {
migrateJadFileAsNecessary(jadFile);
}
}
}
/**
* Migrate the specified JAD file from a Properties
* format to a manifest style format.
*
* @param jadFile
* @throws CoreException
* @throws IOException
*/
private void migrateJadFileAsNecessary(IFile jadFile)
throws CoreException, IOException
{
boolean requiresMigration = shouldMigrateFile(jadFile);
if (requiresMigration) {
migrateJadFile(jadFile);
}
}
/**
* Return a boolean indicating whether the specified file
* requires migration.
*
* @param jadFile
* @return
* @throws CoreException
*/
private boolean shouldMigrateFile(IFile jadFile) throws CoreException {
boolean requiresMigration = true;
// Try opening the file as a Manifest formatted file
// If successful, the file does not need to be migrated
// to the correct format.
InputStream jadStream = jadFile.getContents(true);
try {
// Test the creation of a manifest on the stream
new Manifest(jadStream);
requiresMigration = false;
} catch (Exception e) {
// If there was an error during the load operation, we
// will assume this wasn't a properties file.
} finally {
try {
jadStream.close();
} catch (IOException e1) {
}
}
return requiresMigration;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -