Salome HOME
First publish of HEXABLOCKcomponant
[modules/hexablock.git] / src / HEXABLOCK / HexXmlWriter.cxx
1
2 // C++ : Ecriture en XML
3
4 #include "HexXmlWriter.hxx"
5
6 BEGIN_NAMESPACE_HEXA
7
8 enum { M_CLOSED, M_OPEN, M_SAUT };
9
10 // ========================================================= Constructeur
11 XmlWriter::XmlWriter  ()
12 {
13    xml_file  = stdout;
14    xml_level = 0;
15    xml_pos   = 0;
16 }
17 // ========================================================= openXml 
18 int XmlWriter::openXml (cpchar nomfic)
19 {
20    xml_level    = 0;
21    xml_pos      = 0;
22    string fname = nomfic;
23
24    fname   += ".xml";
25    xml_file = fopen (fname.c_str(), "w");
26    if (xml_file==NULL)
27       {
28       xml_file = stdout;
29       return HERR;
30       }
31    ecrire ("<?xml version='1.0'?>");
32    return HOK;
33 }
34 // ========================================================= closeXml 
35 void XmlWriter::closeXml ()
36 {
37    while (NOT pile_mark.empty())
38        closeMark ();
39
40    if (xml_file!=stdout)
41       fclose (xml_file);
42
43    xml_file = stdout;
44 }
45 // ========================================================= addMark 
46 void XmlWriter::addMark (cpchar balise, bool jump)
47 {
48    openMark (balise); 
49    ecrire (">");
50    if (jump) 
51       {
52       alaLigne ();
53       pile_etat.top () = M_SAUT;
54       }
55    else
56       {
57       pile_etat.top () = M_CLOSED;
58       }
59 }
60 // ========================================================= endMark 
61 void XmlWriter::endMark ()
62 {
63    ecrire ("> ");
64    pile_etat.top () = M_SAUT;
65 }
66 // ========================================================= openMark 
67 void XmlWriter::openMark (cpchar balise)
68 {
69    jumpLine ();
70    string mot = "<";
71    mot +=  balise;
72    ecrire (mot);
73
74    pile_mark.push (balise);
75    pile_etat.push (M_OPEN);
76    xml_level ++;
77 }
78 // ========================================================= closeMark
79 void XmlWriter::closeMark (bool jump)
80 {
81    string balise = pile_mark.top ();
82    string mot    = "</";
83    int    etat   = pile_etat.top ();
84
85    xml_level --;
86    switch (etat)
87       {
88       case M_OPEN : ecrire (" />");
89                     break;
90       case M_SAUT : jumpLine ();
91       default :
92            mot += balise;
93            mot += ">";
94            ecrire (mot);
95       }
96
97    pile_mark.pop ();
98    pile_etat.pop ();
99    alaLigne ();
100    if (jump) alaLigne (true);
101 }
102 // ========================================================= jumpLine 
103 void XmlWriter::jumpLine ()
104 {
105    if (xml_pos>0)
106       putc ('\n', xml_file);
107
108    xml_pos = xml_level * xml_decal;
109    for (int nc=0 ; nc < xml_pos ; nc++) putc (' ', xml_file);
110 }
111 // ========================================================= ecrire
112 void XmlWriter::ecrire (cpchar mot)
113 {
114    int lg   = strlen (mot);
115    xml_pos += lg;
116
117    if (xml_pos >= xml_size)
118        {
119        jumpLine ();
120        xml_pos += lg;
121        }
122
123    fprintf (xml_file, mot);
124 }
125 // ========================================================= alaLigne
126 void XmlWriter::alaLigne (bool force)
127 {
128    if (xml_pos==0 && NOT force) 
129       return;
130
131    fprintf (xml_file, "\n");
132    xml_pos = 0;
133 }
134 // ========================================================= addAttribute 
135 void XmlWriter::addAttribute (cpchar cle, cpchar valeur)
136 {
137    string phrase = " ";
138    phrase += cle;
139    phrase += " = \"";
140    phrase += valeur;
141    phrase += "\"";
142
143    ecrire (phrase);
144 }
145 END_NAMESPACE_HEXA