my objective is to implement an Java to HTML syntax highlighter. The program should
works like this
1) It reads in any .java source code file
2) It parses the file for keywords, strings, numbers, etc.
3) It outputs HTML markup of the source code
4) When you open the resulting HTML in the browser, it is Java with all of the syntax
highlighted.

so i get basic idea what am i supposed to do basically highlight primitive words to html but i have some questions
1. how do i scan each word of a file
-i am thinking of scanning each word at time to see if it is primitive and if it is highlight it
2. if a word is in "" how do highlight the word inside the "".
3. can anyone lay our a structure for me please so i can have a small understanding thanks

Recommended Answers

All 4 Replies

There are lots of people here who will freely give their time to help you become the best Java programmer you can be. There's nobody here who is interested in doing your homework for you.

DaniWeb Member Rules include:
"Do provide evidence of having done some work yourself if posting questions from school or work assignments"
http://www.daniweb.com/forums/faq.php?faq=daniweb_policies
http://www.daniweb.com/forums/announcement8-2.html

I'm not here to reinvent the wheel, so here you go:

  • learn how to upload file through JSP
  • use library such as SyntaxHighlighter
  • work on your project and if you have any problems post back, but in correct forum section

There are lots of people here who will freely give their time to help you become the best Java programmer you can be. There's nobody here who is interested in doing your homework for you.

DaniWeb Member Rules include:
"Do provide evidence of having done some work yourself if posting questions from school or work assignments"
http://www.daniweb.com/forums/faq.php?faq=daniweb_policies
http://www.daniweb.com/forums/announcement8-2.html

I am not asking for answers i know the concept thats y put the questions!!

ok this is what I did so far but it doesnt work anyone know why

//< = &lt;     >=&gt; cos102 slocum h shah
import java.io.*;
import java.util.*;

public class SyntaxHighlighter {
	File javaFile;
	File htmlFile;
	public SyntaxHighlighter(){
		javaFile = new  File("file.java");
		htmlFile = new File("file.html");
	}
	
	public SyntaxHighlighter(File javaFile, File htmlFile){
		javaFile = this.javaFile;
		htmlFile = this.htmlFile;

	}
	public void ScanJavaFile(){
		try {
			PrintWriter writer = new PrintWriter(htmlFile);
			Scanner input = new Scanner(javaFile);
			writer.println("<html>");
			writer.println("<head></head>");
			writer.println("<body>");
			
			
			while (input.hasNextLine()){
				String line = input.nextLine();
				Scanner lineScanner = new Scanner(line);
				writer.println(line + "</br>");
				while(lineScanner.hasNext()){
					String word = lineScanner.next();
					if(isPreserved(word)){
						
					}
					else if(isANumber(word)) {
						
					}
					else if(isAString(word)) {
						
					}
					
				}
				/*
				 * 
				 * This is the meat of the program
				 * 1) read a line
				 * 2) break up that line into "tokens"
				 * 3) scan each token to see if keyword, string or number
				 * 4) if keyword use keyword html
				 * 5) else if number use number html
				 * 6) d
				 */
				
				
			}
			
			writer.println("</body>");
			writer.println("</html>");
		}catch (IOException ioe){
			ioe.printStackTrace();
		}
	}
	public boolean isPreserved(String a) throws IOException{
		PrintWriter writer = new PrintWriter(htmlFile);

		String[] preserved = {"abstract", "assert", "boolean", "break", 
				"byte", "case", "catch", "char", "class", "const*", "continue"
				, "default", "default", "double", "do", "else", "enum", "extends" 
				 , "false", "final", "finally", "float", "for", "goto*", "if", 
				 "implements", "import", "instanceof", "int", "interface", "long"
				 , "native", "new", "null", "package", "private", "protected"
				 , "public", "return", "short", "static", "strictfp", "super", 
				 "switch", "synchronized", "this", "throw", "throws", "transient"
				 , "true", "try", "void", "volatile", "while"};
		for(int i = 0; i <preserved.length; i++){
			if(a.equals(preserved[i])){
				writer.println("<strong>" + preserved[i] + "</strong>");
				return true;
			}
		}
		return false;
	}
	public boolean isANumber(String word) throws IOException{
		PrintWriter writer = new PrintWriter(htmlFile);
		/*
		 * numbers can have 0 - 9
		 * A - F
		 * a - f
		 * and x or X
		 */
		for (int i = 0; i < word.length(); i++){
			if (Character.isDigit(word.charAt(i))){
				//This is a number
				writer.println("<FONT COLOR=\"#cc6600\">" + i + "</FONT>");
				return true;
			}
			else if (word.charAt(i) == 'x' || word.charAt(i) == 'X'){
				writer.println("<FONT COLOR=\"#cc6600\">" + i + "</FONT>");
				return true;
			}
		}
		return false;
	}
	public boolean isAString(String word) throws IOException{
		PrintWriter writer = new PrintWriter(htmlFile);
		// use the '"' or "\"" 
		if(word.equals("\"") ){
			writer.println("<FONT COLOR=\"#33FF33\">" + word + "</FONT>");
			return true;
			
		}
		return false;
	}


	
	
}

and this is the tester

import java.io.*;
import java.util.*;
public class SHTester {
	public static void main(String args[]) throws IOException{
		File javaFile = new File("java.java");
		File htmlFile = new File("html.html");
		SyntaxHighlighter SH = new SyntaxHighlighter(javaFile, htmlFile);
		SH.ScanJavaFile();
		
		
	}

}

i made a random java file so you can create the same

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.