Salome HOME
f75bc5aab5f48adf57bf134e4ace843015c33094
[modules/hexablock.git] / src / HEXABLOCK / HexXmlWriter.hxx
1 // Copyright (C) 2009-2012  CEA/DEN, EDF R&D
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 // Class : Ecriture d'un fichier XML
21 //
22 #ifndef __XML_WRITER_H
23 #define __XML_WRITER_H
24
25 #include "hexa_base.hxx"
26 #include <stack>
27 #include <vector>
28
29 BEGIN_NAMESPACE_HEXA
30
31 class XmlWriter 
32 {
33 public :
34    XmlWriter ();
35   ~XmlWriter ()    { closeXml () ; }
36
37    int    openXml  (string& nom)     { return openXml (nom.c_str()); }
38    int    openXml  (cpchar nom=NULL);
39    void   closeXml ();
40    cpchar getXml   ()                { return xml_buffer.c_str();    }
41
42    void openMark  (cpchar balise);                 // <Balise .. 
43    void addMark   (cpchar balise, bool jump=true); // <Balise> + eol
44    void closeMark (bool jump=false);               // </Balise> ou  />
45    void endMark ();       //         >
46
47    void addAttribute (cpchar attrib, cpchar  valeur); // attrib="valeur"
48    void addAttribute (cpchar attrib, int     valeur);
49    void addAttribute (cpchar attrib, double  valeur);
50    void addAttribute (cpchar attrib, string& valeur);
51
52 private :
53     void jumpLine ();
54     void alaLigne (bool force=false);
55     void ecrire   (cpchar  mot);
56     void ecrire   (string& mot) { ecrire (mot.c_str()) ; }
57     void addMot   (cpchar  mot);
58
59 private :
60     enum  {xml_decal=3, xml_size=80};
61     pfile  xml_file;
62     int    xml_level;
63     int    xml_pos;
64     bool   on_file;
65     std::string  xml_buffer;
66     std::stack <std::string, std::vector <std::string> >  pile_mark; 
67     std::stack <int,         std::vector <int> >          pile_etat; 
68 };
69 // ====================================================== addAttribute (I)
70 inline void XmlWriter::addAttribute (cpchar attrib, int valeur)
71 {
72    char buffer [20]; 
73    sprintf (buffer, "%d", valeur);
74    addAttribute (attrib, buffer);
75 }
76 // ====================================================== addAttribute (R)
77 inline void XmlWriter::addAttribute (cpchar attrib, double valeur)
78 {
79    char buffer [20]; 
80    sprintf (buffer, "%g", valeur);
81    addAttribute (attrib, buffer);
82 }
83 // ====================================================== addAttribute (stl)
84 inline void XmlWriter::addAttribute (cpchar attrib, string& valeur)
85 {
86    addAttribute (attrib, valeur.c_str());
87 }
88 // ====================================================== addMot
89 inline void XmlWriter::addMot (cpchar mot)
90 {
91    if (on_file) 
92       fprintf (xml_file, mot);
93    else 
94       xml_buffer += mot;
95 }
96
97 END_NAMESPACE_HEXA
98 #endif