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

📄 javatohtml.java

📁 JR 公共类库 --Java研究组织开源子项目 本项目的发起的初衷是丰富和完善java的类库
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
          switch (nextChar) {
            //如果下一个字符是'*'则表明是多行注释,将类型设置为多行字符并添加对应的字体颜色及字符
            case '*':
              currentType = COMMENT;
              lineBuffer.append(commentStyle.getBegin());
              lineBuffer.append(contents[i]);
              continue;
              //如果下一个字符是'/'则表明是单行注释,添加对应的字体颜色并直接取字符串的剩余内容
            case '/':
              lineBuffer.append(commentStyle.getBegin());
              lineBuffer.append(line.substring(i));
              lineBuffer.append(commentStyle.getEnd());
              return lineBuffer.toString();
              //不是注释的开始则直接添加内容
            default:
              lineBuffer.append(contents[i]);
              continue;
          }
        }
        //如果是双引号
        else if (contents[i] == '\"') {
          //如果是最开始的位置或者其前一个字符不是'\'则表明是一个引用的开始,设置类型并添加对应的字体颜色及字符
          if (i == 0 || contents[i - 1] != '\\') {
            currentType = QUOTE;
            lineBuffer.append(quoteStyle.getBegin());
            lineBuffer.append(contents[i]);
            continue;
          }
          //否则不是引用的内容,添加内容
          else {
            lineBuffer.append(contents[i]);
            continue;
          }
        }
        //如果不是以上情况则表明是其他的杂项内容,不进行处理,将字符添加到缓冲区
        else {
          lineBuffer.append(contents[i]);
          continue;
        }
      }
      //如果当前类型是单词
      else if (currentType == WORD) {
        //如果是一个java标识符的合法部分表明单词没有结束,将当前字符添加到单词缓冲区
        if (Character.isJavaIdentifierPart(contents[i])) {
          wordBuffer.append(contents[i]);
          continue;
        }
        //否则表明单词结束,将单词缓冲区的内容进行转换并添加到缓冲区并继续判断后面部分的语法成分
        else {
          lineBuffer.append(convertWord(wordBuffer.toString()));
          wordBuffer = new StringBuffer();
          //如果是可能的注释开始
          if (contents[i] == '/') {
            char nextChar = contents[i + 1];
            switch (nextChar) {
              //如果下一个字符是'*'则表明是多行注释,将类型设置为多行字符并添加对应的字体颜色及字符
              case '*':
                currentType = COMMENT;
                lineBuffer.append(commentStyle.getBegin());
                lineBuffer.append(contents[i]);
                continue;
                //如果下一个字符是'/'则表明是单行注释,添加对应的字体颜色并直接取字符串的剩余内容
              case '/':
                lineBuffer.append(commentStyle.getBegin());
                lineBuffer.append(line.substring(i));
                lineBuffer.append(commentStyle.getEnd());
                currentType = NONTYPE;
                return lineBuffer.toString();
            }
          }
          //如果是双引号则表明是一个引用的开始,设置类型并添加对应的字体颜色及字符
          else if (contents[i] == '\"') {
            currentType = QUOTE;
            lineBuffer.append(quoteStyle.getBegin());
            lineBuffer.append(contents[i]);
            continue;
          }
          //如果不是以上情况则表明是其他的杂项内容,不进行处理,将字符添加到缓冲区
          else {
            currentType = NONTYPE;
            lineBuffer.append(contents[i]);
            continue;
          }
        }
      }
      //如果当前类型是引用
      else if (currentType == QUOTE) {
        //如果当前字符不是引号或者如果是引号但是他的前一个字符是'\'则表明是引用的内容,添加内容
        if (contents[i] != '\"' || contents[i - 1] == '\\') {
          lineBuffer.append(contents[i]);
          continue;
        }
        //否则表明是引用的结束,添加内容并结束字体设置
        else {
          lineBuffer.append(contents[i]);
          lineBuffer.append(quoteStyle.getEnd());
          currentType = NONTYPE;
          continue;
        }
      }
      //如果当前类型是多行注释
      else if (currentType == COMMENT) {
        //如果是第一个字符则表明是多行注释的继续的一行,添加字体设置
        if (i == 0) {
          lineBuffer.append(commentStyle.getBegin());
        }
        //如果当前字符是'/'并且他的前一个字符是'*'则表明是注释的结束,添加内容并结束字体设置
        if (contents[i] == '/' && contents[i - 1] == '*') {
          lineBuffer.append(contents[i]);
          lineBuffer.append(commentStyle.getEnd());
          currentType = NONTYPE;
          continue;
        }
        //否则表明不是注释的结束,添加内容
        else {
          lineBuffer.append(contents[i]);
          continue;
        }
      }
    }
    //对最后一个字符进行处理并结束本行的转换
    char endChar = contents[end];
    //如果是没有任何类型,最后一个字符不能构成任何特殊类,直接添加内容
    if (currentType == NONTYPE) {
      lineBuffer.append(endChar);
      return lineBuffer.toString();
    }
    //如果是单词
    else if (currentType == WORD) {
      //如果最后一个字符是标识符的一部分,添加内容并结束风格设置
      if (Character.isJavaIdentifierPart(endChar)) {
        wordBuffer.append(endChar);
        lineBuffer.append(convertWord(wordBuffer.toString()));
        wordBuffer = new StringBuffer();
      }
      //否则表明单词已经结束,转换单词后作为普通内容添加。
      else {
        lineBuffer.append(convertWord(wordBuffer.toString()));
        wordBuffer = new StringBuffer();
        lineBuffer.append(endChar);
      }
      currentType = NONTYPE;
      return lineBuffer.toString();
    }
    //如果是引用,由于字符串不能跨行,因此结束字符串引用不会有问题。
    else if (currentType == QUOTE) {
      lineBuffer.append(endChar);
      lineBuffer.append(quoteStyle.getEnd());
      currentType = NONTYPE;
      return lineBuffer.toString();
    }
    else {
      //如果最后一个字符是'/'并且他的前一个字符是'*'则表明是注释的结束,添加内容并结束字体设置
      if (endChar == '/' && contents[end - 1] == '*') {
        lineBuffer.append(endChar);
        lineBuffer.append(commentStyle.getEnd());
        currentType = NONTYPE;
      }
      //否则表明不是注释的结束,添加内容
      else {
        lineBuffer.append(endChar);
        lineBuffer.append(commentStyle.getEnd());
      }
      return lineBuffer.toString();
    }

  }

  /**
   * 将指定的缓冲阅读器中的内容读出进行转换并将转换后的结果写入记录器。
   * 之所以需要缓冲阅读器是因为在内部是以行为单位进行处理的。
   * @param reader 阅读器
   * @param writer 记录器
   * @since  0.5
   */
  public void convert(BufferedReader reader, Writer writer) {
    try {
      writer.write("<pre>");
      writeLineNumber(writer, HEAD);
      String line = reader.readLine();
      while (line != null) {
        writeLineNumber(writer, LINE);
        writer.write(convertLine(escapeHTMLtag(line)));
        writer.write(Constants.LINE_SEPARATOR);
        line = reader.readLine();
      }
      writeLineNumber(writer, END);
      writer.write("</pre>");
    }
    catch (IOException e) {

    }
  }

  /**
   * 记录器写入行号控制部分。
   * @param writer 记录器
   * @param type 行号控制类型
   * @throws IOException
   */
  private void writeLineNumber(Writer writer, int type) throws IOException {
    if (showLineNumber == true) {
      switch (type) {
        case HEAD:
          writer.write("<ol>");
          break;
        case LINE:
          writer.write("<li>");
          break;
        case END:
          writer.write("</ol>");
          break;
      }
    }
  }
}

⌨️ 快捷键说明

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