001    package jigcell.sbml2;
002    
003    import java.io.BufferedReader;
004    import java.io.BufferedWriter;
005    import java.io.FileReader;
006    import java.io.FileWriter;
007    import java.io.IOException;
008    import java.io.InputStream;
009    import java.io.InputStreamReader;
010    import java.io.OutputStream;
011    import java.io.OutputStreamWriter;
012    import java.io.Reader;
013    import java.io.Writer;
014    import org.xml.sax.Attributes;
015    
016    /**
017     * Represents the SBML Level 2 document that contains the model.
018     *
019     * <p>
020     * This code is licensed under the DARPA BioCOMP Open Source License.  See LICENSE for more details.
021     * </p>
022     *
023     * @author Nicholas Allen
024     */
025    
026    public final class SBMLLevel2Document extends SBase {
027       private Model model;
028    
029       public static SBMLLevel2Document readDocument (InputStream stream) throws IOException {
030          return SBMLLevel2Reader.read (new InputStreamReader (stream));
031       }
032    
033       public static SBMLLevel2Document readDocument (Reader reader) throws IOException {
034          return SBMLLevel2Reader.read (reader);
035       }
036    
037       public static SBMLLevel2Document readDocument (String fileName) throws IOException {
038          return SBMLLevel2Reader.read (new BufferedReader (new FileReader (fileName)));
039       }
040    
041       public SBMLLevel2Document () {
042          this (null);
043       }
044    
045       public SBMLLevel2Document (Model model) {
046          this.model = model;
047       }
048    
049       public int getLevel () {
050          return 2;
051       }
052    
053       public Model getModel () {
054          return model;
055       }
056    
057       public int getVersion () {
058          return 1;
059       }
060    
061       public boolean isValid (Model model) {
062          return super.isValid (model) && getModel () != null;
063       }
064    
065       public void setLevel (int level) {
066          if (level != 2)
067             throw new IllegalArgumentException ("Level is required to be 2.");
068       }
069    
070       public void setModel (Model model) {
071          this.model = model;
072       }
073    
074       public void setVersion (int version) {
075          if (version != 1)
076             throw new IllegalArgumentException ("Version is required to be 1.");
077       }
078    
079       public void writeDocument (OutputStream stream) throws IOException {
080          writeDocument (new OutputStreamWriter (stream));
081       }
082    
083       public void writeDocument (String fileName) throws IOException {
084          writeDocument (new BufferedWriter (new FileWriter (fileName)));
085       }
086    
087       /**
088        * Writes an SBML document and closes the writer.
089        */
090    
091       public void writeDocument (Writer writer) throws IOException {
092          writer.write ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
093          print (null).write (writer);
094          writer.close ();
095       }
096    
097       protected void parse (Attributes attributes) {
098          super.parse (attributes);
099          setLevel (Integer.parseInt (attributes.getValue ("level")));
100          setVersion (Integer.parseInt (attributes.getValue ("version")));
101        }
102    
103       protected XMLPrinter print (XMLPrinter parent) {
104          return print (parent, "sbml");
105       }
106    
107       protected XMLPrinter print (XMLPrinter parent, String name) {
108          XMLPrinter printer = super.print (parent, name);
109          printer.addAttribute ("xmlns", "http://www.sbml.org/sbml/level2");
110          printer.addAttribute ("xmlns:html", "http://www.w3.org/1999/xhtml");
111          printer.addAttribute ("xmlns:jigcell", "http://www.sbml.org/2001/ns/jigcell");
112          printer.addAttribute ("xmlns:math", "http://www.w3.org/1998/Math/MathML");
113          printer.addAttribute ("xmlns:rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
114          printer.addAttribute ("xmlns:sbml", "http://www.sbml.org/sbml/level2");
115          printer.addAttribute ("xmlns:xlink", "http://www.w3.org/1999/xlink");
116          printer.addAttribute ("level", String.valueOf (getLevel ()));
117          printer.addAttribute ("version", String.valueOf (getVersion ()));
118          printer.addElement (getModel ());
119          return printer;
120       }
121    }