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

📄 codegenerator.java

📁 java 调用windows的api
💻 JAVA
字号:
package org.jawin.browser.codegen;

import org.jawin.browser.java.*;
import org.jawin.browser.log.Log;
import org.jawin.browser.xpath.XPathManager;
import org.jawin.browser.xsl.TransformationManager;
import org.jawin.browser.util.Replace;
import org.jawin.browser.dialog.CodeGenerationStatusDialog;
import org.jawin.browser.gui.FrameManager;

import java.util.ArrayList;
import java.io.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.InputSource;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * Generates code from the XML type library tree
 *
 * For performance reasons, it is required to XML DOM parse the
 * input XML so that XSL result fragments can be transformed independantly
 * into Java source files.
 *
 * The process of using this class is simple, first call setXML()
 * with the XML to parse and then call new SwingUtilities.invokeLater()
 * with het getInstance() to generate the source code showing status as it is generated.
 *
 * This is run in a new thread so that the GUI can be responsive and status can be
 * given to the user to show that something is happening, otherwise the
 * GUI might seem to hang.
 *
 * <p>Title: Jawin Code Generation GUI</p>
 * <p>Description: GUI for exploring type libraries and generating Java code</p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: Open Source Incentive</p>
 *
 * @author Josh Passenger
 * @version 1.0
 */

public class CodeGenerator implements Runnable
{
	private String xml = null;
	private Document document = null;
	private DocumentBuilderFactory dfactory = null;
	private CodeGenerationStatusDialog statusDialog = null;
	private volatile boolean stopping = false;
	private static CodeGenerator instance = null;
	private JavaSourcePanel javaSourcePanel = null;

	public void run()
	{
		try
		{
			generateCode();
		}
		catch (Exception e)
		{
			Log.getInstance().exception("CodeGenerator.run() failed to complete code generation", e);
		}
		finally
		{
			if (isStopping())
			{
				javaSourcePanel.clearAll();
			}

			cleanup();
			statusDialog.dispose();
		}
	}

	public boolean isStopping()
	{
		return stopping;
	}

	public static synchronized void initialize()
	{
		try
		{
			instance = new CodeGenerator();
		}
		catch (CodeGenerationException c)
		{
			Log.getInstance().exception("CodeGenerator.initialize() failed to construct the CodeGenerator", c);
		}
	}

	public static CodeGenerator getInstance()
	{
		if (instance == null)
		{
			throw new IllegalStateException("CodeGenerator.getInstance() CodeGenerator was not initialized");
		}

		return instance;
	}

	private CodeGenerator() throws CodeGenerationException
	{
		initializeParser();
	}

	/**
	 * Initializes the XML DOM parser instances
	 *
	 * @throws CodeGenerationException if the parser cannot be initialized
	 */
	private void initializeParser() throws CodeGenerationException
	{
		try
		{
			dfactory = DocumentBuilderFactory.newInstance();
			dfactory.setNamespaceAware(true);
		}
		catch (Exception e)
		{
			instance = null;
			throw new CodeGenerationException("CodeGenerator.initializeParser() failed to create DocumentBuilderFactory, pelase check Xalan is installed correctly", e);
		}
	}

	/**
	 * Store the XML parsing it into a DOM Document and resetting the status
	 * displaying ui elements
	 *
	 * @param newXML the xml to parse
	 * @param newJavaSourcePanel the java tabbed panel to write results to
	 * @throws CodeGenerationException if the docuemnt cannot be pasred for some reason
	 */
	public void setXML(String newXML, JavaSourcePanel newJavaSourcePanel)
		throws CodeGenerationException
	{
		stopping = false;
		xml = newXML;
		javaSourcePanel = newJavaSourcePanel;

		try
		{
			StringReader reader = new StringReader(xml);
			InputSource in = new InputSource(reader);
			document = dfactory.newDocumentBuilder().parse(in);
		}
		catch (Exception e)
		{
			throw new CodeGenerationException("CodeGenerator.setXML() failed to parse the input XML into a DOM Document", e);
		}
	}

	/**
	 * Generate the Java code by selecting a node set for each config
	 * and executing a stylesheet against them.
	 *
	 * Stop processing if the stopping flag has been set
	 *
	 * @param javaTabbedPanel the tabbed panel to create JavaPanel objects in
	 * @throws CodeGenerationException thrown if something goes wrong
	 */
	private void generateCode() throws CodeGenerationException
	{
		javaSourcePanel.clearAll();

		ArrayList sourceFiles = new ArrayList();

		try
		{
			for (int i = 0; i < CodeGenConfigManager.getInstance().getConfigCount(); i++)
			{
				if (isStopping())
				{
					return;
				}

				CodeGenConfig config = CodeGenConfigManager.getInstance().getConfigAt(i);

				String name = config.getName();
				String stylesheet = config.getStylesheet();
				String xpath = config.getXPath();
				String fileNameXPath = config.getFileNameXPath();
				String saveDirectoryXPath = config.getSaveDirectoryXPath();
				String packageNameXPath = config.getPackageNameXPath();
				String imageName = config.getImageName();
				String javaEncodingXPath = config.getJavaEncodingXPath();

				statusDialog.setBaseText("Running: " + name + " generating: ");

				// Log.getInstance().debug("Executing code generation: " + name);

				NodeList list = XPathManager.getInstance().evalNodeList(document, xpath);

				statusDialog.setMaximumAndReset(list.getLength());

				for (int j = 0; j < list.getLength(); j++)
				{
					if (isStopping())
					{
						return;
					}

					Node node = list.item(j);

					String saveDirectory = XPathManager.getInstance().eval(node, saveDirectoryXPath);
					String packageName = XPathManager.getInstance().eval(node, packageNameXPath);
					String fileName = XPathManager.getInstance().eval(node, fileNameXPath);
					String javaEncoding = XPathManager.getInstance().eval(node, javaEncodingXPath);
                                        //System.out.println("javaEncoding="+javaEncoding+" javaEncodingXPath="+javaEncodingXPath+" "+packageNameXPath+" ");
					statusDialog.setText(fileName);
					String fileNameAndPath = getSaveDirectory(saveDirectory, packageName, fileName);

					String javaCode = TransformationManager.getInstance().transformNode(node, stylesheet);
					JavaSourceFile javaSource = new JavaSourceFile(new File(fileNameAndPath), javaCode, javaEncoding, imageName);
					sourceFiles.add(javaSource);
					statusDialog.tickProgressBar();
				}
			}
		}
		catch (Exception e)
		{
			javaSourcePanel.removeAll();
			throw new CodeGenerationException("CodeGenerator.generateCode() failed to complete code generation", e);
		}

		javaSourcePanel.addSourceFiles(sourceFiles);
	}

	public void setStatusDialog(CodeGenerationStatusDialog newStatusDialog)
	{
		statusDialog = newStatusDialog;
	}

	public void cleanup()
	{
		document = null;
		xml = null;
	}

	/**
	 * Create the full file name for this class
	 *
	 * @param saveDirectory where are source files save to for this project
	 * @param packageName what package does this class belong to
	 * @param fileName what is the file name of this class
	 * @return the full path to this file
	 */
	private String getSaveDirectory(String saveDirectory, String packageName, String fileName)
	{
		StringBuffer buffer = new StringBuffer();

		String packageDirectory = Replace.replace(packageName, ".", "/");
		saveDirectory = Replace.replace(saveDirectory, "\\", "/");

		buffer.append(saveDirectory);

		if (!saveDirectory.endsWith("/") && !packageDirectory.startsWith("/"))
		{
			buffer.append("/");
		}

		buffer.append(packageDirectory);

		buffer.append("/");

		buffer.append(fileName);

		return buffer.toString();
	}

	/**
	 * Ask the thread nicely to stop
	 */
	public void pleaseStop()
	{
		stopping = true;
	}


}

⌨️ 快捷键说明

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