[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[stp-commits] r3344 - in org.eclipse.stp.sca/trunk: org.eclipse.stp.sca.common/src/org/eclipse/stp/sca/common/utils org.eclipse.stp.sca.common.java/src/org/eclipse/stp/sca/common/java/utils

Author: vzurczak
Date: 2009-10-13 04:55:58 -0400 (Tue, 13 Oct 2009)
New Revision: 3344

Added:
   org.eclipse.stp.sca/trunk/org.eclipse.stp.sca.common/src/org/eclipse/stp/sca/common/utils/ScaModelUtils.java
Modified:
   org.eclipse.stp.sca/trunk/org.eclipse.stp.sca.common.java/src/org/eclipse/stp/sca/common/java/utils/JDTUtils.java
   org.eclipse.stp.sca/trunk/org.eclipse.stp.sca.common/src/org/eclipse/stp/sca/common/utils/ResourceUtils.java
Log:
Added utility methods.

Modified: org.eclipse.stp.sca/trunk/org.eclipse.stp.sca.common/src/org/eclipse/stp/sca/common/utils/ResourceUtils.java
===================================================================
--- org.eclipse.stp.sca/trunk/org.eclipse.stp.sca.common/src/org/eclipse/stp/sca/common/utils/ResourceUtils.java	2009-09-21 11:38:01 UTC (rev 3343)
+++ org.eclipse.stp.sca/trunk/org.eclipse.stp.sca.common/src/org/eclipse/stp/sca/common/utils/ResourceUtils.java	2009-10-13 08:55:58 UTC (rev 3344)
@@ -61,19 +61,19 @@
 			IResource[] resources = container.members();
 			for (IResource resource : resources) {
 				switch (resource.getType()) {
-				case IResource.FILE:
-					String fileExtension = resource.getFileExtension()
-							.toLowerCase();
-					if (fileExtension.equals(extension)
-							|| fileExtension.equals("*")) //$NON-NLS-1$
-						result.add((IFile) resource);
-					break;
-				case IResource.FOLDER:
-					IFolder subFolder = (IFolder) resource;
-					result.addAll(getFiles(subFolder, extension));
-					break;
-				default:
-					break;
+					case IResource.FILE:
+						String fileExtension = resource.getFileExtension()
+						.toLowerCase();
+						if (fileExtension.equals(extension)
+								|| fileExtension.equals("*")) //$NON-NLS-1$
+							result.add((IFile) resource);
+						break;
+					case IResource.FOLDER:
+						IFolder subFolder = (IFolder) resource;
+						result.addAll(getFiles(subFolder, extension));
+						break;
+					default:
+						break;
 				}
 			}
 		} catch (CoreException e) {
@@ -98,12 +98,12 @@
 			IResource[] resources = container.members();
 			for (IResource resource : resources) {
 				switch (resource.getType()) {
-				case IResource.FOLDER:
-					IFolder subFolder = (IFolder) resource;
-					result.add(subFolder);
-					result.addAll(getFolders(subFolder));
-				default:
-					break;
+					case IResource.FOLDER:
+						IFolder subFolder = (IFolder) resource;
+						result.add(subFolder);
+						result.addAll(getFolders(subFolder));
+					default:
+						break;
 				}
 			}
 		} catch (CoreException e) {
@@ -122,8 +122,8 @@
 	public static IFile getIFileFromEditor() {
 		try {
 			IEditorPart editorPart = PlatformUI.getWorkbench()
-					.getActiveWorkbenchWindow().getActivePage()
-					.getActiveEditor();
+			.getActiveWorkbenchWindow().getActivePage()
+			.getActiveEditor();
 			return ((FileEditorInput) editorPart.getEditorInput()).getFile();
 		} catch (Exception e) {
 			IStatus status = new Status(
@@ -190,6 +190,10 @@
 	}
 
 	/**
+	 * @param container
+	 * @param extensions
+	 * @param resourcesToSkip
+	 * @return
 	 * @see #getDirectValidChildren(IContainer, List)
 	 */
 	public static IResource[] getDirectValidChildren(IContainer container,
@@ -226,7 +230,7 @@
 		File bundleFile;
 		try {
 			bundleFile = FileLocator
-					.getBundleFile(Platform.getBundle(pluginId));
+			.getBundleFile(Platform.getBundle(pluginId));
 			if (bundleFile.isFile())
 				return bundleFile;
 			else if (bundleFile.isDirectory()) {
@@ -260,7 +264,7 @@
 	public static IFile getIFile(File file) {
 		Path path = new Path(file.getAbsolutePath());
 		return ResourcesPlugin.getWorkspace().getRoot()
-				.getFileForLocation(path);
+		.getFileForLocation(path);
 	}
 
 	/**
@@ -284,7 +288,7 @@
 		IProject project = currentCompositeFile.getProject();
 		List<IFile> compo = ResourceUtils.getFiles(project, "composite");
 		Iterator<IFile> composites = ScaResourceFiltersEPManager.getInstance()
-				.filterAllowedFiles(compo).iterator();
+		.filterAllowedFiles(compo).iterator();
 		while (result == null && composites.hasNext()) {
 			IFile compositeFile = composites.next();
 			if (compositeName.equals(compositeFile.getName())) {
@@ -300,10 +304,10 @@
 				int i = 0;
 				while (result == null && i < projects.length) {
 					List<IFile> compo2 = ResourceUtils.getFiles(projects[i],
-							"composite");
+					"composite");
 					Iterator<IFile> compositesIt = ScaResourceFiltersEPManager
-							.getInstance().filterAllowedFiles(compo2)
-							.iterator();
+					.getInstance().filterAllowedFiles(compo2)
+					.iterator();
 					while (result == null && compositesIt.hasNext()) {
 						IFile compositeFile = compositesIt.next();
 						if (compositeName.equals(compositeFile.getName())) {
@@ -318,4 +322,18 @@
 		return result;
 	}
 
+
+	/**
+	 * Returns an IResources corresponding to the File object.
+	 * @param file the file
+	 * @return an instance of IFile, or IContainer, or null if the file is not in the workspace
+	 */
+	public static IResource getResource( File file ) {
+
+		Path path = new Path( file.getAbsolutePath());
+		if( file.isFile())
+			return ResourcesPlugin.getWorkspace().getRoot().getFileForLocation( path );
+
+		return ResourcesPlugin.getWorkspace().getRoot().getContainerForLocation( path );
+	}
 }

Added: org.eclipse.stp.sca/trunk/org.eclipse.stp.sca.common/src/org/eclipse/stp/sca/common/utils/ScaModelUtils.java
===================================================================
--- org.eclipse.stp.sca/trunk/org.eclipse.stp.sca.common/src/org/eclipse/stp/sca/common/utils/ScaModelUtils.java	                        (rev 0)
+++ org.eclipse.stp.sca/trunk/org.eclipse.stp.sca.common/src/org/eclipse/stp/sca/common/utils/ScaModelUtils.java	2009-10-13 08:55:58 UTC (rev 3344)
@@ -0,0 +1,136 @@
+/******************************************************************************
+ * Copyright (c) 2009, EBM WebSourcing
+ * 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:
+ *     EBM WebSourcing - initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.stp.sca.common.utils;
+
+import java.io.File;
+import java.util.HashMap;
+
+import org.eclipse.emf.common.command.BasicCommandStack;
+import org.eclipse.emf.common.util.BasicDiagnostic;
+import org.eclipse.emf.common.util.Diagnostic;
+import org.eclipse.emf.common.util.URI;
+import org.eclipse.emf.ecore.resource.Resource;
+import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
+import org.eclipse.emf.edit.provider.ComposedAdapterFactory;
+import org.eclipse.emf.edit.provider.ReflectiveItemProviderAdapterFactory;
+import org.eclipse.emf.edit.provider.resource.ResourceItemProviderAdapterFactory;
+import org.eclipse.stp.sca.Composite;
+import org.eclipse.stp.sca.DocumentRoot;
+import org.eclipse.stp.sca.common.internal.Messages;
+import org.eclipse.stp.sca.common.internal.builder.ScaCustomDiagnostician;
+import org.eclipse.stp.sca.provider.ScaItemProviderAdapterFactory;
+
+/**
+ * A set of utility method related to the SCA meta-model.
+ * @author Vincent Zurczak - EBM WebSourcing
+ */
+public class ScaModelUtils {
+
+	private final AdapterFactoryEditingDomain editingDomain;
+
+
+	/**
+	 * Constructor.
+	 */
+	public ScaModelUtils() {
+
+		ComposedAdapterFactory adapterFactory =
+			new ComposedAdapterFactory( ComposedAdapterFactory.Descriptor.Registry.INSTANCE );
+
+		adapterFactory.addAdapterFactory( new ResourceItemProviderAdapterFactory());
+		adapterFactory.addAdapterFactory( new ScaItemProviderAdapterFactory());
+		adapterFactory.addAdapterFactory( new ReflectiveItemProviderAdapterFactory());
+
+		BasicCommandStack commandStack = new BasicCommandStack();
+		this.editingDomain = new AdapterFactoryEditingDomain(
+				adapterFactory, commandStack, new HashMap<Resource, Boolean>());
+	}
+
+
+	/**
+	 * Loads a composite file through the SCA meta-model.
+	 * <p>
+	 * This method assumes the different model extensions were already registered
+	 * in the registry instance. In Eclipse, this is done at startup.
+	 * In stand-alone, it should be explicitly done before calling this method.
+	 * </p>
+	 * 
+	 * @param file the file to load
+	 * @return the associated composite model
+	 * @throws InvalidScaModelException if the model could not be loaded
+	 */
+	public Composite getCompositeFile( File file ) throws InvalidScaModelException {
+
+		try {
+			URI uri = URI.createFileURI( file.getAbsolutePath());
+			Resource resource = this.editingDomain.getResourceSet().getResource( uri, true );
+			DocumentRoot root = (DocumentRoot) resource.getContents().get( 0 );
+			return root.getComposite();
+
+		} catch( Exception e ) {
+			throw new InvalidScaModelException( e );
+		}
+	}
+
+
+	/**
+	 * Returns the editing domain used in {@link #getCompositeFile(File)}.
+	 * @return the editingDomain
+	 */
+	public AdapterFactoryEditingDomain getEditingDomain() {
+		return this.editingDomain;
+	}
+
+
+	/**
+	 * An exception thrown if the parsing of an SCA model file fails.
+	 * @author Vincent Zurczak - EBM WebSourcing
+	 */
+	public static class InvalidScaModelException extends Exception {
+
+		/** The serail ID. */
+		private static final long serialVersionUID = -5305543094858796368L;
+
+
+		/**
+		 * Constructor.
+		 * @param sourceException the exception that triggered the creation of this exception
+		 */
+		public InvalidScaModelException( Exception sourceException ) {
+			setStackTrace( sourceException.getStackTrace());
+		}
+	}
+
+
+	/**
+	 * Validates a composite.
+	 * @param composite the composite to validate
+	 * @return a diagnostic
+	 */
+	public static Diagnostic validate( Composite composite ) {
+
+		try {
+			return new ScaCustomDiagnostician().validate( composite.eContainer() );
+
+		} catch( Exception e ) {
+
+			// Exception if a promote only defines the component
+			return new BasicDiagnostic (
+					Diagnostic.ERROR,
+					e.getMessage(),
+					0,
+					Messages.ScaIncrementalBuilder_3,
+					new Object[ 0 ]
+			);
+		}
+	}
+}

Modified: org.eclipse.stp.sca/trunk/org.eclipse.stp.sca.common.java/src/org/eclipse/stp/sca/common/java/utils/JDTUtils.java
===================================================================
--- org.eclipse.stp.sca/trunk/org.eclipse.stp.sca.common.java/src/org/eclipse/stp/sca/common/java/utils/JDTUtils.java	2009-09-21 11:38:01 UTC (rev 3343)
+++ org.eclipse.stp.sca/trunk/org.eclipse.stp.sca.common.java/src/org/eclipse/stp/sca/common/java/utils/JDTUtils.java	2009-10-13 08:55:58 UTC (rev 3344)
@@ -86,12 +86,13 @@
 		return classes;
 	}
 
+
 	/**
+	 * @param _resources
 	 * @param resources
 	 * @return
 	 */
-	public static IResource[] removeResourceFromBinaryFolders(
-			IResource[] _resources) {
+	public static IResource[] removeResourceFromBinaryFolders( IResource[] _resources ) {
 
 		Map<IProject, IJavaProject> projects = new HashMap<IProject, IJavaProject>();
 		List<IResource> toKeep = new ArrayList<IResource>();
@@ -107,8 +108,7 @@
 				continue;
 			}
 
-			// Get Java project and make sure the resource is not in an output
-			// folder
+			// Get Java project and make sure the resource is not in an output folder
 			try {
 				if (p.hasNature(JavaCore.NATURE_ID)) {
 					IJavaProject jp = projects.get(p);
@@ -131,6 +131,7 @@
 		return toKeep.toArray(result);
 	}
 
+
 	/**
 	 * Get the referenced projects from a Java project.
 	 * <p>
@@ -159,7 +160,7 @@
 
 		for (String projectName : projectNames) {
 			IProject project = ResourcesPlugin.getWorkspace().getRoot()
-					.getProject(projectName);
+			.getProject(projectName);
 			try {
 				if (!project.exists() || !project.isOpen()
 						|| !project.hasNature(JavaCore.NATURE_ID))
@@ -177,6 +178,7 @@
 		return result;
 	}
 
+
 	/**
 	 * Get the referenced projects from a Java project.
 	 * <p>
@@ -206,14 +208,14 @@
 		return result;
 	}
 
+
 	/**
 	 * Get the class path from Java project.
 	 * 
 	 * @param javaProject
 	 * @return the class path as a list of string locations.
 	 */
-	public static List<String> getClasspath(IJavaProject javaProject,
-			boolean getReferencedProjectClasspath) {
+	public static List<String> getClasspath(IJavaProject javaProject, boolean getReferencedProjectClasspath) {
 
 		List<String> paths = new ArrayList<String>();
 		try {
@@ -223,31 +225,31 @@
 				for (IClasspathEntry entry : entries) {
 					switch (entry.getEntryKind()) {
 
-					case IClasspathEntry.CPE_PROJECT:
-						if (!getReferencedProjectClasspath)
-							break;
+						case IClasspathEntry.CPE_PROJECT:
+							if (!getReferencedProjectClasspath)
+								break;
 
-						String projectName = entry.getPath().toString();
-						IProject project = ResourcesPlugin.getWorkspace()
-								.getRoot().getProject(projectName);
-						IJavaProject jProject = JavaCore.create(project);
+							String projectName = entry.getPath().toString();
+							IProject project = ResourcesPlugin.getWorkspace()
+							.getRoot().getProject(projectName);
+							IJavaProject jProject = JavaCore.create(project);
 
-						List<String> subPaths = getClasspath(jProject, true);
-						paths.addAll(subPaths);
-						break;
+							List<String> subPaths = getClasspath(jProject, true);
+							paths.addAll(subPaths);
+							break;
 
-					case IClasspathEntry.CPE_LIBRARY:
-						String path = entry.getPath().toString();
-						paths.add(path);
-						break;
+						case IClasspathEntry.CPE_LIBRARY:
+							String path = entry.getPath().toString();
+							paths.add(path);
+							break;
 
-					case IClasspathEntry.CPE_VARIABLE:
-						entry = JavaCore.getResolvedClasspathEntry(entry);
-						if (entry != null) {
-							path = entry.getPath().toString();
-							paths.add(path);
-						}
-						break;
+						case IClasspathEntry.CPE_VARIABLE:
+							entry = JavaCore.getResolvedClasspathEntry(entry);
+							if (entry != null) {
+								path = entry.getPath().toString();
+								paths.add(path);
+							}
+							break;
 					}
 				}
 			}
@@ -259,6 +261,7 @@
 		return paths;
 	}
 
+
 	/**
 	 * @param project
 	 *            an IProject instance
@@ -277,6 +280,7 @@
 		return null;
 	}
 
+
 	/**
 	 * @param qualifiedName
 	 *            the Java qualified name
@@ -287,11 +291,12 @@
 		int index = qualifiedName.lastIndexOf('.');
 		if (index < 0)
 			return new String[] { "", qualifiedName }; //$NON-NLS-1$
-		else
-			return new String[] { qualifiedName.substring(0, index),
-					qualifiedName.substring(index + 1) };
+
+		return new String[] { qualifiedName.substring(0, index),
+				qualifiedName.substring(index + 1) };
 	}
 
+
 	/**
 	 * @param name
 	 * @param eo
@@ -314,6 +319,7 @@
 		return null;
 	}
 
+
 	/**
 	 * Checks whether the given class implements all the super interfaces.
 	 * <p>
@@ -334,10 +340,11 @@
 			String className, List<String> superClassNames) {
 
 		String[] scn = superClassNames.toArray(new String[superClassNames
-				.size()]);
+		                                                  .size()]);
 		return implementsOrExtendsAll(javaProject, className, scn);
 	}
 
+
 	/**
 	 * Checks whether the given class implements all the super interfaces.
 	 * <p>
@@ -390,6 +397,7 @@
 		return notImplemented;
 	}
 
+
 	/**
 	 * Computes the class name from the given parameter.
 	 * <p>
@@ -443,6 +451,13 @@
 		return null;
 	}
 
+
+	/**
+	 * 
+	 * @param transfer
+	 * @param tmpFile
+	 * @return
+	 */
 	public static IResource copyFileEnclosedInJar(JarEntryFile transfer,
 			java.io.File tmpFile) {
 		IResource result = null;
@@ -460,6 +475,13 @@
 		return result;
 	}
 
+
+	/**
+	 * 
+	 * @param in
+	 * @param tmpFile
+	 * @return
+	 */
 	private static boolean copyFile(InputStream in, java.io.File tmpFile) {
 		boolean result = false;
 		FileOutputStream out = null;
@@ -493,4 +515,25 @@
 		return result;
 	}
 
+
+	/**
+	 * Gets the source folders of a IJavaProject.
+	 * @param javaProject
+	 * @return the list of source folders in this Java project
+	 */
+	public static List<IClasspathEntry> getSourceFolders( IJavaProject javaProject ) {
+
+		List<IClasspathEntry> result = new ArrayList<IClasspathEntry>();
+		try {
+			for( IClasspathEntry entry : javaProject.getRawClasspath()) {
+				if( entry.getEntryKind() == IClasspathEntry.CPE_SOURCE )
+					result.add( entry );
+			}
+
+		} catch( JavaModelException e ) {
+			e.printStackTrace();
+		}
+
+		return result;
+	}
 }