Salome HOME
dc6533aecc89a8800889fddf04a0bd44a60c189e
[tools/install.git] / src / SALOME_XmlHandler.cxx
1 //  File      : SALOME_XmlHandler.cxx
2 //  Created   : Thu Dec 18 12:01:00 2002
3 //  Author    : Vadim SANDLER
4 //  Project   : SALOME
5 //  Module    : Installation Wizard
6 //  Copyright : 2004-2005 CEA
7
8 #include "globals.h"
9
10 #include "SALOME_XmlHandler.hxx"
11 #include "SALOME_ProductsView.hxx"
12 #include "SALOME_InstallWizard.hxx"
13
14 #include <qlineedit.h>
15 #include <qdir.h>
16 #include <qregexp.h>
17
18 // ================================================================
19 /*!
20  *  ::isBoolAttributeSet [ static ]
21  *  Returns true if the attribute stores boolean value and 
22  *  corresponds to True value
23  */
24 // ================================================================
25 static bool isBoolAttributeSet( const QString& attr ) {
26   return ( attr.lower() == "true" || 
27            attr.lower() == "yes"  ||
28            attr.lower() == "ok"   || 
29            ( !attr.stripWhiteSpace().isEmpty() && attr.toInt() != 0 ) );
30 }
31
32 // ================================================================
33 /*!
34  *  ::environmentVariable [ static ]
35  *  Seraches for the environment variable and returns it's
36  *  position on the given string
37  */
38 // ================================================================
39 QString environmentVariable( const QString& str, int& start, int& len ) {
40   QString varName = QString::null;
41   len = 0;
42
43   // Environment variable can be given in the form:
44   // - ${VARIABLE} or
45   // - $(VARIABLE) or 
46   // - $VARIABLE   or
47   // - %VARIABLE%
48   // The first symbol should be the letter.
49   QRegExp rx( "\\$\\{([a-zA-Z]+[a-zA-Z0-9_]*)\\}|\\$\\(([a-zA-Z]+[a-zA-Z0-9_]*)\\)|\\$([a-zA-Z]+[a-zA-Z0-9_]*)|\\%([a-zA-Z]+[a-zA-Z0-9_]*)\\%" );
50
51   int pos = rx.search( str, start );
52   if ( pos != -1 )
53   {
54     start = pos;
55     len = rx.matchedLength();
56     QStringList caps = rx.capturedTexts();
57     for ( uint i = 1; i <= caps.count() && varName.isEmpty(); i++ )
58       varName = *caps.at( i );
59   }
60   return varName;
61 }
62 // ================================================================
63 /*!
64  *  ::substituteVars [ static ]
65  *  Substitutes environment variables in the given string
66  *  by their values
67  */
68 // ================================================================
69 static QString substituteVars( const QString& str ) {
70   QString res = str;
71
72   int start( 0 ), len( 0 );
73   while ( true ) {
74     QString envName = environmentVariable( res, start, len );
75     if ( envName.isNull() )
76       break;
77
78     QString newStr = QString::null;
79     if ( ::getenv( envName ) )
80       newStr = QString( ::getenv( envName ) );
81
82     res.replace( start, len, newStr );
83   }
84
85   return res;
86 }
87
88 // ================================================================
89 /*!
90  *  StructureParser::StructureParser
91  *  Constructor
92  */
93 // ================================================================
94 StructureParser::StructureParser( SALOME_InstallWizard* wizard )
95      : QXmlDefaultHandler(), 
96        myWizard( wizard ), 
97        myTree( 0 ), 
98        myTargetDir( 0 ), 
99        myTempDir( 0 )
100 {
101 }
102 // ================================================================
103 /*!
104  *  StructureParser::setProductsList
105  *  Sets products list view
106  */
107 // ================================================================
108 void StructureParser::setProductsList( ProductsView* tree )
109 {
110   myTree = tree;
111 }
112 // ================================================================
113 /*!
114  *  StructureParser::setTargetDir
115  *  Sets target directory widget
116  */
117 // ================================================================
118 void StructureParser::setTargetDir( QLineEdit* dir )
119 {
120   QString home = QDir::homeDirPath();
121   myTargetDir = dir;
122   if ( myTargetDir && !home.isEmpty() )
123     myTargetDir->setText( home + QDir::separator() + "salome" );
124 }
125 // ================================================================
126 /*!
127  *  StructureParser::setTempDir
128  *  Sets temp directory widget
129  */
130 // ================================================================
131 void StructureParser::setTempDir( QLineEdit* dir )
132 {
133   myTempDir = dir;
134   if ( myTempDir )
135     myTempDir->setText( "/tmp" );
136 }
137 // ================================================================
138 /*!
139  *  StructureParser::startElement
140  *  Begins parsing of the xml dom-element
141  */  
142 // ================================================================
143 bool StructureParser::startElement( const QString&        /*namespaceURI*/,
144                                     const QString&        /*localName*/,
145                                     const QString&        qName,
146                                     const QXmlAttributes& attributes)
147 {
148   ___MESSAGE___( qName );
149   ___MESSAGE___( attributes.length() );
150   QCheckListItem* element;
151   if (( qName == "config" ) && ( attributes.length() > 0 ) ) {
152     QString myVersion, myCaption, myCopyright, myLicense, myOS;
153     if ( attributes.value( "version" ) ) {
154       myVersion = attributes.value( "version" ).stripWhiteSpace();
155       if ( myWizard && !myVersion.isEmpty() ) 
156         myWizard->setVersion( myVersion );
157     }
158     if ( attributes.value( "caption" ) ) {
159       myCaption = attributes.value( "caption" ).arg( myVersion ).stripWhiteSpace();
160       if ( myWizard && !myCaption.isEmpty() ) 
161         myWizard->setCaption( myCaption );
162     }
163     if ( attributes.value( "copyright" ) ) {
164       myCopyright = attributes.value( "copyright" ).stripWhiteSpace();
165       if ( myWizard && !myCopyright.isEmpty() ) 
166         myWizard->setCopyright( myCopyright );
167     }
168     if ( attributes.value( "license" ) ) {
169       myLicense = attributes.value( "license" ).stripWhiteSpace();
170       if ( myWizard && !myLicense.isEmpty() ) 
171         myWizard->setLicense( myLicense );
172     }
173     if ( attributes.value( "os" ) ) {
174       myOS = attributes.value( "os" ).stripWhiteSpace();
175       if ( myWizard && !myOS.isEmpty() ) 
176         myWizard->setOS( myOS );
177     }
178   } 
179   else if (( qName == "product" ) && ( attributes.length() > 0 ) && myTree && myWizard ) {
180     if ( isBoolAttributeSet( attributes.value( "disable" ) ) )
181       return true;
182     
183     QString install = attributes.value( "install" );
184     QStringList supported = QStringList::split( ",", attributes.value( "supported" ) );
185     QString script = attributes.value( "script" );
186     QStringList deps = QStringList();
187     if ( attributes.value( "dependancies" ) != "" )
188       deps = QStringList::split( ",", attributes.value( "dependancies" ), false );
189     element = myTree->addItem( attributes.value( "name" ), attributes.value( "version" ), install, supported, script );
190     QStringList diskspace = QStringList::split(",",attributes.value( "installdiskspace" ) );
191     QString descr = QString::null;
192     if ( attributes.value( "description" ) != "" )
193       descr = attributes.value( "description" ).stripWhiteSpace();
194     QString ctx = QString::null;
195     if ( attributes.value( "context" ) != "" )
196       ctx = attributes.value( "context" ).stripWhiteSpace().lower();
197     bool pickUp = isBoolAttributeSet( attributes.value( "pickupenv" ) );
198     myWizard->setDependancies( element, 
199                                Dependancies( attributes.value( "name" ), 
200                                              deps,
201                                              ( diskspace.count() > 0 ? diskspace[ 0 ].toInt() : 0 ), 
202                                              ( diskspace.count() > 1 ? diskspace[1].toInt() : ( diskspace.count() > 0 ? diskspace[0].toInt() : 0 ) ), 
203                                              attributes.value( "temporarydiskspace" ).toInt(),
204                                              install,
205                                              descr,
206                                              ctx,
207                                              pickUp ) );
208   }
209   else if (( qName == "path" ) && ( attributes.length() > 0 ) && myWizard ) {
210     if ( myTargetDir )
211       myTargetDir->setText( substituteVars( attributes.value( "targetdir" ) ) );
212
213     if ( myTempDir ) {
214       if ( !attributes.value( "tempdir" ).stripWhiteSpace().isEmpty() )
215         myTempDir->setText( substituteVars( attributes.value( "tempdir" ) ) );
216     }
217   }
218   return true;
219 }
220 // ================================================================
221 /*!
222  *  StructureParser::endElement
223  *  Finishes parsing of the xml dom-element
224  */
225 // ================================================================
226 bool StructureParser::endElement( const QString&, 
227                                   const QString&,
228                                   const QString& )
229 {
230   return true;
231 }
232