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

📄 antennabuildexporter.java

📁 配置文件
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
	 * @throws ParserConfigurationException
	 * @throws IOException
	 * @throws TransformerException
	 * @throws CoreException 
	 */
	private void exportBuildXml(IProgressMonitor monitor) 
		throws ParserConfigurationException, SAXException, TransformerException, IOException, CoreException 
	{
		// Check to make sure that we don't overwrite a user's build.xml file
		// with our template file.
		// TODO What do we do, if anything, when the build.xml file already exists?
		File buildXmlFile = getMidletSuiteFile("build.xml", monitor);
		if (!buildXmlFile.exists()) {
			// Template values
			Properties props = new Properties();
			props.setProperty("eclipseme.version", EclipseMECorePlugin.getPluginVersion());
			props.setProperty("date", (new Date()).toString());
			props.setProperty("project.name", projectName);
		
			InputStream is = getClass().getResourceAsStream("build.xml");
			if (is != null) {
				// Read the template and do the replacements
				StringBuffer sb = readBuildXmlTemplate(is);
				replaceTemplateValues(sb, props);
		
				// Write the results
				FileOutputStream fos = new FileOutputStream(buildXmlFile);
				OutputStreamWriter writer = new OutputStreamWriter(fos, "UTF-8");
				writer.write(sb.toString());
				writer.close();
			}
		}
	}

	/**
	 * Return the specified element within the specified parent.  If more
	 * elements exist with that name, the first will be returned.  If not
	 * found, <code>null</code> will be returned.
	 * 
	 * @param parentElement
	 * @param elementName
	 * @return
	 */
	private Element findElement(Element parentElement, String elementName) {
		Element element = null;
		
		NodeList elements = parentElement.getElementsByTagName(elementName);
		if (elements.getLength() > 0) {
			element = (Element) elements.item(0);
		}
		
		return element;
	}
	
	/**
	 * Return a File reference to a file with the specified name
	 * in the midlet suite project.
	 * 
	 * @param filename
	 * @param monitor
	 * @return
	 * @throws CoreException 
	 */
	private File getMidletSuiteFile(String filename, IProgressMonitor monitor) 
		throws CoreException 
	{
		IFile file = midletSuite.getProject().getFile(filename); 
		return file.getLocation().toFile();
	}
	
	/**
	 * Get the options to be specified when calling Proguard for
	 * obfuscation.
	 * 
	 * @return
	 */
	private String getProguardOptions() {
		IProject project = midletSuite.getProject();
		PreferenceAccessor obfuscationPrefs = PreferenceAccessor.instance;
		String specifiedOptions = obfuscationPrefs.getSpecifiedProguardOptions(project);
		boolean useSpecified = obfuscationPrefs.isUseSpecifiedProguardOptions(project); 

		return useSpecified ?
				specifiedOptions :
				obfuscationPrefs.getDefaultProguardOptions();
	}

	/**
	 * Return the relative path.
	 * 
	 * @param basePath the path that acts as the base for comparison
	 * @param tgtPath the path being compared to the base path
	 * @return relative path, or null if relation not possible
	 */
	private String getRelativePath(IPath basePath, IPath tgtPath)
	{
		String path = null;
		
		// Find the common path prefix
		int matchingSegments = tgtPath.matchingFirstSegments(basePath);
		
		String baseDevice = basePath.getDevice();
		String tgtDevice = tgtPath.getDevice();
		boolean bothNull = ((baseDevice == null) && (tgtDevice == null));
		
		if (bothNull || baseDevice.equals(tgtDevice)) {
			// Step up the directory tree
			StringBuffer relativePath = new StringBuffer();
			int upSteps = basedirPath.segmentCount() - matchingSegments;
			for (int i = 0; i < upSteps; i++) {
				relativePath.append("/..");
			}
			
			// Tack on the src folder segments
			tgtPath = tgtPath.removeFirstSegments(matchingSegments);
			String[] segments = tgtPath.segments();
			for (int i = 0; i < segments.length; i++) {
				relativePath.append("/").append(segments[i]);
			}
			
			path = relativePath.toString();
		}
		
		return path;
	}

	/**
	 * Return the root of the first located Sun wireless toolkit for use
	 * in setting the appropriate Antenna property or <code>null</code>
	 * if one cannot be found.
	 * 
	 * @return
	 */
	private File getWTKRoot() {
		Preferences prefs =	EclipseMECorePlugin.getDefault().getPluginPreferences();
		return new File(prefs.getString(IEclipseMECoreConstants.PREF_WTK_ROOT));
	}

	/**
	 * Insert the comment text before the specified element.
	 * 
	 * @param element
	 * @param commentString
	 */
	private void insertCommentBefore(Element element, String commentString) {
		Document document = element.getOwnerDocument();
		
		Comment comment = document.createComment(commentString);
		document.insertBefore(comment, element);
	}
	
	/**
	 * Create and return a new child element under the specified parent
	 * element.
	 * 
	 * @param parentElement
	 * @param name
	 * @return
	 */
	private Element newChildElement(Element parentElement, String name) {
		Element element = parentElement.getOwnerDocument().createElement(name);
		parentElement.appendChild(element);
		
		return element;
	}

	/**
	 * Traverse the classpath and update the build information
	 * along the way.
	 * @throws CoreException
	 */
	private BuildClasspathEntryVisitor traverseClasspath(IProgressMonitor monitor) 
		throws CoreException 
	{
		// Use a classpath visitor to build up the build information
		BuildClasspathEntryVisitor visitor = new BuildClasspathEntryVisitor(javaProject);
		visitor.getRunner(true).run(javaProject, visitor, monitor);
		
		return visitor;
	}

	/**
	 * Update the EclipseME build file based on the classpath information
	 * collected during traversal. 
	 * 
	 * @param projectInfoMap
	 */
	private void updateEclipseMeBuildXml(Map projectInfoMap) {
		// Collect the elements
		Document doc = eclipseMeBuildXmlDocument;		
		Element documentElement = doc.getDocumentElement();
		Element pathElement = findElement(documentElement, "path");
		Element wtkBuildElement = findElement(documentElement, "wtkbuild");
		Element wtkPackageElement = findElement(documentElement, "wtkpackage");
		Element previousBuildElement = wtkBuildElement;
		
		// Walk through the project info making the necessary updates
		Iterator iterator = projectInfoMap.entrySet().iterator();
		while (iterator.hasNext()) {
			Map.Entry entry = (Map.Entry) iterator.next();
			ProjectInfo info = (ProjectInfo) entry.getValue();

			// Set the property for the root of the project
			String propertyValue = info.getAntProjectPropertyValue();
			propertyValue = (propertyValue.length() == 0) ? 
					"${basedir}" : "${basedir}" + propertyValue;
			buildProperties.setProperty(
				info.getAntProjectPropertyName(), 
				propertyValue);
			
			// Insert this project's classpath path definition
			pathElement.getParentNode().insertBefore(info.getClasspathElement(), pathElement);
			
			// Insert the project's wtkbuild calls
			Iterator elements = info.getWtkBuildElements();
			while (elements.hasNext()) {
				Element element = (Element) elements.next();

				// Add these to the build file such that the prereq projects
				// get built before the dependent projects
				wtkBuildElement.getParentNode().insertBefore(element, previousBuildElement);
				previousBuildElement = element;
			}
			
			// Insert the fileset definitions for packaging from this project
			if (info.isExported()) {
				// Add the classpath reference
				Element classpathElement = newChildElement(wtkPackageElement, "classpath");
				classpathElement.setAttribute("refid", info.getClasspathElementId());

				elements = info.getPackageFilesetElements().iterator();
				while (elements.hasNext()) {
					Element element = (Element) elements.next();
					wtkPackageElement.appendChild(element);
				}
			}
		}
		
		// Remove the marker elements
		pathElement.getParentNode().removeChild(pathElement);
		wtkBuildElement.getParentNode().removeChild(wtkBuildElement);

		// Update the wtkobfuscate parameters
		Element wktobfuscateElement = findElement(documentElement, "wtkobfuscate");
		updateWtkObfuscateElement(wktobfuscateElement);
	}
	
	/**
	 * Update the WTK obfuscate element to include the parameters.
	 * 
	 * @param wktobfuscateElement
	 */
	private void updateWtkObfuscateElement(Element wktobfuscateElement) {
		String[] keepExpressions = 
			PreferenceAccessor.instance.getProguardKeepExpressions(midletSuite.getProject());

		for (int i = 0; i < keepExpressions.length; i++) {
			StringBuffer sb = new StringBuffer("'-keep ");
			sb.append(keepExpressions[i]).append("'");
			
			addWtkObfuscateArgument(wktobfuscateElement, sb.toString());
		}
		
		addWtkObfuscateArgument(wktobfuscateElement, getProguardOptions());
	}

	/**
	 * Validate that the antenna property is valid.
	 * 
	 * @param monitor
	 * @throws AntennaExportException
	 */
	private void validateAntenna(IProgressMonitor monitor) 
		throws AntennaExportException 
	{
		boolean valid = false;
		
		Preferences prefs =	EclipseMECorePlugin.getDefault().getPluginPreferences();
		String antennaPref = prefs.getString(IEclipseMECoreConstants.PREF_ANTENNA_JAR);
		File antennaFile = new File(antennaPref);
		if (antennaFile.exists()) {
			try {
				ZipFile zipFile = new ZipFile(antennaFile);
				ZipEntry entry = zipFile.getEntry("antenna.properties");
				valid = (entry != null);
				zipFile.close();
			} catch (IOException e) {
				// Assume these cases mean the file is invalid
				EclipseMECorePlugin.log(IStatus.WARNING, "Error testing Antenna settings", e);
			}
		}
		
		if (!valid) {
			throw new AntennaExportException("Antenna library not found or not valid.\nCheck the preference settings.");
		}
	}

	/**
	 * Validate the environment prior to the export.  Throw
	 * an exception if the environment is not valid.
	 * 
	 * @param monitor
	 * @throws AntennaExportException if the environment is not valid
	 */
	private void validateEnvironment(IProgressMonitor monitor)
		throws AntennaExportException
	{
		validateAntenna(monitor);
		validateWTK(monitor);
	}

	/**
	 * Validate that a Sun WTK can be found.
	 * 
	 * @param monitor
	 * @throws AntennaExportException
	 */
	private void validateWTK(IProgressMonitor monitor) 
		throws AntennaExportException 
	{
		File wtkRoot = getWTKRoot();
		if ((wtkRoot == null) || !wtkRoot.exists()) {
			throw new AntennaExportException("Sun WTK not found.\nCheck the platform definition settings.");
		}
	}
	
	/**
	 * Write out the build properties file.
	 * 
	 * @param monitor
	 * @throws IOException
	 * @throws CoreException 
	 */
	private void writeBuildProperties(IProgressMonitor monitor) 
		throws IOException, CoreException
	{
		File buildPropsFile = getMidletSuiteFile("eclipseme-build.properties", monitor);
		FileOutputStream fos = new FileOutputStream(buildPropsFile);
		buildProperties.store(fos, " EclipseME Build Properties - DO NOT ALTER THIS FILE - Make changes in user-build.properties");
		fos.close();		
	}
	
	/**
	 * Write the EclipseME build.xml file.
	 * 
	 * @throws TransformerException
	 * @throws IOException
	 * @throws CoreException 
	 */
	private void writeEclipseMeBuildXml(IProgressMonitor monitor) 
		throws TransformerException, IOException, CoreException
	{
		// Write the output
		File buildXmlFile = getMidletSuiteFile("eclipseme-build.xml", monitor);
		XMLUtils.writeDocument(buildXmlFile, eclipseMeBuildXmlDocument);		
	}
}

⌨️ 快捷键说明

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