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

📄 projectutility.java

📁 用Swing实现的CHM制作工具
💻 JAVA
字号:
package g2w.app.gchm.util;


import g2w.app.gchm.lib.StringUtility;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
import org.jdom.xpath.XPath;

/**
 * A utility to manage the project file.
 * 
 * @author GreatGhoul
 * @version 021 2009-3-22 8:50:16
 */
public class ProjectUtility {
	/**
	 * Prevent getting instanced.
	 */
	private ProjectUtility() {
	};

	public static void scanTopics(File dir, Element elem) {
		Iterator<File> fileIterator = FileUtility.getFilesByNameExtensions(dir,
				"htm", "html").iterator();
		while (fileIterator.hasNext()) {
			File file = fileIterator.next();
			Element topic = new Element("Topic");
			topic.setAttribute("name", HTMUtility.getHTMLTitle(file
					.getAbsolutePath()));
			topic.setAttribute("local", file.getAbsolutePath());
			elem.addContent(topic);
		}

		Iterator<File> dirIterator = FileUtility.getFolders(dir).iterator();
		while (dirIterator.hasNext()) {
			File subDir = dirIterator.next();
			Element heading = new Element("Topic");
			heading.setAttribute("name", subDir.getName());
			heading.setAttribute("local", "");
			scanTopics(subDir, heading);
			if (heading.getContentSize() > 0)
				elem.addContent(heading);
		}
	}

	/**
	 * Store the document.
	 * 
	 * @param document
	 *            The document to store.
	 * @throws URISyntaxException
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	public static void storeDocument(Document document)
			throws URISyntaxException, FileNotFoundException, IOException {
		File projectFile = new File(new URI(document.getBaseURI()));
		storeDocument(document, projectFile);
	}

	/**
	 * Store the document into the given file.
	 * 
	 * @param document
	 *            The document to save.
	 * @param file
	 *            The given file.
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	public static void storeDocument(Document document, File file)
			throws FileNotFoundException, IOException {
		XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
		outputter.output(document, new FileOutputStream(file));
	}

	/**
	 * Return a list of html document files in the folder dir.
	 * 
	 * @param dir
	 *            The source folder.
	 * @param scanSubFolders
	 *            Scans sub folders or not.
	 * @return The document list.
	 */
	public static List<File> getHTMLFiles(File dir, boolean scanSubFolders) {
		List<File> files = new ArrayList<File>();
		files.addAll(FileUtility.getFilesByNameExtensions(dir, "html", "htm"));

		if (scanSubFolders) {
			Iterator<File> iterator = FileUtility.getFolders(dir).iterator();
			while (iterator.hasNext()) {
				files.addAll(getHTMLFiles(iterator.next(), scanSubFolders));
			}
		}

		return files;
	}

	/**
	 * Return the project file from the indicated file.
	 * 
	 * @param path
	 *            The project file.
	 * @return The xml project document.
	 * @throws IOException
	 * @throws JDOMException
	 * @throws FileNotFoundException
	 */
	public static Document getProjectFile(File projectFile)
			throws JDOMException, IOException, FileNotFoundException {
		if (!projectFile.exists()) {
			throw new FileNotFoundException("工程文件" 
					+ projectFile.getAbsolutePath() + "不存在!");
		}

		// Get the xml document object.
		SAXBuilder builder = new SAXBuilder(false);
		Document document = builder.build(projectFile);
		return document;
	}

	/**
	 * Return the project file from the indicated path.
	 * 
	 * @param path
	 *            The project file's path.
	 * @return The xml project document.
	 * @throws IOException
	 * @throws JDOMException
	 * @thorws FileNotFoundException
	 */
	public static Document getProjectFile(String path) 
			throws JDOMException,IOException,FileNotFoundException {
		// Get the project file object.
		File projectFile = new File(path);
		return getProjectFile(projectFile);
	}

	@SuppressWarnings("unchecked")
	public static File createHHPFile(Document document) 
			throws JDOMException, IOException {
		Element root = document.getRootElement();

		File hhpFile = new File(root.getAttributeValue("local"), "gchm.hhp");
		FileWriter writer = new FileWriter(hhpFile);
		PrintWriter out = new PrintWriter(writer);
		
		// Options.
		out.println("[OPTIONS]");
		out.println("Compatibility=1.1 or later");
		out.println("Compiled file=" + root.getAttributeValue("filename"));
		out.println("Contents file=" + "gchm.hhc");
		out.println("Default Window=gchm_window");
		out.println("Default topic=" + root.getAttributeValue("defaultpage"));
		out.println("Display compile progress=No");
		out.println("Index file=" + "gchm.hhk");
		out.println("Language=0x804 Chinese (PRC)");
		out.println("Title=" + root.getAttributeValue("title"));
		out.println();
		
		
		// Windows.
		out.println("[WINDOWS]");
		out.print("gchm_window=,\"gchm.hhc\",\"gchm.hhk\",");
		
		String defaultPage = root.getAttributeValue("defaultpage");
		if (defaultPage != null && !defaultPage.trim().isEmpty())
			out.print(StringUtility.quote(defaultPage.trim(), "\""));
		out.print(",");
		
		String homePage = root.getAttributeValue("homepage");
		if (homePage != null && !homePage.trim().isEmpty())
			out.print(StringUtility.quote(homePage.trim(), "\""));
		out.print(",");
	
		String aboutPage = root.getAttributeValue("aboutpage");
		if (aboutPage != null && !aboutPage.trim().isEmpty())
			out.print(StringUtility.quote(aboutPage.trim(), "\""));
		out.print(",\"About\",,,0x3120,,0x4307e,,,,,,,,0\t");
		out.println();
		
		// Files.
		out.println("[FILES]");
		List topics = XPath.selectNodes(root, "//Topic");
		Iterator<Element> iterator = topics.iterator();

		List files = new ArrayList();
		while (iterator.hasNext()) {
			Element topic = iterator.next();
			String filepath = topic.getAttributeValue("local");
			if (filepath == null || filepath.trim().isEmpty())
				continue;
			out.println(filepath);
			files.add(filepath);
		}
		
		if (!files.contains(defaultPage)) {
			out.println(defaultPage);
			files.add(defaultPage);
		}
		if (!files.contains(homePage)) {
			out.println(homePage);
			files.add(homePage);
		}
		if (!files.contains(aboutPage)) {
			out.println(aboutPage);
			files.add(aboutPage);
		}
		out.println();
		
		// Infotypes
		out.println("[INFOTYPES]");
		out.close();
		writer.close();
		return hhpFile;
	}
	
	/**
	 * Create the index file with the given document.
	 * 
	 * @param document The given document.
	 * @return The index file.
	 * @throws IOException
	 * @throws JDOMException
	 */
	@SuppressWarnings("unchecked")
	public static File createHHKFile(Document document) throws IOException, JDOMException {
		Element root = document.getRootElement();

		File hhkFile = new File(root.getAttributeValue("local"), "gchm.hhk");
		FileWriter writer = new FileWriter(hhkFile);
		PrintWriter out = new PrintWriter(writer);

		out.println("<HTML><HEAD>");
		out
				.println("<meta name=\"GENERATOR\" content=\"Microsoft&reg; HTML Help Workshop 4.1\">");
		out.println("<!-- Sitemap 1.0 -->");
		out.println("</HEAD><BODY><UL>");

		List topics = XPath.selectNodes(root, "//Topic");
		Iterator<Element> iterator = topics.iterator();

		while (iterator.hasNext()) {
			Element topic = iterator.next();
			out.println("<LI><OBJECT type=\"text/sitemap\">");
			out.println("	<param name=\"Name\" value=\""
					+ topic.getAttributeValue("name") + "\">");
			out.println("	<param name=\"Local\" value=\""
					+ topic.getAttributeValue("local") + "\">");
			out.println("</OBJECT>");
		}
		
		out.println("</UL></BODY></HTML>");
		out.close();
		writer.close();
		return hhkFile;
	}

	public static File createHHCFile(Document document) throws IOException {
		Element root = document.getRootElement();

		File hhcFile = new File(root.getAttributeValue("local"), "gchm.hhc");
		FileWriter writer = new FileWriter(hhcFile);
		PrintWriter out = new PrintWriter(writer);
		
		out.println("<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML//EN\">");
		out.println("<HTML>");
		out.println("<HEAD>");
		out.println("<meta name=\"GENERATOR\" content=\"Microsoft&reg; HTML Help Workshop 4.1\">");
		out.println("<!-- Sitemap 1.0 -->");
		out.println("</HEAD>");
		out.println("<BODY>");
//		out.println("<OBJECT type=\"text/site properties\">");
//		out.println("\t<param name=\"ImageType\" value=\"Folder\">");
//		out.println("</OBJECT>");
		out.print(nodeToContent(root, 1));
		out.println("</BODY>");
		out.println("</HTML>");
		out.close();
		writer.close();
		return hhcFile;
	}

	/**
	 * Transferom the node to chm content file format.
	 * 
	 * @param root The node.
	 * @return The formatted string.
	 */
	@SuppressWarnings("unchecked")
	private static String nodeToContent(Element elem, int depth) {
		StringBuilder ret = new StringBuilder();
		String indent = StringUtility.buildString("\t", depth);
		if (elem.getContentSize() > 0) {
			if (!elem.isRootElement()) {
				ret.append(indent + "<LI><OBJECT type=\"text/sitemap\">\n");
				ret.append(indent + "<param name=\"Name\" value=\"" + elem.getAttributeValue("name") + "\">\n");
				ret.append(indent + "<param name=\"Local\" value=\"" + elem.getAttributeValue("local") + "\">\n");
				ret.append(indent + "</OBJECT>\n");
			}
			
			ret.append(indent + "<UL>\n");
			Iterator<Element> iterator = elem.getChildren("Topic").iterator();
			while (iterator.hasNext())
				ret.append(nodeToContent(iterator.next(), depth + 1));
			ret.append(indent + "</UL>\n");
		} else {
			ret.append(indent + "<LI><OBJECT type=\"text/sitemap\">\n");
			ret.append(indent + "<param name=\"Name\" value=\"" + elem.getAttributeValue("name") + "\">\n");
			ret.append(indent + "<param name=\"Local\" value=\"" + elem.getAttributeValue("local") + "\">\n");
			ret.append(indent + "</OBJECT>\n");
		}
		
		return ret.toString();
	}
}

⌨️ 快捷键说明

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