Salome HOME
Updated copyright comment
[modules/hexablock.git] / src / HEXABLOCK / HexXmlWriter.cxx
1
2 // C++ : Ecriture en XML
3
4 // Copyright (C) 2009-2024  CEA, EDF
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, or (at your option) any later version.
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    xml_mode  = InaStudy;
37 }
38 // ========================================================= setStream
39 int XmlWriter::setStream ()
40 {
41    xml_mode  = InaStream;
42    xml_file  = NULL;
43    on_file   = false;
44
45    return HOK;
46 }
47 // ========================================================= setFile
48 int XmlWriter::setFile (pfile afile)
49 {
50    xml_mode  = InaStudy;
51    xml_file  = afile;
52    on_file   = true;
53
54    if (xml_file==NULL)
55       {
56       xml_file = stdout;
57       xml_mode  = InaStream;
58       return HERR;
59       }
60
61    return HOK;
62 }
63 // ========================================================= setFileName 
64 int XmlWriter::setFileName (cpchar nomfic)
65 {
66    xml_mode  = InaFile;
67    xml_file  = stdout;
68    on_file   = true;
69
70    bool suff = true;
71    int  pext = strlen (nomfic) - 4;
72    if (pext > 0)
73       {
74       std::string sext = &nomfic[pext];
75       set_minus (sext);
76       suff = sext != ".xml";
77       }
78
79    std::string fname = nomfic;
80    if (suff) 
81       fname   += ".xml";
82
83    xml_file = fopen (fname.c_str(), "w");
84    if (xml_file==NULL)
85       {
86       xml_file = stdout;
87       return HERR;
88       }
89
90    return HOK;
91 }
92 // ========================================================= startXml 
93 int XmlWriter::startXml ()
94 {
95    xml_level  = 0;
96    xml_pos    = 0;
97    xml_buffer = "";
98
99    ecrire ("<?xml version='1.0'?>");
100    return HOK;
101 }
102 // ========================================================= closeXml 
103 void XmlWriter::closeXml ()
104 {
105    while (NOT pile_mark.empty())
106        closeMark ();
107
108    if (xml_mode == InaFile && xml_file!=stdout)
109       {
110       fclose (xml_file);
111       xml_file = stdout;
112       }
113 }
114 // ========================================================= addMark 
115 void XmlWriter::addMark (cpchar balise, bool jump)
116 {
117    openMark (balise); 
118    ecrire (">");
119    if (jump) 
120       {
121       alaLigne ();
122       pile_etat.top () = M_SAUT;
123       }
124    else
125       {
126       pile_etat.top () = M_CLOSED;
127       }
128 }
129 // ========================================================= endMark 
130 void XmlWriter::endMark ()
131 {
132    ecrire ("> ");
133    pile_etat.top () = M_SAUT;
134 }
135 // ========================================================= openMark 
136 void XmlWriter::openMark (cpchar balise)
137 {
138    jumpLine ();
139    std::string mot = "<";
140    mot +=  balise;
141    ecrire (mot);
142
143    pile_mark.push (balise);
144    pile_etat.push (M_OPEN);
145    xml_level ++;
146 }
147 // ========================================================= closeMark
148 void XmlWriter::closeMark (bool jump)
149 {
150    std::string balise = pile_mark.top ();
151    std::string mot    = "</";
152    int    etat   = pile_etat.top ();
153
154    xml_level --;
155    switch (etat)
156       {
157       case M_OPEN : ecrire (" />");
158                     break;
159       case M_SAUT : jumpLine ();
160       default :
161            mot += balise;
162            mot += ">";
163            ecrire (mot);
164       }
165
166    pile_mark.pop ();
167    pile_etat.pop ();
168    alaLigne ();
169    if (jump) alaLigne (true);
170 }
171 // ========================================================= jumpLine 
172 void XmlWriter::jumpLine ()
173 {
174    if (xml_pos>0)
175       addMot ("\n");
176
177    xml_pos = xml_level * xml_decal;
178    if (xml_pos > 0)
179       {
180       std::string  space (xml_pos, ' ');
181       addMot (space.c_str());
182       }
183 }
184 // ========================================================= ecrire
185 void XmlWriter::ecrire (cpchar mot)
186 {
187    int lg   = strlen (mot);
188    xml_pos += lg;
189
190    if (xml_pos >= xml_size)
191        {
192        jumpLine ();
193        xml_pos += lg;
194        }
195
196    addMot (mot);
197 }
198 // ========================================================= alaLigne
199 void XmlWriter::alaLigne (bool force)
200 {
201    if (xml_pos==0 && NOT force) 
202       return;
203
204    xml_pos = 0;
205    addMot ("\n");
206 }
207 // ========================================================= addAttribute 
208 void XmlWriter::addAttribute (cpchar cle, cpchar valeur)
209 {
210    std::string phrase = " ";
211    phrase += cle;
212    phrase += " = \"";
213    phrase += valeur;
214    phrase += "\"";
215
216    ecrire (phrase);
217 }
218 END_NAMESPACE_HEXA