Skip to content

Commit

Permalink
New structured classes
Browse files Browse the repository at this point in the history
  • Loading branch information
yazan committed Aug 7, 2017
1 parent 601a5d4 commit 4a5ff21
Show file tree
Hide file tree
Showing 318 changed files with 3,207 additions and 53,390 deletions.
247 changes: 247 additions & 0 deletions src/main/java/edu/iris/dmc/Application.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,247 @@
package edu.iris.dmc;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.logging.ConsoleHandler;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.stream.StreamSource;

import com.beust.jcommander.JCommander;

import edu.iris.dmc.fdsn.station.model.FDSNStationXML;
import edu.iris.dmc.station.RuleEngineService;
import edu.iris.dmc.station.actions.Action;
import edu.iris.dmc.station.io.CsvPrintStream;
import edu.iris.dmc.station.io.HtmlPrintStream;
import edu.iris.dmc.station.io.RuleResultPrintStream;
import edu.iris.dmc.station.io.XmlPrintStream;
import edu.iris.dmc.station.rules.Result;
import edu.iris.dmc.station.rules.Rule;
import edu.iris.dmc.station.rules.RuleContext;
import edu.iris.dmc.station.rules.UnitTable;

public class Application {
private static final Logger LOGGER = Logger.getLogger(Application.class.getName());

private static Args args = new Args();

/**
* @param args
* @throws Exception
*/
public static void main(String[] argv) throws Exception {

argv = new String[] { "https://files.anss-sis.scsn.org/production/FDSNstationXML" };

JCommander.newBuilder().addObject(args).build().parse(argv);
if (args.version) {
System.out.println(Application.getVersion());
System.exit(0);
} else if (args.help) {
help();
System.exit(0);
} else if (args.printRules) {
printRules();
System.exit(0);
} else if (args.printUnits) {
printUnits();
System.exit(0);
} else if (args.input == null) {
System.out.println("File is required!");
help();
System.exit(1);
}

Application app = new Application();
app.run();
}

public void run() throws Exception {
final Level logLevel = args.debug ? Level.FINER : Level.INFO;
Handler consoleHandler = new ConsoleHandler();
LOGGER.setLevel(logLevel);
LOGGER.addHandler(consoleHandler);

LEVEL level = LEVEL.parse(args.level.toLowerCase());
OutputStream out = System.out;

if (args.output != null) {
out = new FileOutputStream(new File(args.output));
// Handler fileHandler = new FileHandler(outputFile[1]);
// LOGGER.addHandler(fileHandler);
}

List<Integer> ignoreList = new ArrayList<Integer>();

List<String> input = new ArrayList<>();
// PrintStream stream = new PrintStream(out);

RuleContext rulesContext = RuleContext.of(LEVEL.RESPONSE);
int EXIT = 0;
if (args.input.startsWith("http://")) {
input.add(args.input);
} else {
File file = new File(args.input);
if (!file.exists()) {
System.out.println("File does not exist. File is required!");
help();
System.exit(1);
}

if (file.isDirectory()) {
for (File f : file.listFiles()) {
input.add(f.getAbsolutePath());
}
} else {
input.add(file.getAbsolutePath());
}
}
run(rulesContext, input, args.format, out, level, ignoreList);
if (out != null) {
out.close();
}
System.exit(EXIT);
}

private void run(RuleContext context, List<String> input, String format, OutputStream outputStream, LEVEL level,
List<Integer> ignoreList) {

RuleEngineService ruleEngineService = new RuleEngineService();
try (final RuleResultPrintStream ps = getOutputStream(format, outputStream)) {
ps.printHeader();
InputStream is = null;
Bool bool = new Bool();
for (String uri : input) {
ps.printRow(uri);
if (uri.startsWith("http://")) {
is = new URL(uri).openStream();
} else {
File file = new File(uri);
if (!file.exists()) {
System.out.println("File does not exist. File is required!");
help();
System.exit(1);
}
is = new FileInputStream(new File(uri));
}
FDSNStationXML document = (FDSNStationXML) theMarshaller().unmarshal(new StreamSource(is));

ruleEngineService.executeAllRules(document, context, new Action() {
@Override
public void update(RuleContext context, Result result) {
ps.print(result);
ps.flush();
}
});
}
if (bool.value) {
ps.printRow("PASSED");
}
ps.printFooter();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

private RuleResultPrintStream getOutputStream(String format, OutputStream outputStream) throws IOException {
if (format == null || format.isEmpty() || "html".equalsIgnoreCase(format)) {
return new HtmlPrintStream(outputStream);
} else if ("csv".equalsIgnoreCase(format)) {
return new CsvPrintStream(outputStream);
} else if ("xml".equalsIgnoreCase(format)) {
return new XmlPrintStream(outputStream);
} else {
throw new IOException("Invalid format [" + format + "] requested");
}
}

private Unmarshaller theMarshaller() throws JAXBException {
JAXBContext jaxbContext = JAXBContext.newInstance(edu.iris.dmc.fdsn.station.model.ObjectFactory.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
return jaxbUnmarshaller;

}

private static String getVersion() throws IOException {
Properties prop = new Properties();
InputStream in = Application.class.getClassLoader().getResourceAsStream("application.properties");
prop.load(in);
in.close();
return prop.getProperty("application.version");
}

private static String center(String text, int length, String pad) {
int width = length - text.length();
StringBuilder builder = new StringBuilder();
for (int i = 0; i < width / 2; i++) {
builder.append(pad);
}
builder.append(text);
int remainder = length - builder.length();
for (int i = 0; i < remainder; i++) {
builder.append(pad);
}
return builder.toString();
}

private static void printRules() {

RuleEngineService ruleEngineService = new RuleEngineService();
for (Rule rule : ruleEngineService.getRules()) {
System.out.printf("%-8s %s\n", rule.getId(), rule.getDescription());
}
}

private static void printUnits() {
System.out.println("UNIT TABLE:");
System.out.println("-------------------------------------");
for (String unit : UnitTable.units) {
System.out.println(unit);
}

}

private static void help() throws IOException {
String version = "Version " + getVersion();
version = center(version, 62, " ");

System.out.println("===============================================================");
System.out.println("|" + center("FDSN StationXml validator", 62, " ") + "|");
System.out.println("|" + version + "|");
System.out.println("================================================================");
System.out.println("Usage:");
System.out.println("java -jar stationxml-validator [OPTIONS] [FILE]");
System.out.println("OPTIONS");
System.out.println(" --[net|sta|cha|resp] default is resp ");
System.out.println(" --output : where to output result, default is System.out");
System.out.println(" --ignore-warnings: don't show warnings");
System.out.println(" --ignore-rules : comma seperated numbers of validation rules");
System.out.println(" --print-rules : print a list of validation rules");
System.out.println(" --print-units : print a list of units used to validate");
System.out.println(" --summary : print summary only report for errors if any");
System.out.println(" --debug :");
System.out.println(" --help : print this message");
System.out.println("===============================================================");
System.exit(0);
}

class Bool {
boolean value = true;
}
}
41 changes: 41 additions & 0 deletions src/main/java/edu/iris/dmc/Args.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package edu.iris.dmc;

import com.beust.jcommander.Parameter;

public class Args {

@Parameter(names = { "--help", "-h" }, description = "Print help")
boolean help = false;

@Parameter(names = { "--version", "-v" }, description = "Print version number")
boolean version = false;

@Parameter(names = "--debug", description = "Debug mode")
boolean debug = false;

@Parameter(names = "--ignore-rules", description = "Ignore rules")
boolean ignoreRules = false;

@Parameter(names = "--ignore-warnings", description = "Ignore warnings")
boolean ignoreWarnings = true;
@Parameter(names = { "--rules", "-r" }, description = "Print rules")
boolean printRules = false;
@Parameter(names = { "--units", "-u" }, description = "Print units")
boolean printUnits = false;

@Parameter(names = { "--level", "-l" }, description = "[net|sta|cha|resp]default is resp")
String level = "resp";

@Parameter(names = { "--format", "-f" }, description = "[csv|html|xml]default is html")
String format = "html";

@Parameter(names = { "--summary", "-s" }, description = "Print summary result")
boolean summary = false;

@Parameter(names = { "--output", "-o" }, description = "Where to print")
String output;

@Parameter(description = "Input")
String input;

}
19 changes: 19 additions & 0 deletions src/main/java/edu/iris/dmc/DocumentMarshaller.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package edu.iris.dmc;

import java.io.InputStream;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.stream.StreamSource;

import edu.iris.dmc.fdsn.station.model.FDSNStationXML;

public class DocumentMarshaller {

public static FDSNStationXML unmarshal(InputStream is) throws JAXBException {
JAXBContext jaxbContext = JAXBContext.newInstance(edu.iris.dmc.fdsn.station.model.ObjectFactory.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
return (FDSNStationXML) jaxbUnmarshaller.unmarshal(new StreamSource(is));
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package edu.iris.dmc.fdsn.station.model;
package edu.iris.dmc;

public enum LEVEL {

Expand Down Expand Up @@ -39,3 +39,4 @@ public static LEVEL parse(String value) {
throw new IllegalArgumentException();
}
}

20 changes: 20 additions & 0 deletions src/main/java/edu/iris/dmc/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package edu.iris.dmc;

import java.nio.file.Path;
import java.nio.file.Paths;

public class Main {

public static void main(String[] args) {
Path path = Paths.get("http://iris.edu");

System.out.println(path.getFileName());

System.out.println(path.startsWith("http://"));

System.out.println(path.toUri());


}

}
Loading

0 comments on commit 4a5ff21

Please sign in to comment.