📄 utils.java.svn-base
字号:
/******************************************************************************
* Copyright (c) 2006 BEA Systems, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
******************************************************************************/
package com.newegg.eclipse.soaw.facet.codegen.ui.util;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jst.j2ee.web.componentcore.util.WebArtifactEdit;
import org.eclipse.jst.j2ee.webapplication.Servlet;
import org.eclipse.jst.j2ee.webapplication.ServletMapping;
import org.eclipse.jst.j2ee.webapplication.ServletType;
import org.eclipse.jst.j2ee.webapplication.WebApp;
import org.eclipse.jst.j2ee.webapplication.WebapplicationFactory;
import org.eclipse.wst.common.componentcore.ComponentCore;
import org.eclipse.wst.common.componentcore.resources.IVirtualComponent;
import org.eclipse.wst.common.componentcore.resources.IVirtualFolder;
import org.eclipse.wst.common.environment.IStatusHandler;
import org.eclipse.wst.ws.internal.common.BundleUtils;
import org.osgi.framework.Bundle;
import com.newegg.eclipse.soaw.facet.codegen.ui.CodeGenUIMessages;
import com.newegg.eclipse.soaw.facet.codegen.ui.beans.ClassDataModel;
import com.newegg.eclipse.soaw.facet.codegen.ui.plugin.CodeGenPlugin;
public final class Utils
{
private static Map<String, String[]> dataBaseDriverAndDialect = null;
/**
* @return third part folder that is in the plug_in has installed in eclipse.
*/
public static IPath getThirdLibFolderPath()
{
String pluginId = CodeGenUIMessages.NEWEGG_THIRDPART_PLUGINID;
IPath thirdLibFolderPath = BundleUtils.getJarredPluginPath(pluginId).append(
CodeGenUIMessages.NEWEGG_THIRDPART_LIB_FOLDER);
return thirdLibFolderPath;
}
/**
* Get all framework versions from libraries that is specified.
*
* @return A array for framework version that is specified.
*/
public static String[] getFrameworkVesions(String curFramework)
{
IPath thirdPartParentFolderPath = getThirdLibFolderPath().append(curFramework);
if (thirdPartParentFolderPath == null)
{
return new String[]
{ "" };
}
File thirdPartParentFolder = thirdPartParentFolderPath.toFile();
File[] thirdPartParentFiles = thirdPartParentFolder.listFiles();
String[] thirdPartParentFileVersions = new String[thirdPartParentFiles.length];
for (int i = 0; i < thirdPartParentFiles.length; i++)
{
thirdPartParentFileVersions[i] = thirdPartParentFiles[i].getName();
}
return thirdPartParentFileVersions;
}
/**
* Returns the location of the web project's WEB-INF/lib directory.
*
* @param pj
* the web project
* @return the location of the WEB-INF/lib directory
*/
public static IFolder getWebInfLibDir(final IProject pj)
{
final IVirtualComponent vc = ComponentCore.createComponent(pj);
final IVirtualFolder vf = vc.getRootFolder().getFolder("WEB-INF/lib");
return (IFolder) vf.getUnderlyingFolder();
}
/**
* Copies a resource from within the FormGen plugin to a destination in the workspace.
*
* @param src
* the path of the resource within the plugin
* @param dest
* the destination path within the workspace
*/
public static void copyFromPlugin(final IPath src, final IFile dest) throws CoreException
{
try
{
// final Bundle bundle = CodeGenPlugin.getInstance().getBundle();
// final InputStream in = FileLocator.openStream(bundle, src, false);
final InputStream in = new FileInputStream(src.toFile());
if (!dest.exists())
dest.create(in, true, null);
} catch (IOException e)
{
throw new CoreException(CodeGenPlugin.createErrorStatus(e.getMessage(), e));
}
}
/**
* Adds entries in the web.xml file for the FormGen servlet. Uses the "*.form" for the URL
* pattern.
*
* @param pj
* the web application that the servlet is part of
*/
public static void registerFormGenServlet(final IProject pj)
{
registerFormGenServlet(pj, "*.form");
}
/**
* Adds entries in the web.xml file for the FormGen servlet.
*
* @param pj
* the web application that the servlet is part of
* @param urlPattern
* the url pattern for the servlet
*/
public static void registerFormGenServlet(final IProject pj, final String urlPattern)
{
final WebArtifactEdit artifact = WebArtifactEdit.getWebArtifactEditForWrite(pj);
try
{
final WebApp root = artifact.getWebApp();
final Servlet servlet = WebapplicationFactory.eINSTANCE.createServlet();
final ServletType servletType = WebapplicationFactory.eINSTANCE.createServletType();
servletType.setClassName("com.formgen.core.FormGenServlet");
servlet.setWebType(servletType);
servlet.setServletName("FormGenServlet");
root.getServlets().add(servlet);
final ServletMapping mapping = WebapplicationFactory.eINSTANCE.createServletMapping();
mapping.setServlet(servlet);
mapping.setUrlPattern(urlPattern);
root.getServletMappings().add(mapping);
artifact.saveIfNecessary(null);
} finally
{
artifact.dispose();
}
}
/**
* Create a empty file that the file path is specified.
*
* @param srcPath
* @param pj
* @param classDataModel
* @param handler
* @return
* @throws Exception
*/
public static File getEmptyFileFormIFile(IPath srcPath, IProject pj, ClassDataModel classDataModel,
IStatusHandler handler) throws Exception
{
String packageRelativePath = Utils.getPackageRelativePath(classDataModel.getPackageName());
IPath newFilePath = null;
if (ValidataTool.stringIsNull(packageRelativePath))
{
newFilePath = new Path(srcPath.toFile().getName()).append(classDataModel.getClassName()
+ classDataModel.getFileNameSuffix());
} else
{
newFilePath = new Path(srcPath.toFile().getName()).append(packageRelativePath).append(
classDataModel.getClassName() + classDataModel.getFileNameSuffix());
}
StringWriter sw = new StringWriter(128);
sw.write("");
byte[] bytes = sw.getBuffer().toString().getBytes(pj.getDefaultCharset());
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
IFile newFile = org.eclipse.wst.common.internal.environment.eclipse.FileResourceUtils.createFile(pj,
newFilePath, bais, handler);
if (bais != null)
{
bais.close();
}
return new File(newFile.getLocation().toString());
}
/**
* Create a empty file that the resource is specified.
*
* @param srcPath
* @param pj
* @param classDataModel
* @return
* @throws Exception
*/
public static File getEmptyFileFormResource(IPath srcPath, IProject pj, ClassDataModel classDataModel)
throws Exception
{
IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
IResource resource = workspaceRoot.findMember(new Path(pj.getName()).append(srcPath.toFile().getName()));
File outFile = null;
if (resource != null)
{
String packageRelativePath = Utils.getPackageRelativePath(classDataModel.getPackageName());
String fileName = classDataModel.getClassName() + classDataModel.getFileNameSuffix();
if (ValidataTool.stringIsNull(packageRelativePath))
{
outFile = new File(resource.getLocation().toString());
} else
{
outFile = new File(resource.getLocation().append(packageRelativePath).toString());
}
if (!outFile.exists())
{
outFile.mkdirs();
}
outFile = new File(outFile.getAbsolutePath() + File.separator + fileName);
if (!outFile.exists())
{
outFile.createNewFile();
}
}
return outFile;
}
/**
* Get relative path from valid package full name.
*
* @param packageName
* @return
*/
public static String getPackageRelativePath(String packageName)
{
String packagePath = "";
if (!ValidataTool.stringIsNull(packageName))
{
packagePath = packageName.replace('.', '/');
}
return packagePath;
}
/**
* Get current user from system.
*
* @return
*/
public static String getSystemUserName()
{
return System.getProperty("user.name");
}
/**
* Get current year.
*
* @return
*/
public static String getCurrentYear()
{
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
return String.valueOf(year);
}
/**
* Initialization all database drivers.
*
* @return
*/
private static Map<String, String[]> initDataBaseDriverAndDialect()
{
if (dataBaseDriverAndDialect == null)
{
dataBaseDriverAndDialect = new HashMap<String, String[]>();
setDriverAndDialect("MS_SQL_Server_2005", CodeGenUIMessages.MS_SQL_Server_2005);
setDriverAndDialect("Microsoft_SQL_Server", CodeGenUIMessages.Microsoft_SQL_Server);
setDriverAndDialect("DB2", CodeGenUIMessages.DB2);
setDriverAndDialect("DB2_AS_400", CodeGenUIMessages.DB2_AS_400);
setDriverAndDialect("DB2_OS390", CodeGenUIMessages.DB2_OS390);
setDriverAndDialect("PostgreSQL", CodeGenUIMessages.PostgreSQL);
setDriverAndDialect("MySQL", CodeGenUIMessages.MySQL);
setDriverAndDialect("MySQL_with_InnoDB", CodeGenUIMessages.MySQL_with_InnoDB);
setDriverAndDialect("MySQL_with_MyISAM", CodeGenUIMessages.MySQL_with_MyISAM);
setDriverAndDialect("Oracle_any_version", CodeGenUIMessages.Oracle_any_version);
setDriverAndDialect("Oracle_9i_10g", CodeGenUIMessages.Oracle_9i_10g);
setDriverAndDialect("Sybase", CodeGenUIMessages.Sybase);
setDriverAndDialect("Sybase_Anywhere", CodeGenUIMessages.Sybase_Anywhere);
// setDriverAndDialect("Microsoft_SQL_Server", CodeGenUIMessages.Microsoft_SQL_Server);
// setDriverAndDialect("MS_SQL_Server_2005", CodeGenUIMessages.MS_SQL_Server_2005);
setDriverAndDialect("SAP_DB", CodeGenUIMessages.SAP_DB);
setDriverAndDialect("Informix", CodeGenUIMessages.Informix);
setDriverAndDialect("HypersonicSQL", CodeGenUIMessages.HypersonicSQL);
setDriverAndDialect("Ingres", CodeGenUIMessages.Ingres);
setDriverAndDialect("Progress", CodeGenUIMessages.Progress);
setDriverAndDialect("Mckoi_SQL", CodeGenUIMessages.Mckoi_SQL);
setDriverAndDialect("Interbase", CodeGenUIMessages.Interbase);
setDriverAndDialect("Pointbase", CodeGenUIMessages.Pointbase);
setDriverAndDialect("FrontBase", CodeGenUIMessages.FrontBase);
setDriverAndDialect("Firebird", CodeGenUIMessages.Firebird);
}
return dataBaseDriverAndDialect;
}
/**
* Set driver and dialect.
*
* @param key
* @param inStr
* @return
*/
private static Map<String, String[]> setDriverAndDialect(String key, String inStr)
{
String[] curStrs = inStr.split("\\,");
if (!ValidataTool.stringIsNull(curStrs[1]))
{
dataBaseDriverAndDialect.put(key, curStrs);
}
return dataBaseDriverAndDialect;
}
/**
* Get driver.
*
* @param key
* @return
*/
public static String getDriverNameByKey(String key)
{
initDataBaseDriverAndDialect();
String[] value = dataBaseDriverAndDialect.get(key);
return ValidataTool.stringIsNull(value[1]) ? "" : value[1];
}
/**
* Get dialect.
*
* @param key
* @return
*/
public static String getDialectNameByKey(String key)
{
initDataBaseDriverAndDialect();
String[] value = dataBaseDriverAndDialect.get(key);
return ValidataTool.stringIsNull(value[1]) ? "" : value[0];
}
/**
* Get database types.
*
* @return
*/
public static String[] getDataBaseTypes()
{
initDataBaseDriverAndDialect();
Set<String> set = dataBaseDriverAndDialect.keySet();
String[] databaseTypes = new String[set.size()];
int i = 0;
for (Iterator iterator = set.iterator(); iterator.hasNext();)
{
String curEle = (String) iterator.next();
databaseTypes[i] = curEle;
i++;
}
return databaseTypes;
}
/**
* Get the values from preferences store.
*
* @param key
* @return
*/
public static String getStringFromPreference(String key)
{
IPreferenceStore store = CodeGenPlugin.getInstance().getPreferenceStore();
return store.getString(key);
}
/**
* Assemble the valid package from specify strings.
*
* @param basePackage
* @param relativePackage
* @return
*/
public static String getValidPackage(String basePackage, String relativePackage)
{
if (ValidataTool.stringIsNull(basePackage))
{
return relativePackage;
} else
{
return basePackage + Constants.FILE_SPERATOR_POINT + relativePackage;
}
}
public static void main(String[] args)
{
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -