import java.util.Comparator; /** * A class to compare two HTML tags: * Begin tags are ordered alphabetically ( is before ) * End tags are ordered alphabetically ( is before ) * A begin tag is before an end tag ( is before ) * The comparison is case insensitive */ public class HTMLTagComparator implements Comparator { /** * Compare two tags (assume that the 2 objects are tags) */ public int compare(Object obj1, Object obj2) { String tag1 = (String)obj1; String tag2 = (String)obj2; tag1 = tag1.toUpperCase(); tag2 = tag2.toUpperCase(); // 2 begin tags if (tag1.charAt(1)!='/' && tag2.charAt(1)!='/') return tag1.compareTo(tag2); // 2 end tags else if (tag1.charAt(1)=='/' && tag2.charAt(1)=='/') return tag1.compareTo(tag2); // tag1 is a begin tag and tag2 an end tag else if (tag1.charAt(1)!='/' && tag2.charAt(1)=='/') return -1; // tag1 is an end tag and tag2 a begin tag else return +1; } /** * Compare this Comparator to obj */ public boolean equals(Object obj) { return super.equals(obj); } }