SpeciesReader.C 7.03 KB
Newer Older
Francois Gygi committed
1 2
////////////////////////////////////////////////////////////////////////////////
//
3
// Copyright (c) 2008-2012 The Regents of the University of California
Francois Gygi committed
4
//
5
// This file is part of Qbox //
Francois Gygi committed
6 7
// Qbox is distributed under the terms of the GNU General Public License
// as published by the Free Software Foundation, either version 2 of
Francois Gygi committed
8 9 10 11 12 13
// the License, or (at your option) any later version.
// See the file COPYING in the root directory of this distribution
// or <http://www.gnu.org/licenses/>.
//
////////////////////////////////////////////////////////////////////////////////
//
Francois Gygi committed
14 15 16 17 18 19 20 21
// SpeciesReader.C:
//
////////////////////////////////////////////////////////////////////////////////

#include "Species.h"
#include "SpeciesReader.h"
#include <cassert>
#include <string>
22
#include <sstream>
Francois Gygi committed
23 24 25
#include <iostream>
#include <vector>

Francois Gygi committed
26 27
#include "StructuredDocumentHandler.h"
#include "SpeciesHandler.h"
Francois Gygi committed
28 29 30 31 32
#include <xercesc/util/XMLUniDefs.hpp>
#include <xercesc/sax2/Attributes.hpp>
#include <xercesc/util/PlatformUtils.hpp>
#include <xercesc/sax2/SAX2XMLReader.hpp>
#include <xercesc/sax2/XMLReaderFactory.hpp>
33 34
#include <xercesc/framework/LocalFileInputSource.hpp>
#include <xercesc/framework/MemBufInputSource.hpp>
Francois Gygi committed
35
using namespace xercesc;
36
using namespace std;
Francois Gygi committed
37 38

////////////////////////////////////////////////////////////////////////////////
39
SpeciesReader::SpeciesReader(void) {}
Francois Gygi committed
40 41

////////////////////////////////////////////////////////////////////////////////
42
void SpeciesReader::uri_to_species(const string uri, Species& sp)
Francois Gygi committed
43
{
44 45 46 47 48 49 50 51 52 53 54 55
  // parse the document defined by its URI and initialize
  // the Species object sp

  SAX2XMLReader::ValSchemes valScheme = SAX2XMLReader::Val_Auto;
  //SAX2XMLReader::ValSchemes valScheme = SAX2XMLReader::Val_Always;
  bool doNamespaces = true;
  bool doSchema = true;
  bool schemaFullChecking = true;
  bool namespacePrefixes = false;
  SAX2XMLReader* parser = 0;

  try
Francois Gygi committed
56
  {
57
    XMLPlatformUtils::Initialize();
58
  }
59
  catch (const XMLException& toCatch)
Francois Gygi committed
60
  {
61 62 63 64
    cout << "  Species::readSpecies: Error during XML initialization :\n"
         << StrX(toCatch.getMessage()) << endl;
    throw;
  }
65

66 67 68 69 70 71
  parser = XMLReaderFactory::createXMLReader();
  if (valScheme == SAX2XMLReader::Val_Auto)
  {
    parser->setFeature(XMLUni::fgSAX2CoreValidation, true);
    parser->setFeature(XMLUni::fgXercesDynamic, true);
  }
72

73 74 75 76
  if (valScheme == SAX2XMLReader::Val_Never)
  {
    parser->setFeature(XMLUni::fgSAX2CoreValidation, false);
  }
77

78 79 80 81 82
  if (valScheme == SAX2XMLReader::Val_Always)
  {
    parser->setFeature(XMLUni::fgSAX2CoreValidation, true);
    parser->setFeature(XMLUni::fgXercesDynamic, false);
  }
83

84 85 86 87
  parser->setFeature(XMLUni::fgSAX2CoreNameSpaces, doNamespaces);
  parser->setFeature(XMLUni::fgXercesSchema, doSchema);
  parser->setFeature(XMLUni::fgXercesSchemaFullChecking, schemaFullChecking);
  parser->setFeature(XMLUni::fgSAX2CoreNameSpacePrefixes, namespacePrefixes);
88

89 90
  int nlink = 0;
  string current_uri = uri;
91

92 93
  while ( sp.description() == "undefined" && nlink++ < 5 )
  {
94

95
    try
96
    {
97 98 99 100 101
      SpeciesHandler* sp_handler = new SpeciesHandler(sp);
      StructuredDocumentHandler handler(sp_handler);
      parser->setContentHandler(&handler);
      parser->setErrorHandler(&handler);
      parser->parse(uri.c_str());
Francois Gygi committed
102
      // errorCount = parser->getErrorCount();
103
      delete sp_handler;
104
    }
105

106
    catch (const XMLException& toCatch)
107
    {
108 109 110 111 112 113
      cout << "\nAn error occurred\n  Error: "
           << StrX(toCatch.getMessage())
           << "\n" << endl;
      XMLPlatformUtils::Terminate();
      delete parser;
      throw;
114
    }
115

116
    catch (const SAXParseException& e)
117
    {
118 119 120 121 122 123 124 125
      cout << "\na SAXParseException occurred in file "
           << StrX(e.getSystemId())
           << ", line " << e.getLineNumber()
           << ", char " << e.getColumnNumber()
           << "\n  Message: " << StrX(e.getMessage()) << endl;
      XMLPlatformUtils::Terminate();
      delete parser;
      throw;
126
    }
127

128 129 130 131
    if ( sp.description() == "undefined" )
      current_uri = sp.uri();
  }
  sp.uri_ = current_uri;
132

133 134 135
  delete parser;
  XMLPlatformUtils::Terminate();
}
136

137 138 139 140 141 142 143 144 145 146 147 148 149
////////////////////////////////////////////////////////////////////////////////
void SpeciesReader::uri_to_string(const string uri, const string name,
     string& xmlstr)
{
  // parse a species document defined by its URI and initialize
  // the string xmlstr with the corresponding XML <species> element
  //
  Species sp(name);
  uri_to_species(uri,sp);
  ostringstream spstream;
  const bool expanded_form = true;
  sp.print(spstream,expanded_form);
  xmlstr = spstream.str();
Francois Gygi committed
150 151 152
}

////////////////////////////////////////////////////////////////////////////////
153
void SpeciesReader::string_to_species(const string xmlstr, Species& sp)
Francois Gygi committed
154
{
155 156 157 158 159 160 161 162 163 164 165
  // parse the xmlstr string containing a <species> element and
  // initialize the Species object sp
  SAX2XMLReader::ValSchemes valScheme = SAX2XMLReader::Val_Auto;
  //SAX2XMLReader::ValSchemes valScheme = SAX2XMLReader::Val_Always;
  bool doNamespaces = true;
  bool doSchema = true;
  bool schemaFullChecking = true;
  bool namespacePrefixes = false;
  SAX2XMLReader* parser = 0;

  try
Francois Gygi committed
166
  {
167
    XMLPlatformUtils::Initialize();
Francois Gygi committed
168
  }
169
  catch (const XMLException& toCatch)
Francois Gygi committed
170
  {
171 172 173 174
    cout << "  Species::readSpecies: Error during XML initialization :\n"
         << StrX(toCatch.getMessage()) << endl;
    throw;
  }
175

176 177 178 179 180
  parser = XMLReaderFactory::createXMLReader();
  if (valScheme == SAX2XMLReader::Val_Auto)
  {
    parser->setFeature(XMLUni::fgSAX2CoreValidation, true);
    parser->setFeature(XMLUni::fgXercesDynamic, true);
Francois Gygi committed
181
  }
182

183 184 185 186
  if (valScheme == SAX2XMLReader::Val_Never)
  {
    parser->setFeature(XMLUni::fgSAX2CoreValidation, false);
  }
187

188
  if (valScheme == SAX2XMLReader::Val_Always)
Francois Gygi committed
189
  {
190 191
    parser->setFeature(XMLUni::fgSAX2CoreValidation, true);
    parser->setFeature(XMLUni::fgXercesDynamic, false);
Francois Gygi committed
192 193
  }

194 195 196 197 198 199 200 201
  parser->setFeature(XMLUni::fgSAX2CoreNameSpaces, doNamespaces);
  parser->setFeature(XMLUni::fgXercesSchema, doSchema);
  parser->setFeature(XMLUni::fgXercesSchemaFullChecking, schemaFullChecking);
  parser->setFeature(XMLUni::fgSAX2CoreNameSpacePrefixes, namespacePrefixes);

  MemBufInputSource* memBufIS = 0;

  try
Francois Gygi committed
202
  {
203 204 205 206 207 208 209
    memBufIS = new MemBufInputSource
    ( (const XMLByte*) &xmlstr[0], xmlstr.size(), "buf_id", false);
    SpeciesHandler* sp_handler = new SpeciesHandler(sp);
    StructuredDocumentHandler handler(sp_handler);
    parser->setContentHandler(&handler);
    parser->setErrorHandler(&handler);
    parser->parse(*memBufIS);
Francois Gygi committed
210
    // errorCount = parser->getErrorCount();
211 212 213 214 215 216 217 218 219 220 221
    delete sp_handler;
  }

  catch (const XMLException& toCatch)
  {
    cout << "\nAn error occurred\n  Error: "
         << StrX(toCatch.getMessage())
         << "\n" << endl;
    XMLPlatformUtils::Terminate();
    delete parser;
    throw;
Francois Gygi committed
222
  }
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237

  catch (const SAXParseException& e)
  {
    cout << "\na SAXParseException occurred in file "
         << StrX(e.getSystemId())
         << ", line " << e.getLineNumber()
         << ", char " << e.getColumnNumber()
         << "\n  Message: " << StrX(e.getMessage()) << endl;
    XMLPlatformUtils::Terminate();
    delete parser;
    throw;
  }

  delete parser;
  XMLPlatformUtils::Terminate();
Francois Gygi committed
238
}