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

📄 antennabuildexporter.java

📁 eclipseme的最新版本的source,欢迎j2me程序员使用
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/**
 * Copyright (c) 2003-2005 Craig Setera
 * All Rights Reserved.
 * Licensed under the Eclipse Public License - v 1.0
 * For more information see http://www.eclipse.org/legal/epl-v10.html
 */
package eclipseme.core.internal.tools;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Preferences;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaProject;
import org.w3c.dom.Comment;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import eclipseme.core.IEclipseMECoreConstants;
import eclipseme.core.internal.EclipseMECorePlugin;
import eclipseme.core.internal.PreferenceAccessor;
import eclipseme.core.internal.utils.FilteringClasspathEntryVisitor;
import eclipseme.core.internal.utils.Utils;
import eclipseme.core.internal.utils.XMLUtils;
import eclipseme.core.model.ApplicationDescriptor;
import eclipseme.core.model.IJADConstants;
import eclipseme.core.model.IMidletSuiteProject;
import eclipseme.core.model.device.IDevice;

/**
 * Tool for exporting an Antenna build file.
 * <p />
 * Copyright (c) 2003-2005 Craig Setera<br>
 * All Rights Reserved.<br>
 * Licensed under the Eclipse Public License - v 1.0<p/>
 * <br>
 * $Revision: 1.25 $
 * <br>
 * $Date: 2007/01/20 22:39:22 $
 * <br>
 * @author Craig Setera
 */
public class AntennaBuildExporter {
	private static final String NO_EXPORT = "_no_export";
	private static final String PATH_BUILD_CLASSES_NO_EXPORT = "${path.build.classes}/" + NO_EXPORT;
	private static final String PATH_BUILD_CLASSES = "${path.build.classes}";

	// Holder for project-specific information built during the visitation of the classpath
	private class ProjectInfo {
		private String safeProjectName;
		private IJavaProject javaProject;
		private boolean exported;
		private Element classpathElement;
		private Map wtkBuildElements;
		private List packageFilesetElements;
		
		/**
		 * Construct a new project information instance for the
		 * specified java project.
		 * 
		 * @param javaProject
		 */
		ProjectInfo(IJavaProject javaProject, boolean exported) {
			this.javaProject = javaProject;
			this.exported = exported;
			
			wtkBuildElements = new HashMap();
			packageFilesetElements = new ArrayList();
			
			// Calculate a "safe" project name
			safeProjectName = javaProject.getElementName();
			safeProjectName = safeProjectName.replace(' ', '_');
		}
		
		/**
		 * Add a new fileset element to the list.
		 * 
		 * @param element
		 */
		public void addPackageFilesetElement(Element element) {
			packageFilesetElements.add(element);
		}
		
		/**
		 * Add a new wtkbuild element to the list.
		 * 
		 * @param srcEntry
		 * @param element
		 */
		public void addWtkBuildElement(IClasspathEntry srcEntry, Element element) {
			if (!wtkBuildElements.containsKey(srcEntry)) {
				wtkBuildElements.put(srcEntry, element);
			}
		}
		
		/**
		 * Return the Ant property name to be used for this project.
		 */
		public String getAntProjectPropertyName() {
			return "project.root." + safeProjectName;
		}
		
		/**
		 * Return the Ant project property value.
		 * 
		 * @return
		 */
		public String getAntProjectPropertyValue() {
			String relativePath = null;
			
			// Figure out the property value relative to the base directory
			IPath projectPath = javaProject.getProject().getLocation(); 
			relativePath = getRelativePath(basedirPath, projectPath);
			if (relativePath == null) {
				relativePath = projectPath.toString();
			}
			
			return relativePath;
		}
		
		/**
		 * Return the destination directory to be used for class build output.
		 * 
		 * @return
		 */
		public String getBuildDestination() {
			return exported ? PATH_BUILD_CLASSES : PATH_BUILD_CLASSES_NO_EXPORT;
		}
		
		/**
		 * Return the classpath element.
		 * 
		 * @return
		 */
		public Element getClasspathElement() {
			if (classpathElement == null) {
				classpathElement = eclipseMeBuildXmlDocument.createElement("path");
				classpathElement.setAttribute("id", getClasspathElementId());
				
				Element pathElement = newChildElement(classpathElement, "path");
				pathElement.setAttribute("location", PATH_BUILD_CLASSES);

				pathElement = newChildElement(classpathElement, "path");
				pathElement.setAttribute("location", PATH_BUILD_CLASSES_NO_EXPORT);
			}
			
			return classpathElement;
		}
		
		/**
		 * Return the identifier of the classpath path element.
		 * 
		 * @return
		 */
		public String getClasspathElementId() {
			return "classpath." + safeProjectName;
		}
		
		/**
		 * @return Returns the packageFilesetElements.
		 */
		public List getPackageFilesetElements() {
			return packageFilesetElements;
		}
		
		/**
		 * @return Returns the wtkBuildElements.
		 */
		public Iterator getWtkBuildElements() {
			return wtkBuildElements.values().iterator();
		}
		
		/**
		 * @return Returns the exported flag indication
		 */
		public boolean isExported() {
			return exported;
		}
	}
	
	// Classpath entry visitor for helping build the build.xml file
	private class BuildClasspathEntryVisitor extends FilteringClasspathEntryVisitor 
	{
		private Map projectInfoMap;
		
		/**
		 * Construct a new visitor that will be responsible for
		 * creating the necessary elements.
		 */
		public BuildClasspathEntryVisitor(IJavaProject rootProject)
		{
			projectInfoMap = new LinkedHashMap();
			createProjectInfo(rootProject, true);
		}

		/**
		 * Return the map of project information.
		 * 
		 * @return
		 */
		public Map getProjectInfoMap() {
			return projectInfoMap;
		}
		
		/**
		 * @see eclipseme.core.internal.utils.IClasspathEntryVisitor#visitLibraryEntry(org.eclipse.jdt.core.IClasspathEntry, org.eclipse.jdt.core.IJavaProject, org.eclipse.core.runtime.IProgressMonitor)
		 */
		public void visitLibraryEntry(
			IClasspathEntry entry,
			IJavaProject javaProject, 
			IProgressMonitor monitor)
				throws CoreException 
		{
			ProjectInfo projectInfo = getProjectInfo(javaProject);
			Element classpathElement = projectInfo.getClasspathElement();
			
			IPath libLocation = getLibraryLocation(entry);
			if (libLocation != null) {
				File libFile = libLocation.toFile();

				Element pathElement = newChildElement(classpathElement, "path");
				String relativePath = getProjectRelativeValue(javaProject, projectInfo, libLocation);
				pathElement.setAttribute("location", relativePath);
				addIncludesAndExcludes(entry, pathElement);
				
				if (isLibraryExported(entry))
				{
					if (libFile.isDirectory()) {
						// Create a new zip fileset for the packaging
						Element filesetElement = eclipseMeBuildXmlDocument.createElement("fileset");
						projectInfo.addPackageFilesetElement(filesetElement);
						filesetElement.setAttribute("dir", relativePath);
					} else {
						// Create a new zip fileset for the packaging
						Element zipFilesetElement = 
							eclipseMeBuildXmlDocument.createElement("zipfileset");
						projectInfo.addPackageFilesetElement(zipFilesetElement);
						zipFilesetElement.setAttribute("src", relativePath);
						addIncludesAndExcludes(entry, zipFilesetElement);
					}
				}
			}
		}
		
		/**
		 * @see eclipseme.core.internal.utils.IClasspathEntryVisitor#visitProject(IClasspathEntry, org.eclipse.jdt.core.IJavaProject, org.eclipse.jdt.core.IJavaProject, org.eclipse.core.runtime.IProgressMonitor)
		 */
		public boolean visitProject(
			IClasspathEntry entry,
			IJavaProject javaProject, 
			IJavaProject classpathProject, IProgressMonitor monitor)
				throws CoreException 
		{
			// Force the project info into the map so that it
			// is held in the correct order
			boolean exported = entry.isExported();
			createProjectInfo(classpathProject, exported);
			
			return super.visitProject(entry, javaProject, classpathProject, monitor); 
		}
		
		/**
		 * @see eclipseme.core.internal.utils.IClasspathEntryVisitor#visitSourceEntry(org.eclipse.jdt.core.IClasspathEntry, org.eclipse.jdt.core.IJavaProject, org.eclipse.core.runtime.IProgressMonitor)
		 */
		public void visitSourceEntry(
			IClasspathEntry entry,
			IJavaProject javaProject, 
			IProgressMonitor monitor)
				throws CoreException 
		{
			IPath srcLocation = getSourceLocation(entry);
			
			if (srcLocation != null) {
				ProjectInfo projectInfo = getProjectInfo(javaProject);
				
				// Create the wtkbuild task call
				String buildTask = projectInfo.isExported() ? "wtkbuild" : "javac";
				Element buildElement = eclipseMeBuildXmlDocument.createElement(buildTask);
				projectInfo.addWtkBuildElement(entry, buildElement);
				buildElement.setAttribute("destdir", projectInfo.getBuildDestination());
				buildElement.setAttribute("sourcepath", "");
				buildElement.setAttribute("encoding", "${src.encoding}");
				buildElement.setAttribute("source", "1.3");
				
				// Calculate the src directory relative to the specified
				// project.
				String relativePath = getProjectRelativeValue(javaProject, projectInfo, srcLocation);
				buildElement.setAttribute("srcdir", relativePath);
				
				// Add the inclusion and exclusion patterns as necessary
				addIncludesAndExcludes(entry, buildElement);
				
				// Add the classpath reference
				Element classpathElement = newChildElement(buildElement, "classpath");
				classpathElement.setAttribute("refid", projectInfo.getClasspathElementId());
				
				// Make sure to copy the non-Java resources into the package
				Element srcPackageElement = eclipseMeBuildXmlDocument.createElement("fileset");
				srcPackageElement.setAttribute("dir", relativePath);
				
				Element javaExclude = newChildElement(srcPackageElement, "exclude");
				javaExclude.setAttribute("name", "**/*.java");
				Element buildExclude = newChildElement(srcPackageElement, "exclude");
				buildExclude.setAttribute("name", "build/");
				addIncludesAndExcludes(entry, srcPackageElement);
				projectInfo.addPackageFilesetElement(srcPackageElement);
			} else {
				EclipseMECorePlugin.log(
						IStatus.WARNING, 
						"Skipping unresolvable classpath entry " + entry);
			}
		}

		/**
		 * Add the inclusion and exclusion elements as necessary.
		 * 
		 * @param entry
		 * @param element
		 */
		private void addIncludesAndExcludes(IClasspathEntry entry, Element element) {
			for (int i = 0; i < entry.getExclusionPatterns().length; i++) {
				IPath pattern = entry.getExclusionPatterns()[i];
				Element exclusionElement = newChildElement(element, "exclude");
				exclusionElement.setAttribute("name", pattern.toString());
			}

			for (int i = 0; i < entry.getInclusionPatterns().length; i++) {

⌨️ 快捷键说明

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