import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Stack;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class WellFormdness {
public static void main(String[] args) {
String filename = args[0];
String xmlString = "";
Stack stack = new Stack();
try {
BufferedReader br = new BufferedReader(new FileReader(filename));
String line = "";
while ((line = br.readLine()) != null) {
xmlString = xmlString+line;
}
//strip comments from the XML string
Pattern comm_p = Pattern.compile("");
Matcher comm_m = comm_p.matcher(xmlString);
xmlString = comm_m.replaceAll("");
Pattern pattern = Pattern.compile("<[a-zA-Z\\-_][a-zA-Z0-9\\-_]*:[a-zA-Z\\-_][a-zA-Z0-9\\-_]*\t*|<[a-zA-Z\\-_][a-zA-Z0-9\\-_]*\t*|/>|[a-zA-Z\\-_][a-zA-Z0-9\\-_]*:[a-zA-Z\\-_][a-zA-Z0-9\\-_]*>|[a-zA-Z\\-_][a-zA-Z0-9\\-_]*>");
Matcher matcher = pattern.matcher(xmlString);
boolean isWellFormed = true;
while (matcher.find()) {
String group = matcher.group();
String tagName = "";
if (group.startsWith("<")) {
tagName = group.substring(1);
stack.push(tagName);
}
else if (group.endsWith("/>")) {
if (!stack.empty()) {
stack.pop();
}
}
else if (group.endsWith(">")) {
tagName = group.substring(0, group.length()-1);
String stackTop = (String)stack.pop();
if (!tagName.equals(stackTop)) {
isWellFormed = false;
break;
}
}
}
if (isWellFormed == true) {
System.out.println("XML document is well formed");
}
else {
System.out.println("XML document is not well formed");
}
}
catch(FileNotFoundException ex) {
ex.printStackTrace();
}
catch(IOException ex) {
ex.printStackTrace();
}
}
}