4 // Copyright (C) 2009-2012 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 // ====================================================== parseFile
83 int XmlTree::parseFile (const string& ficnom)
93 xml_file = fopen (ficnom.c_str(), "r");
96 cout << " **** Fichier XML '" << ficnom << "' inaccessible"
101 int ier = parseXml ();
104 // ====================================================== parseFlow
105 int XmlTree::parseFlow (cpchar flux)
115 int ier = parseXml ();
118 // ====================================================== parseXml
119 int XmlTree::parseXml ()
123 enum EnumEtat { M_PREMS, M_BALISE_OUVERTE, M_NEUTRE };
124 EnumEtat etat = M_PREMS;
126 XmlTree* node = this;
128 string nom, valeur, foo;
129 while ((item_lu=readItem (nom)) != M_NONE)
134 item_lu = getItem (nom, M_IDENT);
138 node = node -> addChild (nom);
139 etat = M_BALISE_OUVERTE;
143 if (etat != M_BALISE_OUVERTE)
144 putError (" balise ouverte");
149 if (etat == M_BALISE_OUVERTE)
150 putError (" balise ouverte");
151 getItem (nom, M_IDENT);
152 getItem (foo, M_END);
153 node = node -> getParent ();
157 if (etat != M_BALISE_OUVERTE)
158 putError (" balise deja fermee");
159 node = node -> getParent ();
164 getItem (valeur, M_EQUALS);
165 getItem (valeur, M_STRING);
166 node -> addAttribut (nom, valeur);
169 case M_COMMENT : goTo ("-->");
172 case M_PROLOG : goTo ("?>");
180 putError ("Item incorrect");
185 // ====================================================== getItem
186 EnumItem XmlTree::getItem (string& value, EnumItem waited)
188 EnumItem item = readItem (value);
192 putError ("Erreur de sequence");
195 // ====================================================== readItem
196 EnumItem XmlTree::readItem (string& value)
202 if (fic_pos>=len_buffer)
210 char car = fic_buffer [fic_pos++];
221 else if (isalpha (car))
227 char ncar = fic_pos >= len_buffer ? ' ' : fic_buffer [fic_pos];
228 if (car=='/' && ncar == '>')
238 return M_BEGIN_CLOSE;
256 // ====================================================== getIdent
257 int XmlTree::getIdent (string& ident)
261 for (int nc=fic_pos-1; nc<len_buffer ; nc++)
263 char car = fic_buffer[nc];
264 if (isalnum (car) || car =='_')
266 ident.push_back (car);
275 fic_pos = len_buffer;
278 // ====================================================== goTo
279 int XmlTree::goTo (cpchar ouca)
281 int nbc = strlen (ouca) - 1;
285 while ((car = getChar ()) != EOF)
297 // ====================================================== getString
298 int XmlTree::getString (string& chaine)
303 while ((car = getChar ()) != EOF)
307 chaine.push_back (car);
312 // ====================================================== getChar
313 int XmlTree::getChar ()
317 if (fic_pos<len_buffer)
320 return fic_buffer [fic_pos-1];
322 int ier = readLine ();
329 // ====================================================== readLine
330 int XmlTree::readLine ()
341 while (ier==HEND_LINE)
343 int carac = xml_flow != NULL ? xml_flow [pos_flow++]
346 if (carac==EOL || carac==CRLF)
350 else if (carac!=EOF && carac!=EOS)
352 fic_buffer.push_back (carac);
355 else if (len_buffer > 0)
368 // ====================================================== putError
369 void XmlTree::putError (cpchar mess)
371 printf (" ***** Erreur : %s\n", mess);
372 printf (" +++ Derniere ligne lue : nro %d, %deme caractere\n",
374 printf ("%s\n", fic_buffer.c_str());
377 // ====================================================== addChild
378 XmlTree* XmlTree::addChild (const string& nom)
380 XmlTree* child = new XmlTree (nom, this);
381 tab_items.push_back (child);
385 // ====================================================== addAttribut
386 void XmlTree::addAttribut (const string& nom, const string& value)
388 tab_attributs.push_back (nom);
389 tab_values .push_back (value);
391 // printf (" %s = %s\n", nom.c_str(), value.c_str());
393 // ====================================================== dump
394 void XmlTree::dump (int niveau)
397 for (int niv=0 ; niv<niveau ; niv++)
400 cout << marge << item_name << endl;
402 for (int nc=0 ; nc<nbr_attributs ; nc++)
404 cout << marge << " : "
405 << tab_attributs [nc] << " = '" << tab_values [nc]
409 for (int nc=0 ; nc<nbr_items ; nc++)
411 tab_items [nc]->dump (niveau+1);