⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 resourcedeltabuilder.java

📁 eclipseme的最新版本的source,欢迎j2me程序员使用
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
			// Figure out the source resource.  If this is outside
			// the workspace, set it to the project so we have
			// something to attach an error marker to.
			Object resolvedEntry = Utils.getResolvedClasspathEntry(entry);
			IResource srcResource = (resolvedEntry instanceof IResource) ?
					(IResource) resolvedEntry :
					buildInfo.getCurrentJavaProject().getProject();
					
			// Convert to a java.io.File
			IPath srcPath = entry.getPath().makeAbsolute();
			File srcFile = new File(Utils.getResolvedClasspathEntryFile(entry));
			
			// Check to see if there has been an attempt to preverify the file
			if ((srcFile != null) && !buildInfo.hasLibraryBeenPreverified(srcFile)) {
				// Log that we tried to preverify this file
				buildInfo.addPreverifiedLibrary(srcFile);

				// Look up the appropriate target folder and resource
				IResource target = null;
				IFolder targetFolder = null;
				if (srcFile.isDirectory() && !srcFile.isArchive()) {
					target = verifiedClassesFolder;
					targetFolder = verifiedClassesFolder;
				} else {
					target = verifiedLibsFolder.getFile(srcPath.lastSegment());
					targetFolder = verifiedLibsFolder;
				}

				File tgtFile = new File(target.getLocation().toFile());

				// Determine whether or not the library needs preverification
				boolean requiresPreverification = false;
				if (srcFile.isDirectory()) {
					requiresPreverification = 
						doClassesRequirePreverification(srcFile, tgtFile);
				} else {
					requiresPreverification = isSourceNewerThanTarget(srcFile, tgtFile);
				}
				
				if (requiresPreverification) {
					attemptLibraryPreverification(srcResource, srcFile, targetFolder, monitor);
				}
			}
		}
	}
	
	/**
	 * Return a boolean indicating whether the specified resource is being
	 * held within a library path.
	 * 
	 * @param resource
	 * @return
	 */
	private boolean isLibraryResource(IPath[] libraryPaths, IResource resource) {
		boolean isLibraryClass = false;
		
		for (int i = 0; i < libraryPaths.length; i++) {
			IPath libraryPath = libraryPaths[i];
			if (libraryPath.isPrefixOf(resource.getFullPath())) {
				isLibraryClass = true;
				break;
			}
		}
		return isLibraryClass;
	}

	/**
	 * Return a boolean indicating whether the source file is newer than the 
	 * target file.
	 * 
	 * @param srcFile
	 * @param tgtFile
	 * @return
	 */
	private boolean isSourceNewerThanTarget(File srcFile, File tgtFile) {
		return 
			!tgtFile.exists() || 
			(tgtFile.lastModified() < srcFile.lastModified());
	}

	/**
	 * Add the specified file contents to the deployed jar file with
	 * the relative path.
	 * 
	 * @param relativePath
	 * @param contents
	 * @throws CoreException 
	 */
	private void addFileToRuntimeJar(IPath relativePath, IFile contents, IProgressMonitor monitor) 
		throws CoreException 
	{
		File deployedFile = new File(buildInfo.getRuntimeJarFile(monitor), relativePath.toString());
		deployedFile.copyFrom(contents.getLocation().toFile());
	}
	
	/**
	 * Attempt to preverify the specified library.
	 * 
	 * @param srcResource
	 * @param srcFile
	 * @param verifiedLibsFolder
	 * @param monitor
	 * @throws CoreException
	 */
	private void attemptLibraryPreverification(
		IResource srcResource, 
		File srcFile, 
		IFolder verifiedLibsFolder, 
		IProgressMonitor monitor) 
			throws CoreException 
	{
		buildInfo.setPackageDirty(true);
		try {
			// Run the preverifier
			PreverificationError[] errors =
				buildInfo.getMidletSuite().preverifyJarFile(srcFile, verifiedLibsFolder, monitor);
			
			// Handle errors that may have occurred.
			// Should this actually be bubbled up as an exception?
			if (errors.length > 0) {
				createJarErrorMarker(srcResource, srcFile, errors);
			} else {
				File tgtFile = new File(verifiedLibsFolder.getLocation().toString());
				
				// Deploy the preverified library contents into the deployed jar
				if (srcFile.isFile() || srcFile.isArchive()) {
					// An archive file
					tgtFile = new File(tgtFile, srcFile.getName());
				}
				
				// An actual directory of classes
				FileFilter filter = new FileFilter() {
					public boolean accept(java.io.File pathname) {
						String path = pathname.getPath();
						return !path.toUpperCase().endsWith(MANIFEST_FILE_NAME);
					}
				};
				copyAll(filter, tgtFile, buildInfo.getRuntimeJarFile(monitor));
				File.umount(tgtFile);
			}
		} catch (IOException e) {
			EclipseMECorePlugin.throwCoreException(IStatus.ERROR, -999, e);
		}
	}

	/**
	 * Copy all of the files from the source to the target recursively.  Use
	 * the provided filter to determine whether the source file should be
	 * copied.
	 * 
	 * @param filter
	 * @param srcFile
	 * @param tgtFile
	 * @throws IOException
	 */
	private void copyAll(FileFilter filter, File srcFile, File tgtFile)
		throws IOException
	{
		if (srcFile.isFile() && !srcFile.isArchive()) {
			tgtFile.copyFrom(srcFile);
		} else {
			java.io.File[] listFiles = srcFile.listFiles(filter);
			for (int i = 0; i < listFiles.length; i++) {
				copyAll(filter, new File(listFiles[i]), new File(tgtFile, listFiles[i].getName()));
			}
		}
	}
	
	/**
	 * Log any errors that occurred during preverification.
	 * 
	 * @param srcResource
	 * @param srcFile
	 * @param errors
	 * @throws CoreException
	 */
	private void createJarErrorMarker(
		IResource srcResource, 
		File srcFile, 
		PreverificationError[] errors) 
			throws CoreException 
	{
		StringBuffer sb = new StringBuffer("Preverification errors:\n");
		
		for (int i = 0; i < errors.length; i++) {
			sb.append(PreverificationUtils.getErrorText(errors[i])).append("\n");
		}
		
		IMarker marker = srcResource.createMarker(IJavaModelMarker.JAVA_MODEL_PROBLEM_MARKER);
		marker.setAttribute(IMarker.MESSAGE, sb.toString());
		marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_ERROR);
	}

	/**
	 * Clear the specified container of all resources recursively.
	 * 
	 * @param buildInfo
	 * @param container
	 * @param monitor
	 * @throws CoreException
	 */
	private void clearContainer(BuildInfo buildInfo, IContainer container, IProgressMonitor monitor) 
		throws CoreException 
	{
		Utils.clearContainer(container, monitor);
		buildInfo.setPackageDirty(true);
	}

	/**
	 * Add the class file resources inside this resource and all
	 * subfolders.
	 * 
	 * @param classes
	 * @param resources
	 * @param resource
	 * @param libraryFolderPaths
	 */
	private void collectClassesAndResources(
		List classes, 
		List resources, 
		IResource resource, 
		IPath[] libraryFolderPaths) 
			throws CoreException 
	{
		if (buildLoggingConfig.isPreverifierTraceEnabled()) {
			BuildConsoleProxy.instance.traceln("> ResourceDeltaBuilder.collectClassesAndResources; resource = " + resource); 
		}

		if (resource.getType() == IResource.FILE) {
			if (!isLibraryResource(libraryFolderPaths, resource)) {
				IFile file = (IFile) resource;
				if ("class".equals(file.getFileExtension())) {
					classes.add(resource);
				} else {
					resources.add(resource);
				}
			}
		} else {
			IContainer container = (IContainer) resource;
			IResource[] members = container.members();
			for (int i = 0; i < members.length; i++) {
				collectClassesAndResources(classes, resources, members[i], libraryFolderPaths);
			}
		}
		
		if (buildLoggingConfig.isPreverifierTraceEnabled()) {
			BuildConsoleProxy.instance.traceln("< ResourceDeltaBuilder.collectClassesAndResources"); 
		}
	}
	
	/**
	 * Collect and return the paths of the libraries that are folders
	 * rather than archives.
	 * 
	 * @param monitor
	 * @throws CoreException
	 */
	private IPath[] collectLibraryFolderPaths(IProgressMonitor monitor) throws CoreException {
		if (buildLoggingConfig.isPreverifierTraceEnabled()) {
			BuildConsoleProxy.instance.traceln("> ResourceDeltaBuilder.collectLibraryFolderPaths"); 
		}

		LibraryCollectionVisitor visitor = new LibraryCollectionVisitor();
		visitor.getRunner().run(buildInfo.getCurrentJavaProject(), visitor, monitor);
		
		ArrayList libraryFolders = new ArrayList();
		Iterator collectedLibraries = visitor.getLibraryEntries().iterator();
		while (collectedLibraries.hasNext()) {
			IClasspathEntry entry = (IClasspathEntry) collectedLibraries.next();
			File entryFile = new File(Utils.getResolvedClasspathEntryFile(entry));
			if (entryFile.isDirectory()) {
				libraryFolders.add(entry.getPath().makeAbsolute());
			}
		}
		
		if (buildLoggingConfig.isPreverifierTraceEnabled()) {
			BuildConsoleProxy.instance.traceln("< ResourceDeltaBuilder.collectLibraryFolderPaths"); 
		}
		return (IPath[]) libraryFolders.toArray(new IPath[libraryFolders.size()]);
	}
	
	/**
	 * Create an error marker for the specific type with
	 * the specified error message.
	 * 
	 * @param error
	 */
	private void createErrorMarkerFor(PreverificationError error) 
		throws JavaModelException, CoreException
	{
		IMarker marker = null;
		
		// Calculate the resource
		IClassErrorInformation classInfo = error.getLocation().getClassInformation();
		String typeName = (classInfo == null) ? "" : classInfo.getName().replace('/', '.');
		String message = PreverificationUtils.getErrorText(error);
	
		IType type = buildInfo.getCurrentJavaProject().findType(typeName);
		if (type != null) {
			IResource resource = type.getResource();
			
			// Sometimes the resource doesn't come back... This is supposed
			// to be only when the resource is in an external archive.
			if (resource != null) {
				// Create the marker and set the attributes
				marker = resource.createMarker(IJavaModelMarker.JAVA_MODEL_PROBLEM_MARKER);
				marker.setAttribute(IMarker.MESSAGE, message);
				marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_ERROR);
				
				int lineNumber = error.getLocation().getLineNumber();
				if (lineNumber != -1) {
					marker.setAttribute(IMarker.LINE_NUMBER, lineNumber);
				}
				
				setMarkerRangeAttributes(marker, error, type);
			}
		}
		
		// Fallback position if nothing specific is possible
		if (marker == null) {
			createProjectLevelPreverifyMarker(typeName, message);
		}
	}

	/**
	 * Create an error marker on the project about a type verification.  This will
	 * happen when an error occurs trying to preverify and yet the type resource
	 * cannot be located for some reason.
	 * 
	 * @param typeName
	 * @param message
	 * @throws CoreException
	 */
	private void createProjectLevelPreverifyMarker(String typeName, String message) 
		throws CoreException 
	{
		StringBuffer sb = new StringBuffer("Type ");
		sb.append(typeName).append(" ").append(message);
		
		IProject project = buildInfo.getCurrentJavaProject().getProject();
		IMarker marker = project.createMarker(IJavaModelMarker.JAVA_MODEL_PROBLEM_MARKER);
		marker.setAttribute(IMarker.MESSAGE, sb.toString());
		marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_ERROR);
	}
	
	/**
	 * Return a boolean indicating whether or not the classes in the specified
	 * directory and subdirectories need preverification.
	 * 
	 * @param srcFile
	 * @param tgtFile
	 * @return
	 */
	private boolean doClassesRequirePreverification(File srcFile, File tgtFile) {
		boolean requirePreverify = false;
		
		if (srcFile.isDirectory()) {
			if (!tgtFile.exists()) {
				requirePreverify = true;
			} else {
				java.io.File[] classesAndDirectories = srcFile.listFiles(new FileFilter() {
					public boolean accept(java.io.File pathname) {
						return pathname.isDirectory() || pathname.getName().endsWith(".class");
					}
				});
				
				for (int i = 0; i < classesAndDirectories.length; i++) {
					File srcFile2 = new File(classesAndDirectories[i]);
					File tgtFile2 = new File(tgtFile, classesAndDirectories[i].getName());
					requirePreverify = doClassesRequirePreverification(srcFile2, tgtFile2);
					if (requirePreverify) break;
				}
			}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -