KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thaiopensource > relaxng > util > Driver


1 package com.thaiopensource.relaxng.util;
2
3 import com.thaiopensource.util.OptionParser;
4 import com.thaiopensource.util.PropertyMapBuilder;
5 import com.thaiopensource.util.Version;
6 import com.thaiopensource.util.Localizer;
7 import com.thaiopensource.validate.Flag;
8 import com.thaiopensource.validate.Option;
9 import com.thaiopensource.validate.OptionArgumentException;
10 import com.thaiopensource.validate.SchemaReader;
11 import com.thaiopensource.validate.ValidateProperty;
12 import com.thaiopensource.validate.ValidationDriver;
13 import com.thaiopensource.validate.auto.AutoSchemaReader;
14 import com.thaiopensource.validate.rng.CompactSchemaReader;
15 import com.thaiopensource.validate.rng.RngProperty;
16 import com.thaiopensource.xml.sax.ErrorHandlerImpl;
17 import org.xml.sax.InputSource JavaDoc;
18 import org.xml.sax.SAXException JavaDoc;
19
20 import java.io.IOException JavaDoc;
21
22 class Driver {
23   static private String JavaDoc usageKey = "usage";
24
25   static public void setUsageKey(String JavaDoc key) {
26     usageKey = key;
27   }
28
29   static public void main(String JavaDoc[] args) {
30     System.exit(new Driver().doMain(args));
31   }
32
33   private boolean timing = false;
34   private String JavaDoc encoding = null;
35   private Localizer localizer = new Localizer(Driver.class);
36
37   public int doMain(String JavaDoc[] args) {
38     ErrorHandlerImpl eh = new ErrorHandlerImpl(System.out);
39     OptionParser op = new OptionParser("itcdfe:p:", args);
40     PropertyMapBuilder properties = new PropertyMapBuilder();
41     ValidateProperty.ERROR_HANDLER.put(properties, eh);
42     RngProperty.CHECK_ID_IDREF.add(properties);
43     SchemaReader sr = null;
44     boolean compact = false;
45
46     try {
47       while (op.moveToNextOption()) {
48         switch (op.getOptionChar()) {
49         case 'i':
50           properties.put(RngProperty.CHECK_ID_IDREF, null);
51           break;
52         case 'c':
53           compact = true;
54           break;
55         case 'd':
56           {
57             if (sr == null)
58               sr = new AutoSchemaReader();
59             Option option = sr.getOption(SchemaReader.BASE_URI + "diagnose");
60             if (option == null) {
61               eh.print(localizer.message("no_schematron", op.getOptionCharString()));
62               return 2;
63             }
64             properties.put(option.getPropertyId(), Flag.PRESENT);
65           }
66           break;
67         case 't':
68           timing = true;
69           break;
70         case 'e':
71           encoding = op.getOptionArg();
72           break;
73         case 'f':
74           RngProperty.FEASIBLE.add(properties);
75           break;
76         case 'p':
77           {
78             if (sr == null)
79               sr = new AutoSchemaReader();
80             Option option = sr.getOption(SchemaReader.BASE_URI + "phase");
81             if (option == null) {
82               eh.print(localizer.message("no_schematron", op.getOptionCharString()));
83               return 2;
84             }
85             try {
86               properties.put(option.getPropertyId(), option.valueOf(op.getOptionArg()));
87             }
88             catch (OptionArgumentException e) {
89               eh.print(localizer.message("invalid_phase", op.getOptionArg()));
90               return 2;
91             }
92           }
93           break;
94         }
95       }
96     }
97     catch (OptionParser.InvalidOptionException e) {
98       eh.print(localizer.message("invalid_option", op.getOptionCharString()));
99       return 2;
100     }
101     catch (OptionParser.MissingArgumentException e) {
102       eh.print(localizer.message("option_missing_argument",op.getOptionCharString()));
103       return 2;
104     }
105     if (compact)
106       sr = CompactSchemaReader.getInstance();
107     args = op.getRemainingArgs();
108     if (args.length < 1) {
109       eh.print(localizer.message(usageKey, Version.getVersion(Driver.class)));
110       return 2;
111     }
112     long startTime = System.currentTimeMillis();
113     long loadedPatternTime = -1;
114     boolean hadError = false;
115     try {
116       ValidationDriver driver = new ValidationDriver(properties.toPropertyMap(), sr);
117       InputSource JavaDoc in = ValidationDriver.uriOrFileInputSource(args[0]);
118       if (encoding != null)
119         in.setEncoding(encoding);
120       if (driver.loadSchema(in)) {
121         loadedPatternTime = System.currentTimeMillis();
122     for (int i = 1; i < args.length; i++) {
123       if (!driver.validate(ValidationDriver.uriOrFileInputSource(args[i])))
124         hadError = true;
125     }
126       }
127       else
128     hadError = true;
129     }
130     catch (SAXException JavaDoc e) {
131       hadError = true;
132       eh.printException(e);
133     }
134     catch (IOException JavaDoc e) {
135       hadError = true;
136       eh.printException(e);
137     }
138     if (timing) {
139       long endTime = System.currentTimeMillis();
140       if (loadedPatternTime < 0)
141         loadedPatternTime = endTime;
142       eh.print(localizer.message("elapsed_time",
143                new Object JavaDoc[] {
144                          new Long JavaDoc(loadedPatternTime - startTime),
145                          new Long JavaDoc(endTime - loadedPatternTime),
146                          new Long JavaDoc(endTime - startTime)
147                        }));
148     }
149     if (hadError)
150       return 1;
151     return 0;
152   }
153
154 }
155
Popular Tags