Salome HOME
Merge from BR_V5_DEV 16Feb09
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_XmlHandler.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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 // SMESH SMESHGUI : reading of xml file with list of available hypotheses and algorithms
23 // File   : SMESHGUI_XmlHandler.cxx
24 // Author : Julia DOROVSKIKH, Open CASCADE S.A.S.
25 // SMESH includes
26 //
27 #include "SMESHGUI_XmlHandler.h"
28
29 #include "SMESHGUI.h"
30 #include "SMESHGUI_Hypotheses.h"
31
32 // SALOME GUI includes
33 #include "SUIT_ResourceMgr.h"
34
35 // SALOME KERNEL includes
36 #include <utilities.h>
37
38 /*!
39   Constructor
40 */
41 SMESHGUI_XmlHandler::SMESHGUI_XmlHandler()
42 {
43 }
44
45 /*!
46   Destructor
47 */
48 SMESHGUI_XmlHandler::~SMESHGUI_XmlHandler()
49 {
50 }
51
52 /*!
53   Starts parsing of document. Does some initialization
54
55   Reimplemented from QXmlDefaultHandler.
56 */
57 bool SMESHGUI_XmlHandler::startDocument()
58 {
59   myErrorProt = "";
60   return true;
61 }
62
63 /*!
64   Does different actions depending on the name of the tag and the
65   state you are in document.
66
67   Reimplemented from QXmlDefaultHandler.
68 */
69 bool SMESHGUI_XmlHandler::startElement (const QString&, const QString&, 
70                                         const QString& qName, 
71                                         const QXmlAttributes& atts)
72 {
73   if (qName == "meshers")
74   {
75     myHypothesesMap.clear();
76     myAlgorithmsMap.clear();
77   }
78   else if (qName == "meshers-group") // group of hypotheses and algorithms
79   {
80 //    if (atts.value("server-lib") != "")
81     {
82       myPluginName = atts.value("name");
83       myServerLib  = atts.value("server-lib");
84       myClientLib  = atts.value("gui-lib");
85 /* It's Need to tranlate lib name for WIN32 or X platform
86  * (only client lib, because server lib translates in SMESH_Gen_i::createHypothesis
87  *  for normal work of *.py files )
88  */
89       if( !myClientLib.isEmpty() )
90       {
91 #ifdef WNT
92       //myServerLib += ".dll";
93         myClientLib += ".dll";
94 #else
95       //myServerLib = "lib" + myServerLib + ".so";
96         myClientLib = "lib" + myClientLib + ".so";
97 #endif
98       }
99
100
101       QString aResName = atts.value("resources");
102       if (aResName != "")
103       {
104         MESSAGE("Loading Resources " << aResName.toLatin1().data());
105         SUIT_ResourceMgr* resMgr = SMESHGUI::resourceMgr();
106         QString lang = resMgr->stringValue( resMgr->langSection(), "language", "en" );
107         resMgr->loadTranslator( "resources", QString( "%1_msg_%2.qm" ).arg( aResName, lang ) );
108         resMgr->loadTranslator( "resources", QString( "%1_images.qm" ).arg( aResName, lang ) );
109       }
110     }
111   }
112   else if (qName == "hypotheses") // group of hypotheses
113   {
114   }
115   else if (qName == "algorithms") // group of algorithms
116   {
117   }
118   else if (qName == "hypothesis" || qName == "algorithm") // hypothesis or algorithm
119   {
120     if (atts.value("type") != "")
121     {
122       QString aHypAlType = atts.value("type");
123       QString aLabel = atts.value("label-id");
124       QString anIcon = atts.value("icon-id");
125       bool isAux = atts.value("auxiliary") == "true";
126       bool isNeedGeom = true, isSupportSubmeshes = false;
127       QString aNeedGeom = atts.value("need-geom");
128       if ( !aNeedGeom.isEmpty() )
129         isNeedGeom = (aNeedGeom == "true");
130       QString suppSub = atts.value("support-submeshes");
131       if ( !suppSub.isEmpty() )
132         isSupportSubmeshes = (suppSub == "true");
133
134       QString aDimStr = atts.value("dim");
135       aDimStr = aDimStr.remove( ' ' );
136       QStringList aDimList = aDimStr.split( ',', QString::SkipEmptyParts );
137       QStringList::iterator anIter;
138       bool isOk;
139       QList<int> aDim;
140       for ( anIter = aDimList.begin(); anIter != aDimList.end(); ++anIter )
141       {
142         int aVal = (*anIter).toInt( &isOk );
143         if ( isOk )
144           aDim.append( aVal );
145       }
146
147       // for algo
148       enum { HYPOS = 0, OPT_HYPOS, INPUT, OUTPUT, NB_ATTRIBUTES };
149       const char* name [NB_ATTRIBUTES] = { "hypos", "opt-hypos", "input", "output" };
150       QStringList attr [NB_ATTRIBUTES];
151       for ( int i = 0; i < NB_ATTRIBUTES; ++i ) {
152         QString aStr = atts.value( name[i] );
153         if ( !aStr.isEmpty() ) {
154           aStr.remove( ' ' );
155           attr[ i ] = aStr.split( ',', QString::SkipEmptyParts );
156         }
157       }
158       
159       HypothesisData* aHypData =
160         new HypothesisData (aHypAlType, myPluginName, myServerLib, myClientLib,
161                             aLabel, anIcon, aDim, isAux,
162                             attr[ HYPOS ], attr[ OPT_HYPOS ], attr[ INPUT ], attr[ OUTPUT ],
163                             isNeedGeom, isSupportSubmeshes );
164
165       if (qName == "algorithm")
166       {
167         myAlgorithmsMap[aHypAlType] = aHypData;
168       }
169       else
170       {
171         myHypothesesMap[aHypAlType] = aHypData;
172       }
173     }
174   }
175   else if (qName == "hypotheses-set-group") // group of sets of hypotheses
176   {
177   }
178   else if (qName == "hypotheses-set") // a set of hypotheses
179   {
180     if (atts.value("name") != "")
181     {
182       HypothesesSet* aHypoSet = new HypothesesSet ( atts.value("name") );
183       myListOfHypothesesSets.append( aHypoSet );
184
185       for ( int isHypo = 0; isHypo < 2; ++isHypo )
186       {
187         QString aHypos = isHypo ? atts.value("hypos") : atts.value("algos");
188         aHypos = aHypos.remove( ' ' );
189         QStringList* aHypoList = isHypo ? & aHypoSet->HypoList : & aHypoSet->AlgoList;
190         *aHypoList = aHypos.split( ',', QString::SkipEmptyParts );
191       }
192     }
193   }
194   else
195   {
196     // error
197     return false;
198   }
199   return true;
200 }
201
202
203 /*!
204   Reimplemented from QXmlDefaultHandler.
205 */
206 bool SMESHGUI_XmlHandler::endElement (const QString&, const QString&, const QString&)
207 {
208   return true;
209 }
210
211
212 /*!
213   Reimplemented from QXmlDefaultHandler.
214 */
215 bool SMESHGUI_XmlHandler::characters (const QString& ch)
216 {
217   // we are not interested in whitespaces
218   QString ch_simplified = ch.simplified();
219   if ( ch_simplified.isEmpty() )
220     return true;
221   return true;
222 }
223
224
225 /*!
226   Returns the default error string.
227
228   Reimplemented from QXmlDefaultHandler.
229 */
230 QString SMESHGUI_XmlHandler::errorString()
231 {
232   return "the document is not in the quote file format";
233 }
234
235 /*!
236   Returns the error protocol if parsing failed
237
238   Reimplemented from QXmlDefaultHandler.
239 */
240 QString SMESHGUI_XmlHandler::errorProtocol()
241 {
242   return myErrorProt;
243 }
244
245 /*!
246   Returns exception
247
248   Reimplemented from QXmlDefaultHandler.
249 */
250 bool SMESHGUI_XmlHandler::fatalError (const QXmlParseException& exception)
251 {
252   myErrorProt += QString("fatal parsing error: %1 in line %2, column %3\n")
253     .arg(exception.message())
254     .arg(exception.lineNumber())
255     .arg(exception.columnNumber());
256   
257   return QXmlDefaultHandler::fatalError( exception );
258 }