AtomSetHandler.C 6.21 KB
Newer Older
Francois Gygi committed
1 2
////////////////////////////////////////////////////////////////////////////////
//
Francois Gygi committed
3 4 5 6
// Copyright (c) 2008 The Regents of the University of California
//
// This file is part of Qbox
//
Francois Gygi committed
7 8
// 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
9 10 11 12 13 14
// 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
15 16 17 18 19 20
// AtomSetHandler.C
//
////////////////////////////////////////////////////////////////////////////////

#include "AtomSetHandler.h"
#include "AtomSet.h"
Francois Gygi committed
21
#include "Species.h"
Francois Gygi committed
22 23 24
#include "SpeciesHandler.h"
#include "SpeciesReader.h"
#include "StrX.h"
Francois Gygi committed
25
#include "SampleReader.h"
Francois Gygi committed
26 27 28 29 30 31 32
using namespace xercesc;
#include <iostream>
#include <cassert>
#include <sstream>
using namespace std;

////////////////////////////////////////////////////////////////////////////////
33
AtomSetHandler::AtomSetHandler(AtomSet& as) :
Francois Gygi committed
34 35 36 37 38 39 40 41 42
  as_(as) {}

////////////////////////////////////////////////////////////////////////////////
AtomSetHandler::~AtomSetHandler() {}

////////////////////////////////////////////////////////////////////////////////
void AtomSetHandler::startElement(const XMLCh* const uri,
  const XMLCh* const localname, const XMLCh* const qname,
  const Attributes& attributes)
43
{
Francois Gygi committed
44 45
  // cout << " AtomSetHandler::startElement " << StrX(qname) << endl;
  string locname(XMLString::transcode(localname));
46

Francois Gygi committed
47 48
  // consider only elements that are dealt with directly by AtomSetHandler
  // i.e. "atom". The "species" element is delegated to a SpeciesHandler
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
  if ( locname == "unit_cell")
  {
    D3vector a,b,c;
    unsigned int len = attributes.getLength();
    for (unsigned int index = 0; index < len; index++)
    {
      string attrname(XMLString::transcode(attributes.getLocalName(index)));
      string attrval(XMLString::transcode(attributes.getValue(index)));
      istringstream stst(attrval);
      if ( attrname == "a")
      {
        stst >> a;
      }
      else if ( attrname == "b" )
      {
        stst >> b;
      }
      else if ( attrname == "c" )
      {
        stst >> c;
      }
    }

Francois Gygi committed
72
    as_.set_cell(a,b,c);
73 74
  }
  else if ( locname == "atom")
Francois Gygi committed
75 76 77
  {
    // set default velocity to zero
    current_atom_velocity = D3vector(0.0,0.0,0.0);
78

Francois Gygi committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
    unsigned int len = attributes.getLength();
    for (unsigned int index = 0; index < len; index++)
    {
      string attrname(XMLString::transcode(attributes.getLocalName(index)));
      if ( attrname == "name")
      {
        current_atom_name = StrX(attributes.getValue(index)).localForm();
      }
      else if ( attrname == "species" )
      {
        current_atom_species = StrX(attributes.getValue(index)).localForm();
      }
    }
  }
}

////////////////////////////////////////////////////////////////////////////////
void AtomSetHandler::endElement(const XMLCh* const uri,
  const XMLCh* const localname, const XMLCh* const qname, string& content)
{
  string locname(XMLString::transcode(localname));
  // cout << " AtomSetHandler::endElement " << locname << endl;
  istringstream stst(content);
102 103 104 105
  if ( locname == "unit_cell")
  {
  }
  else if ( locname == "atom")
Francois Gygi committed
106 107 108
  {
    // create an instance of Atom using the current values read so far
    // add the Atom to the current AtomSet
109
    // cout << " AtomSetHandler::endElement: creating Atom(position="
Francois Gygi committed
110
    //      << current_atom_position
111
    //      << ", velocity=" << current_atom_velocity
Francois Gygi committed
112 113 114
    //      << ", name=" << current_atom_name
    //      << ", species=" << current_atom_species
    //      << ")" << endl;
115

Francois Gygi committed
116 117 118 119 120 121 122 123 124 125 126 127
    try
    {
      Atom* a = new Atom(current_atom_name, current_atom_species,
                         current_atom_position, current_atom_velocity);
      as_.addAtom(a);
    }
    catch (...)
    {
      cout << " AtomSetHandler::endElement: and exception occurred"
           << " in AtomSet::addAtom" << endl;
      throw;
    }
128

Francois Gygi committed
129 130 131 132 133 134 135 136 137 138 139 140 141
  }
  else if ( locname == "position" )
  {
    stst >> current_atom_position;
  }
  else if ( locname == "velocity" )
  {
    stst >> current_atom_velocity;
  }
}

////////////////////////////////////////////////////////////////////////////////
StructureHandler* AtomSetHandler::startSubHandler(const XMLCh* const uri,
142
    const XMLCh* const localname, const XMLCh* const qname,
Francois Gygi committed
143 144 145 146 147
    const Attributes& attributes)
{
  // check if element qname can be processed by another StructureHandler
  // If it can, return a pointer to the StructureHandler, otherwise return 0
  // cout << " AtomSetHandler::startSubHandler " << StrX(qname) << endl;
148

Francois Gygi committed
149 150 151 152 153 154 155 156 157 158 159 160 161
  string locname(XMLString::transcode(localname));
  if ( locname == "species")
  {
    // check for species attributes
    unsigned int len = attributes.getLength();
    for (unsigned int index = 0; index < len; index++)
    {
      string attrname(XMLString::transcode(attributes.getLocalName(index)));
      if ( attrname == "name")
      {
        current_species_name = StrX(attributes.getValue(index)).localForm();
      }
    }
162

Francois Gygi committed
163
    // delegate to SpeciesHandler
164
    current_species = new Species(current_species_name);
Francois Gygi committed
165 166 167 168 169 170 171 172 173 174
    return new SpeciesHandler(*current_species);
  }
  else
  {
    return 0;
  }
}

////////////////////////////////////////////////////////////////////////////////
void AtomSetHandler::endSubHandler(const XMLCh* const uri,
175
    const XMLCh* const localname, const XMLCh* const qname,
Francois Gygi committed
176 177 178 179 180 181
    const StructureHandler* const last)
{
  string locname(XMLString::transcode(localname));
  // cout << " AtomSetHandler::endSubHandler " << locname << endl;
  if ( locname == "species" )
  {
182
    SpeciesReader sp_reader;
183

Francois Gygi committed
184 185 186
    // check if only the uri was provided
    if ( current_species->uri() != "" )
    {
Francois Gygi committed
187
      // href was found in species definition
Francois Gygi committed
188
      // attempt to read the species from that uri
189

190
      sp_reader.uri_to_species(current_species->uri(),*current_species);
Francois Gygi committed
191
    }
192 193

    // cout << "AtomSetHandler::endSubHandler: adding Species:"
Francois Gygi committed
194 195 196 197 198 199 200 201 202 203 204 205 206 207
    //      << current_species_name << endl;
    try
    {
      as_.addSpecies(current_species,current_species_name);
    }
    catch (...)
    {
      cout << " AtomSetHandler::endSubHandler: and exception occurred"
           << " in AtomSet::addSpecies" << endl;
      throw;
    }
  }
  delete last;
}