Changeset 964 for XmlTools2/trunk/xmlpatch.cpp
- Timestamp:
- Feb 24, 2014, 4:36:44 PM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
XmlTools2/trunk/xmlpatch.cpp
r960 r964 47 47 void XmlPatch::insertNodesOperation(const QString &xmlString, XmlFilter &filters, const QString &xPathExpression, const QString &filesWildcard){ 48 48 49 Q StringListfilesToProcess;49 QVector<QString> filesToProcess; 50 50 QList<pugi::xml_node> nodesToInsertion; 51 51 pugi::xml_document newNode; … … 115 115 void XmlPatch::removeNodesOperation(XmlFilter &filters, const QString &xPathExpression, const QString &filesWildcard){ 116 116 117 Q StringListfilesToProcess;117 QVector<QString> filesToProcess; 118 118 119 119 QList<pugi::xml_node> nodesToDeletion; … … 212 212 } 213 213 214 QScriptValue echo(QScriptContext *context, QScriptEngine *engine)215 {216 std::cout << context->argument(0).toString().toUtf8().constData() << std::endl;217 218 return "";219 }220 221 214 void XmlPatch::executeCustomCommandOperation(const QString &jsString, const QString &filesWildcard){ 222 215 223 QString rexmlString, jsxmlString; 224 QStringList filesToProcess=UtilXmlTools::getAllXmlFilesByWildcard(filesWildcard); 216 QVector<QString> filesToProcess=UtilXmlTools::getAllXmlFilesByWildcard(filesWildcard); 225 217 226 218 if(filesToProcess.isEmpty()){ … … 228 220 } 229 221 230 QFile rexmlfile(":/resources/libs/rexml.js"); 231 QFile jsxmlfile(":/resources/libs/jsxml.js"); 232 233 rexmlfile.open(QFile::ReadOnly | QFile::Text); 234 jsxmlfile.open(QFile::ReadOnly | QFile::Text); 235 236 rexmlString=QTextStream(&rexmlfile).readAll(); 237 jsxmlString=QTextStream(&jsxmlfile).readAll(); 238 239 // Process all XmlFiles 240 #pragma omp parallel for 241 for(int i=0; i<filesToProcess.size(); i++){ 242 243 QString currXmlFileString; 244 245 QScriptEngine engine; 246 QScriptValue engineResult; // variable to check for js_errors 247 double elapsed_secs; // elapsed seconds that a user script took 248 clock_t begin; // seconds that a script started 249 250 // Add echo function so user can debug the code 251 QScriptValue echoFunction = engine.newFunction(echo); 252 engine.globalObject().setProperty("echo", echoFunction); 253 254 engine.evaluate(rexmlString); // load js libraries 255 engine.evaluate(jsxmlString); 256 257 if(this->backupsEnabled){ 258 UtilXmlTools::backupFile(filesToProcess[i], this->verboseEnabled); 259 } 260 261 QFile currXmlFile(filesToProcess[i]); 262 263 if(!currXmlFile.open(QFile::ReadOnly | QFile::Text)){ 264 UtilXmlTools::displayErrorMessage("@CUSTOM_CODE","Error loading '" + filesToProcess[i] + "' file for read operation."); 265 } 266 267 currXmlFileString=QTextStream(&currXmlFile).readAll(); 268 269 currXmlFile.close(); // close reading 270 271 engine.globalObject().setProperty("$xmlData",currXmlFileString); 272 273 begin = clock(); 274 275 // main needs to be called so the user code is evaluated 276 // alternatively you can do: myFunc=engine.evaluate('(function main(){})'); myFunc.call(); 277 // Note the () around the function 278 engineResult=engine.evaluate("main(); function main() {"+jsString+"}"); // main funtion allows to use return to exit prematurely from user code 279 280 if(this->verboseEnabled){ 281 elapsed_secs = double(clock() - begin) / CLOCKS_PER_SEC; 282 283 // Warn the user if the script took much time 284 if(elapsed_secs>SLOW_SCRIPT_TIME){ 285 std::cout << "Warning: Slow javascript code detected.\n" << 286 "Warning: Script execution seconds: " << elapsed_secs 287 << std::endl; 288 } 289 } 290 291 if (engine.hasUncaughtException()) { 292 displayJsException(engine,engineResult); 293 } 294 295 if(!currXmlFile.open(QFile::WriteOnly | QFile::Text | QIODevice::Truncate)){ 296 UtilXmlTools::displayErrorMessage("@CUSTOM_CODE","Error loading '" + filesToProcess[i] + "' file for @CUSTOM_CODE write operation."); 297 } 298 299 engineResult=engine.globalObject().property("$xmlData"); 300 301 if (engine.hasUncaughtException()) { 302 displayJsException(engine,engineResult); 303 } 304 305 QTextStream(&currXmlFile) << engineResult.toString(); // retreive the modified xml by javascript and save it to the file 306 } 222 this->customCodeOperation.executeCustomCode(jsString,filesToProcess,this->backupsEnabled,this->verboseEnabled); 223 307 224 308 225 UtilXmlTools::displaySuccessMessage(filesToProcess.size(), "@CUSTOM_CODE"); … … 515 432 return line.mid(startValueIdx,endValueIdx-startValueIdx); // return the value of the parameter (is in the middle of the mandatory quotes) 516 433 } 517 518 void XmlPatch::displayJsException(QScriptEngine &engine, QScriptValue &engineResult){519 if (engine.hasUncaughtException()) {520 UtilXmlTools::displayErrorMessage("@CUSTOM_CODE","Uncaught js exception (user code) at line " +QString::number(engine.uncaughtExceptionLineNumber()) + ":\n" + engineResult.toString());521 }522 }
Note:
See TracChangeset
for help on using the changeset viewer.