mathajax

Programming Keyword Coloring

To improve presentation of programming code in a post, The keywords of it is coloured by inclusion of css (casecade style sheet) by manually or automatic with help of a tool.

we have coded a simple Java program to carry out colouring Java-keywords using Java regular expression.

programming keyword colouring

Colouring keywords - step by step explanation

The following five steps are used to finish colouring keywords

  1. Keywords Collection
  2. Read Content - Source File
  3. Replace Html special character
  4. Include style sheet
  5. Write Content - Destination File

Read Content - Source File

  • The source file, programming code is read as a string object (content) by FileReader object.

Replace Html special character

  • The source content has html special characters ( < , >, & and etc. ) is to be replaced by the respective string of characters.

Keyword Collection

  • A Array-List object is used to store keywords which are to be coloured.
  • The array of keyword is converted into string keyword regular expression by joining OR operator (|).

Include style sheet

  • The keyword regular expression applied on the source content using Pattern and Matcher object.
  • wherever the matcher object finds keyword match, include style sheet around the keyword.
  • The method returns string content,after inclusion of style sheet.

Write content into destination file

  • The string content is written into destination file by FileWriter object.


Jave Programming Keyword Colouring

 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Keyword {

 public static String Filereader(File file)   
  {           
  byte content[]=null;        
  try {
     FileInputStream fis = new FileInputStream(file);
     int size = fis.available();
     content=new byte[size];  
     fis.read(content, 0, size);
     fis.close();
      } catch (IOException e) {
   e.printStackTrace();
  }         
     return new String(content);     
 }
  
   public static void  Filewriter(String content,File file, String prestr,
                                           String poststr) {      
  
  try {
   FileWriter fwriter=new FileWriter(file);
   fwriter.write(prestr+"rn");
   fwriter.write(content);
   fwriter.write(poststr+"rn");
   fwriter.close();
     } catch (IOException e) {   
    e.printStackTrace();
      }  
 }
 
  public static String replacespchar(String str,String regx,String repstr[]) 
   {
   StringBuffer sb=new StringBuffer();
   Pattern rp= Pattern.compile(regx);   
   Matcher mc= rp.matcher(str);
      
    while ( mc.find() ) {
     
   switch ( mc.group() ) 
   {
     case "&" :
      mc.appendReplacement(sb, repstr[0]);
      break;
      
     case "<" :
      mc.appendReplacement(sb, repstr[1]);
      break;
      
     case ">" :
      mc.appendReplacement(sb, repstr[2]);
      break;
      
     case "\"" :
      mc.appendReplacement(sb, repstr[3]);
      break;         
   }   
    }
   mc.appendTail(sb);
      return sb.toString();
 }

 public static String coloring(String content,String regx,String prestr,
                                                  String poststr) {  
  Pattern p= Pattern.compile(regx);
  Matcher m = p.matcher(content);
    
  StringBuffer sbuf = new StringBuffer();
         while (m.find()) {              
          m.appendReplacement(sbuf, prestr + m.group() + poststr);                      
             }
         m.appendTail(sbuf);
        return sbuf.toString();
 }
 
 
   public static void main(String[] args) {
           
      String srcfile = "BiggestAmong.java";
      String content=Keyword.Filereader(new File(srcfile));
      
      String spc[]= {"&", "<",">","\""};
      String repstr[]= {"&amp;", "&lt;","&gt;","&quot;"};
   String spcregx = String.join("|", spc);   
   content=Keyword.replacespchar(content, spcregx, repstr);
      
    String keyw[]= {"package","import","public", "void","static","class"};
    String keyregx = String.join("|", keyw);      
    String prestr ="<span style="color:#7c0b2f;font-weight: bold;">";
      String poststr = "</span>";     
    content = Keyword.coloring(content, keyregx, prestr, poststr);
      
      
      String quotregx="&quot;.*&quot;|'.'";
      String prestr2 ="<span style="color:#0000ff">";
      String poststr2 = "</span>";
    content = Keyword.coloring(content, quotregx, prestr2, poststr2);
      
      String prestr3 ="<div><pre><code>";
      String poststr3 = "</code></pre></div>";
      String dstfile = "BiggestAmong.html";
      Keyword.Filewriter(content,new File(dstfile),prestr3,poststr3); 
      System.out.println("finished");
 }

}


 

Programming Keyword Colouring Java programming output


 

package cljavabasics;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class BiggestAmong {

 public static void main(String[] args) throws IOException {
    
  InputStreamReader dis =new InputStreamReader ( System.in );
  BufferedReader br =new BufferedReader(dis);
  
     System.out.println("Find a number biggest among 3 numbers n");
     System.out.println("Enter value for A");    
     String  tmp1 =br.readLine();
     int a = Integer.parseInt(tmp1);
     
     
     System.out.println("Enter value for B");    
     String  tmp2 =br.readLine();
     int b = Integer.parseInt(tmp2);
     
     
     System.out.println("Enter value for C");    
     String  tmp3 =br.readLine();
     int c = Integer.parseInt(tmp3);
     
     if ( a>b &&  a>c  )
      System.out.println("A is biggest : " + a);
     else if ( b>c ) 
      System.out.println("B is biggest : " + b);
     else 
      System.out.println("C is biggest : " + c);
 }
}

Comments