4 // Copyright (C) 2009-2013 CEA/DEN, EDF R&D
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "HexXmlTree.hxx"
27 enum { HEND_FILE = -2, HEND_LINE = -1, CRLF = 13 };
29 // ====================================================== Constructeur
30 XmlTree::XmlTree (const string& nom, XmlTree* dad)
48 // ====================================================== Destructeur
52 // ====================================================== findChild
53 XmlTree* XmlTree::findChild (const string& nom)
55 for (int nro = 0 ; nro<nbr_items ; nro++)
57 if (nom == tab_items [nro]->item_name)
58 return tab_items [nro];
62 // ====================================================== findAttribute
63 int XmlTree::findAttribute (const string& nom)
65 for (int nro = 0 ; nro<nbr_attributs ; nro++)
67 if (nom == tab_attributs [nro])
72 // ====================================================== findValue
73 const string& XmlTree::findValue (const string& nom)
75 for (int nro = 0 ; nro<nbr_attributs ; nro++)
77 if (nom == tab_attributs [nro])
78 return tab_values [nro];
82 // ====================================================== findInteger
83 int XmlTree::findInteger (const string& nom)
86 const string chnum = findValue (nom);
90 int lg = chnum.size();
91 for (int nc=0 ; nc<lg ; ++nc)
94 if (car >= '0' && car <= '9')
95 val = 10*val + car - '0';
99 // ====================================================== parseFile
100 int XmlTree::parseFile (const string& ficnom)
110 xml_file = fopen (ficnom.c_str(), "r");
113 cout << " **** Fichier XML '" << ficnom << "' inaccessible"
118 int ier = parseXml ();
121 // ====================================================== parseStream
122 int XmlTree::parseStream (cpchar flux, int& posit)
132 int ier = parseXml ();
136 // ====================================================== parseXml
137 int XmlTree::parseXml ()
141 enum EnumEtat { M_PREMS, M_BALISE_OUVERTE, M_NEUTRE };
142 EnumEtat etat = M_PREMS;
144 XmlTree* node = this;
146 string nom, valeur, foo;
147 while ((item_lu=readItem (nom)) != M_NONE)
152 item_lu = getItem (nom, M_IDENT);
156 node = node -> addChild (nom);
157 etat = M_BALISE_OUVERTE;
161 if (etat != M_BALISE_OUVERTE)
162 putError (" balise ouverte");
167 if (etat == M_BALISE_OUVERTE)
168 putError (" balise ouverte");
169 getItem (nom, M_IDENT);
170 getItem (foo, M_END);
171 node = node -> getParent ();
177 if (etat != M_BALISE_OUVERTE)
178 putError (" balise deja fermee");
179 node = node -> getParent ();
184 getItem (valeur, M_EQUALS);
185 getItem (valeur, M_STRING);
186 node -> addAttribut (nom, valeur);
189 case M_COMMENT : goTo ("-->");
192 case M_PROLOG : goTo ("?>");
200 putError ("Item incorrect");
205 // ====================================================== getItem
206 EnumItem XmlTree::getItem (string& value, EnumItem waited)
208 EnumItem item = readItem (value);
212 putError ("Erreur de sequence");
215 // ====================================================== readItem
216 EnumItem XmlTree::readItem (string& value)
222 if (fic_pos>=len_buffer)
230 char car = fic_buffer [fic_pos++];
241 else if (isalpha (car))
247 char ncar = fic_pos >= len_buffer ? ' ' : fic_buffer [fic_pos];
248 if (car=='/' && ncar == '>')
258 return M_BEGIN_CLOSE;
276 // ====================================================== getIdent
277 int XmlTree::getIdent (string& ident)
281 for (int nc=fic_pos-1; nc<len_buffer ; nc++)
283 char car = fic_buffer[nc];
284 if (isalnum (car) || car =='_')
286 ident.push_back (car);
295 fic_pos = len_buffer;
298 // ====================================================== goTo
299 int XmlTree::goTo (cpchar ouca)
301 int nbc = strlen (ouca) - 1;
305 while ((car = getChar ()) != EOF)
317 // ====================================================== getString
318 int XmlTree::getString (string& chaine)
323 while ((car = getChar ()) != EOF)
327 chaine.push_back (car);
332 // ====================================================== getChar
333 int XmlTree::getChar ()
337 if (fic_pos<len_buffer)
340 return fic_buffer [fic_pos-1];
342 int ier = readLine ();
349 // ====================================================== readLine
350 int XmlTree::readLine ()
361 while (ier==HEND_LINE)
363 int carac = xml_flow != NULL ? xml_flow [pos_flow++]
366 if (carac==EOL || carac==CRLF)
370 else if (carac!=EOF && carac!=EOS)
372 fic_buffer.push_back (carac);
375 else if (len_buffer > 0)
388 // ====================================================== putError
389 void XmlTree::putError (cpchar mess)
391 printf (" ***** Erreur : %s\n", mess);
392 printf (" +++ Derniere ligne lue : nro %d, %deme caractere\n",
394 printf ("%s\n", fic_buffer.c_str());
397 // ====================================================== addChild
398 XmlTree* XmlTree::addChild (const string& nom)
400 XmlTree* child = new XmlTree (nom, this);
401 tab_items.push_back (child);
405 // ====================================================== addAttribut
406 void XmlTree::addAttribut (const string& nom, const string& value)
408 tab_attributs.push_back (nom);
409 tab_values .push_back (value);
411 // printf (" %s = %s\n", nom.c_str(), value.c_str());
413 // ====================================================== dump
414 void XmlTree::dump (int niveau)
417 for (int niv=0 ; niv<niveau ; niv++)
420 cout << marge << item_name << endl;
422 for (int nc=0 ; nc<nbr_attributs ; nc++)
424 cout << marge << " : "
425 << tab_attributs [nc] << " = '" << tab_values [nc]
429 for (int nc=0 ; nc<nbr_items ; nc++)
431 tab_items [nc]->dump (niveau+1);