import java.util.regex.*;
import java.io.*;
import java.util.ArrayList;

/**
 * XMLChecker.java
 * @author Eric Alexander
 *
 * This class is used to validate XML.
 * Uses a stack to make sure XML tags are properly nested.
 * 
 * Usage: java XMLChecker filename
 */
public class XMLChecker {
	
	public static void main(String[] args) throws FileNotFoundException, IOException {
		// Make sure we are given correct number of arguments
		if (args.length != 1) {
			System.out.println("Usage: java XMLChecker filename.xml");
			return;
		}
		
		// Read file, store as string (xmlString)
		BufferedReader in = new BufferedReader(new FileReader(args[0]));
		String xmlString = "";
		String line = in.readLine();
		while (line != null) {
			xmlString = xmlString + line;
			line = in.readLine();
		}
		in.close();
		
		// Extract XML tags using a regular expression - store in tagList
		Pattern regex = Pattern.compile("<.[^>]*>");
		Matcher matcher = regex.matcher(xmlString);
		
		ArrayList<String> tagList = new ArrayList<String>();
		while (matcher.find()) {
			tagList.add(matcher.group());
		}
		
		// Use a stack to check the well-formedness of the XML.
		// YOUR CODE SHOULD REPLACE THIS
		System.out.println(tagList);
	}
}
