001 package com.thaiopensource.relaxng.util;
002
003 import com.oxygenxml.validate.nvdl.util.Log4jChecker;
004 import com.thaiopensource.util.OptionParser;
005 import com.thaiopensource.util.PropertyMapBuilder;
006 import com.thaiopensource.util.Version;
007 import com.thaiopensource.util.Localizer;
008 import com.thaiopensource.validate.Flag;
009 import com.thaiopensource.validate.Option;
010 import com.thaiopensource.validate.OptionArgumentException;
011 import com.thaiopensource.validate.SchemaReader;
012 import com.thaiopensource.validate.ValidateProperty;
013 import com.thaiopensource.validate.ValidationDriver;
014 import com.thaiopensource.validate.auto.AutoSchemaReader;
015 import com.thaiopensource.validate.rng.CompactSchemaReader;
016 import com.thaiopensource.validate.rng.RngProperty;
017 import com.thaiopensource.xml.sax.ErrorHandlerImpl;
018 import org.xml.sax.InputSource;
019 import org.xml.sax.SAXException;
020
021 import java.io.IOException;
022
023 public class Driver {
024 static private String usageKey = "usage";
025
026 static public void setUsageKey(String key) {
027 usageKey = key;
028 }
029
030 static public void main(String[] args) {
031 System.exit(new Driver().doMain(args));
032 }
033
034 private boolean timing = false;
035 private String encoding = null;
036 private Localizer localizer = new Localizer(Driver.class);
037
038 public int doMain(String[] args) {
039 Log4jChecker.checkLog4j();
040 ErrorHandlerImpl eh = new ErrorHandlerImpl(System.out);
041 OptionParser op = new OptionParser("itcdfe:p:", args);
042 PropertyMapBuilder properties = new PropertyMapBuilder();
043 ValidateProperty.ERROR_HANDLER.put(properties, eh);
044 RngProperty.CHECK_ID_IDREF.add(properties);
045 SchemaReader sr = null;
046 boolean compact = false;
047
048 try {
049 while (op.moveToNextOption()) {
050 switch (op.getOptionChar()) {
051 case 'i':
052 properties.put(RngProperty.CHECK_ID_IDREF, null);
053 break;
054 case 'c':
055 compact = true;
056 break;
057 case 'd':
058 {
059 if (sr == null)
060 sr = new AutoSchemaReader();
061 Option option = sr.getOption(SchemaReader.BASE_URI + "diagnose");
062 if (option == null) {
063 eh.print(localizer.message("no_schematron", op.getOptionCharString()));
064 return 2;
065 }
066 properties.put(option.getPropertyId(), Flag.PRESENT);
067 }
068 break;
069 case 't':
070 timing = true;
071 break;
072 case 'e':
073 encoding = op.getOptionArg();
074 break;
075 case 'f':
076 RngProperty.FEASIBLE.add(properties);
077 break;
078 case 'p':
079 {
080 if (sr == null)
081 sr = new AutoSchemaReader();
082 Option option = sr.getOption(SchemaReader.BASE_URI + "phase");
083 if (option == null) {
084 eh.print(localizer.message("no_schematron", op.getOptionCharString()));
085 return 2;
086 }
087 try {
088 properties.put(option.getPropertyId(), option.valueOf(op.getOptionArg()));
089 }
090 catch (OptionArgumentException e) {
091 eh.print(localizer.message("invalid_phase", op.getOptionArg()));
092 return 2;
093 }
094 }
095 break;
096 }
097 }
098 }
099 catch (OptionParser.InvalidOptionException e) {
100 eh.print(localizer.message("invalid_option", op.getOptionCharString()));
101 return 2;
102 }
103 catch (OptionParser.MissingArgumentException e) {
104 eh.print(localizer.message("option_missing_argument",op.getOptionCharString()));
105 return 2;
106 }
107 if (compact)
108 sr = CompactSchemaReader.getInstance();
109 args = op.getRemainingArgs();
110 if (args.length < 1) {
111 eh.print(localizer.message(usageKey, Version.getVersion(Driver.class)));
112 return 2;
113 }
114 long startTime = System.currentTimeMillis();
115 long loadedPatternTime = -1;
116 boolean hadError = false;
117 try {
118 ValidationDriver driver = new ValidationDriver(properties.toPropertyMap(), sr);
119 InputSource in = ValidationDriver.uriOrFileInputSource(args[0]);
120 if (encoding != null)
121 in.setEncoding(encoding);
122 if (driver.loadSchema(in)) {
123 loadedPatternTime = System.currentTimeMillis();
124 for (int i = 1; i < args.length; i++) {
125 if (!driver.validate(ValidationDriver.uriOrFileInputSource(args[i])))
126 hadError = true;
127 }
128 }
129 else
130 hadError = true;
131 }
132 catch (SAXException e) {
133 hadError = true;
134 eh.printException(e);
135 }
136 catch (IOException e) {
137 hadError = true;
138 eh.printException(e);
139 }
140 if (timing) {
141 long endTime = System.currentTimeMillis();
142 if (loadedPatternTime < 0)
143 loadedPatternTime = endTime;
144 eh.print(localizer.message("elapsed_time",
145 new Object[] {
146 new Long(loadedPatternTime - startTime),
147 new Long(endTime - loadedPatternTime),
148 new Long(endTime - startTime)
149 }));
150 }
151 if (hadError)
152 return 1;
153 return 0;
154 }
155
156 }