Need help in completing this code
Hi, I have written a code which will identify all the supposedly HTML Tags in the input string & put them in new string e.g. ABC.
[ code] package com.test;
import java.util.ArrayList;
class Test {
/**
* @param args
*/
public static void main(String[] args) {
String Demo = "<h1>This is a string </h1>that we </br> want to ttest";
try{
ArrayList result = token(Demo );
// System.out.println("Hi");
for(int i=0; i<result.size();i++)
System.out.println("" + result.get(i));
}
catch(Exception e){
System.out.println("Wrong size of token");
}
}
public static ArrayList token(String test) throws Exception
{
ArrayList result= new ArrayList();
StringBuffer tt = new StringBuffer();
boolean flag =false;
Exception e = new Exception();
for(int i=0; i<test.length();i++)
{
// System.out.println("flag "+flag);
if(test.charAt(i)=='<')
{
//System.out.println("in "+test.charAt(i));
tt = new StringBuffer();
flag= true;
continue;
}
if(test.charAt(i)=='>')
{
flag=false;
if(test.length()!=0)
{
result.add(tt.toString());
if(tt.length()>6)
{
throw new Exception(e);
}
tt = new StringBuffer();
}
continue;
}
if(flag)
{
tt.append(String.valueOf(test.charAt(i)));
//System.out.println("tt "+tt.toString());
}
}
return result;
}
} [ /code]
Now I want to define a allowable set of HTML tags either as a hashmap or Set and then compare if the generated string ABC is part of this allowable set. If not then throw a validation error.
Allowable set will contain tags like <b>,<strong>,<p>,<br> and this code should allow input text to contain tags only within this set and not allow tags like <javascript>