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.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » Development Class » Document HTMLScreenshots 
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.