Salome HOME
Fix bug 12796: Warning missed for the bad file 'test18.med'
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_XmlHandler.cxx
1 //  SMESH SMESHGUI : reading of xml file with list of available hypotheses and algorithms
2 //  Copyright (C) 2003  CEA
3 //
4 //  This library is free software; you can redistribute it and/or
5 // //  modify it under the terms of the GNU Lesser General Public
6 //  License as published by the Free Software Foundation; either
7 //  version 2.1 of the License.
8 //
9
10 //  This library is distributed in the hope that it will be useful,
11 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 //  Lesser General Public License for more details.
14 //
15 //  You should have received a copy of the GNU Lesser General Public
16 //  License along with this library; if not, write to the Free Software
17 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18 //
19 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 //
21 //
22 //
23 //  File   : SMESHGUI_XmlHandler.cxx
24 //  Author : Julia DOROVSKIKH
25 //  Module : SMESH
26 //  $Header$
27
28 #define  INCLUDE_MENUITEM_DEF 
29
30 // QT Include
31 #include <qfileinfo.h>
32 #include <qstringlist.h>
33
34 #include "SMESHGUI.h"
35 #include "SUIT_ResourceMgr.h"
36 #include "SUIT_Desktop.h"
37
38 #include "SMESHGUI_XmlHandler.h"
39 #include "SMESHGUI_Hypotheses.h"
40 #include "SMESHGUI_Utils.h"
41
42 #include "utilities.h"
43
44 using namespace std;
45
46 /*!
47   Constructor
48 */
49 SMESHGUI_XmlHandler::SMESHGUI_XmlHandler()
50 {
51 }
52
53 /*!
54   Destructor
55 */
56 SMESHGUI_XmlHandler::~SMESHGUI_XmlHandler()
57 {
58 }
59
60 /*!
61   Starts parsing of document. Does some initialization
62
63   Reimplemented from QXmlDefaultHandler.
64 */
65 bool SMESHGUI_XmlHandler::startDocument()
66 {
67   myErrorProt = "";
68   return TRUE;
69 }
70
71 /*!
72   Does different actions depending on the name of the tag and the
73   state you are in document.
74
75   Reimplemented from QXmlDefaultHandler.
76 */
77 bool SMESHGUI_XmlHandler::startElement (const QString&, const QString&, 
78                                         const QString& qName, 
79                                         const QXmlAttributes& atts)
80 {
81   if (qName == "meshers")
82   {
83     myHypothesesMap.clear();
84     myAlgorithmsMap.clear();
85   }
86   else if (qName == "meshers-group") // group of hypotheses and algorithms
87   {
88 //    if (atts.value("server-lib") != "")
89     {
90       myPluginName = atts.value("name");
91       myServerLib  = atts.value("server-lib");
92       myClientLib  = atts.value("gui-lib");
93
94       QString aResName = atts.value("resources");
95       if (aResName != "")
96       {
97         MESSAGE("Loading Resources " << aResName);
98         SUIT_ResourceMgr* resMgr = SMESHGUI::resourceMgr();
99         resMgr->loadTranslator("resources",aResName+"_msg_en.qm");
100         resMgr->loadTranslator("resources",aResName+"_images.qm");
101       }
102     }
103   }
104   else if (qName == "hypotheses") // group of hypotheses
105   {
106   }
107   else if (qName == "algorithms") // group of algorithms
108   {
109   }
110   else if (qName == "hypothesis" || qName == "algorithm") // hypothesis or algorithm
111   {
112     if (atts.value("type") != "")
113     {
114       QString aHypAlType = atts.value("type");
115       QString aLabel = atts.value("label-id");
116       QString anIcon = atts.value("icon-id");
117       bool isAux = atts.value("auxiliary") == "true";
118       
119       QString aDimStr = atts.value("dim");
120       aDimStr = aDimStr.remove( ' ' );
121       QStringList aDimList = QStringList::split( ',', aDimStr );
122       QStringList::iterator anIter;
123       bool isOk;
124       QValueList<int> aDim;
125       for ( anIter = aDimList.begin(); anIter != aDimList.end(); ++anIter )
126       {
127         int aVal = (*anIter).toInt( &isOk );
128         if ( isOk )
129           aDim.append( aVal - 1 );
130       }
131
132       // for algo
133       enum { HYPOS = 0, OPT_HYPOS, INPUT, OUTPUT, NB_ATTRIBUTES };
134       const char* name [NB_ATTRIBUTES] = { "hypos", "opt-hypos", "input", "output" };
135       QStringList attr [NB_ATTRIBUTES];
136       for ( int i = 0; i < NB_ATTRIBUTES; ++i ) {
137         QString aStr = atts.value( name[i] );
138         if ( !aStr.isEmpty() ) {
139           aStr.remove( ' ' );
140           attr[ i ] = QStringList::split( ',', aStr );
141         }
142       }
143       
144       HypothesisData* aHypLibNames =
145         new HypothesisData (aHypAlType, myPluginName, myServerLib, myClientLib,
146                             aLabel, anIcon, aDim, isAux,
147                             attr[ HYPOS ], attr[ OPT_HYPOS ], attr[ INPUT ], attr[ OUTPUT ]);
148
149       if (qName == "algorithm")
150       {
151         myAlgorithmsMap[(char*)aHypAlType.latin1()] = aHypLibNames;
152       }
153       else
154       {
155         myHypothesesMap[(char*)aHypAlType.latin1()] = aHypLibNames;
156       }
157     }
158   }
159   else if (qName == "hypotheses-set-group") // group of sets of hypotheses
160   {
161   }
162   else if (qName == "hypotheses-set") // a set of hypotheses
163   {
164     if (atts.value("name") != "")
165     {
166       HypothesesSet* aHypoSet = new HypothesesSet ( atts.value("name") );
167       myListOfHypothesesSets.push_back( aHypoSet );
168
169       for ( int isHypo = 0; isHypo < 2; ++isHypo )
170       {
171         QString aHypos = isHypo ? atts.value("hypos") : atts.value("algos");
172         aHypos = aHypos.remove( ' ' );
173         QStringList* aHypoList = isHypo ? & aHypoSet->HypoList : & aHypoSet->AlgoList;
174         *aHypoList = QStringList::split( ',', aHypos );
175       }
176     }
177   }
178   else
179   {
180     // error
181     return FALSE;
182   }
183   return TRUE;
184 }
185
186
187 /*!
188   Reimplemented from QXmlDefaultHandler.
189 */
190 bool SMESHGUI_XmlHandler::endElement (const QString&, const QString&, const QString&)
191 {
192   return TRUE;
193 }
194
195
196 /*!
197   Reimplemented from QXmlDefaultHandler.
198 */
199 bool SMESHGUI_XmlHandler::characters (const QString& ch)
200 {
201   // we are not interested in whitespaces
202   QString ch_simplified = ch.simplifyWhiteSpace();
203   if ( ch_simplified.isEmpty() )
204     return TRUE;
205   return TRUE;
206 }
207
208
209 /*!
210   Returns the default error string.
211
212   Reimplemented from QXmlDefaultHandler.
213 */
214 QString SMESHGUI_XmlHandler::errorString()
215 {
216   return "the document is not in the quote file format";
217 }
218
219 /*!
220   Returns the error protocol if parsing failed
221
222   Reimplemented from QXmlDefaultHandler.
223 */
224 QString SMESHGUI_XmlHandler::errorProtocol()
225 {
226   return myErrorProt;
227 }
228
229 /*!
230   Returns exception
231
232   Reimplemented from QXmlDefaultHandler.
233 */
234 bool SMESHGUI_XmlHandler::fatalError (const QXmlParseException& exception)
235 {
236   myErrorProt += QString("fatal parsing error: %1 in line %2, column %3\n")
237     .arg(exception.message())
238     .arg(exception.lineNumber())
239     .arg(exception.columnNumber());
240   
241   return QXmlDefaultHandler::fatalError( exception );
242 }