import org.jdom.*;
import org.jdom.input.SAXBuilder;
import java.util.*;
public class XCount {
public static void main(String[] args) {
if (args.length == 0) {
System.out.println("Usage: java XCount URL1 URL2...");
}
SAXBuilder builder = new SAXBuilder();
System.out.println(
"File\tElements\tAttributes\tComments\tProcessing Instructions\tCharacters");
// start parsing...
for (int i = 0; i < args.length; i++) {
// command line should offer URIs or file names
try {
Document doc = builder.build(args[i]);
System.out.print(args[i] + ":\t");
String result = count(doc);
System.out.println(result);
}
// indicates a well-formedness or other error
catch (JDOMException e) {
System.out.println(args[i]
+ " is not a well formed XML document.");
System.out.println(e.getMessage());
}
}
}
private static int numCharacters = 0;
private static int numComments = 0;
private static int numElements = 0;
private static int numAttributes = 0;
private static int numProcessingInstructions = 0;
public static String count(Document doc) {
numCharacters = 0;
numComments = 0;
numElements = 0;
numAttributes = 0;
numProcessingInstructions = 0;
List children = doc.getContent();
Iterator iterator = children.iterator();
while (iterator.hasNext()) {
Object o = iterator.next();
if (o instanceof Element) {
numElements++;
count((Element) o);
}
else if (o instanceof Comment) numComments++;
else if (o instanceof ProcessingInstruction) {
numProcessingInstructions++;
}
}
String result = numElements + "\t" + numAttributes + "\t"
+ numComments + "\t" + numProcessingInstructions + "\t"
+ numCharacters;
return result;
}
public static void count(Element element) {
List attributes = element.getAttributes();
numAttributes += attributes.size();
List children = element.getContent();
Iterator iterator = children.iterator();
while (iterator.hasNext()) {
Object o = iterator.next();
if (o instanceof Element) {
numElements++;
count((Element) o);
}
else if (o instanceof Comment) numComments++;
else if (o instanceof ProcessingInstruction) {
numProcessingInstructions++;
}
else if (o instanceof Text) {
Text t = (Text) o;
String s = t.getText();
numCharacters += s.length();
}
else if (o instanceof CDATA) {
CDATA c = (CDATA) o;
String s = c.getText();
numCharacters += s.length();
}
}
}
}