| 1 |
|
#include "xmlcustomcode.h" |
| 2 |
|
|
| 3 |
< |
// http://stackoverflow.com/questions/7092765/what-does-it-mean-to-have-an-undefined-reference-to-a-static-member |
| 4 |
< |
QVector<QScriptEngine*> XmlCustomCode::scriptEngines; |
| 5 |
< |
QVector<QScriptValue*> XmlCustomCode::jsFunctions; |
| 6 |
< |
QVector<QScriptValue*> XmlCustomCode::getXmlDataFunctions; |
| 7 |
< |
QVector<QScriptValue*> XmlCustomCode::setXmlDataFunctions; |
| 3 |
> |
XmlCustomCode* XmlCustomCode::uniqueInstance = NULL; |
| 4 |
|
|
| 5 |
|
QScriptValue echo(QScriptContext *context, QScriptEngine*) |
| 6 |
|
{ |
| 11 |
|
|
| 12 |
|
XmlCustomCode::XmlCustomCode(): numThreads(omp_get_num_procs()*2) |
| 13 |
|
{ |
| 14 |
< |
// create individual thread script engines if empty |
| 15 |
< |
if(this->scriptEngines.isEmpty()){ |
| 16 |
< |
this->scriptEngines.reserve(this->numThreads); |
| 17 |
< |
this->jsFunctions.reserve(this->numThreads); |
| 18 |
< |
this->getXmlDataFunctions.reserve(this->numThreads); |
| 23 |
< |
this->setXmlDataFunctions.reserve(this->numThreads); |
| 24 |
< |
|
| 25 |
< |
QString jsxmlString; |
| 26 |
< |
QFile jsxmlfile(":/resources/libs/jsxml.js"); |
| 27 |
< |
|
| 28 |
< |
jsxmlfile.open(QFile::ReadOnly | QFile::Text); |
| 29 |
< |
|
| 30 |
< |
jsxmlString=QTextStream(&jsxmlfile).readAll(); |
| 31 |
< |
|
| 32 |
< |
for(int i=0; i<this->numThreads; i++){ |
| 33 |
< |
this->scriptEngines.append(new QScriptEngine()); |
| 34 |
< |
this->jsFunctions.append(new QScriptValue()); |
| 35 |
< |
|
| 36 |
< |
// main needs to be called so the user code is evaluated |
| 37 |
< |
// alternatively you can do: myFunc=engine.evaluate('(function main(){})'); myFunc.call(); |
| 38 |
< |
// Note the () around the function |
| 39 |
< |
this->getXmlDataFunctions.append(new QScriptValue(this->scriptEngines.at(i)->evaluate("(function getXmlData() { return $xmlData; })"))); |
| 40 |
< |
this->setXmlDataFunctions.append(new QScriptValue(this->scriptEngines.at(i)->evaluate("(function setXmlData(newXmlData) { $xmlData=newXmlData; })"))); |
| 41 |
< |
|
| 42 |
< |
// Add echo function so user can debug the code |
| 43 |
< |
QScriptValue echoFunction = this->scriptEngines.at(i)->newFunction(echo); |
| 44 |
< |
this->scriptEngines.at(i)->globalObject().setProperty("echo", echoFunction); |
| 14 |
> |
// create individual thread script engines |
| 15 |
> |
this->scriptEngines.reserve(this->numThreads); |
| 16 |
> |
this->jsFunctions.reserve(this->numThreads); |
| 17 |
> |
this->getXmlDataFunctions.reserve(this->numThreads); |
| 18 |
> |
this->setXmlDataFunctions.reserve(this->numThreads); |
| 19 |
|
|
| 20 |
< |
// Add the js library for XmlEditing |
| 21 |
< |
this->scriptEngines.at(i)->evaluate(jsxmlString); |
| 22 |
< |
} |
| 20 |
> |
QString jsxmlString; |
| 21 |
> |
QFile jsxmlfile(":/resources/libs/jsxml.js"); |
| 22 |
> |
|
| 23 |
> |
jsxmlfile.open(QFile::ReadOnly | QFile::Text); |
| 24 |
> |
|
| 25 |
> |
jsxmlString=QTextStream(&jsxmlfile).readAll(); |
| 26 |
> |
|
| 27 |
> |
for(int i=0; i<this->numThreads; i++){ |
| 28 |
> |
this->scriptEngines.append(new QScriptEngine()); |
| 29 |
> |
this->jsFunctions.append(new QScriptValue()); |
| 30 |
> |
|
| 31 |
> |
// main needs to be called so the user code is evaluated |
| 32 |
> |
// alternatively you can do: myFunc=engine.evaluate('(function main(){})'); myFunc.call(); |
| 33 |
> |
// Note the () around the function |
| 34 |
> |
this->getXmlDataFunctions.append(new QScriptValue(this->scriptEngines.at(i)->evaluate("(function getXmlData() { return $xmlData; })"))); |
| 35 |
> |
this->setXmlDataFunctions.append(new QScriptValue(this->scriptEngines.at(i)->evaluate("(function setXmlData(newXmlData) { $xmlData=newXmlData; })"))); |
| 36 |
> |
|
| 37 |
> |
// Add echo function so user can debug the code |
| 38 |
> |
QScriptValue echoFunction = this->scriptEngines.at(i)->newFunction(echo); |
| 39 |
> |
this->scriptEngines.at(i)->globalObject().setProperty("echo", echoFunction); |
| 40 |
> |
|
| 41 |
> |
// Add the js library for XmlEditing |
| 42 |
> |
this->scriptEngines.at(i)->evaluate(jsxmlString); |
| 43 |
> |
} |
| 44 |
> |
} |
| 45 |
> |
|
| 46 |
> |
XmlCustomCode* XmlCustomCode::getInstance(){ |
| 47 |
> |
|
| 48 |
> |
if (uniqueInstance==NULL){ // allow only one instance |
| 49 |
> |
uniqueInstance = new XmlCustomCode(); |
| 50 |
|
} |
| 51 |
+ |
|
| 52 |
+ |
return uniqueInstance; |
| 53 |
|
} |
| 54 |
|
|
| 55 |
|
void XmlCustomCode::executeCustomCode(const QString &jsString, const QVector<QString> &filesToProcess, const bool backupsEnabled, const bool verboseEnabled){ |
| 71 |
|
for(int i=0; i<filesToProcess.size(); i++){ |
| 72 |
|
|
| 73 |
|
customCodeUnwinding(filesToProcess.at(i),currXmlFileString,*this->scriptEngines.at(0),begin,elapsed_secs,engineResult, |
| 74 |
< |
*this->jsFunctions.at(0),*this->getXmlDataFunctions.at(0),*this->setXmlDataFunctions.at(0),backupsEnabled,verboseEnabled); |
| 74 |
> |
*this->jsFunctions.at(0),*this->getXmlDataFunctions.at(0),*this->setXmlDataFunctions.at(0),backupsEnabled,verboseEnabled); |
| 75 |
|
} |
| 76 |
|
} |
| 77 |
|
else{ // Multithread if there are many files |
| 101 |
|
for(int i=alreadyProcessedFiles; i<filesToProcess.size(); i++){ |
| 102 |
|
|
| 103 |
|
customCodeUnwinding(filesToProcess.at(i),currXmlFileString,*this->scriptEngines.at(0),begin,elapsed_secs,engineResult, |
| 104 |
< |
*this->jsFunctions.at(0),*this->getXmlDataFunctions.at(0),*this->setXmlDataFunctions.at(0),backupsEnabled,verboseEnabled); |
| 104 |
> |
*this->jsFunctions.at(0),*this->getXmlDataFunctions.at(0),*this->setXmlDataFunctions.at(0),backupsEnabled,verboseEnabled); |
| 105 |
|
} |
| 106 |
|
} |
| 107 |
|
} |