import java.util.StringTokenizer;
import datastructures.Stack;
import datastructures.NodeStack;
import java.io.*;
/** Simplified test of matching tags in an HTML document. */
public class HTML {
/** Nested class to store simple HTML tags */
public static class Tag {
String name; // The name of this tag
boolean opening; // Is true iff this is an opening tag
public Tag() { // Default constructor
name = "";
opening = false;
}
public Tag(String nm, boolean op) { // Preferred constructor
name = nm;
opening = op;
}
/** Is this an opening tag? */
public boolean isOpening() { return opening; }
/** Return the name of this tag */
public String getName() {return name; }
}
/** Test if every opening tag has a matching closing tag. */
public boolean isHTMLMatched(Tag[] tag) {
Stack S = new NodeStack(); // Stack for matching tags
for (int i=0; (i<tag.length) && (tag[i] != null); i++) {
if (tag[i].isOpening())
S.push(tag[i].getName()); // opening tag; push its name on the stack
else {
if (S.isEmpty()) // nothing to match
return false;
if (!((String) S.pop()).equals(tag[i].getName())) // wrong match
return false;
}
}
if (S.isEmpty())
return true; // we matched everything
return false; // we have some tags that never were matched
}