ViewVC Help
View File | Revision Log | View Changeset | Root Listing
root/Oni2/XmlTools2/trunk/xmlpatch.cpp
(Generate patch)

Comparing XmlTools2/trunk/xmlpatch.cpp (file contents):
Revision 960 by s10k, Sun Feb 16 15:10:52 2014 UTC vs.
Revision 964 by s10k, Mon Feb 24 15:36:44 2014 UTC

# Line 46 | Line 46 | void XmlPatch::readAndProcessPatchFile()
46  
47   void XmlPatch::insertNodesOperation(const QString &xmlString, XmlFilter &filters, const QString &xPathExpression, const QString &filesWildcard){
48  
49 <    QStringList filesToProcess;
49 >    QVector<QString> filesToProcess;
50      QList<pugi::xml_node> nodesToInsertion;
51      pugi::xml_document newNode;
52      pugi::xml_parse_result result;
# Line 114 | Line 114 | void XmlPatch::insertNodesOperation(cons
114  
115   void XmlPatch::removeNodesOperation(XmlFilter &filters, const QString &xPathExpression, const QString &filesWildcard){
116  
117 <    QStringList filesToProcess;
117 >    QVector<QString> filesToProcess;
118  
119      QList<pugi::xml_node> nodesToDeletion;
120  
# Line 211 | Line 211 | void XmlPatch::executeCommandOperation(c
211      UtilXmlTools::displaySuccessMessage(1,"@COMMAND");
212   }
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
214   void XmlPatch::executeCustomCommandOperation(const QString &jsString, const QString &filesWildcard){
215  
216 <    QString rexmlString, jsxmlString;
224 <    QStringList filesToProcess=UtilXmlTools::getAllXmlFilesByWildcard(filesWildcard);
216 >    QVector<QString> filesToProcess=UtilXmlTools::getAllXmlFilesByWildcard(filesWildcard);
217  
218      if(filesToProcess.isEmpty()){
219          UtilXmlTools::displayErrorMessage("@CUSTOM_CODE","No XML files were found for the wildcard: "+filesWildcard);
220      }
221  
222 <    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
222 >    this->customCodeOperation.executeCustomCode(jsString,filesToProcess,this->backupsEnabled,this->verboseEnabled);
223  
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    }
224  
225      UtilXmlTools::displaySuccessMessage(filesToProcess.size(), "@CUSTOM_CODE");
226   }
# Line 514 | Line 431 | QString XmlPatch::getPatchParameterValue
431  
432      return line.mid(startValueIdx,endValueIdx-startValueIdx); // return the value of the parameter (is in the middle of the mandatory quotes)
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 }

Diff Legend

Removed lines
+ Added lines
< Changed lines (old)
> Changed lines (new)