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

📄 javatohtmlcreator.java

📁 使用java写的在线生成html文本的源代码发布版(在线源代码生成器)
💻 JAVA
字号:
/**
 * Copyright: Copyright (c) 2002-2003
 * Company: JavaResearch(http://www.javaresearch.org)
 */

package org.jr.java2html;

import java.io.*;
import java.util.*;

import org.jr.io.*;
import org.jr.text.*;

/**
 * 将一个目录下的源代码全部转换为类似javadoc生成的文档的结构的HTML版本源代码生成器。
 * 这个类是应用的主类,如果你使用其他几个类并且模仿本类的代码可以生成你自己的应用,当然不局限于生成源代码文档,可能是其他的什么文档都可以。
 * 底层的设计是没有针对性的,但是可能不能考虑所有的应用。
 * 请注意这个根工具和javadoc的不同在于它没有javadoc那样的功能可以调用javac完成类的解析。
 * 这个工具生成良好的文档需要依赖你的源代码的组织是良好的,例如符合JBuilder的源代码组织的强制要求(包名和源代码的组织应该一致)。
 * <br>最后更新日期:2003年5月23日
 * @author cherami@javaresearch.org
 * @version  0.8
 */

public class JavaToHTMLCreator {
  /**
   * 替换模版中CREATOR属性用的字符串。
   */
  static final String CREATOR =
      "JavaToHTMLCreator(http://www.javaresearch.org)";
  /**
   * 替换模版中的其他区域相关的常量的资源束。
   */
  static final ResourceBundle resources = ResourceBundle.getBundle(
      "org.jr.java2html.resources");
  TemplatePropertyMap properties = Utility.convertTo(resources);
  String templatePath="templates/";
  public static final String INDEX_TEMPLATE = "index.html";
  public static final String ALL_PACKAGES_TEMPLATE = "allpackages.html";
  public static final String ALL_CLASSES_TEMPLATE = "allclasses.html";
  public static final String COPYRIGHT_TEMPLATE = "copyright.html";
  public static final String PACKAGE_TEMPLATE = "package.html";
  public static final String CLASS_TEMPLATE = "class.html";
  public static final String JAVA2HTML_JS = "java2html.js";
  public static final String STYLESHEET_CSS = "stylesheet.css";
  static final String HTML_EXT = ".java.html";
  static JavaToHTML converter = new JavaToHTML(true);
  String code = "gb2312";
  String userPropertiesFile;
  /**
   * 构造一个JavaToHTMLCreator并添加共通的属性。
   */
  public JavaToHTMLCreator() {
    addCommonProperties();
  }
  /**
   * 应用的入口方法。
   * @param args 命令行参数数组
   */
  public static void main(String[] args) {
    if (args.length < 2) {
      System.out.println(
          "Usage:java [-Djava2html.code=code] [-Djava2html.debugable=false/true] org.jr.java2html.JavaToHTMLCreator sourceDir destDir [userPropertiesFile] [templatePath]");
      System.out.println(
          "   or java -jar [-Djava2html.code=code] [-Djava2html.debugable=false/true] java2html.jar sourceDir destDir [userPropertiesFile] [templatePath]");
      System.exit(1);
    }
    JavaToHTMLCreator htmlCreator = new JavaToHTMLCreator();
    String sourcePath = args[0];
    String destPath = args[1];
    if (args.length > 2) {
      htmlCreator.setUserPropertyFile(args[2]);
    }
    if (args.length > 3) {
      htmlCreator.setTemplatePath(args[3]+"/");
    }
    String code=System.getProperty("java2html.code");
    if (code!=null) {
      htmlCreator.setCode(code);
    }
    String debug=System.getProperty("java2html.debugable");
    if (debug!=null) {
      boolean debugable=Boolean.getBoolean(debug);
      Utility.setDebugable(debugable);
    }
    try {
      FileUtil.copy(htmlCreator.templatePath + JAVA2HTML_JS, destPath + "/" + JAVA2HTML_JS, true);
      FileUtil.copy(htmlCreator.templatePath + STYLESHEET_CSS,
                    destPath + "/" + STYLESHEET_CSS, true);
      htmlCreator.createIndexFile(destPath);
      htmlCreator.createCopyrightFile(destPath);
      TreeMap dirs = Utility.analyseDirectory(sourcePath);
      htmlCreator.createAllPackagesFile(destPath, dirs);
      htmlCreator.createAllClassesFile(destPath, dirs);
      htmlCreator.createPackageFiles(destPath, dirs);
      htmlCreator.createClassFiles(sourcePath, destPath, dirs);
    }
    catch (IOException e) {
      e.printStackTrace();
    }
  }
  /**
   * 设置生成的HTML文件的编码方式。
   * @param code 编码方式
   */
  public void setCode(String code) {
    this.code = code;
    properties.put("CODE", code);
  }
  /**
   * 设置用户定义的属性文件的文件名
   * @param fileName 属性文件文件名
   */
  public void setUserPropertyFile(String fileName) {
    userPropertiesFile = fileName;
    Properties userProperties=new Properties();
    try {
      userProperties.load(new FileInputStream(userPropertiesFile));
      Enumeration keys=userProperties.propertyNames();
      while (keys.hasMoreElements()) {
        String key=(String)keys.nextElement();
        String value=userProperties.getProperty(key);
        properties.put(key,value);
      }
    }
    catch (FileNotFoundException fe) {
      System.err.println(fe.getMessage());
    }
    catch (IOException ioe) {
      System.err.println(ioe.getMessage());
    }
  }
  /**
   * 设置模版文件的目录。
   * 因此可以自己修改模版,但是还不能支持动态的设置模版的文件名。
   * 文件名必须和自带的模版的文件名完全一致。
   * @param templatePath 模版目录
   */
  public void setTemplatePath(String templatePath) {
    this.templatePath = templatePath;
  }

  private boolean createIndexFile(String destPath) throws IOException {
    TemplateFile file = new TemplateFile(templatePath + INDEX_TEMPLATE);
    file.setFile(destPath + "/" + INDEX_TEMPLATE);
    file.setProperties(properties);
    return file.createFile();
  }

  private boolean createCopyrightFile(String destPath) throws
      IOException {
    TemplateFile file = new TemplateFile(templatePath +
                                         COPYRIGHT_TEMPLATE);
    file.setFile(destPath + "/" + COPYRIGHT_TEMPLATE);
    file.setProperties(properties);
    return file.createFile();
  }

  private boolean createAllPackagesFile(String destPath,
                                        TreeMap packages) throws
      IOException {
    TemplateFile file = new TemplateFile(templatePath +
                                         ALL_PACKAGES_TEMPLATE);
    file.setFile(destPath + "/" + ALL_PACKAGES_TEMPLATE);
    TemplatePropertyMap loopProperties = new TemplatePropertyMap();
    properties.put(TemplateFile.getLoopName(0), loopProperties);
    int packageCount = packages.size() - 1;
    loopProperties.put(TemplateFile.SIZE, String.valueOf(packageCount));
    Iterator keys = packages.keySet().iterator();
    keys.next(); //跳过全部类对于的那个元素
    ArrayList packageNames = new ArrayList();
    ArrayList packagePaths = new ArrayList();
    while (keys.hasNext()) {
      String packageName = (String) keys.next();
      packageNames.add(packageName);
      packagePaths.add(packageName.replace('.', '/'));
    }
    loopProperties.put("PACKAGE_PATH", packagePaths);
    loopProperties.put("PACKAGE_NAME", packageNames);
    file.setProperties(properties);

    return file.createFile();
  }

  private boolean createAllClassesFile(String destPath, TreeMap packages) throws
      IOException {
    boolean result;
    TemplateFile file = new TemplateFile(templatePath +
                                         ALL_CLASSES_TEMPLATE);
    file.setFile(destPath + "/" + ALL_CLASSES_TEMPLATE);
    TemplatePropertyMap loopProperties = new TemplatePropertyMap();
    properties.put(TemplateFile.getLoopName(0), loopProperties);
    ArrayList fullClassNames = (ArrayList) packages.get(Utility.ALL_CLASSES);
    Collections.sort(fullClassNames, new ClassNameComparator());
    loopProperties.put(TemplateFile.SIZE,
                             String.valueOf(fullClassNames.size()));
    ArrayList packagePaths = new ArrayList();
    ArrayList classFiles = new ArrayList();
    ArrayList classNames = new ArrayList();
    for (int i = 0; i < fullClassNames.size(); i++) {
      String fullClassName = (String) fullClassNames.get(i);
      int lastIndex = fullClassName.lastIndexOf(".");
      if (lastIndex > 0) {
        packagePaths.add(fullClassName.substring(0, lastIndex).replace('.', '/'));
        classFiles.add(fullClassName.substring(lastIndex + 1) + HTML_EXT);
        classNames.add(fullClassName.substring(lastIndex + 1));
      }
      else {
        packagePaths.add(".");
        classFiles.add(fullClassName + HTML_EXT);
        classNames.add(fullClassName);
      }
    }
    loopProperties.put("PACKAGE_PATH", packagePaths);
    loopProperties.put("CLASS_FILE", classFiles);
    loopProperties.put("CLASS_NAME", classNames);
    file.setProperties(properties);
    result = file.createFile();
    return result;
  }

  private boolean createPackageFiles(String destPath, TreeMap packages) throws
      IOException {
    boolean result = true;
    TemplateFile file = new TemplateFile(templatePath +
                                         PACKAGE_TEMPLATE);
    file.setBufferable(true);
    Iterator keys = packages.keySet().iterator();
    keys.next(); //跳过全部类对于的那个元素
    while (keys.hasNext()) {
      String packageName = (String) keys.next();
      String packagePath = packageName.replace('.', '/');
      int packageDeep = 0;
      if (packageName.length() > 0) {
        packageDeep = 1;
      }
      String stylePath = Utility.fill("../",
                                      Utility.getCharCount(packageName, '.') +
                                      packageDeep);
      stylePath = stylePath.substring(0, stylePath.length() - 1);
      properties.put("PACKAGE_TITLE", packageName);
      properties.put("PACKAGE_NAME", packageName);
      properties.put("COMMONFILE_PATH", stylePath);
      file.setFile(destPath + "/" + packagePath + "/" + PACKAGE_TEMPLATE);
      file.setProperties(properties);
      TemplatePropertyMap loopProperties = new TemplatePropertyMap();
      properties.put(TemplateFile.getLoopName(0), loopProperties);
      ArrayList classNames = (ArrayList) packages.get(packageName);
      ArrayList classFiles = new ArrayList();
      for (int i = 0; i < classNames.size(); i++) {
        classFiles.add(classNames.get(i) + HTML_EXT);
      }

      int classCount = classNames.size();
      loopProperties.put(TemplateFile.SIZE, String.valueOf(classCount));
      loopProperties.put("CLASS_FILE", classFiles);
      loopProperties.put("CLASS_NAME", classNames);

      if (!file.createFile()) {
        result = false;
      }
    }
    return result;

  }

  private boolean createClassFiles(String sourcePath, String destPath,
                                   TreeMap packages) throws
      IOException {
    boolean result = true;
    TemplateFile file = new TemplateFile(templatePath +
                                         CLASS_TEMPLATE);
    file.setBufferable(true);
    file.setProperties(properties);
    ArrayList fullClassNames = (ArrayList) packages.get(Utility.ALL_CLASSES);
    for (int i = 0; i < fullClassNames.size(); i++) {
      String fullClassName = (String) fullClassNames.get(i);
      String packagePath;
      String className;
      int lastIndex = fullClassName.lastIndexOf(".");
      if (lastIndex > 0) {
        packagePath = fullClassName.substring(0, lastIndex).replace('.', '/');
        className = fullClassName.substring(lastIndex + 1);
      }
      else {
        packagePath = ".";
        className = fullClassName;
      }
      int packageDeep = 0;
      if (!packagePath.equals(".")) {
        packageDeep = 1;
      }
      String stylePath = Utility.fill("../",
                                      Utility.getCharCount(packagePath, '/') +
                                      packageDeep);
      if (packageDeep != 0) {
        stylePath = Utility.trimLastChar(stylePath);
      }
      else {
        stylePath = ".";
      }
      properties.put("CLASS_TITLE", className);
      properties.put("COMMONFILE_PATH", stylePath);
      BufferedReader reader = new BufferedReader(new FileReader(sourcePath +
          "/" + packagePath + "/" + className + ".java"));
      StringWriter writer = new StringWriter();
      converter.convert(reader, writer);
      properties.put("CLASS_SOURCE", writer.toString());
      file.setFile(destPath + "/" + packagePath + "/" + className +
                   HTML_EXT);

      if (!file.createFile()) {
        result = false;
      }
    }
    return result;

  }

  private void addCommonProperties() {
    properties.put("CREATOR", CREATOR);
    properties.put("CODE", code);
    properties.put("CONTENTS", "JavaToHTMLCreator所生成的源代码。");
  }

}

⌨️ 快捷键说明

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