Salome HOME
Merge from V6_main_20120808 08Aug12
[modules/hexablock.git] / src / HEXABLOCK / HexXmlWriter.cxx
1
2 // C++ : Ecriture en XML
3
4 // Copyright (C) 2009-2012  CEA/DEN, EDF R&D
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "HexXmlWriter.hxx"
24
25 BEGIN_NAMESPACE_HEXA
26
27 enum { M_CLOSED, M_OPEN, M_SAUT };
28
29 // ========================================================= Constructeur
30 XmlWriter::XmlWriter  ()
31 {
32    xml_file  = stdout;
33    xml_level = 0;
34    xml_pos   = 0;
35    on_file   = true;
36 }
37 // ========================================================= openXml 
38 int XmlWriter::openXml (cpchar nomfic)
39 {
40    xml_level  = 0;
41    xml_pos    = 0;
42    xml_buffer = "";
43    xml_file   = stdout;
44    on_file    = false;
45
46    if (nomfic != NULL)
47       {
48       on_file   = true;
49       bool suff = true;
50       int  pext = strlen (nomfic) - 4;
51       if (pext > 0)
52          {
53          string sext = &nomfic[pext];
54          set_minus (sext);
55          suff = sext != ".xml";
56          }
57       string fname = nomfic;
58       if (suff) 
59          fname   += ".xml";
60       xml_file = fopen (fname.c_str(), "w");
61       if (xml_file==NULL)
62          {
63          xml_file = stdout;
64          return HERR;
65          }
66       }
67
68    ecrire ("<?xml version='1.0'?>");
69    return HOK;
70 }
71 // ========================================================= closeXml 
72 void XmlWriter::closeXml ()
73 {
74    while (NOT pile_mark.empty())
75        closeMark ();
76
77    if (xml_file!=stdout)
78       fclose (xml_file);
79
80    xml_file = stdout;
81 }
82 // ========================================================= addMark 
83 void XmlWriter::addMark (cpchar balise, bool jump)
84 {
85    openMark (balise); 
86    ecrire (">");
87    if (jump) 
88       {
89       alaLigne ();
90       pile_etat.top () = M_SAUT;
91       }
92    else
93       {
94       pile_etat.top () = M_CLOSED;
95       }
96 }
97 // ========================================================= endMark 
98 void XmlWriter::endMark ()
99 {
100    ecrire ("> ");
101    pile_etat.top () = M_SAUT;
102 }
103 // ========================================================= openMark 
104 void XmlWriter::openMark (cpchar balise)
105 {
106    jumpLine ();
107    string mot = "<";
108    mot +=  balise;
109    ecrire (mot);
110
111    pile_mark.push (balise);
112    pile_etat.push (M_OPEN);
113    xml_level ++;
114 }
115 // ========================================================= closeMark
116 void XmlWriter::closeMark (bool jump)
117 {
118    string balise = pile_mark.top ();
119    string mot    = "</";
120    int    etat   = pile_etat.top ();
121
122    xml_level --;
123    switch (etat)
124       {
125       case M_OPEN : ecrire (" />");
126                     break;
127       case M_SAUT : jumpLine ();
128       default :
129            mot += balise;
130            mot += ">";
131            ecrire (mot);
132       }
133
134    pile_mark.pop ();
135    pile_etat.pop ();
136    alaLigne ();
137    if (jump) alaLigne (true);
138 }
139 // ========================================================= jumpLine 
140 void XmlWriter::jumpLine ()
141 {
142    if (xml_pos>0)
143       addMot ("\n");
144
145    xml_pos = xml_level * xml_decal;
146    if (xml_pos > 0)
147       {
148       string  space (xml_pos, ' ');
149       addMot (space.c_str());
150       }
151 }
152 // ========================================================= ecrire
153 void XmlWriter::ecrire (cpchar mot)
154 {
155    int lg   = strlen (mot);
156    xml_pos += lg;
157
158    if (xml_pos >= xml_size)
159        {
160        jumpLine ();
161        xml_pos += lg;
162        }
163
164    addMot (mot);
165 }
166 // ========================================================= alaLigne
167 void XmlWriter::alaLigne (bool force)
168 {
169    if (xml_pos==0 && NOT force) 
170       return;
171
172    xml_pos = 0;
173    addMot ("\n");
174 }
175 // ========================================================= addAttribute 
176 void XmlWriter::addAttribute (cpchar cle, cpchar valeur)
177 {
178    string phrase = " ";
179    phrase += cle;
180    phrase += " = \"";
181    phrase += valeur;
182    phrase += "\"";
183
184    ecrire (phrase);
185 }
186 END_NAMESPACE_HEXA