package dk.brics.dsd;
import dk.brics.jwig.analysis.summarygraph.*;
import org.jdom.*;
import org.jdom.output.*;
import java.util.*;
import java.io.*;
import java.net.*;
/**
* DSD 2.0 command-line validator.
* This is a simple application of the DSD 2.0 validation API.
* (See the source code DSD.java for details.)
* @author Anders Møller
*/
public class DSD
{
public static void main(String[] args)
{
MyHandler handler = new MyHandler();
Validator validator = new Validator(handler);
XMLOutputter outputter = new XMLOutputter("", false, "ISO-8859-1");
String appname = null, dsdname = null;
Document app = null, dsd = null, res = null;
long parsing, validating = 0;
try {
int i = 0;
if (args.length>i && !args[i].equals("--help")) {
if (args.length>i)
appname = args[i++];
if (args.length>i)
dsdname = args[i++];
}
if (args.length==0 || args.length>i || appname==null) {
System.out.println("DSD 2.0 Validator\n" +
"Copyright (C) 2002 BRICS\n\n" +
"Usage: dsd " +
"[ ]\n\n" +
"See http://www.brics.dk/DSD/ for more information " +
"about DSD 2.0.");
System.exit(-1);
}
parsing = System.currentTimeMillis();
File cwd = new File(System.getProperty("user.dir"));
URL context = cwd.toURL();
URL appurl = new URL(context, appname);
System.err.println("Loading instance document " + appname + "...");
app = validator.makeDocument(appurl);
if (dsdname!=null) {
System.err.println("Loading schema document " + dsdname + "...");
dsd = validator.makeDocument(new URL(context, dsdname));
}
else {
dsdname = validator.findDSDref(app);
if (dsdname == null) {
System.err.println("Error: no DSD specified.");
System.exit(-1);
}
System.err.println("Loading schema document " + dsdname + "...");
dsd = validator.makeDocument(new URL(appurl, dsdname));
}
try {
System.err.println("Parsing schema...");
Schema dsddoc = new Schema(dsd);
validating = System.currentTimeMillis();
System.err.println("Validating...");
res = validator.process(app, dsddoc);
if (handler.errors==0)
System.err.println("Document is valid!");
else {
System.err.println(handler.errors + " error" + (handler.errors>1?"s":"") +
" detected. See output document for details.");
}
outputter.output(res, System.out);
} catch (SchemaErrorException e) {
System.err.println("Fatal error in the DSD document.\n" +
"Try validating it with the meta-DSD: " +
"http://www.brics.dk/DSD/dsd2.dsd\n");
e.printStackTrace();
System.exit(-1);
}
System.err.println("[loading+parsing time: " + (validating-parsing) + "ms, " +
"validating time: " + (System.currentTimeMillis()-validating) + "ms]");
} catch (JDOMException e) {
System.err.println(e.getMessage());
System.exit(-1);
} catch (MalformedURLException e) {
System.err.print("Malformed URL: ");
System.err.println(e.getMessage());
System.exit(-1);
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}
System.exit(0);
}
}
class MyHandler implements ValidationErrorHandler
{
int errors = 0;
public boolean error(Node node, Element element, String message, Element dsd)
{
if (errors==0)
System.err.println("Document is invalid!");
System.err.println("Error: " + message);
errors++;
return true;
}
}