corpus-services  1.0
ValidatorSettings.java
Go to the documentation of this file.
1 
10 package de.uni_hamburg.corpora.validation;
11 
12 import java.io.File;
13 import java.io.IOException;
14 import java.util.Collection;
15 import java.util.Map;
16 import java.util.HashMap;
17 import java.util.List;
18 import java.util.ArrayList;
19 import org.apache.commons.cli.Option;
20 import org.apache.commons.cli.Options;
21 import org.apache.commons.cli.CommandLine;
22 import org.apache.commons.cli.CommandLineParser;
23 import org.apache.commons.cli.DefaultParser;
24 import org.apache.commons.cli.HelpFormatter;
25 import org.apache.commons.cli.ParseException;
26 import org.ini4j.Ini;
27 
28 
32 public class ValidatorSettings {
33 
34  boolean verbose = false;
35  boolean replace = false;
36  File iniFile;
37  Ini optionsIni;
38  File baseDirectory;
39  File dataDirectory;
40  List<File> inputFiles;
41  File outputFile;
42 
43  String name;
44  String header;
45  String footer;
46 
47  public ValidatorSettings() {
48  inputFiles = new ArrayList<File>();
49  this.name = "UnnamedValidator";
50  this.header = "This validator is so unfinished it doesn't have a header!";
51  this.footer = "This validator is so unfinished it doesn't have a footer!";
52  }
53 
54  public ValidatorSettings(String name, String header, String footer) {
55  inputFiles = new ArrayList<File>();
56  this.name = name;
57  this.header = header;
58  this.footer = footer;
59  }
60 
61  /* getters and setters */
62 
63  public boolean isVerbose() {
64  return verbose;
65  }
66 
67  public Collection<File> getInputFiles() {
68  return inputFiles;
69  }
70 
71  public File getOutputFile() {
72  return outputFile;
73  }
74 
75  public File getBaseDirectory() {
76  return baseDirectory;
77  }
78 
79  public File getDataDirectory() {
80  return dataDirectory;
81  }
82 
84  public void amendOptions(Ini options) throws IOException {
85  for (String sectionName : options.keySet()) {
86  if (sectionName.equals("hzsk-validator")) {
87  Ini.Section commons = options.get(sectionName);
88  if (commons.containsKey("verbose")) {
89  verbose = true;
90  }
91  if (commons.containsKey("basedir")) {
92  baseDirectory = new File(commons.get("basedir"));
93  }
94  if (commons.containsKey("input")) {
95  inputFiles.add(new File(commons.get("input")));
96  }
97  if (commons.containsKey("output")) {
98  outputFile = new File(commons.get("output"));
99  }
100  } else {
101  Ini.Section section = options.get(sectionName);
102  Map<String, String> optionSet = new HashMap<String, String>();
103  for (String keyName : section.keySet()) {
104  if (verbose) {
105  System.out.print(keyName + " = " +
106  section.get(keyName));
107  }
108  }
109  }
110  }
111  }
112 
113 
120  public CommandLine handleCommandLine(String[] args, List<Option> extraOptions) {
121  CommandLine cmd;
122  Options parameters = new Options();
123  HelpFormatter formatter = new HelpFormatter();
124  // parse
125  try {
126  parameters.addOption("h", "help", false,
127  "print this help screen");
128  parameters.addOption("v", "verbose", false,
129  "print verbosely while processing");
130  parameters.addOption("R", "recursive", false,
131  "check recursively for references found in a file");
132  parameters.addOption("o", "output", true,
133  "fix problems where possible, write output to given file");
134  parameters.addOption("c", "configuration", true,
135  "read configuration from ini file");
136  parameters.addOption("i", "input", true,
137  "input file to validate");
138  parameters.addOption("b", "base-dir", true,
139  "base directory for solving relative file references etc.");
140  parameters.addOption("d", "data-dir", true,
141  "data directory for solving relative file references etc.");
142  parameters.addOption("X", "in-place", false,
143  "fix problems in place, replacing original file");
144  for (Option option : extraOptions) {
145  parameters.addOption(option);
146  }
147  CommandLineParser parser = new DefaultParser();
148  cmd = parser.parse(parameters, args);
149  } catch (ParseException pe) {
150  formatter.printHelp(name, header, parameters, footer, true);
151  return null;
152  }
153  // check obligatories and conflicts
154  if (!cmd.hasOption("input") && !cmd.hasOption("configuration") &&
155  (cmd.getArgs().length < 1)) {
156  System.err.println("No inputs given and no configuration found");
157  formatter.printHelp(name, header, parameters, footer, true);
158  return null;
159  } else if (cmd.hasOption("input") && cmd.hasOption("configuration")) {
160  System.err.println("Input and configuration are mutually " +
161  "exclusive");
162  formatter.printHelp(name, header, parameters, footer, true);
163  return null;
164  } else if (cmd.hasOption("input") && (cmd.getArgs().length > 0)) {
165  System.err.println("Unrecognised parameters with --input: " +
166  cmd.getArgs());
167  formatter.printHelp(name, header, parameters, footer, true);
168  return null;
169  } else if (cmd.hasOption("configuration") &&
170  (cmd.getArgs().length > 0)) {
171  System.err.println("Unrecognised parameters with --configuration: " +
172  cmd.getArgs());
173  formatter.printHelp(name, header, parameters, footer, true);
174  return null;
175  }
176  // apply important / global parameters first
177  if (cmd.hasOption("help")) {
178  formatter.printHelp(name, header, parameters, footer, true);
179  return null;
180  }
181  // create options stuff based on input stuff
182  verbose = false;
183  if (cmd.hasOption("verbose")) {
184  System.out.println("Printing long report");
185  verbose = true;
186  }
187  // set base dir
188  if (cmd.hasOption("base-dir")) {
189  baseDirectory = new File(cmd.getOptionValue("base-dir"));
190  } else if (cmd.hasOption("configuration")) {
191  iniFile = new File(cmd.getOptionValue("configuration"));
192  if (iniFile.isFile()) {
193  baseDirectory = iniFile.getParentFile();
194  }
195  }
196  if (cmd.hasOption("data-dir")) {
197  dataDirectory = new File(cmd.getOptionValue("data-dir"));
198  }
199  if (cmd.hasOption("configuration")) {
200  if (verbose) {
201  System.out.println("Reading configuration from " +
202  cmd.getOptionValue("configuration"));
203  }
204  try {
205  optionsIni =
206  new Ini(new File(cmd.getOptionValue("configuration")));
207  amendOptions(optionsIni);
208  } catch (IOException ioe) {
209  System.err.println("Configuration file not found: " +
210  cmd.getOptionValue("configuration") + " (or one of " +
211  "the referred files...)\n" +
212  ioe);
213  }
214  }
215  // parse rest command lines any ways
216  if (replace) {
217  if (verbose) {
218  System.out.println("Making corrections in place");
219  }
220  } else if (cmd.hasOption("output")) {
221  if (verbose) {
222  System.out.println("Making corrections to " +
223  cmd.getOptionValue("output"));
224  }
225  outputFile = new File(cmd.getOptionValue("output"));
226  }
227  if (cmd.hasOption("input")) {
228  if (verbose) {
229  System.out.println("Adding input: " +
230  cmd.getOptionValue("input"));
231  }
232  inputFiles.add(new File(cmd.getOptionValue("input")));
233  }
234  for (String s : cmd.getArgs()) {
235  if (verbose) {
236  System.out.println("Adding input: " + s);
237  }
238  inputFiles.add(new File(s));
239  }
240  return cmd;
241  }
242 
243 
244 }
CommandLine handleCommandLine(String[] args, List< Option > extraOptions)
ValidatorSettings(String name, String header, String footer)