Debian Codegraph @

mod/rng-validate/src/main/com/thaiopensource/relaxng/util/Driver.java

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