/tools/porting/src/portingrules.cpp

https://bitbucket.org/ultra_iter/qt-vtl · C++ · 296 lines · 181 code · 35 blank · 80 comment · 62 complexity · f96a5abf65cd270848f3a5c058b25cdb MD5 · raw file

  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
  4. ** All rights reserved.
  5. ** Contact: Nokia Corporation (qt-info@nokia.com)
  6. **
  7. ** This file is part of the qt3to4 porting application of the Qt Toolkit.
  8. **
  9. ** $QT_BEGIN_LICENSE:LGPL$
  10. ** GNU Lesser General Public License Usage
  11. ** This file may be used under the terms of the GNU Lesser General Public
  12. ** License version 2.1 as published by the Free Software Foundation and
  13. ** appearing in the file LICENSE.LGPL included in the packaging of this
  14. ** file. Please review the following information to ensure the GNU Lesser
  15. ** General Public License version 2.1 requirements will be met:
  16. ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
  17. **
  18. ** In addition, as a special exception, Nokia gives you certain additional
  19. ** rights. These rights are described in the Nokia Qt LGPL Exception
  20. ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
  21. **
  22. ** GNU General Public License Usage
  23. ** Alternatively, this file may be used under the terms of the GNU General
  24. ** Public License version 3.0 as published by the Free Software Foundation
  25. ** and appearing in the file LICENSE.GPL included in the packaging of this
  26. ** file. Please review the following information to ensure the GNU General
  27. ** Public License version 3.0 requirements will be met:
  28. ** http://www.gnu.org/copyleft/gpl.html.
  29. **
  30. ** Other Usage
  31. ** Alternatively, this file may be used in accordance with the terms and
  32. ** conditions contained in a signed written agreement between you and Nokia.
  33. **
  34. **
  35. **
  36. **
  37. **
  38. ** $QT_END_LICENSE$
  39. **
  40. ****************************************************************************/
  41. #include "portingrules.h"
  42. #include "logger.h"
  43. #include "qtsimplexml.h"
  44. #include <QFile>
  45. #include <QFileInfo>
  46. #include <QDir>
  47. QT_BEGIN_NAMESPACE
  48. PortingRules *PortingRules::theInstance = 0;
  49. void PortingRules::createInstance(QString xmlFilePath)
  50. {
  51. deleteInstance();
  52. theInstance = new PortingRules(xmlFilePath);
  53. }
  54. PortingRules *PortingRules::instance()
  55. {
  56. if(theInstance) {
  57. return theInstance;
  58. } else {
  59. qWarning("Error: must create a PortingRules instance with createInstance() before calling instance()\n");
  60. return 0;
  61. }
  62. }
  63. void PortingRules::deleteInstance()
  64. {
  65. if(theInstance) {
  66. delete theInstance;
  67. theInstance = 0;
  68. }
  69. }
  70. PortingRules::PortingRules(QString xmlFilePath)
  71. {
  72. parseXml(xmlFilePath);
  73. }
  74. QList<TokenReplacement*> PortingRules::getTokenReplacementRules()
  75. {
  76. if(tokenRules.isEmpty())
  77. addLogWarning(QLatin1String("Warning: token rules list is empty"));
  78. return tokenRules;
  79. }
  80. QStringList PortingRules::getHeaderList(QtVersion qtVersion)
  81. {
  82. if(qt3Headers.isEmpty() || qt4Headers.isEmpty())
  83. addLogWarning(QLatin1String("Warning: headers list is empty"));
  84. if (qtVersion==Qt3)
  85. return qt3Headers;
  86. else //Qt4
  87. return qt4Headers;
  88. }
  89. QHash<QByteArray, QByteArray> PortingRules::getNeededHeaders()
  90. {
  91. if(neededHeaders.isEmpty())
  92. addLogWarning(QLatin1String("Warning: needed headers list is empty"));
  93. return neededHeaders;
  94. }
  95. QStringList PortingRules::getInheritsQt()
  96. {
  97. if(tokenRules.isEmpty())
  98. addLogWarning(QLatin1String("Warning: inheritsQtClass list is empty"));
  99. return inheritsQtClass;
  100. }
  101. QHash<QByteArray, QByteArray> PortingRules::getClassLibraryList()
  102. {
  103. if(classLibraryList.isEmpty())
  104. addLogWarning(QLatin1String("Warning: classLibraryList list is empty"));
  105. return classLibraryList;
  106. }
  107. QHash<QByteArray, QByteArray> PortingRules::getHeaderReplacements()
  108. {
  109. return headerReplacements;
  110. }
  111. /*
  112. Loads rule xml file given by fileName, and sets up data structures.
  113. The rules can generally be divided into to types, replacement rules and
  114. info rules.
  115. Replacement rules has the form Qt3Symobl -> Qt4Symbol
  116. Info rules includes the NeedHeader, Qt3Header, Qt4Header, InhertitsQt
  117. rule types.
  118. */
  119. void PortingRules::parseXml(QString fileName)
  120. {
  121. QtSimpleXml *xmlPointer = loadXml(fileName);
  122. QtSimpleXml &xml = *xmlPointer;
  123. int ruleCount = xml[QLatin1String("Rules")].numChildren();
  124. ++ruleCount;
  125. for(int rule=0; rule<ruleCount; ++rule) {
  126. QtSimpleXml &currentRule = xml[QLatin1String("Rules")][rule];
  127. QString ruleType = currentRule.attribute(QLatin1String("Type"));
  128. if(isReplacementRule(ruleType)) {
  129. QString qt3Symbol = currentRule[QLatin1String("Qt3")].text();
  130. QString qt4Symbol = currentRule[QLatin1String("Qt4")].text();
  131. QString disable = currentRule.attribute(QLatin1String("Disable"));
  132. if(disable == QLatin1String("True") || disable == QLatin1String("true")) {
  133. disableRule(currentRule);
  134. continue;
  135. }
  136. if (isRuleDisabled(currentRule))
  137. continue;
  138. if(ruleType == QLatin1String("RenamedHeader")) {
  139. headerReplacements.insert(qt3Symbol.toLatin1(), qt4Symbol.toLatin1());
  140. } else if(ruleType == QLatin1String("RenamedClass") || ruleType == QLatin1String("RenamedToken") ) {
  141. tokenRules.append(new ClassNameReplacement(
  142. qt3Symbol.toLatin1(), qt4Symbol.toLatin1()));
  143. } else if(ruleType == QLatin1String("RenamedEnumvalue") || ruleType == QLatin1String("RenamedType") ||
  144. ruleType == QLatin1String("RenamedQtSymbol") ) {
  145. checkScopeAddRule(currentRule);
  146. }
  147. } else if(ruleType == QLatin1String("NeedHeader")) {
  148. const QByteArray className = currentRule[QLatin1String("Class")].text().toLatin1();
  149. const QByteArray headerName = currentRule[QLatin1String("Header")].text().toLatin1();
  150. neededHeaders.insert(className, headerName);
  151. }
  152. else if(ruleType == QLatin1String("qt3Header")) {
  153. qt3Headers += currentRule.text();
  154. }
  155. else if(ruleType == QLatin1String("qt4Header")) {
  156. qt4Headers += currentRule.text();
  157. }
  158. else if(ruleType == QLatin1String("InheritsQt")) {
  159. inheritsQtClass += currentRule.text();
  160. }
  161. else if(ruleType == QLatin1String("Qt4Class")) {
  162. // Get library name, make it lowercase and chop of the "Qt" prefix.
  163. const QByteArray libraryName = currentRule[QLatin1String("Library")].text().toLatin1().toLower().mid(2);
  164. classLibraryList.insert(currentRule[QLatin1String("Name")].text().toLatin1(), libraryName);
  165. }
  166. }
  167. QString includeFile = xml[QLatin1String("Rules")][QLatin1String("Include")].text();
  168. if(!includeFile.isNull()) {
  169. QString resolvedIncludeFile = resolveFileName(fileName, includeFile);
  170. if (!resolvedIncludeFile.isEmpty())
  171. parseXml(resolvedIncludeFile);
  172. }
  173. delete xmlPointer;
  174. }
  175. /*
  176. Check if the rule in currentRule describes a qualified name
  177. (like QButton::ToggleState). If so, create a scoped ScopedTokenReplacement,
  178. else create a GenericTokenReplacement
  179. */
  180. void PortingRules::checkScopeAddRule(/*const */QtSimpleXml &currentRule)
  181. {
  182. QByteArray oldToken = currentRule[QLatin1String("Qt3")].text().toLatin1();
  183. QByteArray newToken = currentRule[QLatin1String("Qt4")].text().toLatin1();
  184. if (oldToken.contains(QByteArray("::")))
  185. tokenRules.append(new ScopedTokenReplacement(oldToken, newToken));
  186. else
  187. tokenRules.append(new GenericTokenReplacement(oldToken, newToken));
  188. }
  189. /*
  190. Loads the xml-file given by fileName into a new'ed QtSimpleXml, which is
  191. returned by pointer.
  192. */
  193. QtSimpleXml *PortingRules::loadXml(const QString fileName) const
  194. {
  195. QFile f(fileName);
  196. if(!f.open(QIODevice::ReadOnly)) {
  197. qFatal("Could not find rule file %s", fileName.toLatin1().constData());
  198. }
  199. QtSimpleXml *xml = new QtSimpleXml();
  200. if(!xml->setContent(&f))
  201. addLogError(QLatin1String("Xml parsing failed: ") + xml->errorString());
  202. return xml;
  203. }
  204. /*
  205. Resolves includeFilePath against currentFilePath. If currentFilePath
  206. contains foo/bar.xml, and includeFilePath contains bar2.xml, the returned
  207. result will be foo/bar2.xml. If includeFilePath is absolute, it is returned
  208. unmodified.
  209. */
  210. QString PortingRules::resolveFileName(const QString currentFilePath,
  211. const QString includeFilePath) const
  212. {
  213. if(QFileInfo(includeFilePath).isAbsolute())
  214. return includeFilePath;
  215. QString relativeDirectory = QFileInfo(currentFilePath).dir().dirName();
  216. QString testFileName = relativeDirectory + QLatin1String("/") + includeFilePath;
  217. if (QFile::exists(testFileName))
  218. return testFileName;
  219. return QString();
  220. }
  221. /*
  222. Checks if a rule is a replacement rule.
  223. */
  224. bool PortingRules::isReplacementRule(const QString ruleType) const
  225. {
  226. return (ruleType == QLatin1String("RenamedHeader") || ruleType == QLatin1String("RenamedClass") ||
  227. ruleType == QLatin1String("RenamedToken") || ruleType == QLatin1String("RenamedEnumvalue") ||
  228. ruleType == QLatin1String("RenamedType") || ruleType == QLatin1String("RenamedQtSymbol") );
  229. }
  230. /*
  231. Disables a replacement rule given by the replacementRule parameter
  232. */
  233. void PortingRules::disableRule(QtSimpleXml &replacementRule)
  234. {
  235. RuleDescription ruleDescription(replacementRule);
  236. disabledRules.append(ruleDescription);
  237. }
  238. /*
  239. Checks if a replacement rule is disabled or not
  240. */
  241. bool PortingRules::isRuleDisabled(QtSimpleXml &replacementRule) const
  242. {
  243. RuleDescription ruleDescription(replacementRule);
  244. return disabledRules.contains(ruleDescription);
  245. }
  246. /*
  247. Adds a warning to the global logger.
  248. */
  249. void PortingRules::addLogWarning(const QString text) const
  250. {
  251. Logger::instance()->addEntry(new PlainLogEntry(QLatin1String("Warning"), QLatin1String("Porting"), text));
  252. }
  253. /*
  254. Adds an error to the global logger.
  255. */
  256. void PortingRules::addLogError(const QString text) const
  257. {
  258. Logger::instance()->addEntry(new PlainLogEntry(QLatin1String("Error"), QLatin1String("Porting"), text));
  259. }
  260. QT_END_NAMESPACE