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

📄 antennabuildexporter.java

📁 配置文件
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
				IPath pattern = entry.getInclusionPatterns()[i];
				Element inclusionElement = newChildElement(element, "include");
				inclusionElement.setAttribute("name", pattern.toString());
			}
		}

		/**
		 * Create a new project information structure.
		 * 
		 * @param javaProject
		 * @param exported
		 */
		private void createProjectInfo(IJavaProject javaProject, boolean exported)
		{
			ProjectInfo info = new ProjectInfo(javaProject, exported);
			projectInfoMap.put(javaProject, info);
		}
		
		/**
		 * Get the holder of information for the specified project.
		 * 
		 * @param javaProject
		 * @return
		 */
		private ProjectInfo getProjectInfo(IJavaProject javaProject) {
			return (ProjectInfo) projectInfoMap.get(javaProject);
		}

		/**
		 * Get the file system location for the specified classpath entry.
		 * 
		 * @param entry
		 * @return
		 * @throws CoreException 
		 */
		private IPath getLibraryLocation(IClasspathEntry entry) 
			throws CoreException 
		{
			IPath libLocation = null;

			Object resolved = Utils.getResolvedClasspathEntry(entry);
			if (resolved instanceof IResource) {
				IResource libResource = (IResource) resolved; 
				libLocation = libResource.getLocation();
			} else if (resolved instanceof File) {
				libLocation = entry.getPath();
			}
			
			return libLocation;
		}

		/**
		 * Return the project relative path.
		 * 
		 * @param javaProject
		 * @param projectInfo
		 * @param location
		 * @return
		 * @throws CoreException 
		 */
		private String getProjectRelativeValue(
			IJavaProject javaProject, 
			ProjectInfo projectInfo, 
			IPath location) 
				throws CoreException 
		{
			StringBuffer sb = new StringBuffer();
			IPath projectPath = javaProject.getProject().getLocation();
			
			String relativePath = getRelativePath(projectPath, location);
			if (relativePath == null) {
				// Can't get relation to projectPath, use location as it is
				sb.append(location.toString());
			} else {
				sb
					.append("${")
					.append(projectInfo.getAntProjectPropertyName())
					.append("}")
					.append(relativePath);
			}
			
			return sb.toString();
		}

		/**
		 * Get the file system location for the specified classpath entry.
		 * 
		 * @param entry
		 * @return
		 * @throws CoreException 
		 */
		private IPath getSourceLocation(IClasspathEntry entry) 
			throws CoreException 
		{
			IPath sourceLocation = null;

			Object resolved = Utils.getResolvedClasspathEntry(entry);
			if (resolved instanceof IResource) {
				IResource srcResource = (IResource) resolved; 
				sourceLocation = srcResource.getLocation();
			}
			
			return sourceLocation;
		}
	}
	
	private static final Pattern SUBSTITUTION_PATTERN =
		Pattern.compile("\\@\\{(.+?)\\}");

	private IMidletSuiteProject midletSuite;
	private IJavaProject javaProject;
	private IPath basedirPath;
	private String projectName;
	private Properties buildProperties;
	private Document eclipseMeBuildXmlDocument;
	
	/**
	 * Construct a new exporter for the specified midlet suite project.
	 * 
	 * @param midletSuite
	 */
	public AntennaBuildExporter(IMidletSuiteProject midletSuite) {
		super();
		this.midletSuite = midletSuite;
		this.javaProject = midletSuite.getJavaProject();
		
		basedirPath = javaProject.getProject().getLocation(); 
		projectName = midletSuite.getProject().getName();
	}

	/**
	 * Do the export for the midlet suite.
	 * 
	 * @throws CoreException
	 */
	public void doExport(IProgressMonitor monitor)
		throws CoreException, AntennaExportException
	{
		// Validate the environment is ok.
		validateEnvironment(monitor);
		
		try {
			// Read in the templates
			buildProperties = createInitialProperties();
			eclipseMeBuildXmlDocument = readEclipseMeBuildTemplate();

			// Traverse the classpath and update the results along the way
			BuildClasspathEntryVisitor visitor = traverseClasspath(monitor);
			updateEclipseMeBuildXml(visitor.getProjectInfoMap());
			
			// Write out the results
			exportBuildXml(monitor);
			writeBuildProperties(monitor);
			writeEclipseMeBuildXml(monitor);
			
			// Refresh so these files show in the workbench.
			javaProject.getProject().refreshLocal(IResource.DEPTH_ONE, monitor);
			
		} catch (Exception e) {
			if (e instanceof CoreException) {
				throw (CoreException) e;
			} else {
				EclipseMECorePlugin.throwCoreException(IStatus.ERROR, -999, e);
			}
		}
	}

	/**
	 * Create the initial properties object.
	 * 
	 * @return
	 */
	private Properties createInitialProperties() {
		Properties props = new Properties();

		// The basics
		props.setProperty("midlet.name", javaProject.getProject().getName());
		props.setProperty("path.build", "${basedir}/build");
		props.setProperty("path.build.classes", "${basedir}/build/classes");
		props.setProperty("path.build.output", "${basedir}/deployed");
		
		// Antenna stuff
		Preferences prefs =	EclipseMECorePlugin.getDefault().getPluginPreferences();
		props.setProperty(
				"wtk.home", 
				prefs.getString(IEclipseMECoreConstants.PREF_WTK_ROOT));
		props.setProperty(
				"path.antenna.jar", 
				prefs.getString(IEclipseMECoreConstants.PREF_ANTENNA_JAR));
		boolean autoVersion = 
			PreferenceAccessor.instance.getAutoversionPackage(midletSuite.getProject());
		props.setProperty(
				"flag.autoversion", 
				Boolean.toString(autoVersion));
		props.setProperty("flag.preverify", "true");
		
		// Set default encoding to UTF-8
		props.setProperty("src.encoding", "UTF-8");

		// Set the classpath to match the device
		IDevice device = midletSuite.getDevice();
		if (device != null) {
			props.setProperty("wtk.midpapi", device.getClasspath().toString());
		}
		
		// Properties necessary for running in the emulator
		props.setProperty("run.device.name", "DefaultColorPhone");
		props.setProperty("run.trace.options", "");
		
		// Add some default properties based on various things
		addObfuscationProperties(props);
		addVersionProperties(props);
		
		return props;
	}

	/**
	 * Add the obfuscation-related properties.
	 * 
	 * @param props
	 */
	private void addObfuscationProperties(Properties props) {
		File proguardFile = EclipseMECorePlugin.getProguardJarFile();
		boolean obfuscate = ((proguardFile != null) && proguardFile.exists());
		
		// props.setProperty("flag.obfuscate", Boolean.toString(obfuscate));

		if (obfuscate) {
			File proguardHome = proguardFile.getParentFile().getParentFile();
			props.setProperty("wtk.proguard.home", proguardHome.toString());
		}
	}

	/**
	 * Add the appropriate J2ME version properties to match the JAD versions.
	 * 
	 * @param props
	 */
	private void addVersionProperties(Properties props) {
		ApplicationDescriptor descriptor = midletSuite.getApplicationDescriptor();
		Properties manifestProperties = descriptor.getManifestProperties();
		
		String versionString = 
			manifestProperties.getProperty(IJADConstants.JAD_MICROEDITION_CONFIG);
		if (versionString != null) {
			int index = versionString.indexOf('-');
			versionString = (index == -1) ? versionString : versionString.substring(index + 1);
			props.setProperty("wtk.cldc.version", versionString);
		}
		
		versionString = manifestProperties.getProperty(IJADConstants.JAD_MICROEDITION_PROFILE);
		if (versionString != null) {
			int index = versionString.indexOf('-');
			versionString = (index == -1) ? versionString : versionString.substring(index + 1);
			props.setProperty("wtk.midp.version", versionString);
		}
	}

	/**
	 * Add a new argument to the WTK obfuscate element.
	 * 
	 * @param wktobfuscateElement
	 * @param argument
	 */
	private void addWtkObfuscateArgument(Element wktobfuscateElement, String argument) {
		Element argumentElement = newChildElement(wktobfuscateElement, "argument");
		argumentElement.setAttribute("value", argument);
	}

	/**
	 * Read the build.xml template file.
	 * 
	 * @param is
	 * @return
	 * @throws UnsupportedEncodingException
	 * @throws IOException
	 */
	private StringBuffer readBuildXmlTemplate(InputStream is) 
		throws UnsupportedEncodingException, IOException 
	{
		// Read in the template
		int charsRead;
		char[] buffer = new char[1024];
		
		Reader reader = new InputStreamReader(is, "UTF-8");
		StringBuffer sb = new StringBuffer();
		while ((charsRead = reader.read(buffer)) != -1) {
			sb.append(buffer, 0, charsRead);
		}
		
		is.close();
		return sb;
	}

	/**
	 * Read the template EclipseME build file.
	 * @return
	 * @throws IOException
	 * @throws SAXException
	 * @throws ParserConfigurationException
	 */
	private Document readEclipseMeBuildTemplate() 
		throws ParserConfigurationException, SAXException, IOException 
	{
		Document document = null;
		
		InputStream is = getClass().getResourceAsStream("eclipseme-build.xml");
		if (is == null) {
			throw new IOException("build.xml template not found");
		} else {
			// Read the document
			document = XMLUtils.readDocument(is);

			// Alter the "project" element's name attribute
			Element projectElement = document.getDocumentElement();
			projectElement.setAttribute("name", "eclipseme-" + projectName);

			// Add some warning comments
			StringBuffer comment = new StringBuffer();
			comment
				.append("\n\tAutomatically generated by EclipseME on ")
				.append(new Date())
				.append("\n\tDO NOT ALTER THIS FILE.  IT WILL BE OVERWRITTEN\n\n")
				.append("\tChanges may be made to build.xml and user-build.properties\n");
			insertCommentBefore(projectElement, comment.toString());
		}
		
		return document;
	}

	/**
	 * Replace the build xml template values.
	 * 
	 * @param sb
	 * @param props
	 */
	private void replaceTemplateValues(StringBuffer sb, Properties props) {
		// Replace template values
		int offset = 0;
		Matcher matcher = SUBSTITUTION_PATTERN.matcher(sb);
		
		while (matcher.find(offset)) {
			String referencedValue = matcher.group(1);
			String resolvedValue = props.getProperty(referencedValue, referencedValue);
			sb.replace(matcher.start(), matcher.end(), resolvedValue);
			
			// Figure out the new offset, based on the replaced
			// string length
			offset = matcher.start() + resolvedValue.length();
		}
	}

	/**
	 * Export the build.xml file.
	 * 
	 * @param monitor
	 * @throws SAXException

⌨️ 快捷键说明

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