public final static int CAPACITY = 1000; // Tag array size upper bound
/* Parse an HTML document into an array of html tags */
public Tag[] parseHTML(BufferedReader r)
throws IOException {
String line; // a line of text
boolean inTag = false; // true iff we are in a tag
Tag[] tag = new Tag[CAPACITY]; // our tag array (initially all null)
int count = 0; // tag counter
while ((line = r.readLine()) != null) {
// Create a string tokenizer for HTML tags (use < and > as delimiters)
StringTokenizer st = new StringTokenizer(line,"<> \t",true);
while (st.hasMoreTokens()) {
String token = (String) st.nextToken();
if (token.equals("<")) // opening a new HTML tag
inTag = true;
else if (token.equals(">")) // ending an HTML tag
inTag = false;
else if (inTag) { // we have a opening or closing HTML tag
if ( (token.length() == 0) || (token.charAt(0) != '/') )
tag[count++] = new Tag(token, true); // opening tag
else // ending tag
tag[count++] = new Tag(token.substring(1), false); // skip the '/'
} // Note: we ignore anything not in an HTML tag
}
}
return tag; // our array of tags
}
/** Tester method */
public static void main(String[] args) throws IOException {
BufferedReader stdr; // Standard Input Reader
stdr = new BufferedReader(new InputStreamReader(System.in));
HTML tagChecker = new HTML();
if (tagChecker.isHTMLMatched(tagChecker.parseHTML(stdr)))
System.out.println("The input file is a matched HTML document.");
else
System.out.println("The input file is not a matched HTML document.");
}
}