// RemoveHTMLComments.java (c) Kari Laitinen
// http://www.naturalprogramming.com
// 2014-05-22 File created.
// 2014-05-22 Last modification.
/*
The 'better' regex in the following page did not work:
http://stackoverflow.com/questions/1657066/java-regular-expression-finding-comments-in-code
http://stackoverflow.com/questions/9205988/writing-a-java-program-to-remove-the-comments-in-same-java-program
http://ostermiller.org/findcomment.html
*/
import java.io.* ;
import java.util.* ;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class RemoveHTMLComments
{
public static void main( String[] not_in_use )
{
Scanner keyboard = new Scanner( System.in ) ;
System.out.print( "\n This program removes comments from a file."
+ "\n Give a file name: " ) ;
String file_name_from_user = "TestFile.html" ; // keyboard.nextLine() ;
StringBuilder file_as_stringbuilder = new StringBuilder() ;
try
{
BufferedReader file_to_print =
new BufferedReader( new FileReader( file_name_from_user ) ) ;
int line_counter = 0 ;
boolean end_of_file_encountered = false ;
while ( end_of_file_encountered == false )
{
String text_line_from_file = file_to_print.readLine() ;
if ( text_line_from_file == null )
{
end_of_file_encountered = true ;
}
else
{
line_counter ++ ;
file_as_stringbuilder.append( text_line_from_file + "\n" ) ;
}
}
file_to_print.close() ;
System.out.print( "\n " + line_counter + " lines processed.\n" ) ;
// we'll remove the in a very crude way
int index_of_comment_start = file_as_stringbuilder.indexOf( "" ) ;
while( index_of_comment_start != -1 &&
index_of_comment_end != -1 )
{
file_as_stringbuilder.replace( index_of_comment_start,
index_of_comment_end + 3,
" " ) ;
index_of_comment_start = file_as_stringbuilder.indexOf( "" ) ;
}
String file_as_string = file_as_stringbuilder.toString() ;
System.out.print( "" + file_as_string ) ;
}
catch ( FileNotFoundException caught_file_not_found_exception )
{
System.out.print( "\n \"" + file_name_from_user + "\" not found.\n" ) ;
}
catch ( IOException caught_io_exception )
{
System.out.print( "\n\n File reading error. \n" ) ;
}
}
}