diff --git a/src/main/java/at/ac/univie/mminf/qskos4j/cmd/ReportCollector.java b/src/main/java/at/ac/univie/mminf/qskos4j/cmd/ReportCollector.java index 76be191..b7282a3 100644 --- a/src/main/java/at/ac/univie/mminf/qskos4j/cmd/ReportCollector.java +++ b/src/main/java/at/ac/univie/mminf/qskos4j/cmd/ReportCollector.java @@ -1,11 +1,19 @@ package at.ac.univie.mminf.qskos4j.cmd; -import at.ac.univie.mminf.qskos4j.issues.Issue; -import at.ac.univie.mminf.qskos4j.result.Result; -import at.ac.univie.mminf.qskos4j.util.IssueDescriptor; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileOutputStream; +import java.io.FileWriter; +import java.io.IOException; +import java.net.URL; +import java.text.SimpleDateFormat; +import java.util.Collection; +import java.util.Date; +import java.util.Iterator; + import org.eclipse.rdf4j.RDF4JException; -import org.eclipse.rdf4j.model.Statement; import org.eclipse.rdf4j.model.IRI; +import org.eclipse.rdf4j.model.Statement; import org.eclipse.rdf4j.model.Value; import org.eclipse.rdf4j.model.ValueFactory; import org.eclipse.rdf4j.model.impl.SimpleValueFactory; @@ -21,18 +29,14 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.*; -import java.net.URL; -import java.text.SimpleDateFormat; -import java.util.Collection; -import java.util.Date; -import java.util.Iterator; -import java.util.List; - +import at.ac.univie.mminf.qskos4j.issues.Issue; +import at.ac.univie.mminf.qskos4j.result.Result; +import at.ac.univie.mminf.qskos4j.util.IssueDescriptor; class ReportCollector { - private final Logger logger = LoggerFactory.getLogger(ReportCollector.class); + private final Logger logger = LoggerFactory + .getLogger(ReportCollector.class); private Collection issues; private String reportFileName; @@ -42,63 +46,66 @@ class ReportCollector { private String computedOn; - public ReportCollector(Collection issues, - String reportFileName, - Collection vocabFileNames, - boolean includeSummary, boolean inDQV, String analizedDataset) { + public ReportCollector(final Collection issues, + final String reportFileName, + final Collection vocabFileNames, + final boolean includeSummary, final boolean inDQV, + final String analizedDataset) { this.issues = issues; this.reportFileName = reportFileName; this.includeSummary = includeSummary; this.vocabFileNames = vocabFileNames; - this.inDQV= inDQV; + this.inDQV = inDQV; this.computedOn = analizedDataset; } - void outputIssuesReport(boolean shouldWriteGraphs) - throws IOException, RDF4JException - { + void outputIssuesReport(final boolean shouldWriteGraphs) + throws IOException, RDF4JException { - File reportFile = createReportFile(); - BufferedWriter reportWriter = new BufferedWriter(new FileWriter(reportFile)); + final File reportFile = createReportFile(); + final BufferedWriter reportWriter = new BufferedWriter(new FileWriter( + reportFile)); processIssues(); String reportSummary = ""; - if (includeSummary) { + if (this.includeSummary) { reportSummary = createReportSummary(); - logger.info("\n" +reportSummary); + this.logger.info("\n" + reportSummary); } writeReportHeader(reportWriter, reportFile, reportSummary); writeReportBody(reportWriter, reportFile, shouldWriteGraphs); if (this.inDQV) { - File DQVFile = createDQVFile(); - FileOutputStream out = new FileOutputStream(DQVFile, true); - RDFWriter writer = Rio.createWriter(RDFFormat.TURTLE, out); - logger.info("\n" + "Writing results as instances of the Data Quality Vocabulary"); + final File DQVFile = createDQVFile(); + final FileOutputStream out = new FileOutputStream(DQVFile, true); + final RDFWriter writer = Rio.createWriter(RDFFormat.TURTLE, out); + this.logger + .info("\n" + + "Writing results as instances of the Data Quality Vocabulary"); writeDQVReportBody(writer, DQVFile, shouldWriteGraphs); - logger.info("\n" + "DQV file:" + DQVFile.getPath()); + this.logger.info("\n" + "DQV file:" + DQVFile.getPath()); out.close(); } reportWriter.close(); - } - private void writeReportHeader(BufferedWriter reportWriter, - File vocabFile, - String reportSummary) throws IOException - { - String issuedDate = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z").format(new Date()); + private void writeReportHeader(final BufferedWriter reportWriter, + final File vocabFile, final String reportSummary) + throws IOException { + final String issuedDate = new SimpleDateFormat( + "EEE, d MMM yyyy HH:mm:ss Z").format(new Date()); reportWriter.write("This is the quality report of the file(s) "); - Iterator vocabIt = vocabFileNames.iterator(); + final Iterator vocabIt = this.vocabFileNames.iterator(); while (vocabIt.hasNext()) { - String vocabFileName = vocabIt.next(); + final String vocabFileName = vocabIt.next(); reportWriter.write(vocabFileName); - if (vocabIt.hasNext()) reportWriter.write(", "); + if (vocabIt.hasNext()) + reportWriter.write(", "); } - reportWriter.write(", generated by qSKOS on " +issuedDate); + reportWriter.write(", generated by qSKOS on " + issuedDate); reportWriter.newLine(); reportWriter.newLine(); reportWriter.write(reportSummary); @@ -106,59 +113,60 @@ private void writeReportHeader(BufferedWriter reportWriter, private void processIssues() throws RDF4JException { int issueNumber = 0; - Iterator issueIt = issues.iterator(); + final Iterator issueIt = this.issues.iterator(); while (issueIt.hasNext()) { - Issue issue = issueIt.next(); + final Issue issue = issueIt.next(); issueNumber++; - logger.info("Processing issue " + issueNumber + " of " + issues.size() + " (" + issue.getIssueDescriptor().getName() + ")"); + this.logger.info("Processing issue " + issueNumber + " of " + + this.issues.size() + " (" + + issue.getIssueDescriptor().getName() + ")"); issue.getResult(); } - logger.info("Report complete!"); + this.logger.info("Report complete!"); } private String createReportSummary() throws IOException, RDF4JException { - StringBuffer summary = new StringBuffer(); + final StringBuffer summary = new StringBuffer(); summary.append("* Summary of Quality Issue Occurrences:\n"); - for (Issue issue : issues) { - summary.append(issue.getIssueDescriptor().getName() + ": " + prepareOccurrenceText(issue) + "\n"); + for (final Issue issue : this.issues) { + summary.append(issue.getIssueDescriptor().getName() + ": " + + prepareOccurrenceText(issue) + "\n"); } summary.append("\n"); return summary.toString(); } - private String prepareOccurrenceText(Issue issue) throws RDF4JException { + private String prepareOccurrenceText(final Issue issue) + throws RDF4JException { String occurrenceText = ""; if (issue.getResult().isProblematic()) { occurrenceText = "FAIL"; try { - String occurrenceCount = Long.toString(issue.getResult().occurrenceCount()); - occurrenceText += " (" +occurrenceCount+ ")"; - } - catch (UnsupportedOperationException e) { + final String occurrenceCount = Long.toString(issue.getResult() + .occurrenceCount()); + occurrenceText += " (" + occurrenceCount + ")"; + } catch (final UnsupportedOperationException e) { // ignore this } - } - else { + } else { occurrenceText = "OK (no potential problems found)"; } return occurrenceText; } - private void writeReportBody(BufferedWriter reportWriter, - File reportFile, - boolean shouldWriteGraphs) - throws IOException, RDF4JException - { + private void writeReportBody(final BufferedWriter reportWriter, + final File reportFile, final boolean shouldWriteGraphs) + throws IOException, RDF4JException { reportWriter.write("* Detailed coverage of each Quality Issue:\n\n"); - Iterator issueIt = issues.iterator(); + final Iterator issueIt = this.issues.iterator(); while (issueIt.hasNext()) { - Issue issue = issueIt.next(); + final Issue issue = issueIt.next(); writeTextReport(issue, reportWriter); @@ -173,140 +181,164 @@ private void writeReportBody(BufferedWriter reportWriter, } private File createReportFile() throws IOException { - File file = new File(reportFileName); + final File file = new File(this.reportFileName); + System.out.println("trying to create report file " + + this.reportFileName); + System.out.println("file.exists()=" + file.exists()); + System.out.println("file.canRead()=" + file.canRead()); + System.out.println("file.canWrite()=" + file.canWrite()); + System.out.println("file.canExecute()=" + file.canExecute()); file.createNewFile(); return file; } private File createDQVFile() throws IOException { - int ind=reportFileName.lastIndexOf("."); + final int ind = this.reportFileName.lastIndexOf("."); String DQVFileName; - DQVFileName= ind!=1? reportFileName.substring(0,ind )+ ".ttl": reportFileName+ ".ttl"; - File file = new File(DQVFileName); + DQVFileName = ind != 1 ? this.reportFileName.substring(0, ind) + ".ttl" + : this.reportFileName + ".ttl"; + final File file = new File(DQVFileName); file.createNewFile(); return file; } - private void writeDQVReport(Issue issue, RepositoryConnection repCon, ValueFactory f, String sdate) - throws IOException, RDF4JException - { - ValueFactory factory = SimpleValueFactory.getInstance(); - - String ndqv= "http://www.w3.org/ns/dqv#"; - String nex="http://w3id.org/quality/qskos/"; - - IRI ptype= f.createIRI("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"); - IRI pcomputedOn = f.createIRI(ndqv+"computedOn"); - IRI pdate = f.createIRI("http://purl.org/dc/terms/date"); - IRI pvalue = f.createIRI(ndqv+"value"); - IRI pisMeasurementOf = f.createIRI(ndqv+"isMeasurementOf"); - - String datasetName= this.computedOn.substring(this.computedOn.lastIndexOf("/")+1, this.computedOn.length()); - IRI measure= f.createIRI(nex+datasetName+issue.getIssueDescriptor().getName().replace(" ", "")+sdate); - repCon.add(measure, ptype, f.createIRI(ndqv+"QualityMeasurement")); - - if (this.computedOn.startsWith("http://")) repCon.add(measure, pcomputedOn,f.createIRI(this.computedOn)); + private void writeDQVReport(final Issue issue, + final RepositoryConnection repCon, final ValueFactory f, + final String sdate) throws IOException, RDF4JException { + final ValueFactory factory = SimpleValueFactory.getInstance(); + + final String ndqv = "http://www.w3.org/ns/dqv#"; + final String nex = "http://w3id.org/quality/qskos/"; + + final IRI ptype = f + .createIRI("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"); + final IRI pcomputedOn = f.createIRI(ndqv + "computedOn"); + final IRI pdate = f.createIRI("http://purl.org/dc/terms/date"); + final IRI pvalue = f.createIRI(ndqv + "value"); + final IRI pisMeasurementOf = f.createIRI(ndqv + "isMeasurementOf"); + + final String datasetName = this.computedOn.substring( + this.computedOn.lastIndexOf("/") + 1, this.computedOn.length()); + final IRI measure = f + .createIRI(nex + datasetName + + issue.getIssueDescriptor().getName().replace(" ", "") + + sdate); + repCon.add(measure, ptype, f.createIRI(ndqv + "QualityMeasurement")); + + if (this.computedOn.startsWith("http://")) + repCon.add(measure, pcomputedOn, f.createIRI(this.computedOn)); else { - Value datasetPath = factory.createLiteral(this.computedOn, XMLSchema.STRING); - repCon.add(measure, pcomputedOn,datasetPath); + final Value datasetPath = factory.createLiteral(this.computedOn, + XMLSchema.STRING); + repCon.add(measure, pcomputedOn, datasetPath); } - Value ldate = factory.createLiteral(sdate, XMLSchema.DATE); + final Value ldate = factory.createLiteral(sdate, XMLSchema.DATE); repCon.add(measure, pdate, ldate); - int i = new Integer(0); - String res ; + final int i = new Integer(0); + String res; Value lval; try { - long r = issue.getResult().occurrenceCount(); + final long r = issue.getResult().occurrenceCount(); if (issue.getResult().isProblematic()) - res = Long.toString(issue.getResult().occurrenceCount()); + res = Long.toString(issue.getResult().occurrenceCount()); else res = "0"; - lval = factory.createLiteral( res , XMLSchema.INTEGER); - }catch (java.lang.UnsupportedOperationException e) { - // in case the issue is not associated with a list of elements having the problem the methods occurenceCount is not defined. That happen with the issue "No Common Languages:" - // then we encode the result as a boolean, plus a annotation explaining . - if (issue.getResult().isProblematic()) lval = factory.createLiteral( "true" , XMLSchema.BOOLEAN); - else lval = factory.createLiteral( "false" , XMLSchema.BOOLEAN); - } + lval = factory.createLiteral(res, XMLSchema.INTEGER); + } catch (final java.lang.UnsupportedOperationException e) { + // in case the issue is not associated with a list of elements + // having the problem the methods occurenceCount is not defined. + // That happen with the issue "No Common Languages:" + // then we encode the result as a boolean, plus a annotation + // explaining . + if (issue.getResult().isProblematic()) + lval = factory.createLiteral("true", XMLSchema.BOOLEAN); + else + lval = factory.createLiteral("false", XMLSchema.BOOLEAN); + } repCon.add(measure, pvalue, lval); - IRI uriDimension= f.createIRI(nex+"numOf"+issue.getIssueDescriptor().getName().replace(" ", "")); + final IRI uriDimension = f.createIRI(nex + "numOf" + + issue.getIssueDescriptor().getName().replace(" ", "")); repCon.add(measure, pisMeasurementOf, uriDimension); } - private String createIssueHeader(IssueDescriptor issueDescriptor) { - String header = "--- " +issueDescriptor.getName(); - URL weblink = issueDescriptor.getWeblink(); - header += "\nDescription: " +issueDescriptor.getDescription(); + private String createIssueHeader(final IssueDescriptor issueDescriptor) { + String header = "--- " + issueDescriptor.getName(); + final URL weblink = issueDescriptor.getWeblink(); + header += "\nDescription: " + issueDescriptor.getDescription(); if (weblink != null) { - header += "\nDetailed information: " +weblink.toString(); + header += "\nDetailed information: " + weblink.toString(); } return header; } - private String getDotFilesPath(File reportFile) { - String absolutePath = reportFile.getAbsolutePath(); - return absolutePath.substring(0, absolutePath.lastIndexOf(File.separator)); + private String getDotFilesPath(final File reportFile) { + final String absolutePath = reportFile.getAbsolutePath(); + return absolutePath.substring(0, + absolutePath.lastIndexOf(File.separator)); } - private void writeGraphFiles(Issue issue, String dotFilesPath) throws IOException, RDF4JException { - BufferedWriter graphFileWriter = new BufferedWriter(new FileWriter(dotFilesPath + issue.getIssueDescriptor().getId() + ".dot")); - issue.getResult().generateReport(graphFileWriter, Result.ReportFormat.DOT); + private void writeGraphFiles(final Issue issue, final String dotFilesPath) + throws IOException, RDF4JException { + final BufferedWriter graphFileWriter = new BufferedWriter( + new FileWriter(dotFilesPath + + issue.getIssueDescriptor().getId() + ".dot")); + issue.getResult().generateReport(graphFileWriter, + Result.ReportFormat.DOT); graphFileWriter.close(); } - private void writeDQVReportBody(RDFWriter rdfFile, - File reportFile, - boolean shouldWriteGraphs) - throws IOException, RDF4JException - { + private void writeDQVReportBody(final RDFWriter rdfFile, + final File reportFile, final boolean shouldWriteGraphs) + throws IOException, RDF4JException { - Repository myRepository = new SailRepository(new MemoryStore()); + final Repository myRepository = new SailRepository(new MemoryStore()); myRepository.initialize(); - ValueFactory factory = myRepository.getValueFactory(); - RepositoryConnection myConnection = myRepository.getConnection(); + final ValueFactory factory = myRepository.getValueFactory(); + final RepositoryConnection myConnection = myRepository.getConnection(); - //reportWriter.write("* Detailed coverage of each Quality Issue:\n\n"); - SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); - Date date = new Date(); - String dates = dateFormat.format(date); + // reportWriter.write("* Detailed coverage of each Quality Issue:\n\n"); + final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); + final Date date = new Date(); + final String dates = dateFormat.format(date); - Iterator issueIt = issues.iterator(); + final Iterator issueIt = this.issues.iterator(); while (issueIt.hasNext()) { - Issue issue = issueIt.next(); - writeDQVReport(issue, myConnection, factory, dateFormat.format(date)); + final Issue issue = issueIt.next(); + writeDQVReport(issue, myConnection, factory, + dateFormat.format(date)); } - RepositoryResult myGraph = myConnection.getStatements(null, null, null, true); + final RepositoryResult myGraph = myConnection.getStatements( + null, null, null, true); try { rdfFile.startRDF(); - while (myGraph.hasNext()){ + while (myGraph.hasNext()) { rdfFile.handleStatement(myGraph.next()); } rdfFile.endRDF(); - } - catch (RDF4JException e) { + } catch (final RDF4JException e) { // oh no, do something! - } - finally{ + } finally { myGraph.close(); myConnection.close(); } } - private void writeTextReport(Issue issue, BufferedWriter writer) - throws IOException, RDF4JException - { + private void writeTextReport(final Issue issue, final BufferedWriter writer) + throws IOException, RDF4JException { writer.write(createIssueHeader(issue.getIssueDescriptor())); writer.newLine(); - issue.getResult().generateReport(writer, Result.ReportFormat.TXT, Result.ReportStyle.SHORT); + issue.getResult().generateReport(writer, Result.ReportFormat.TXT, + Result.ReportStyle.SHORT); writer.newLine(); - issue.getResult().generateReport(writer, Result.ReportFormat.TXT, Result.ReportStyle.EXTENSIVE); + issue.getResult().generateReport(writer, Result.ReportFormat.TXT, + Result.ReportStyle.EXTENSIVE); writer.newLine(); writer.flush(); diff --git a/src/main/java/at/ac/univie/mminf/qskos4j/cmd/VocEvaluate.java b/src/main/java/at/ac/univie/mminf/qskos4j/cmd/VocEvaluate.java index 510d259..dedab55 100644 --- a/src/main/java/at/ac/univie/mminf/qskos4j/cmd/VocEvaluate.java +++ b/src/main/java/at/ac/univie/mminf/qskos4j/cmd/VocEvaluate.java @@ -1,5 +1,15 @@ package at.ac.univie.mminf.qskos4j.cmd; +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import org.eclipse.rdf4j.RDF4JException; +import org.eclipse.rdf4j.repository.Repository; +import org.eclipse.rdf4j.rio.RDFFormat; + import at.ac.univie.mminf.qskos4j.QSkos; import at.ac.univie.mminf.qskos4j.issues.Issue; import at.ac.univie.mminf.qskos4j.progress.ConsoleProgressMonitor; @@ -7,290 +17,286 @@ import at.ac.univie.mminf.qskos4j.util.IssueDescriptor; import at.ac.univie.mminf.qskos4j.util.vocab.InvalidRdfException; import at.ac.univie.mminf.qskos4j.util.vocab.RepositoryBuilder; + import com.beust.jcommander.JCommander; import com.beust.jcommander.Parameter; import com.beust.jcommander.ParameterException; import com.beust.jcommander.Parameters; -import org.eclipse.rdf4j.RDF4JException; -import org.eclipse.rdf4j.repository.Repository; -import org.eclipse.rdf4j.rio.RDFFormat; - -import java.io.File; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; public class VocEvaluate { - + public final static String CMD_NAME_ANALYZE = "analyze"; - public final static String CMD_NAME_SUMMARIZE = "summarize"; - + public final static String CMD_NAME_SUMMARIZE = "summarize"; + private static JCommander jc; private CommandSummarize parsedCommand; - private QSkos qskos; - private ReportCollector reportCollector; - - @Parameter(names = {"-v", "--version"}, description = "Outputs version of the tool") + private QSkos qskos; + private ReportCollector reportCollector; + + @Parameter(names = { "-v", "--version" }, description = "Outputs version of the tool") private boolean outputVersion = false; - + @Parameters(commandNames = CMD_NAME_SUMMARIZE, commandDescription = "Computes basic statistics of a given vocabulary") private class CommandSummarize { - @SuppressWarnings("unused") + @SuppressWarnings("unused") @Parameter(description = "vocabularyfile") private List vocabFilenames; - @SuppressWarnings("unused") - @Parameter(names = {"-a", "--auth-resource-identifier"}, description = "Authoritative resource identifier") + @SuppressWarnings("unused") + @Parameter(names = { "-a", "--auth-resource-identifier" }, description = "Authoritative resource identifier") private String authoritativeResourceIdentifier; - @SuppressWarnings("unused") - @Parameter(names = {"-c", "--check"}, description = "Comma-separated list of issue/statistics IDs to check for") + @SuppressWarnings("unused") + @Parameter(names = { "-c", "--check" }, description = "Comma-separated list of issue/statistics IDs to check for") private String selectedIds; - @SuppressWarnings("unused") - @Parameter(names = {"-dc", "--dont-check"}, description = "Comma-separated list of issue/statistics IDs NOT to check for") + @SuppressWarnings("unused") + @Parameter(names = { "-dc", "--dont-check" }, description = "Comma-separated list of issue/statistics IDs NOT to check for") private String excludedIds; - @Parameter(names = {"-xl", "--skosxl"}, description = "Enable SKOSXL support") + @Parameter(names = { "-xl", "--skosxl" }, description = "Enable SKOSXL support") private boolean enableSkosXl = false; - @Parameter(names = {"-np", "--no-progress"}, description = "Suppresses output of a progress indicator") - private boolean noProgressBar = false; + @Parameter(names = { "-np", "--no-progress" }, description = "Suppresses output of a progress indicator") + private boolean noProgressBar = false; + + @SuppressWarnings("unused") + @Parameter(names = { "-d", "--debug" }, description = "Enable additional informative/debug output") + private boolean debug; - @SuppressWarnings("unused") - @Parameter(names = {"-d", "--debug"}, description = "Enable additional informative/debug output") - private boolean debug; + @SuppressWarnings("unused") + @Parameter(names = { "-o", "--output" }, description = "Name of the file that holds the generated report") + private String reportFileName; - @SuppressWarnings("unused") - @Parameter(names = {"-o", "--output"}, description = "Name of the file that holds the generated report") - private String reportFileName; + @SuppressWarnings("unused") + @Parameter(names = { "-sf", "--stream-friendly" }, description = "Print the progress indicator in a stream-friendly format") + private boolean streamFriendly; - @SuppressWarnings("unused") - @Parameter(names = {"-sf", "--stream-friendly"}, description = "Print the progress indicator in a stream-friendly format") - private boolean streamFriendly; - - @SuppressWarnings("unused") - @Parameter(names = {"-DQV", "--DataQualityVocabulary"}, description = "Print quality results as QualityMeasurement of W3C Data Quality Vocabulary") - private boolean inDQV; + @SuppressWarnings("unused") + @Parameter(names = { "-DQV", "--DataQualityVocabulary" }, description = "Print quality results as QualityMeasurement of W3C Data Quality Vocabulary") + private boolean inDQV; + + } - } - @Parameters(commandNames = CMD_NAME_ANALYZE, commandDescription = "Analyzes quality issues of a given vocabulary") private class CommandAnalyze extends CommandSummarize { - @SuppressWarnings("unused") - @Parameter(names = {"-sp", "--use-subset-percentage"}, description = "Use a specified percentage of the vocabulary triples for evaluation") + @SuppressWarnings("unused") + @Parameter(names = { "-sp", "--use-subset-percentage" }, description = "Use a specified percentage of the vocabulary triples for evaluation") private Float randomSubsetSize_percent; - @Parameter(names = {"-wg", "--write-graphs"}, description = "Writes graphs as .dot files to current directory") + @Parameter(names = { "-wg", "--write-graphs" }, description = "Writes graphs as .dot files to current directory") private boolean writeGraphs = false; } - - public static void main(String[] args) { + + public static void main(final String[] args) { try { new VocEvaluate(args); + } catch (final ParameterException paramExc) { + jc.usage(); + System.err.println("!! " + paramExc.getMessage()); + } catch (final IOException ioException) { + System.err.println("!! Error reading file: " + + ioException.getMessage()); + } catch (final RDF4JException rdfException) { + System.err.println("!! Error processing vocabulary: " + + rdfException.getMessage()); } - catch (ParameterException paramExc) { - jc.usage(); - System.err.println("!! " +paramExc.getMessage()); - } - catch (IOException ioException) { - System.err.println("!! Error reading file: " +ioException.getMessage()); - } - catch (RDF4JException rdfException) { - System.err.println("!! Error processing vocabulary: " +rdfException.getMessage()); - } } - - public VocEvaluate(String[] args) throws RDF4JException, IOException - { - qskos = new QSkos(); - parseCmdParams(args); - - if (outputVersion) { - System.out.println("Version: " +getClass().getPackage().getImplementationVersion()); + + public VocEvaluate(final String[] args) throws RDF4JException, IOException { + this.qskos = new QSkos(); + parseCmdParams(args); + + if (this.outputVersion) { + System.out.println("Version: " + + getClass().getPackage().getImplementationVersion()); } - - if (parsedCommand == null) { + + if (this.parsedCommand == null) { jc.usage(); - return; + return; } try { listIssuesOrEvaluate(); + } catch (final InvalidRdfException e) { + System.err + .println("!! Provided input file does not contain valid RDF data"); + System.exit(1); + } catch (final Exception e) { + e.printStackTrace(System.err); + System.exit(1); } - catch (InvalidRdfException e) { - System.err.println("!! Provided input file does not contain valid RDF data"); - System.exit(1); - } - catch (Exception e) { - e.printStackTrace(System.err); - System.exit(1); - } } - - private void parseCmdParams(String[] args) { + + private void parseCmdParams(final String[] args) { jc = new JCommander(this); - - CommandAnalyze commandAnalyze = new CommandAnalyze(); - CommandSummarize commandSummarize = new CommandSummarize(); - + + final CommandAnalyze commandAnalyze = new CommandAnalyze(); + final CommandSummarize commandSummarize = new CommandSummarize(); + jc.addCommand(commandAnalyze); - jc.addCommand(commandSummarize); + jc.addCommand(commandSummarize); jc.parse(args); - - String command = jc.getParsedCommand(); - if (command != null) { + + final String command = jc.getParsedCommand(); + if (command != null) { if (command.equals(CMD_NAME_ANALYZE)) { - parsedCommand = commandAnalyze; + this.parsedCommand = commandAnalyze; } if (command.equals(CMD_NAME_SUMMARIZE)) { - parsedCommand = commandSummarize; + this.parsedCommand = commandSummarize; } } } - - private void listIssuesOrEvaluate() throws RDF4JException, IOException - { - if (parsedCommand.vocabFilenames == null) { - if (parsedCommand instanceof CommandAnalyze) { + + private void listIssuesOrEvaluate() throws RDF4JException, IOException { + if (this.parsedCommand.vocabFilenames == null) { + if (this.parsedCommand instanceof CommandAnalyze) { outputIssueDetails(IssueDescriptor.IssueType.ANALYTICAL); - } - else { + } else { outputIssueDetails(IssueDescriptor.IssueType.STATISTICAL); } - } - else { + } else { checkVocabFilenameGiven(); evaluate(); } } - private void outputIssueDetails(IssueDescriptor.IssueType constraintType) { - for (Issue issue : qskos.getAllIssues()) { - IssueDescriptor issueDescriptor = issue.getIssueDescriptor(); + private void outputIssueDetails( + final IssueDescriptor.IssueType constraintType) { + for (final Issue issue : this.qskos.getAllIssues()) { + final IssueDescriptor issueDescriptor = issue.getIssueDescriptor(); if (issueDescriptor.getType() == constraintType) { System.out.println("---"); - System.out.println("ID: " +issueDescriptor.getId()); - System.out.println("Name: " +issueDescriptor.getName()); - System.out.println("Description: " +issueDescriptor.getDescription()); - if (issueDescriptor.getWeblink() != null) { - System.out.println("Further Informaton: <" +issueDescriptor.getWeblink().toString()+ ">"); - } + System.out.println("ID: " + issueDescriptor.getId()); + System.out.println("Name: " + issueDescriptor.getName()); + System.out.println("Description: " + + issueDescriptor.getDescription()); + if (issueDescriptor.getWeblink() != null) { + System.out.println("Further Informaton: <" + + issueDescriptor.getWeblink().toString() + ">"); + } } } } - - private void checkVocabFilenameGiven() throws ParameterException - { - if (parsedCommand.vocabFilenames == null) { + + private void checkVocabFilenameGiven() throws ParameterException { + if (this.parsedCommand.vocabFilenames == null) { throw new ParameterException("Please provide a vocabulary file"); } - if (parsedCommand.reportFileName == null) { - throw new ParameterException("Please provide a report output file"); - } + if (this.parsedCommand.reportFileName == null) { + throw new ParameterException("Please provide a report output file"); + } } - - private void evaluate() throws RDF4JException, IOException - { + + private void evaluate() throws RDF4JException, IOException { setup(); - String command = jc.getParsedCommand(); - String datasetAnalized=new String(); - for (String s : parsedCommand.vocabFilenames) {datasetAnalized+= s;} - - reportCollector = new ReportCollector(extractMeasures(), - parsedCommand.reportFileName, - parsedCommand.vocabFilenames, - command.equals(CMD_NAME_ANALYZE), parsedCommand.inDQV, datasetAnalized); - reportCollector.outputIssuesReport(shouldWriteGraphs()); + final String command = jc.getParsedCommand(); + String datasetAnalized = new String(); + for (final String s : this.parsedCommand.vocabFilenames) { + datasetAnalized += s; + } + + this.reportCollector = new ReportCollector(extractMeasures(), + this.parsedCommand.reportFileName, + this.parsedCommand.vocabFilenames, + command.equals(CMD_NAME_ANALYZE), this.parsedCommand.inDQV, + datasetAnalized); + this.reportCollector.outputIssuesReport(shouldWriteGraphs()); } - + private void setup() throws RDF4JException, IOException { - setupLogging(); - - RepositoryBuilder repositoryBuilder = new RepositoryBuilder(); - File inputFile = new File(parsedCommand.vocabFilenames.get(0)); - Repository repo = repositoryBuilder.setUpFromFile(inputFile, null, useRdfXmlFormatIfExtensionIsXml(inputFile)); - qskos.setRepositoryConnection(repo.getConnection()); - qskos.setAuthResourceIdentifier(parsedCommand.authoritativeResourceIdentifier); - qskos.addSparqlEndPoint("http://sparql.sindice.com/sparql"); - qskos.addSparqlEndPoint("http://semantic.ckan.net/sparql"); - - if (parsedCommand instanceof CommandAnalyze) { - qskos.setSubsetSize(((CommandAnalyze) parsedCommand).randomSubsetSize_percent); + setupLogging(); + + final RepositoryBuilder repositoryBuilder = new RepositoryBuilder(); + final File inputFile = new File( + this.parsedCommand.vocabFilenames.get(0)); + final Repository repo = repositoryBuilder.setUpFromFile(inputFile, + null, useRdfXmlFormatIfExtensionIsXml(inputFile)); + this.qskos.setRepositoryConnection(repo.getConnection()); + this.qskos + .setAuthResourceIdentifier(this.parsedCommand.authoritativeResourceIdentifier); + this.qskos.addSparqlEndPoint("http://sparql.sindice.com/sparql"); + this.qskos.addSparqlEndPoint("http://semantic.ckan.net/sparql"); + + if (this.parsedCommand instanceof CommandAnalyze) { + this.qskos + .setSubsetSize(((CommandAnalyze) this.parsedCommand).randomSubsetSize_percent); } - - if (parsedCommand.enableSkosXl) { - repositoryBuilder.enableSkosXlSupport(); + + if (this.parsedCommand.enableSkosXl) { + repositoryBuilder.enableSkosXlSupport(); } - if (!parsedCommand.noProgressBar) { - if (parsedCommand.streamFriendly) { - qskos.setProgressMonitor(new StreamProgressMonitor()); - } - else { - qskos.setProgressMonitor(new ConsoleProgressMonitor()); - } - } - } - - private RDFFormat useRdfXmlFormatIfExtensionIsXml(File inputFile) { - if (inputFile.getName().toLowerCase().endsWith(".xml")) { - return RDFFormat.RDFXML; - } - return null; - } - - private void setupLogging() { - if (parsedCommand.debug) { - System.setProperty("root-level", "DEBUG"); - } - } + if (!this.parsedCommand.noProgressBar) { + if (this.parsedCommand.streamFriendly) { + this.qskos.setProgressMonitor(new StreamProgressMonitor()); + } else { + this.qskos.setProgressMonitor(new ConsoleProgressMonitor()); + } + } + } + + private RDFFormat useRdfXmlFormatIfExtensionIsXml(final File inputFile) { + if (inputFile.getName().toLowerCase().endsWith(".xml")) { + return RDFFormat.RDFXML; + } + return null; + } + + private void setupLogging() { + if (this.parsedCommand.debug) { + System.setProperty("root-level", "DEBUG"); + } + } private boolean shouldWriteGraphs() { - return parsedCommand instanceof CommandAnalyze && ((CommandAnalyze) parsedCommand).writeGraphs; + return this.parsedCommand instanceof CommandAnalyze + && ((CommandAnalyze) this.parsedCommand).writeGraphs; } - - private Collection extractMeasures() - { + + private Collection extractMeasures() { Collection resultingIssues; - Collection selectedIssues = qskos.getIssues(parsedCommand.selectedIds); - Collection excludedIssues = qskos.getIssues(parsedCommand.excludedIds); + final Collection selectedIssues = this.qskos + .getIssues(this.parsedCommand.selectedIds); + final Collection excludedIssues = this.qskos + .getIssues(this.parsedCommand.excludedIds); if (!selectedIssues.isEmpty()) { resultingIssues = selectedIssues; - } - else if (!excludedIssues.isEmpty()) { + } else if (!excludedIssues.isEmpty()) { resultingIssues = getAllIssuesForCommand(); resultingIssues.removeAll(excludedIssues); - } - else { + } else { resultingIssues = getAllIssuesForCommand(); } - + return resultingIssues; } - + private Collection getAllIssuesForCommand() { - List issuesForCommand = new ArrayList<>(); - - for (Issue issue : qskos.getAllIssues()) { - String command = jc.getParsedCommand(); + final List issuesForCommand = new ArrayList<>(); - IssueDescriptor.IssueType issueType = issue.getIssueDescriptor().getType(); + for (final Issue issue : this.qskos.getAllIssues()) { + final String command = jc.getParsedCommand(); - if ((issueType == IssueDescriptor.IssueType.ANALYTICAL && command.equals(CMD_NAME_ANALYZE)) || - (issueType == IssueDescriptor.IssueType.STATISTICAL && command.equals(CMD_NAME_SUMMARIZE))) - { + final IssueDescriptor.IssueType issueType = issue + .getIssueDescriptor().getType(); + + if ((issueType == IssueDescriptor.IssueType.ANALYTICAL && command + .equals(CMD_NAME_ANALYZE)) + || (issueType == IssueDescriptor.IssueType.STATISTICAL && command + .equals(CMD_NAME_SUMMARIZE))) { issuesForCommand.add(issue); } } - + return issuesForCommand; } diff --git a/src/main/java/at/ac/univie/mminf/qskos4j/util/url/UrlDereferencer.java b/src/main/java/at/ac/univie/mminf/qskos4j/util/url/UrlDereferencer.java index 08fb414..8ba5a6f 100644 --- a/src/main/java/at/ac/univie/mminf/qskos4j/util/url/UrlDereferencer.java +++ b/src/main/java/at/ac/univie/mminf/qskos4j/util/url/UrlDereferencer.java @@ -1,7 +1,12 @@ package at.ac.univie.mminf.qskos4j.util.url; +import java.io.IOException; +import java.net.URISyntaxException; +import java.net.URL; + import org.apache.http.Header; import org.apache.http.HttpResponse; +import org.apache.http.client.config.CookieSpecs; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.client.methods.RequestBuilder; @@ -10,67 +15,67 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.IOException; -import java.net.URISyntaxException; -import java.net.URL; - public class UrlDereferencer { - private final Logger logger = LoggerFactory.getLogger(UrlDereferencer.class); + private final Logger logger = LoggerFactory + .getLogger(UrlDereferencer.class); private final int HTTP_GET_TIMOUT_MILLIS = 60000; - - public String getContentType(URL url) throws UrlNotDereferencableException { + + public String getContentType(final URL url) + throws UrlNotDereferencableException { try { - HttpResponse response = sendRequest(url); - + final HttpResponse response = sendRequest(url); + if (isValidResponse(response)) { - Header contentType = response.getFirstHeader("Content-Type"); + final Header contentType = response + .getFirstHeader("Content-Type"); if (contentType == null || contentType.getValue().isEmpty()) { - throw new NoContentTypeProvidedException(); + throw new NoContentTypeProvidedException(); } - + return contentType.getValue(); } - } - catch (Exception e) { + } catch (final Exception e) { // fall through } throw new UrlNotDereferencableException(url); } - - private HttpResponse sendRequest(URL url) - throws URISyntaxException, IOException - { - logger.debug("dereferencing: " +url.toString()); - HttpUriRequest request = RequestBuilder.get(url.toURI()).setConfig( - RequestConfig.custom() - .setConnectionRequestTimeout(HTTP_GET_TIMOUT_MILLIS) - .setConnectTimeout(HTTP_GET_TIMOUT_MILLIS) - .setSocketTimeout(HTTP_GET_TIMOUT_MILLIS) - .build()) + private HttpResponse sendRequest(final URL url) throws URISyntaxException, + IOException { + this.logger.debug("dereferencing: " + url.toString()); - .setHeader("Accept", "text/html,application/xhtml+xml,application/xml,text/plain,*/*;q=0.5") - .build(); + final HttpUriRequest request = RequestBuilder + .get(url.toURI()) + .setConfig( + RequestConfig + .custom() + .setConnectionRequestTimeout( + this.HTTP_GET_TIMOUT_MILLIS) + .setConnectTimeout(this.HTTP_GET_TIMOUT_MILLIS) + .setSocketTimeout(this.HTTP_GET_TIMOUT_MILLIS) + .setCookieSpec(CookieSpecs.STANDARD).build()) - CloseableHttpClient httpClient = HttpClientBuilder.create().build(); - HttpResponse response = httpClient.execute(request); - httpClient.close(); + .setHeader("Accept", + "text/html,application/xhtml+xml,application/xml,text/plain,*/*;q=0.5") + .build(); - return response; + final CloseableHttpClient httpClient = HttpClientBuilder.create() + .build(); + final HttpResponse response = httpClient.execute(request); + httpClient.close(); + + return response; } - private boolean isValidResponse(HttpResponse response) - { - int statusCode = response.getStatusLine().getStatusCode(); - logger.debug("response status: " +statusCode); - - if (Integer.toString(statusCode).startsWith("4") || - Integer.toString(statusCode).startsWith("5")) - { + private boolean isValidResponse(final HttpResponse response) { + final int statusCode = response.getStatusLine().getStatusCode(); + this.logger.debug("response status: " + statusCode); + + if (Integer.toString(statusCode).startsWith("4") + || Integer.toString(statusCode).startsWith("5")) { return false; - } - else { + } else { switch (statusCode) { case 200: return true; @@ -79,5 +84,5 @@ private boolean isValidResponse(HttpResponse response) } } } - + }