Skip to main content
summaryrefslogtreecommitdiffstats
blob: afa712e5ecf3f3e1753d6ac764157cd949f827b6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*******************************************************************************
 * Copyright (c) 2003, 2005 IBM Corporation and others.
 * 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
 *
 * Contributors:
 * IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.j2ee.internal.wizard;

import java.io.File;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.jst.j2ee.application.Application;
import org.eclipse.jst.j2ee.client.ApplicationClient;
import org.eclipse.jst.j2ee.commonarchivecore.internal.ApplicationClientFile;
import org.eclipse.jst.j2ee.commonarchivecore.internal.Archive;
import org.eclipse.jst.j2ee.commonarchivecore.internal.EARFile;
import org.eclipse.jst.j2ee.commonarchivecore.internal.EJBJarFile;
import org.eclipse.jst.j2ee.commonarchivecore.internal.WARFile;
import org.eclipse.jst.j2ee.commonarchivecore.internal.impl.CommonarchiveFactoryImpl;
import org.eclipse.jst.j2ee.ejb.EJBJar;
import org.eclipse.jst.j2ee.internal.J2EEVersionConstants;
import org.eclipse.jst.j2ee.internal.plugin.J2EEPlugin;
import org.eclipse.jst.j2ee.webapplication.WebApp;
import org.eclipse.wst.common.frameworks.internal.WTPPlugin;



/**
 * @author Sachin
 * 
 * To change this generated comment edit the template variable "typecomment":
 * Window>Preferences>Java>Templates. To enable and disable the creation of type comments go to
 * Window>Preferences>Java>Code Generation.
 */
public class ImportUtil {

	public static final int UNKNOWN = 0;
	public static final int EARFILE = 1;
	public static final int EJBJARFILE = 2;
	public static final int WARFILE = 3;
	public static final int CLIENTJARFILE = 4;
	public static final int RARFILE = 5;
	public static final int IMPORTCLASSTYPE = 6;
	public static final int J2EE14 = 256;
	public static final int J2EE13 = 128;
	public static final int J2EE12 = 64;
	public static final int J2EESpec = J2EE12 + J2EE13 + J2EE14;
	public static final String EAR = "EAR"; //$NON-NLS-1$
	public static final String EJB = "EJB"; //$NON-NLS-1$
	public static final String WAR = "WEB"; //$NON-NLS-1$
	public static final String JAR = "CLIENT"; //$NON-NLS-1$
	public static final String RAR = "RAR"; //$NON-NLS-1$
	public static final String[] SUFFIXES = {"", EAR, EJB, WAR, JAR, RAR, ""}; //$NON-NLS-1$ //$NON-NLS-2$

	public static int getFileType(String fileName) {
		Archive anArchive = null;
		try {
			try {
				anArchive = CommonarchiveFactoryImpl.getActiveFactory().openArchive(fileName);
				int archiveType = getArchiveType(anArchive);
				if (archiveType == UNKNOWN && isImportClassType(fileName))
					return IMPORTCLASSTYPE;
				return archiveType;
			} catch (Exception e) {
				if (isImportClassType(fileName))
					return IMPORTCLASSTYPE;
				return UNKNOWN;
			}
		} finally {
			if (anArchive != null && anArchive.isOpen())
				anArchive.close();
		}
	}

	//TODO: use new getJ2EEVerions switch statements
	public static int getVersionedFileType(String fileName) {
		Archive anArchive = null;
		try {
			int archiveType = UNKNOWN;
			try {
				anArchive = CommonarchiveFactoryImpl.getActiveFactory().openArchive(fileName);
				try {
					if (anArchive.isEJBJarFile()) {
						archiveType = EJBJARFILE;
						EJBJar ejbJar = ((EJBJarFile) anArchive).getDeploymentDescriptor();
						if (ejbJar.getVersionID() == J2EEVersionConstants.EJB_1_1_ID)
							archiveType |= J2EE12;
						else if (ejbJar.getVersionID() == J2EEVersionConstants.EJB_2_0_ID)
							archiveType |= J2EE13;
						else if (ejbJar.getVersionID() == J2EEVersionConstants.EJB_2_1_ID)
							archiveType |= J2EE14;
					} else if (anArchive.isWARFile()) {
						archiveType = WARFILE;
						WebApp war = ((WARFile) anArchive).getDeploymentDescriptor();
						if (war.getVersionID() == J2EEVersionConstants.WEB_2_2_ID)
							archiveType |= J2EE12;
						else if (war.getVersionID() == J2EEVersionConstants.WEB_2_3_ID)
							archiveType |= J2EE13;
						else if (war.getVersionID() == J2EEVersionConstants.WEB_2_4_ID)
							archiveType |= J2EE14;
					} else if (anArchive.isApplicationClientFile()) {
						archiveType = CLIENTJARFILE;
						ApplicationClient appClient = ((ApplicationClientFile) anArchive).getDeploymentDescriptor();
						if (appClient.getVersionID() == J2EEVersionConstants.J2EE_1_2_ID)
							archiveType |= J2EE12;
						else if (appClient.getVersionID() == J2EEVersionConstants.J2EE_1_3_ID)
							archiveType |= J2EE13;
						else if (appClient.getVersionID() == J2EEVersionConstants.J2EE_1_4_ID)
							archiveType |= J2EE14;
					} else if (anArchive.isRARFile()) {
						archiveType = RARFILE | J2EE13;
					} else if (anArchive.isEARFile()) {
						archiveType = EARFILE;
						Application app = ((EARFile) anArchive).getDeploymentDescriptor();
						if (app.getVersionID() == J2EEVersionConstants.J2EE_1_2_ID)
							archiveType |= J2EE12;
						else if (app.getVersionID() == J2EEVersionConstants.J2EE_1_3_ID)
							archiveType |= J2EE13;
						else if (app.getVersionID() == J2EEVersionConstants.J2EE_1_4_ID)
							archiveType |= J2EE14;
					}
				} catch (Exception e) {
					//Ignore
				}

			} catch (Exception e) {
				//Ignore
			}
			if (archiveType == UNKNOWN && isImportClassType(fileName))
				archiveType = IMPORTCLASSTYPE;
			return archiveType;
		} finally {
			if (anArchive != null && anArchive.isOpen())
				anArchive.close();
		}
	}

	public static int getArchiveType(Archive anArchive) {
		int type = UNKNOWN;
		try {
			if (anArchive.isEJBJarFile())
				type = EJBJARFILE;
			else if (anArchive.isWARFile())
				type = WARFILE;
			else if (anArchive.isApplicationClientFile())
				type = CLIENTJARFILE;
			else if (anArchive.isRARFile())
				type = RARFILE;
			else if (anArchive.isEARFile())
				type = EARFILE;
		} catch (Exception e) {
			//Ignore
		}
		return type;
	}

	public static boolean isImportClassType(String fileName) {
		File file = new File(fileName);
		String fileExtension = getExtension(file);
		if (file.isFile()) {
			if (fileExtension.equalsIgnoreCase("jar") || //$NON-NLS-1$
						fileExtension.equalsIgnoreCase("zip") || //$NON-NLS-1$
						fileExtension.equalsIgnoreCase("class")) //$NON-NLS-1$
				return true;
		} else if (file.isDirectory()) { //disable/enable drag/drop directories
			return false;
		}
		return false;
	}

	public static String getExtension(File f) {
		String ext = null;
		String s = f.getName();
		int i = s.lastIndexOf('.');

		if (i > 0 && i < s.length() - 1) {
			ext = s.substring(i + 1).toLowerCase();
		}
		return ext;
	}


	public static String findMatchingProjectName(String projectName) {
		if (projectName.trim().length() > 0) {
			IWorkspaceRoot root = J2EEPlugin.getWorkspace().getRoot();
			IProject[] projects = root.getProjects();
			String lowerCaseName = projectName.toLowerCase();
			// iterate through all projects a compare lowercase names
			if (projectName == null || projectName.length() == 0) {
				if (projects.length == 1)
					return projects[0].getName();
				return null;
			}
			for (int i = 0; i < projects.length; i++) {
				if (projects[i].exists()) {
					if (WTPPlugin.isPlatformCaseSensitive()) {
						if (projects[i].getName().equals(projectName))
							return projects[i].getName();
					} else {
						if (projects[i].getName().toLowerCase().equals(lowerCaseName))
							return projects[i].getName();
					}
				}
			}
		}
		return projectName;
	}



}

Back to the top