diff --git a/src/org/sbml/jsbml/SBMLDocument.java b/src/org/sbml/jsbml/SBMLDocument.java index 34802ae97..c107135d9 100644 --- a/src/org/sbml/jsbml/SBMLDocument.java +++ b/src/org/sbml/jsbml/SBMLDocument.java @@ -20,14 +20,18 @@ package org.sbml.jsbml; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; import java.util.HashMap; import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; import java.util.Map; +import javax.xml.stream.XMLStreamException; + import org.sbml.jsbml.util.NotImplementedException; import org.sbml.jsbml.util.StringTools; +import org.sbml.jsbml.validator.SBMLValidator; /** * Represents the 'sbml' root node of a SBML file. @@ -76,7 +80,13 @@ public class SBMLDocument extends AbstractSBase { * Memorizes all {@link SBMLError} when parsing the file containing this * document. */ - private List listOfErrors; + private SBMLErrorLog listOfErrors; + + /** + * Contains all the parameter to validate the SBML document + */ + private HashMap checkConsistencyParameters = new HashMap(); + /** * Represents the 'model' XML subnode of a SBML file. */ @@ -155,25 +165,58 @@ public void addNamespace(String namespaceName, String prefix, String URI) { } /** - * Not yet implemented! - * - * It is supposed to fill this {@link SBMLDocument}'s {@link #listOfErrors} + * Validates the {@link SBMLDocument} using the + * SBML.org online validator (http://sbml.org/validator/). + *

+ * It will fill this {@link SBMLDocument}'s {@link #listOfErrors} * with {@link SBMLError}s for each problem within this whole data * structure. You will then be able to obtain this list by calling * {@link #getError(int)} or {@link #getListOfErrors()}. * - * @return - * @throws NotImplementedException - * Currently, this method is not implemented, so please catch a - * {@link NotImplementedException} in your program until we - * remove this error. + * @return the number of errors found */ public int checkConsistency() { - listOfErrors = getListOfErrors(); - // TODO: IMPLEMENT! - throw new NotImplementedException(); + + File tmpFile = null; + + try { + tmpFile = File.createTempFile("jsbml-", ".xml"); + } catch (IOException e) { + e.printStackTrace(); + } + + try { + new SBMLWriter().writeSBML(this, tmpFile); + } catch (FileNotFoundException e) { // TODO : log the errors. Send an exception ?? + e.printStackTrace(); + return -1; + } catch (XMLStreamException e) { + e.printStackTrace(); + return -1; + } catch (SBMLException e) { + e.printStackTrace(); + return -1; + } + + HashMap parameters = new HashMap(); + + // System.out.println("SBMLDocument.checkConsistency : tmp file = " + tmpFile.getAbsolutePath()); + + listOfErrors = SBMLValidator.checkConsistency(tmpFile.getAbsolutePath(), parameters); + + return listOfErrors.getNumErrors(); } + public void printErrors() { + int nbErrors = listOfErrors.getNumErrors(); + + for (int i = 0; i < nbErrors; i++) { + System.out.println(listOfErrors.getError(i)); + } + } + + + /* * (non-Javadoc) * @@ -306,16 +349,15 @@ public String getElementName() { } /** - * Not yet implemented! * * @param i - * @return + * @return */ public SBMLError getError(int i) { if (isSetListOfErrors()) { - return listOfErrors.get(i); + return listOfErrors.getError(i); } - throw new IndexOutOfBoundsException(Integer.toString(i)); + throw new IndexOutOfBoundsException(Integer.toString(i)); // TODO : test if the number is too low or too big } /** @@ -324,13 +366,23 @@ public SBMLError getError(int i) { * * @return */ - public List getListOfErrors() { + public SBMLErrorLog getListOfErrors() { if (listOfErrors == null) { - listOfErrors = new LinkedList(); + listOfErrors = new SBMLErrorLog(); } return listOfErrors; } + /** + * This method returns a collection of all {@link SBMLError}s reflecting + * problems in the overall data structure of this {@link SBMLDocument}. + * + * @return + */ + public SBMLErrorLog getErrorLog() { + return getListOfErrors(); + } + /** * Returns the model of this {@link SBMLDocument}. * @@ -345,7 +397,7 @@ public Model getModel() { * @return */ public int getNumErrors() { - return isSetListOfErrors() ? listOfErrors.size() : 0; + return isSetListOfErrors() ? listOfErrors.getNumErrors() : 0; } /** @@ -369,7 +421,7 @@ public Map getSBMLDocumentNamespaces() { * @return */ private boolean isSetListOfErrors() { - return (listOfErrors != null) && (listOfErrors.size() > 0); + return (listOfErrors != null) && (listOfErrors.getNumErrors() > 0); } /** diff --git a/src/org/sbml/jsbml/validator/SBMLValidator.java b/src/org/sbml/jsbml/validator/SBMLValidator.java index 2b19ae672..5501b3f03 100644 --- a/src/org/sbml/jsbml/validator/SBMLValidator.java +++ b/src/org/sbml/jsbml/validator/SBMLValidator.java @@ -341,13 +341,15 @@ public static SBMLErrorLog checkConsistency(String fileName, String output = "xml"; parameters.put("output", output); + parameters.put("offcheck", "u"); // getting an XML output of the error log // describe there : // http://sbml.org/Facilities/Validator/Validator_Web_API result = Validator.validateSBML(fileName, parameters); - // print(result, System.out); + // DEBUG + print(result, System.out); XStream xstream = new XStream(new DomDriver()); // To parse XML // using DOM diff --git a/test/org/sbml/jsbml/test/ValidateSBML.java b/test/org/sbml/jsbml/test/ValidateSBML.java new file mode 100644 index 000000000..a7b08d144 --- /dev/null +++ b/test/org/sbml/jsbml/test/ValidateSBML.java @@ -0,0 +1,66 @@ +package org.sbml.jsbml.test; + +import java.io.File; +import java.io.FileNotFoundException; + +import javax.xml.stream.XMLStreamException; + +import org.sbml.jsbml.SBMLDocument; +import org.sbml.jsbml.SBMLReader; + +public class ValidateSBML { + + public static void main (String[] args) throws FileNotFoundException, XMLStreamException + { + if (args.length < 1) + { + System.out.println("Usage: java validateSBML filename"); + System.exit(1); + } + + String filename = args[0]; + SBMLReader reader = new SBMLReader(); + SBMLDocument document; + long start, stop; + + start = System.currentTimeMillis(); + document = reader.readSBML(filename); + stop = System.currentTimeMillis(); + + if (document.getNumErrors() > 0) + { + print("Encountered the following errors while reading the SBML file:\n"); + document.printErrors(); + print("\nFurther consistency checking and validation aborted.\n"); + System.exit(1); + } + else + { + long errors = document.checkConsistency(); + long size = new File(filename).length(); + + println(" filename: " + filename); + println(" file size: " + size); + println(" read time (ms): " + (stop - start)); + println(" validation error(s): " + errors); + + if (errors > 0) + { + document.printErrors(); + System.exit(1); + } + } + } + + + static void print (String msg) + { + System.out.print(msg); + } + + static void println (String msg) + { + System.out.println(msg); + } + +}