Exemple #1
0
  /**
   * Highlight stream.
   *
   * @param name the name
   * @param input the input
   * @param rendererName the renderer name
   * @param encoding the encoding
   * @return the string
   * @throws IOException Signals that an I/O exception has occurred.
   */
  public static String highlightStream(
      String name, InputStream input, String rendererName, String encoding) throws IOException {

    Renderer jspRenderer = XhtmlRendererFactory.getRenderer(rendererName);
    if (jspRenderer != null) {
      ByteArrayOutputStream bos = new ByteArrayOutputStream();
      jspRenderer.highlight(name, input, bos, encoding, true);

      ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());

      Tokenizer tokenizer = new Tokenizer(new InputStreamReader(bis, encoding));
      tokenizer.addSymbol(new TokenizerSymbol("EOL", "\n", null, false, false, true, false));
      tokenizer.addSymbol(new TokenizerSymbol("EOL", "\r\n", null, false, false, true, false));

      //
      // JHighlight adds HTML comment as the first line, so if
      // we number the lines we could end up with a line number and no line
      // to avoid that we just ignore the first line alltogether.
      //
      StringBuilder buffer = new StringBuilder();
      long counter = 0;
      while (tokenizer.hasMore()) {
        Token tk = tokenizer.nextToken();
        if ("EOL".equals(tk.getName())) {
          counter++;
          buffer.append(tk.getText());
        } else if (counter > 0) {
          buffer.append("<span class=\"codeline\">");
          buffer.append("<span class=\"linenum\">");
          buffer.append(leftPad(Long.toString(counter), 6, "&nbsp;"));
          buffer.append("</span>");
          buffer.append(tk.getText());
          buffer.append("</span>");
        }
      }
      return buffer.toString();
    }
    return null;
  }
Exemple #2
0
  /**
   * Gets the jsp encoding.
   *
   * @param is the is
   * @return the jsp encoding
   * @throws IOException Signals that an I/O exception has occurred.
   */
  public static String getJspEncoding(InputStream is) throws IOException {

    String encoding = null;
    String contentType = null;

    Tokenizer jspTokenizer = new Tokenizer();
    jspTokenizer.addSymbol("\n", true);
    jspTokenizer.addSymbol(" ", true);
    jspTokenizer.addSymbol("\t", true);
    jspTokenizer.addSymbol(new TokenizerSymbol("dir", "<%@", "%>", false, false, true, false));

    StringTokenizer directiveTokenizer = new StringTokenizer();
    directiveTokenizer.addSymbol("\n", true);
    directiveTokenizer.addSymbol(" ", true);
    directiveTokenizer.addSymbol("\t", true);
    directiveTokenizer.addSymbol("=");
    directiveTokenizer.addSymbol("\"", "\"", false);
    directiveTokenizer.addSymbol("'", "'", false);

    StringTokenizer contentTypeTokenizer = new StringTokenizer();
    contentTypeTokenizer.addSymbol(" ", true);
    contentTypeTokenizer.addSymbol(";", true);

    try (Reader reader = new InputStreamReader(is, "ISO-8859-1")) {
      jspTokenizer.setReader(reader);
      while (jspTokenizer.hasMore()) {
        Token token = jspTokenizer.nextToken();
        if ("dir".equals(token.getName())) {
          directiveTokenizer.setString(token.getInnerText());
          if (directiveTokenizer.hasMore()
              && directiveTokenizer.nextToken().getText().equals("page")) {
            while (directiveTokenizer.hasMore()) {
              Token directiveToken = directiveTokenizer.nextToken();
              if ("pageEncoding".equals(directiveToken.getText())) {
                if (directiveTokenizer.hasMore()
                    && "=".equals(directiveTokenizer.nextToken().getText())) {
                  if (directiveTokenizer.hasMore()) {
                    encoding = directiveTokenizer.nextToken().getInnerText();
                    break;
                  }
                }
              } else if ("contentType".equals(directiveToken.getText())) {
                if (directiveTokenizer.hasMore()
                    && "=".equals(directiveTokenizer.nextToken().getText())) {
                  if (directiveTokenizer.hasMore()) {
                    contentType = directiveTokenizer.nextToken().getInnerText();
                  }
                }
              }
            }
          }
        }
      }
    }

    if (encoding == null && contentType != null) {
      contentTypeTokenizer.setString(contentType);
      while (contentTypeTokenizer.hasMore()) {
        String token = contentTypeTokenizer.nextToken().getText();
        if (token.startsWith("charset=")) {
          encoding = token.substring("charset=".length());
          break;
        }
      }
    }

    return encoding != null ? encoding : "ISO-8859-1";
  }