geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sppa...@apache.org
Subject svn commit: r521731 - in /geronimo/sandbox/j2g/src/sources-tool/src: ./ org/ org/apache/ org/apache/apps/ org/apache/apps/j2gmigrator/ org/apache/apps/j2gmigrator/sources/
Date Fri, 23 Mar 2007 14:11:15 GMT
Author: sppatel
Date: Fri Mar 23 07:11:14 2007
New Revision: 521731

URL: http://svn.apache.org/viewvc?view=rev&rev=521731
Log:
accidently deleted src in last commit

Added:
    geronimo/sandbox/j2g/src/sources-tool/src/
    geronimo/sandbox/j2g/src/sources-tool/src/org/
    geronimo/sandbox/j2g/src/sources-tool/src/org/apache/
    geronimo/sandbox/j2g/src/sources-tool/src/org/apache/apps/
    geronimo/sandbox/j2g/src/sources-tool/src/org/apache/apps/j2gmigrator/
    geronimo/sandbox/j2g/src/sources-tool/src/org/apache/apps/j2gmigrator/sources/
    geronimo/sandbox/j2g/src/sources-tool/src/org/apache/apps/j2gmigrator/sources/FileFilterImpl.java
  (with props)
    geronimo/sandbox/j2g/src/sources-tool/src/org/apache/apps/j2gmigrator/sources/JavaParser.java
  (with props)
    geronimo/sandbox/j2g/src/sources-tool/src/org/apache/apps/j2gmigrator/sources/JavaSourceFilter.java
  (with props)
    geronimo/sandbox/j2g/src/sources-tool/src/org/apache/apps/j2gmigrator/sources/ParsedSource.java
  (with props)
    geronimo/sandbox/j2g/src/sources-tool/src/org/apache/apps/j2gmigrator/sources/SourceIdentificationTool.java
  (with props)

Added: geronimo/sandbox/j2g/src/sources-tool/src/org/apache/apps/j2gmigrator/sources/FileFilterImpl.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/j2g/src/sources-tool/src/org/apache/apps/j2gmigrator/sources/FileFilterImpl.java?view=auto&rev=521731
==============================================================================
--- geronimo/sandbox/j2g/src/sources-tool/src/org/apache/apps/j2gmigrator/sources/FileFilterImpl.java
(added)
+++ geronimo/sandbox/j2g/src/sources-tool/src/org/apache/apps/j2gmigrator/sources/FileFilterImpl.java
Fri Mar 23 07:11:14 2007
@@ -0,0 +1,65 @@
+/**
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+
+package org.apache.apps.j2gmigrator.sources;
+
+import java.io.File;
+import java.io.FileFilter;
+
+/**
+ * File filter for JSP files.
+ */
+public class FileFilterImpl implements FileFilter
+{
+	/**
+	 * Files extensions.
+	 */
+	private String[] extensions;
+
+	/**
+	 * Creates file filter whith specifed extensions.
+	 * 
+	 * @param extentions
+	 */
+	public FileFilterImpl(String[] extentions)
+	{
+		this.extensions = extentions;
+	}
+
+	/**
+	 * @see java.io.FileFilter#accept(java.io.File)
+	 */
+	public boolean accept(File file)
+	{
+		boolean isAllowed = false;
+		if (file.isDirectory())
+		{
+			return true;
+		}
+		else
+		{
+			for (int i = 0; i < extensions.length; i++)
+			{
+				if (file.getName().endsWith(extensions[i]))
+				{
+					isAllowed = true;
+				}
+			}
+		}
+		return isAllowed;
+	}
+}

Propchange: geronimo/sandbox/j2g/src/sources-tool/src/org/apache/apps/j2gmigrator/sources/FileFilterImpl.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/sandbox/j2g/src/sources-tool/src/org/apache/apps/j2gmigrator/sources/FileFilterImpl.java
------------------------------------------------------------------------------
    svn:keywords = Date Rev

Propchange: geronimo/sandbox/j2g/src/sources-tool/src/org/apache/apps/j2gmigrator/sources/FileFilterImpl.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: geronimo/sandbox/j2g/src/sources-tool/src/org/apache/apps/j2gmigrator/sources/JavaParser.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/j2g/src/sources-tool/src/org/apache/apps/j2gmigrator/sources/JavaParser.java?view=auto&rev=521731
==============================================================================
--- geronimo/sandbox/j2g/src/sources-tool/src/org/apache/apps/j2gmigrator/sources/JavaParser.java
(added)
+++ geronimo/sandbox/j2g/src/sources-tool/src/org/apache/apps/j2gmigrator/sources/JavaParser.java
Fri Mar 23 07:11:14 2007
@@ -0,0 +1,455 @@
+/**
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+
+package org.apache.apps.j2gmigrator.sources;
+
+import java.io.File;
+import java.io.FileFilter;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.apps.j2gmigrator.FatalToolException;
+import org.apache.apps.j2gmigrator.ToolConstants;
+import org.apache.apps.j2gmigrator.jasper.JspCompiler;
+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.IncrementalProjectBuilder;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.Path;
+import org.eclipse.jdt.core.IClasspathEntry;
+import org.eclipse.jdt.core.ICompilationUnit;
+import org.eclipse.jdt.core.IJavaProject;
+import org.eclipse.jdt.core.IPackageFragment;
+import org.eclipse.jdt.core.JavaCore;
+import org.eclipse.jdt.core.JavaModelException;
+import org.eclipse.jdt.core.dom.AST;
+import org.eclipse.jdt.core.dom.ASTNode;
+import org.eclipse.jdt.core.dom.ASTParser;
+import org.eclipse.jdt.launching.JavaRuntime;
+
+
+/**
+ * Parser for java sources. Compiles JSP files to template project and parses specifed sources
and
+ * compiled JSPs.
+ */
+public class JavaParser {
+	/**
+	 * List of sources dirs.
+	 */
+	private String[] sources;
+
+	/**
+	 * List of libraries.
+	 */
+	private String[] libraries;
+
+	/**
+	 * Web sources.
+	 */
+	private String[] webContent;
+
+	/**
+	 * JSP files extensions.
+	 */
+	private String[] extensions = { "jsp" };
+
+	/**
+	 * JSP files filter.
+	 */
+	private FileFilter jspFilter;
+
+	/**
+	 * Project which provides the environment (locations of sources including compiled JSP,
+	 * libraries) to work.
+	 */
+	private IProject project;
+
+	/**
+	 * Java project representation.
+	 */
+	private IJavaProject javaProject;
+
+	/**
+	 * Source folder name.
+	 */
+	private final String SRC_FOLDER_NAME = "src";
+
+	/**
+	 * Binaries folder name.
+	 */
+	private final String BIN_FOLDER_NAME = "bin";
+
+	/**
+	 * Libraies folder name.
+	 */
+	private final String LIB_FOLDER_NAME = "lib";
+
+	/**
+	 * JSP folder name.
+	 */
+	private final String JSP_FOLDER_NAME = "jsp";
+
+	/**
+	 * AST Parser for java sources.
+	 */
+	private ASTParser parser;
+
+	/**
+	 * WEB-INF directory name.
+	 */
+	private final String WEB_INF = "WEB-INF";
+
+	/**
+	 * Classes directory name.
+	 */
+	private final String CLASSES_CAT = "classes";
+
+	/**
+	 * Libraries directory name.
+	 */
+	private final String LIB_CAT = "lib";
+	
+	/**
+	 * Geronimo excluded lib.
+	 */
+	private static final String GERONIMO_EXCLUDED_LIB = "geronimo-j2ee_1.4_spec-1.1.jar"; 
+
+	/**
+	 * Java libraries file filter.
+	 */
+	private FileFilter jarFilter;
+
+	/**
+	 * For sources prepared for parsing.
+	 */
+	private List units;
+
+	/**
+	 * Creates java parser for specifed sources and libraries, compiles jsp files and save compiled
+	 * in temporary project.
+	 * 
+	 * @param sources not null list of source directories.
+	 * @param libraries not null list of jar files or directories.
+	 * @param webContent not null list of web content locations.
+	 * @param jspExt not null list of jsp files extensions.
+	 * @param created and opened project Eclipse project which will be provides sources and
+	 *            libraries.
+	 * @throws FatalToolException if any problems occurs while parser preparing.
+	 */
+
+	public JavaParser(String[] sources, String[] libraries, String[] webContent, String[] jspExt,
File geronimoLib, File geronimoRep, IProject project) throws FatalToolException {
+		units = new ArrayList();
+		String[] exts = { "jar", "zip" };
+		jarFilter = new FileFilterImpl(exts);
+		this.sources = sources;
+		ArrayList libs = new ArrayList();
+		for (int i = 0; i < webContent.length; i++) {
+			File webinfCat = new File(webContent[i] + File.separator + WEB_INF);
+			if (webinfCat.exists()) {
+				File classes = new File(webinfCat, CLASSES_CAT);
+
+				if (classes.exists()) {
+					libs.add(classes.getAbsolutePath());
+				}
+
+				File lib = new File(webinfCat, LIB_CAT);
+				if (lib.exists() && lib.isDirectory()) {
+					File[] files = lib.listFiles(jarFilter);
+					for (int j = 0; j < files.length; j++) {
+						libs.add(files[j].getAbsolutePath());
+					}
+				}
+			}
+			else {
+				throw new FatalToolException("Path '" + webContent[i] + "' does not pointed to the web
content directory. Could not find " + webinfCat.getAbsolutePath() + " directory.");
+			}
+		}
+
+		for (int i = 0; i < libraries.length; i++) {
+			libs.add(libraries[i]);
+		}
+		this.libraries = (String[]) libs.toArray(new String[libs.size()]);
+		this.webContent = webContent;
+		this.project = project;
+		this.javaProject = JavaCore.create(project);
+
+		try {
+			ArrayList classpathList = new ArrayList();
+			classpathList.addAll(linkSources());
+			classpathList.addAll(linkLibraries());
+			IClasspathEntry jreEntry = JavaCore.newContainerEntry(new Path(JavaRuntime.JRE_CONTAINER),
false);
+			classpathList.add(jreEntry);
+			ArrayList repJars = new ArrayList();
+			getJarsRecursively(repJars, geronimoRep);
+			ArrayList libJars = new ArrayList();
+			getJarsRecursively(libJars, geronimoLib);
+			classpathList.addAll(linkGeronimo(repJars, ToolConstants.GERONIMO_REP));
+			classpathList.addAll(linkGeronimo(libJars, ToolConstants.GERONIMO_LIB));
+			IClasspathEntry[] entries = (IClasspathEntry[]) classpathList.toArray(new IClasspathEntry[classpathList.size()]);
+			javaProject.setRawClasspath(entries, null);
+			project.build(IncrementalProjectBuilder.INCREMENTAL_BUILD, null);
+			project.refreshLocal(IResource.DEPTH_INFINITE, null);
+			classpathList.addAll(compileAndLinkJsp(jspExt));
+			entries = (IClasspathEntry[]) classpathList.toArray(new IClasspathEntry[classpathList.size()]);
+			javaProject.setRawClasspath(entries, null);
+			parser = ASTParser.newParser(AST.JLS3);
+		} catch (CoreException ex) {
+			throw new FatalToolException("Cannot create template project classpath file.", ex);
+		}
+	}
+
+	/**
+	 * Gets paths to the jar-files and puts it to list.
+	 */
+	public void getJarsRecursively(List list, File file) {
+		if (file.isDirectory()) {
+			File[] files = file.listFiles(jarFilter);
+			for (int i = 0; i < files.length; i++) {
+				getJarsRecursively(list, files[i]);
+			}
+		}
+		else {
+			list.add(file);
+		}
+	}
+
+	/**
+	 * Links Geronimo libraries to the temporary project.
+	 * @throws FatalToolException
+	 * 
+	 */
+	private List linkGeronimo(List jars, String baseName) throws FatalToolException {
+		ArrayList libs = new ArrayList();
+		for (int i = 0; i < jars.size(); i++) {
+			File jar = (File) jars.get(i);
+			String libName = jar.getName();
+			if (!libName.equals(GERONIMO_EXCLUDED_LIB)) {
+				String extension = libName.substring(libName.lastIndexOf("."), libName.length());
+				IFile sourceFolderEntry = project.getFile(generateName(baseName, i) + extension);
+				try {
+					sourceFolderEntry.createLink(Path.fromOSString(jar.getAbsolutePath()), IResource.NONE,
null);
+					libs.add(JavaCore.newLibraryEntry(sourceFolderEntry.getFullPath(), null, null));
+				} catch (CoreException ex) {
+					throw new FatalToolException("Error occurs while linking geronimo library: " + jar.getAbsolutePath(),
ex);
+				}
+			}
+		}
+		return libs;
+	}
+
+	/**
+	 * Compiles jsps to the jsp source folder to the project and returns List of a classpath
+	 * entries.
+	 * @see org.eclipse.jdt.core.IClasspathEntry
+	 * @param webContent paths to a directories with jsps.
+	 * @param jspExt jsp extensions.
+	 * @return List of a classpath entries.
+	 * @throws FatalToolException any errors occurs while jsp compiling.
+	 */
+	private List compileAndLinkJsp(String[] jspExt) throws FatalToolException {
+		ArrayList compiledJsp = new ArrayList();
+		if (webContent != null && webContent.length > 0) {
+			if (jspExt != null && jspExt.length > 0) {
+				extensions = jspExt;
+			}
+			jspFilter = new FileFilterImpl(extensions);
+			ClassLoader savedContextClassLoader = Thread.currentThread().getContextClassLoader();
+			Thread.currentThread().setContextClassLoader(new URLClassLoader(new URL[0]));
+			String classPath = "";
+			for (int i = 0; i < libraries.length; i++) {
+				classPath += File.pathSeparator + libraries[i];
+			}
+			IPath outputFodler = project.getFolder(BIN_FOLDER_NAME).getRawLocation();
+			classPath += File.pathSeparator + outputFodler.toOSString() + "/";
+			try {
+				for (int i = 0; i < webContent.length; i++) {
+					String jspFolderName = generateName(JSP_FOLDER_NAME, i);
+					IFolder compiledJspFolder = project.getFolder(jspFolderName);
+					compiledJspFolder.create(true, true, null);
+					compiledJspFolder.setDerived(true);
+					compiledJsp.add(JavaCore.newSourceEntry(compiledJspFolder.getFullPath()));
+					File jspSourceFolder = new File(webContent[i]);
+					JspCompiler jspCompiler = null;
+					jspCompiler = new JspCompiler(jspFilter);
+					compiledJspFolder.setDefaultCharset(jspCompiler.getEncoding(), null);
+					String jspOutputDir = compiledJspFolder.getLocation().toOSString();
+					String uriRoot = jspSourceFolder.getAbsolutePath();
+					jspCompiler.compileJsp(classPath, jspOutputDir, uriRoot);
+				}
+				project.refreshLocal(IResource.DEPTH_INFINITE, null);
+			} catch (CoreException ex) {
+				throw new FatalToolException("Cannot create jsp source folder in temporary project.",
ex);
+			} finally {
+				Thread.currentThread().setContextClassLoader(savedContextClassLoader);
+			}
+		}
+		return compiledJsp;
+	}
+
+	/**
+	 * Preapres java sources for parsing.
+	 * @throws FatalToolException if any problems occurs while preparing sources.
+	 */
+	public void prepareSources() throws FatalToolException {
+		for (int i = 0; i < sources.length; i++) {
+			File sourceFile = new File(sources[i]);
+			IFolder srcFolder = project.getFolder(generateName(SRC_FOLDER_NAME, i));
+			String basePackage = srcFolder.getFullPath().toPortableString();
+			prepareRecursively(basePackage, sourceFile);
+		}
+	}
+
+	/**
+	 * Prepares compiled jsp sources for parsing.
+	 * @throws FatalToolException if any problems occurs while preparing jsp sources.
+	 */
+	public void prepareJspSources() throws FatalToolException {
+		for (int i = 0; i < webContent.length; i++) {
+			IFolder jspFolder = project.getFolder(generateName(JSP_FOLDER_NAME, i));
+			String basePackage = jspFolder.getFullPath().toPortableString();
+			prepareRecursively(basePackage, jspFolder.getLocation().toFile());
+		}
+	}
+
+	/**
+	 * Recursively find and prepares sources provided by sourceFile variable and puts results
into
+	 * units list.
+	 * @param packagePath path to processed package relative to workspace.
+	 * @param sourceFile processed file.
+	 * @throws FatalToolException if any problems occurs while preparing sources.
+	 */
+	private void prepareRecursively(String packagePath, File sourceFile) throws FatalToolException
{
+		if (sourceFile.isDirectory()) {
+			File[] files = sourceFile.listFiles(JavaSourceFilter.FILTER);
+			for (int i = 0; i < files.length; i++) {
+				if (files[i].isDirectory()) {
+					prepareRecursively(packagePath + IPath.SEPARATOR + files[i].getName(), files[i]);
+				}
+				else {
+					try {
+						IPath path = Path.fromPortableString(packagePath);
+						IPackageFragment fragment = javaProject.findPackageFragment(path);
+						ICompilationUnit compilationUnit = fragment.getCompilationUnit(files[i].getName());
+						units.add(new ParsedSource(compilationUnit, files[i]));
+					} catch (JavaModelException ex) {
+						throw new FatalToolException("Error occurs while parsing sources.", ex);
+					}
+				}
+			}
+		}
+		else {
+			if (sourceFile.exists()) {
+				throw new FatalToolException("'" + sourceFile.getAbsolutePath() + "'; Source directories
allowed only.");
+			}
+			else {
+				throw new FatalToolException("'" + sourceFile.getAbsolutePath() + "' directory does not
exists.");
+			}
+		}
+	}
+
+	/**
+	 * Parse next in the list of sources.
+	 * @return parsed source or null if list is empty.
+	 */
+	public ParsedSource parseNext() {
+		if (!units.isEmpty()) {
+			ParsedSource unit = (ParsedSource) units.get(0);
+			units.remove(unit);
+			parser.setResolveBindings(true);
+			parser.setSource(unit.getCompilationUnit());
+			ASTNode node = parser.createAST(null);
+			unit.setNode(node);
+			return unit;
+		}
+		return null;
+	}
+
+	/**
+	 * Links sources paths to a temporary projects sources folder.
+	 * @throws FatalToolException if any problems occurs while linking source folders.
+	 */
+	private List linkSources() throws FatalToolException {
+		ArrayList srcList = new ArrayList();
+		for (int i = 0; i < sources.length; i++) {
+			File file = new File(sources[i]);
+			if (!file.exists()) {
+				throw new FatalToolException("Source directory '" + file.getAbsolutePath() + "' does
not exists.");
+			}
+			IFolder sourceFolderEntry = project.getFolder(generateName(SRC_FOLDER_NAME, i));
+			try {
+				sourceFolderEntry.createLink(Path.fromOSString(file.getAbsolutePath()), IResource.NONE,
null);
+				srcList.add(JavaCore.newSourceEntry(sourceFolderEntry.getFullPath()));
+			} catch (CoreException ex) {
+				throw new FatalToolException("Error occurs while linking source directory: " + file.getAbsolutePath(),
ex);
+			}
+		}
+		return srcList;
+	}
+
+	/**
+	 * Links libraries paths to a temporary projects libraries folder.
+	 * @throws FatalToolException if any problems occurs while linking libraries.
+	 */
+	private List linkLibraries() throws FatalToolException {
+		ArrayList libList = new ArrayList();
+		if (libraries != null) {
+			for (int i = 0; i < libraries.length; i++) {
+				File file = new File(libraries[i]);
+				try {
+					if (file.isDirectory()) {
+						if (!file.exists()) {
+							throw new FatalToolException("Directory '" + file.getAbsolutePath() + "' does not
exists.");
+						}
+						IFolder libFolderEntry = project.getFolder(generateName(LIB_FOLDER_NAME, i));
+						libFolderEntry.createLink(Path.fromOSString(file.getAbsolutePath()), IResource.NONE,
null);
+						IClasspathEntry libDir = JavaCore.newLibraryEntry(libFolderEntry.getFullPath(), null,
null);
+						libList.add(libDir);
+					}
+					else {
+						if (!file.exists()) {
+							throw new FatalToolException("File '" + file.getAbsolutePath() + "' does not exists.");
+						}
+						String libName = file.getName();
+						String extension = libName.substring(libName.lastIndexOf("."), libName.length());
+						IFile libJarEntry = project.getFile(generateName(LIB_FOLDER_NAME, i) + extension);
+						libJarEntry.createLink(Path.fromOSString(file.getAbsolutePath()), IResource.NONE, null);
+						IClasspathEntry libJar = JavaCore.newLibraryEntry(libJarEntry.getFullPath(), null,
null);
+						libList.add(libJar);
+					}
+				} catch (CoreException ex) {
+					throw new FatalToolException("Error occurs while linking library: " + file.getAbsolutePath(),
ex);
+				}
+			}
+		}
+		return libList;
+	}
+
+	/**
+	 * Generates folders names.
+	 * @param base
+	 * @param number
+	 * @return generated folder name. (base_number)
+	 */
+	private String generateName(String base, int number) {
+		return base + "_" + number;
+	}
+}
\ No newline at end of file

Propchange: geronimo/sandbox/j2g/src/sources-tool/src/org/apache/apps/j2gmigrator/sources/JavaParser.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/sandbox/j2g/src/sources-tool/src/org/apache/apps/j2gmigrator/sources/JavaParser.java
------------------------------------------------------------------------------
    svn:keywords = Date Rev

Propchange: geronimo/sandbox/j2g/src/sources-tool/src/org/apache/apps/j2gmigrator/sources/JavaParser.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: geronimo/sandbox/j2g/src/sources-tool/src/org/apache/apps/j2gmigrator/sources/JavaSourceFilter.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/j2g/src/sources-tool/src/org/apache/apps/j2gmigrator/sources/JavaSourceFilter.java?view=auto&rev=521731
==============================================================================
--- geronimo/sandbox/j2g/src/sources-tool/src/org/apache/apps/j2gmigrator/sources/JavaSourceFilter.java
(added)
+++ geronimo/sandbox/j2g/src/sources-tool/src/org/apache/apps/j2gmigrator/sources/JavaSourceFilter.java
Fri Mar 23 07:11:14 2007
@@ -0,0 +1,45 @@
+/**
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+
+package org.apache.apps.j2gmigrator.sources;
+
+import java.io.File;
+import java.io.FileFilter;
+
+/**
+ * Filter for .java files.
+ */
+public class JavaSourceFilter implements FileFilter
+{
+	/**
+	 * Java sources suffix.
+	 */
+	public static final String JAVA_SUFFIX = ".java";
+	
+	/**
+	 * Static filter.
+	 */
+	public static final JavaSourceFilter FILTER = new JavaSourceFilter();
+	
+	/**
+	 * @see java.io.FileFilter#accept(java.io.File)
+	 */
+	public boolean accept(File file)
+	{
+		return file.isDirectory() || file.getName().endsWith(JAVA_SUFFIX);
+	}
+}
\ No newline at end of file

Propchange: geronimo/sandbox/j2g/src/sources-tool/src/org/apache/apps/j2gmigrator/sources/JavaSourceFilter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/sandbox/j2g/src/sources-tool/src/org/apache/apps/j2gmigrator/sources/JavaSourceFilter.java
------------------------------------------------------------------------------
    svn:keywords = Date Rev

Propchange: geronimo/sandbox/j2g/src/sources-tool/src/org/apache/apps/j2gmigrator/sources/JavaSourceFilter.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: geronimo/sandbox/j2g/src/sources-tool/src/org/apache/apps/j2gmigrator/sources/ParsedSource.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/j2g/src/sources-tool/src/org/apache/apps/j2gmigrator/sources/ParsedSource.java?view=auto&rev=521731
==============================================================================
--- geronimo/sandbox/j2g/src/sources-tool/src/org/apache/apps/j2gmigrator/sources/ParsedSource.java
(added)
+++ geronimo/sandbox/j2g/src/sources-tool/src/org/apache/apps/j2gmigrator/sources/ParsedSource.java
Fri Mar 23 07:11:14 2007
@@ -0,0 +1,88 @@
+/**
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+
+package org.apache.apps.j2gmigrator.sources;
+
+import java.io.File;
+
+import org.eclipse.jdt.core.ICompilationUnit;
+import org.eclipse.jdt.core.dom.ASTNode;
+
+/**
+ * Class provides parsed source node and file.
+ */
+public class ParsedSource
+{
+	/**
+	 * Syntax tree. 
+	 */
+	private ASTNode node;
+	
+	/**
+	 * Source unit.
+	 */
+	private ICompilationUnit compilationUnit;
+	
+	/**
+	 * Source real file.
+	 */
+	private File file;
+	
+	/**
+	 * Creates object with specifed source unit and syntax tree.
+	 * @param node syntax tree.
+	 * @param file source file.
+	 */
+	public ParsedSource(ICompilationUnit compilationUnit, File file)
+	{
+		this.file = file;
+		this.compilationUnit = compilationUnit;
+	}
+
+	/**
+	 * @return the syntax tree.
+	 */
+	public ASTNode getNode()
+	{
+		return node;
+	}
+
+	/**
+	 * @return the compilation unit.
+	 */
+	public ICompilationUnit getCompilationUnit()
+	{
+		return compilationUnit;
+	}
+	
+	/**
+	 * @return the source file.
+	 */
+	public File getFile()
+	{
+		return file;
+	}
+	
+	/**
+	 * Sets parsed AST node for current source.
+	 * @param node
+	 */
+	public void setNode(ASTNode node)
+	{
+		this.node = node;
+	}
+}
\ No newline at end of file

Propchange: geronimo/sandbox/j2g/src/sources-tool/src/org/apache/apps/j2gmigrator/sources/ParsedSource.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/sandbox/j2g/src/sources-tool/src/org/apache/apps/j2gmigrator/sources/ParsedSource.java
------------------------------------------------------------------------------
    svn:keywords = Date Rev

Propchange: geronimo/sandbox/j2g/src/sources-tool/src/org/apache/apps/j2gmigrator/sources/ParsedSource.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: geronimo/sandbox/j2g/src/sources-tool/src/org/apache/apps/j2gmigrator/sources/SourceIdentificationTool.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/j2g/src/sources-tool/src/org/apache/apps/j2gmigrator/sources/SourceIdentificationTool.java?view=auto&rev=521731
==============================================================================
--- geronimo/sandbox/j2g/src/sources-tool/src/org/apache/apps/j2gmigrator/sources/SourceIdentificationTool.java
(added)
+++ geronimo/sandbox/j2g/src/sources-tool/src/org/apache/apps/j2gmigrator/sources/SourceIdentificationTool.java
Fri Mar 23 07:11:14 2007
@@ -0,0 +1,371 @@
+/**
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+
+package org.apache.apps.j2gmigrator.sources;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.apps.j2gmigrator.FatalToolException;
+import org.apache.apps.j2gmigrator.IJavaMigration;
+import org.apache.apps.j2gmigrator.RollbackManager;
+import org.apache.apps.j2gmigrator.Tool;
+import org.apache.apps.j2gmigrator.ToolConstants;
+import org.eclipse.core.resources.ICommand;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.IProjectDescription;
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.resources.IWorkspace;
+import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IConfigurationElement;
+import org.eclipse.core.runtime.IExtension;
+import org.eclipse.core.runtime.IExtensionPoint;
+import org.eclipse.core.runtime.ILog;
+import org.eclipse.core.runtime.Platform;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.jdt.core.ICompilationUnit;
+import org.eclipse.jdt.core.JavaCore;
+import org.eclipse.jdt.core.JavaModelException;
+import org.eclipse.jdt.core.dom.CompilationUnit;
+import org.eclipse.jface.text.BadLocationException;
+import org.eclipse.jface.text.Document;
+import org.eclipse.text.edits.TextEdit;
+
+
+/**
+ * Class coordinates a work of plugins.
+ */
+public class SourceIdentificationTool extends Tool {
+	/**
+	 * Sources parameter.
+	 */
+	private final static String SRC_PARAM = "-src";
+
+	/**
+	 * Libraries parameter.
+	 */
+	private final static String LIB_PARAM = "-lib";
+
+	/**
+	 * Web sources parameter.
+	 */
+	private final static String WEB_PARAM = "-web";
+
+	/**
+	 * Jsp extensions parameter.
+	 */
+	private final static String JSPEXT_PARAM = "-jspext";
+
+	/**
+	 * Geronimo home path parameter.
+	 */
+	private final static String GERONIMO_PARAM = "-geronimo";
+
+	/**
+	 * Allowed parameters.
+	 */
+	private final static String[] ALLOWED_PARAMS = { SRC_PARAM, LIB_PARAM, WEB_PARAM, JSPEXT_PARAM,
GERONIMO_PARAM };
+
+	/**
+	 * Parameters delimeter.
+	 */
+	private final static String DELIMETER = ";";
+
+	/**
+	 * Temporary project.
+	 */
+	private static IProject project;
+
+	/**
+	 * Plug-in id
+	 */
+	public final static String PLUGIN_ID = "com.ibm.j2g.sources";
+
+	/** Geronimo repository. */
+	private final static String REPOSITORY = "repository";
+
+	/** Geronimo common libraries. */
+	private final static String LIBRARIES = "lib";
+
+	/**
+	 * Extension point id
+	 */
+	public final static String EXTENSION_POINT_ID = PLUGIN_ID + ".migrations";
+
+	/**
+	 * Geronimo home system environment variable name.
+	 */
+	private final static String GERONIMO_HOME = "GERONIMO_HOME";
+
+	/**
+	 * Geronimo home.
+	 */
+	private String geronimoHomePath;
+
+	/**
+	 * @see org.eclipse.core.runtime.IPlatformRunnable
+	 */
+	public Object run(Object args) throws Exception {
+		try {
+			String[] arguments = (String[]) args;
+			if (arguments.length == 0) {
+				usage();
+				return new Integer(0);
+			}
+			project = createTemporaryProject();
+			Map params = parseArguments(arguments);
+			List geronimoHome = (List) params.get(GERONIMO_PARAM);
+			if (geronimoHome != null && geronimoHome.size() != 0) {
+				geronimoHomePath = (String) geronimoHome.get(0);
+			}
+			else {
+				geronimoHomePath = System.getProperty(GERONIMO_HOME);
+				if (geronimoHomePath == null) {
+					usage();
+					throw new FatalToolException("Missing mandatory parameter '-geronimo'");
+				}
+			}
+			File geronimo = new File(geronimoHomePath);
+			File geronimoRep = new File(geronimo, REPOSITORY);
+			File geronimoLib = new File(geronimo, LIBRARIES);
+			if (!geronimoRep.exists() || !geronimoLib.exists()) {
+				throw new FatalToolException("Invalid Geronimo installation. Cannot find libaries and
repository directories.");
+			}
+
+			List src = (List) params.get(SRC_PARAM);
+			List lib = (List) params.get(LIB_PARAM);
+			List web = (List) params.get(WEB_PARAM);
+			List jspext = (List) params.get(JSPEXT_PARAM);
+			if (src != null) {
+				RollbackManager rollbackManager = new RollbackManager(JavaSourceFilter.FILTER);
+				Iterator sources = src.iterator();
+				while (sources.hasNext()) {
+					File file = new File((String) sources.next());
+					rollbackManager.restore(file);
+				}
+			}
+			JavaParser parser = null;
+			parser = new JavaParser(getStringArray(src), getStringArray(lib), getStringArray(web),
getStringArray(jspext), geronimoLib, geronimoRep, project);
+			parser.prepareSources();
+
+			IJavaMigration[] plugs = loadPlugins();
+
+			ParsedSource unit;
+			while ((unit = parser.parseNext()) != null) {
+				migrate(plugs, unit, true);
+			}
+
+			parser.prepareJspSources();
+			while ((unit = parser.parseNext()) != null) {
+				migrate(plugs, unit, false);
+			}
+
+		} catch (FatalToolException ex) {
+			getOutput().fatal(ex.getMessage());
+			return new Integer(1);
+		} finally {
+			IWorkspace workspace = ResourcesPlugin.getWorkspace();
+			IProject project = workspace.getRoot().getProject(ToolConstants.TEMPORARY_PROJECT_NAME);
+			project.refreshLocal(IResource.DEPTH_INFINITE, null);
+			project.delete(true, null);
+		}
+		return new Integer(0);
+	}
+
+	/**
+	 * Print tool usage text.
+	 */
+	protected void usage() {
+		getOutput().fatal("Usage: jsrc2g.cmd {-src dir_name[;dir_nameN]}{-lib dir_or_jar_name[;dir_or_jar_nameN]}{-web
dir_name[;dir_nameN]} -geronimo geronimo_home");
+	}
+
+	/**
+	 * Converts List with Strings to String[] array.
+	 * @param list list of strings.
+	 * @return
+	 */
+	private String[] getStringArray(List list) {
+		if (list != null) {
+			return (String[]) list.toArray(new String[list.size()]);
+		}
+		else {
+			return new String[0];
+		}
+	}
+
+	/**
+	 * Parses arguments which was passed to the plugin.
+	 * @param args arguments array.
+	 * @return map of parameters where key - parameter names and values - lists of parameters.
+	 * @throws FatalToolException if wrong parameter was found.
+	 */
+	private Map parseArguments(String[] args) throws FatalToolException {
+		HashMap result = new HashMap();
+		int pointer = 0;
+		while (pointer < args.length) {
+			String key = args[pointer];
+			if (isCorrectParameter(key)) {
+				pointer++;
+				if (args.length > pointer) {
+					result.put(key, parseComplexPath(args[pointer]));
+					pointer++;
+				}
+				else {
+					throw new FatalToolException("Missing parameter for key:" + key);
+				}
+			}
+			else {
+				throw new FatalToolException("Wrong parameter:" + args[pointer]);
+			}
+		}
+		return result;
+	}
+
+	/**
+	 * Checks correctness of parameters.
+	 * @param arg paramter.
+	 * @return true if parameter allowed.
+	 */
+	private boolean isCorrectParameter(String arg) {
+		for (int i = 0; i < ALLOWED_PARAMS.length; i++) {
+			if (arg.equals(ALLOWED_PARAMS[i])) {
+				return true;
+			}
+		}
+		return false;
+	}
+
+	/**
+	 * Parses strings formated follows: some_string_1[;some_string_N]
+	 * @param path string like this "some_string_1[;some_string_N]"
+	 * @return list of parsed strings.
+	 */
+	private List parseComplexPath(String path) {
+		ArrayList result = new ArrayList();
+		int index = path.indexOf(DELIMETER);
+		while (index > 0) {
+			result.add(path.substring(0, index));
+			path = path.substring(index + 1);
+			index = path.indexOf(DELIMETER);
+		}
+		result.add(path);
+		return result;
+	}
+
+	/**
+	 * Creates opened temporary Eclipse project in current workspace.
+	 * @return opened temporary Eclipse project.
+	 * @throws FatalToolException
+	 */
+	private IProject createTemporaryProject() throws FatalToolException {
+		IProject project;
+		IWorkspace workspace = ResourcesPlugin.getWorkspace();
+		try {
+			project = workspace.getRoot().getProject(ToolConstants.TEMPORARY_PROJECT_NAME);
+			IProjectDescription desc = workspace.newProjectDescription(project.getName());
+			desc.setNatureIds(new String[] { JavaCore.NATURE_ID });
+			ICommand command = desc.newCommand();
+			command.setBuilderName(JavaCore.BUILDER_ID);
+			desc.setBuildSpec(new ICommand[] { command });
+			project.create(desc, null);
+			project.open(null);
+		} catch (CoreException ex) {
+			throw new FatalToolException("Cannot create or open temporary project. Please try to clean
workspace '" + workspace.getRoot().getLocation().toOSString() + "' manually.", ex);
+		}
+		return project;
+	}
+
+	/**
+	 * Loads java source migration plug-ins from the extension registry.
+	 * @return array of IJavaMigration instaces.
+	 */
+	private IJavaMigration[] loadPlugins() {
+		List plugins = new ArrayList();
+		IExtensionPoint point = Platform.getExtensionRegistry().getExtensionPoint(EXTENSION_POINT_ID);
+		IExtension[] exts = point.getExtensions();
+		for (int i = 0; i < exts.length; ++i) {
+			IConfigurationElement[] els = exts[i].getConfigurationElements();
+			for (int j = 0; j < els.length; ++j) {
+				try {
+					plugins.add(els[j].createExecutableExtension(ToolConstants.CLASS_PROPERTY));
+				} catch (CoreException ex) {
+					ILog log = Platform.getLog(Platform.getBundle(PLUGIN_ID));
+					log.log(new Status(Status.WARNING, PLUGIN_ID, Status.OK, "Failed to instantiate java
source migration plug-in.", ex));
+				}
+			}
+		}
+		return (IJavaMigration[]) plugins.toArray(new IJavaMigration[plugins.size()]);
+	}
+
+	/**
+	 * Migrates given source and execute saving if it's necessary.
+	 * 
+	 * @param plugs available migration plug-ins.
+	 * @param source source to migration.
+	 * @param saveMigrated if true then migrated source will be saved.
+	 * @throws FatalToolException
+	 */
+	private void migrate(IJavaMigration[] plugs, ParsedSource source, boolean saveMigrated)
throws FatalToolException {
+		boolean isModified = false;
+		CompilationUnit unit = (CompilationUnit) source.getNode();
+		unit.recordModifications();
+		for (int i = 0; i < plugs.length; ++i) {
+			isModified = plugs[i].migrate(source.getNode()) || isModified;
+		}
+		if (isModified && saveMigrated) {
+			saveSource(source);
+		}
+	}
+
+	/**
+	 * Creates buckup copy and saves modified source.
+	 * @param source
+	 * @throws FatalToolException
+	 * @throws IOException
+	 */
+	private void saveSource(ParsedSource source) throws FatalToolException {
+		ICompilationUnit sourceUnit = source.getCompilationUnit();
+		CompilationUnit parsedUnit = (CompilationUnit) source.getNode();
+		try {
+			String contents = sourceUnit.getBuffer().getContents();
+			File file = new File(source.getFile().getAbsolutePath() + RollbackManager.BACKUP_SUFFIX);
+			file.createNewFile();
+			FileOutputStream out = new FileOutputStream(file);
+			out.write(contents.getBytes());
+			out.close();
+			Document document = new Document(contents);
+			TextEdit edits = parsedUnit.rewrite(document, sourceUnit.getJavaProject().getOptions(true));
+			edits.apply(document);
+			String newSource = document.get();
+			sourceUnit.getBuffer().setContents(newSource);
+			sourceUnit.save(null, true);
+		} catch (JavaModelException ex) {
+			getOutput().error("Java element does not exists or it is read-only: " + sourceUnit.getElementName());
+		} catch (IOException ex) {
+			getOutput().error("I/O error occurs while creating backup copy of source file: " + source.getFile().getAbsolutePath());
+		} catch (BadLocationException ex) {
+			getOutput().error("Cannot edit source file: " + source.getFile().getAbsolutePath());
+		}
+	}
+}
\ No newline at end of file

Propchange: geronimo/sandbox/j2g/src/sources-tool/src/org/apache/apps/j2gmigrator/sources/SourceIdentificationTool.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/sandbox/j2g/src/sources-tool/src/org/apache/apps/j2gmigrator/sources/SourceIdentificationTool.java
------------------------------------------------------------------------------
    svn:keywords = Date Rev

Propchange: geronimo/sandbox/j2g/src/sources-tool/src/org/apache/apps/j2gmigrator/sources/SourceIdentificationTool.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message