Salome HOME
Merge from V6_main_20120808 08Aug12
[samples/atomgen.git] / src / ATOMGEN / ATOMGEN_XmlParser.py
1 # Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
2 #
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Lesser General Public
5 # License as published by the Free Software Foundation; either
6 # version 2.1 of the License.
7 #
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 # Lesser General Public License for more details.
12 #
13 # You should have received a copy of the GNU Lesser General Public
14 # License along with this library; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 #
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 #
19
20 import xml.dom.minidom
21 from ATOMGEN_Data import *
22
23 __doc_name__  = "Atomic"
24 __doc_tag__   = "document"
25 __mol_tag__   = "molecule"
26 __atom_tag__  = "atom"
27 __name_attr__ = "name"
28 __x_attr__    = "x"
29 __y_attr__    = "y"
30 __z_attr__    = "z"
31
32 def readXmlFile( fileName ):
33     """
34     Reads the atomic data from the XML file
35     """
36     data = []
37     doc = xml.dom.minidom.parse( fileName )
38     if str( doc.doctype.name ) != __doc_name__:
39         return # wrong file format
40     docnode = doc.getElementsByTagName( __doc_tag__ )
41     if not docnode:
42         return # wrong file format
43     mols = docnode[0].getElementsByTagName( __mol_tag__ )
44     for mol in mols:
45         c_name = str( mol.getAttribute( __name_attr__ ) )
46         ac =  Molecule( c_name )
47         atoms = mol.getElementsByTagName( __atom_tag__ )
48         for atom in atoms:
49             a_name = str( atom.getAttribute( __name_attr__ ) )
50             x = float( atom.getAttribute( __x_attr__ ) )
51             y = float( atom.getAttribute( __y_attr__ ) )
52             z = float( atom.getAttribute( __z_attr__ ) )
53             ac.addAtom( Atom( a_name, x, y, z ) )
54         data.append( ac )
55     return data
56
57 def writeXmlFile( filename, data ):
58     """
59     Writes the atomic data to the XML file
60     """
61     if not data:
62         raise Exception( "Bad data" )
63     doc = xml.dom.minidom.Document()
64     doc.appendChild(xml.dom.minidom.DocumentType( __doc_name__ ) )
65     docnode = doc.appendChild( doc.createElement( __doc_tag__ ) )
66     for mol in data:
67         molnode = doc.createElement( __mol_tag__ )
68         molnode.setAttribute( __name_attr__, mol.name )
69         for atom in mol.atoms:
70             atomnode = doc.createElement( __atom_tag__ )
71             atomnode.setAttribute( __name_attr__, atom.name )
72             atomnode.setAttribute( __x_attr__, str( atom.x ) )
73             atomnode.setAttribute( __y_attr__, str( atom.y ) )
74             atomnode.setAttribute( __z_attr__, str( atom.z ) )
75             molnode.appendChild( atomnode )
76         docnode.appendChild( molnode )
77     file = open( filename, "w" )
78     file.write( doc.toprettyxml() )
79     file.close()
80     pass