Changeset 964
- Timestamp:
- Feb 24, 2014, 4:36:44 PM (11 years ago)
- Location:
- XmlTools2/trunk
- Files:
-
- 2 added
- 1 deleted
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
XmlTools2/trunk/XmlTools.pro
r953 r964 8 8 QMAKE_CXXFLAGS+= -fopenmp 9 9 QMAKE_LFLAGS += -fopenmp #OpenMP (multithreading) support 10 11 # More optimization 12 QMAKE_CFLAGS_RELEASE += -O3 13 QMAKE_CXXFLAGS_RELEASE += -O3 14 15 10 16 11 17 … … 25 31 utilxmltools.cpp \ 26 32 xmlfilter.cpp \ 27 optionsparser.cpp 33 optionsparser.cpp \ 34 xmlcustomcode.cpp 28 35 29 36 HEADERS += main.h \ … … 36 43 utilxmltools.h \ 37 44 xmlfilter.h \ 38 optionsparser.h 45 optionsparser.h \ 46 xmlcustomcode.h 39 47 40 48 OTHER_FILES += -
XmlTools2/trunk/optionsparser.h
r953 r964 3 3 4 4 #include "xmlpatch.h" 5 6 #include <QCommandLineOption> 7 #include <QCommandLineParser> 5 8 6 9 class OptionsParser -
XmlTools2/trunk/resources.qrc
r906 r964 2 2 <qresource prefix="/resources"> 3 3 <file>libs/jsxml.js</file> 4 <file>libs/rexml.js</file>5 4 </qresource> 6 5 </RCC> -
XmlTools2/trunk/util.h
r942 r964 6 6 #include <QString> 7 7 #include <QStringList> 8 #include <QVector> 8 9 #include <iostream> // cout, cin etc. 9 10 -
XmlTools2/trunk/utilxmltools.cpp
r955 r964 3 3 namespace UtilXmlTools{ 4 4 5 QStringList getAllXmlFilesByWildcard(const QString &wildcard){ 5 // As this will not likely be modified we use QVector instead of QList since it is more cache friendly 6 QVector<QString> getAllXmlFilesByWildcard(const QString &wildcard){ 6 7 QStringList validFilesMatching; 7 8 QStringList filesMatching; … … 19 20 } 20 21 21 return validFilesMatching ;22 return validFilesMatching.toVector(); 22 23 23 24 } 24 25 25 Q StringListgetAllPatchFilesByWildcard(const QString &wildcard){26 QVector<QString> getAllPatchFilesByWildcard(const QString &wildcard){ 26 27 QStringList validFilesMatching; 27 28 QStringList filesMatching; … … 39 40 } 40 41 41 return validFilesMatching ;42 return validFilesMatching.toVector(); 42 43 43 44 } -
XmlTools2/trunk/utilxmltools.h
r935 r964 10 10 namespace UtilXmlTools{ 11 11 12 Q StringListgetAllXmlFilesByWildcard(const QString &wildcard);13 Q StringListgetAllPatchFilesByWildcard(const QString &wildcard);12 QVector<QString> getAllXmlFilesByWildcard(const QString &wildcard); 13 QVector<QString> getAllPatchFilesByWildcard(const QString &wildcard); 14 14 void backupFile(const QString &file, bool verboseEnabled); 15 15 void getAllNamedElements(pugi::xml_node &node, QList<pugi::xml_node> &result, XmlFilter &filters); -
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 } -
XmlTools2/trunk/xmlpatch.h
r958 r964 3 3 4 4 #include "xmltools.h" 5 #include "xmlcustomcode.h" 5 6 #include "optionsparser.h" 6 #include <omp.h> // OpenMP support7 #include <ctime> // script execution time calculation8 9 #define SLOW_SCRIPT_TIME 0.1 // if a user script takes more than 0.1 seconds to execute give a warning.10 7 11 8 class XmlPatch … … 15 12 void readAndProcessPatchFile(); 16 13 private: 17 Q StringListpatchFilesToProcess;14 QVector<QString> patchFilesToProcess; 18 15 QString forceTargetFilesWildcard; 19 16 pugi::xml_document document; 20 17 pugi::xml_node rootNode; 21 18 bool backupsEnabled, verboseEnabled; 19 XmlCustomCode customCodeOperation; 22 20 QString getPatchParameterValue(const QString& line, QString parameter); 23 21 void insertNodesOperation(const QString &xmlString, XmlFilter &filters, const QString &xPathExpression, const QString &filesWildcard=""); … … 27 25 void checkPatchVersion(const QString &file, QTextStream &fileStream); 28 26 void checkAndProcessValidCommands(QTextStream &fileStream); 29 void displayJsException(QScriptEngine &engine, QScriptValue &engineResult);30 27 }; 31 28 -
XmlTools2/trunk/xmltools.h
r956 r964 4 4 5 5 #include <string> 6 #include <QtCore>7 6 #include <stdio.h> 8 #include <QScriptEngine>9 7 10 8 #include "utilxmltools.h" … … 32 30 pugi::xml_document document; 33 31 pugi::xml_node rootNode; 34 Q StringListfilesToProcess;32 QVector<QString> filesToProcess; 35 33 QString xPathExpression; 36 34 XmlFilter filters;
Note:
See TracChangeset
for help on using the changeset viewer.