Remove Comment : Document HTML « Development Class « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Development Class » Document HTML 




Remove Comment
        


//package org.zkoss.maven.yuicompressor.util;


public class Comments {
//
//  public static String readFile(String file) {
//    try {
//      Scanner scanner = new Scanner(new FileReader(new File(file)));
//      StringBuffer sb = new StringBuffer();
//      while (scanner.hasNextLine()) {
//        sb.append(scanner.nextLine()+"\r\n");
//        
//      }
//      return sb.toString();
//    } catch (Exception e) {
//      return "";
//    }
//  }
//
//  public static void main(String[] args) {
//
//    System.out.println(removeComment(readFile("skin.js")));
//    
//  }

  public static String removeComment(String input) {

    StringBuffer sb = new StringBuffer(input);
    char NQ = ' ', quote = NQ;
    int len = sb.length();
    for (int j = 0, lineno = 1; j < len; j++) {
      if (sb.charAt(j== '\n')
        ++lineno;

      if (quote != NQ) {
        if (sb.charAt(j== quote){
          quote = NQ;
        }else if (sb.charAt(j== '\\') {
          j++;
          //fix for  "123\\\r\n123" 
          if(sb.charAt(j)=='\r'j++;
          // if(sb.charAt(j) == '\n') j++;
        else if (sb.charAt(j== '\n') {
          throw new IllegalStateException("Unterminated string at line " + lineno);
        }
      else if (sb.charAt(j== '/' && j + < len && (sb.charAt(j + 1== '*' || sb.charAt(j + 1== '/')) {
        int l = j;
        boolean eol = sb.charAt(++j== '/';
        while (++j < len) {
          if (sb.charAt(j== '\n')
            ++lineno;

          if (eol) {
            if (sb.charAt(j== '\n') {
              sb.delete(l, sb.charAt(j - 1== '\r' ? j - : j);
              len = sb.length();
              j = l;
              break;
            }
          else if (sb.charAt(j== '*' && j + < len && sb.charAt(j + 1== '/') {
            sb.delete(l, j + 2);
            len = sb.length();
            j = l;
            break;
          }
        }
      else if (sb.charAt(j== '\'' || sb.charAt(j== '"') {
        quote = sb.charAt(j);
      else if (sb.charAt(j== '/') { // regex
        boolean regex = false;
        for (int k = j;;) {
          if (--k < 0) {
            regex = true;
            break;
          }

          char ck = sb.charAt(k);
          if (!Character.isWhitespace(ck)) {
            regex = ck == '(' || ck == ',' || ck == '=' || ck == ':' || ck == '?' || ck == '{' || ck == '['
                || ck == ';' || ck == '!' || ck == '&' || ck == '|' || ck == '^'
                || (ck == 'n' && k > && "return".equals(sb.substring(k - 5, k + 1)))
                || (ck == 'e' && k > && "case".equals(sb.substring(k - 3, k + 1)));
            break;
          }
        }
        if (regex) {
          while (++j < len && sb.charAt(j!= '/') {
            if (sb.charAt(j== '\\')
              j++;
            else if (sb.charAt(j== '\n') {
              throw new IllegalStateException("Unterminated regex at line " + lineno);
            }
          }
        }
      }
    }
    return sb.toString();
  }

}

   
    
    
    
    
    
    
    
  














Related examples in the same category
1.HTMLDocument: Element Iterator Example
2.HTMLEditorKit DemoHTMLEditorKit Demo
3.SimpleAttributeSet ExampleSimpleAttributeSet Example
4.Text Tab SampleText Tab Sample
5.Styled DocumentStyled Document
6.Html utils for working with tag's names and attributes.
7.Escape HTML
8.Escape HTML
9.Encode HTML
10.Replace all the occurences of HTML escape strings with the respective characters.
11.HTML Rewriter
12.HTML Encode
13.XMLWriter is a generic class that provides common behavior to writers of a tagged language such as XML, WordML and HTML.
14.Escape html entities.
15.Html Encoder
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.