• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdesdk API Reference
  • KDE Home
  • Contact Us
 

okteta

  • sources
  • kde-4.12
  • kdesdk
  • okteta
  • kasten
  • controllers
  • view
  • structures
  • parsers
scriptfileparser.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of the Okteta Kasten Framework, made within the KDE community.
3  *
4  * Copyright 2010, 2011, 2012 Alex Richardson <alex.richardson@gmx.de>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) version 3, or any
10  * later version accepted by the membership of KDE e.V. (or its
11  * successor approved by the membership of KDE e.V.), which shall
12  * act as a proxy defined in Section 6 of version 3 of the license.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 #include "scriptfileparser.h"
24 
25 #include "scriptvalueconverter.h"
26 #include "parserutils.h"
27 #include "../datatypes/topleveldatainformation.h"
28 #include "../datatypes/dummydatainformation.h"
29 #include "../script/scriptengineinitializer.h"
30 #include "../script/scriptlogger.h"
31 
32 #include <QScriptEngine>
33 
34 ScriptFileParser::ScriptFileParser(const QString& pluginName, const QString& absolutePath)
35  : AbstractStructureParser(pluginName, absolutePath)
36 {
37 }
38 
39 ScriptFileParser::~ScriptFileParser()
40 {
41 }
42 
43 QStringList ScriptFileParser::parseStructureNames() const
44 {
45  return QStringList() << mPluginName;
46 }
47 
48 QVector<TopLevelDataInformation*> ScriptFileParser::parseStructures() const
49 {
50  QVector<TopLevelDataInformation*> ret;
51 
52  QScriptEngine* engine = ScriptEngineInitializer::newEngine();
53  ScriptLogger* logger = new ScriptLogger();
54 
55  QScriptValue value = loadScriptValue(logger, engine);
56  DataInformation* dataInf;
57  if (!value.isValid())
58  dataInf = new DummyDataInformation(0, mPluginName);
59  else
60  dataInf = ScriptValueConverter::convert(value, mPluginName, logger);
61 
62  if (!dataInf)
63  dataInf = new DummyDataInformation(0, mPluginName);
64  const QFileInfo fileInfo(mAbsolutePath);
65  TopLevelDataInformation* top = new TopLevelDataInformation(dataInf, logger, engine, fileInfo);
66  //handle default lock offset
67  QScriptValue lockOffset = value.property(ParserStrings::PROPERTY_DEFAULT_LOCK_OFFSET);
68  if (lockOffset.isValid())
69  {
70  ParsedNumber<quint64> offset = ParserUtils::uint64FromScriptValue(lockOffset);
71  if (!offset.isValid)
72  dataInf->logError() << "Default lock offset is not a valid number:" << offset.string;
73  else
74  top->setDefaultLockOffset(offset.value);
75  }
76  ret.append(top);
77  return ret;
78 }
79 
80 QScriptValue ScriptFileParser::loadScriptValue(ScriptLogger* logger, QScriptEngine* engine) const
81 {
82  QFile scriptFile(mAbsolutePath);
83  if (!scriptFile.open(QIODevice::ReadOnly))
84  {
85  logger->error() << "Could not open file " << mAbsolutePath;
86  return QScriptValue();
87  }
88 
89  QTextStream stream(&scriptFile);
90  QString contents = stream.readAll();
91  scriptFile.close();
92  engine->evaluate(contents, mAbsolutePath);
93  if (engine->hasUncaughtException())
94  {
95  //check if it was a syntax error:
96  QScriptSyntaxCheckResult syntaxError = QScriptEngine::checkSyntax(contents);
97  if (syntaxError.state() == QScriptSyntaxCheckResult::Error)
98  {
99  //give a detailed syntax error message
100  logger->error() << "Syntax error in script: " << syntaxError.errorMessage();
101  logger->error() << "Line number: " << syntaxError.errorLineNumber()
102  << "Column:" << syntaxError.errorColumnNumber();
103  }
104  else
105  {
106  //just print the generic exception message
107  logger->error() << "Error evaluating script: " << engine->uncaughtException().toString();
108  logger->error() << "Line number: " << engine->uncaughtExceptionLineNumber();
109  logger->error() << "Backtrace: " << engine->uncaughtExceptionBacktrace();
110  }
111  return QScriptValue();
112  }
113  QScriptValue obj = engine->globalObject();
114  QScriptValue initMethod = obj.property(QLatin1String("init"));
115  if (!initMethod.isFunction())
116  {
117  logger->error() << "Script has no 'init' function! Cannot evaluate script!";
118  return QScriptValue();
119  }
120 
121  QScriptValue thisObj = engine->newObject();
122  QScriptValueList args;
123  QScriptValue result = initMethod.call(thisObj, args);
124  if (result.isError())
125  {
126  logger->error() << "Exception occurred while calling init():" << result.toString();
127  return QScriptValue();
128  }
129  return result;
130 }
DataInformation
Interface that must be implemented by all datatypes.
Definition: datainformation.h:67
DummyDataInformation
Definition: dummydatainformation.h:30
ScriptFileParser::ScriptFileParser
ScriptFileParser(const QString &pluginName, const QString &absolutePath)
Definition: scriptfileparser.cpp:34
ParsedNumber::value
T value
Definition: parserutils.h:88
ParsedNumber::string
QString string
Definition: parserutils.h:87
ParsedNumber
Holds a number that was converted either from a QScriptValue or a QString.
Definition: parserutils.h:84
ScriptFileParser::~ScriptFileParser
virtual ~ScriptFileParser()
Definition: scriptfileparser.cpp:39
QVector
Definition: scriptvalueconverter.h:30
ScriptFileParser::parseStructures
virtual QVector< TopLevelDataInformation * > parseStructures() const
Definition: scriptfileparser.cpp:48
ParserStrings::PROPERTY_DEFAULT_LOCK_OFFSET
const QString PROPERTY_DEFAULT_LOCK_OFFSET
Definition: parserutils.h:112
parserutils.h
TopLevelDataInformation
Definition: topleveldatainformation.h:46
ScriptEngineInitializer::newEngine
QScriptEngine * newEngine()
Definition: scriptengineinitializer.cpp:106
ScriptFileParser::parseStructureNames
virtual QStringList parseStructureNames() const
Definition: scriptfileparser.cpp:43
ScriptLogger
NOT THREAD SAFE!
Definition: scriptlogger.h:35
scriptfileparser.h
TopLevelDataInformation::setDefaultLockOffset
void setDefaultLockOffset(Okteta::Address offset)
Definition: topleveldatainformation.cpp:217
AbstractStructureParser::mPluginName
const QString mPluginName
Definition: abstractstructureparser.h:45
ScriptLogger::error
QDebug error(const DataInformation *origin)
Definition: scriptlogger.h:68
ScriptValueConverter::convert
DataInformation * convert(const QScriptValue &value, const QString &name, ScriptLogger *logger, DataInformation *parent)
If the value is one element.
Definition: scriptvalueconverter.cpp:36
AbstractStructureParser::mAbsolutePath
const QString mAbsolutePath
Definition: abstractstructureparser.h:46
ParserUtils::uint64FromScriptValue
ParsedNumber< quint64 > uint64FromScriptValue(const QScriptValue &val)
Definition: parserutils.cpp:120
ParsedNumber::isValid
bool isValid
Definition: parserutils.h:89
scriptvalueconverter.h
DataInformation::logError
QDebug logError() const
just a shorthand for logger->error(this)
Definition: datainformation.h:324
AbstractStructureParser
Definition: abstractstructureparser.h:36
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:04:09 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

okteta

Skip menu "okteta"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdesdk API Reference

Skip menu "kdesdk API Reference"
  • kapptemplate
  • kcachegrind
  • kompare
  • lokalize
  • okteta
  • umbrello
  •   umbrello

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal