PageRenderTime 77ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/generator/asttoxml.cpp

http://qtd.googlecode.com/
C++ | 171 lines | 102 code | 29 blank | 40 comment | 5 complexity | c2b1d224f4e8d5375f176f215dc13ab6 MD5 | raw file
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 1992-2008 Nokia. All rights reserved.
  4. **
  5. ** This file is part of Qt Jambi.
  6. **
  7. ** * Commercial Usage
  8. * Licensees holding valid Qt Commercial licenses may use this file in
  9. * accordance with the Qt Commercial License Agreement provided with the
  10. * Software or, alternatively, in accordance with the terms contained in
  11. * a written agreement between you and Nokia.
  12. *
  13. *
  14. * GNU General Public License Usage
  15. * Alternatively, this file may be used under the terms of the GNU
  16. * General Public License versions 2.0 or 3.0 as published by the Free
  17. * Software Foundation and appearing in the file LICENSE.GPL included in
  18. * the packaging of this file. Please review the following information
  19. * to ensure GNU General Public Licensing requirements will be met:
  20. * http://www.fsf.org/licensing/licenses/info/GPLv2.html and
  21. * http://www.gnu.org/copyleft/gpl.html. In addition, as a special
  22. * exception, Nokia gives you certain additional rights. These rights
  23. * are described in the Nokia Qt GPL Exception version 1.2, included in
  24. * the file GPL_EXCEPTION.txt in this package.
  25. *
  26. * Qt for Windows(R) Licensees
  27. * As a special exception, Nokia, as the sole copyright holder for Qt
  28. * Designer, grants users of the Qt/Eclipse Integration plug-in the
  29. * right for the Qt/Eclipse Integration to link to functionality
  30. * provided by Qt Designer and its related libraries.
  31. *
  32. *
  33. * If you are unsure which license is appropriate for your use, please
  34. * contact the sales department at qt-sales@nokia.com.
  35. **
  36. ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
  37. ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  38. **
  39. ****************************************************************************/
  40. #include "asttoxml.h"
  41. #include "control.h"
  42. #include "parser.h"
  43. #include "binder.h"
  44. #include <QXmlStreamWriter>
  45. #include <QTextStream>
  46. #include <QTextCodec>
  47. #include <QFile>
  48. void astToXML(QString name) {
  49. QFile file(name);
  50. if (!file.open(QFile::ReadOnly))
  51. return;
  52. QTextStream stream(&file);
  53. stream.setCodec(QTextCodec::codecForName("UTF-8"));
  54. QByteArray contents = stream.readAll().toUtf8();
  55. file.close();
  56. Control control;
  57. Parser p(&control);
  58. pool __pool;
  59. TranslationUnitAST *ast = p.parse(contents, contents.size(), &__pool);
  60. CodeModel model;
  61. Binder binder(&model, p.location());
  62. FileModelItem dom = binder.run(ast);
  63. QFile outputFile;
  64. if (!outputFile.open(stdout, QIODevice::WriteOnly))
  65. {
  66. return;
  67. }
  68. QXmlStreamWriter s( &outputFile);
  69. s.setAutoFormatting( true );
  70. s.writeStartElement("code");
  71. QHash<QString, NamespaceModelItem> namespaceMap = dom->namespaceMap();
  72. foreach (NamespaceModelItem item, namespaceMap.values()) {
  73. writeOutNamespace(s, item);
  74. }
  75. QHash<QString, ClassModelItem> typeMap = dom->classMap();
  76. foreach (ClassModelItem item, typeMap.values()) {
  77. writeOutClass(s, item);
  78. }
  79. s.writeEndElement();
  80. }
  81. void writeOutNamespace(QXmlStreamWriter &s, NamespaceModelItem &item) {
  82. s.writeStartElement("namespace");
  83. s.writeAttribute("name", item->name());
  84. QHash<QString, NamespaceModelItem> namespaceMap = item->namespaceMap();
  85. foreach (NamespaceModelItem item, namespaceMap.values()) {
  86. writeOutNamespace(s, item);
  87. }
  88. QHash<QString, ClassModelItem> typeMap = item->classMap();
  89. foreach (ClassModelItem item, typeMap.values()) {
  90. writeOutClass(s, item);
  91. }
  92. QHash<QString, EnumModelItem> enumMap = item->enumMap();
  93. foreach (EnumModelItem item, enumMap.values()) {
  94. writeOutEnum(s, item);
  95. }
  96. s.writeEndElement();
  97. }
  98. void writeOutEnum(QXmlStreamWriter &s, EnumModelItem &item) {
  99. QString qualified_name = item->qualifiedName().join("::");
  100. s.writeStartElement("enum");
  101. s.writeAttribute("name", qualified_name);
  102. EnumeratorList enumList = item->enumerators();
  103. for(int i=0; i < enumList.size() ; i++) {
  104. s.writeStartElement("enumerator");
  105. if( !enumList[i]->value().isEmpty() )
  106. s.writeAttribute("value", enumList[i]->value());
  107. s.writeCharacters(enumList[i]->name());
  108. s.writeEndElement();
  109. }
  110. s.writeEndElement();
  111. }
  112. void writeOutFunction(QXmlStreamWriter &s, FunctionModelItem &item) {
  113. QString qualified_name = item->qualifiedName().join("::");
  114. s.writeStartElement("function");
  115. s.writeAttribute("name", qualified_name);
  116. ArgumentList arguments = item->arguments();
  117. for(int i=0; i < arguments.size() ; i++) {
  118. s.writeStartElement("argument");
  119. s.writeAttribute("type", arguments[i]->type().qualifiedName().join("::"));
  120. s.writeEndElement();
  121. }
  122. s.writeEndElement();
  123. }
  124. void writeOutClass(QXmlStreamWriter &s, ClassModelItem &item) {
  125. QString qualified_name = item->qualifiedName().join("::");
  126. s.writeStartElement("class");
  127. s.writeAttribute("name", qualified_name);
  128. QHash<QString, EnumModelItem> enumMap = item->enumMap();
  129. foreach (EnumModelItem item, enumMap.values()) {
  130. writeOutEnum(s, item);
  131. }
  132. QHash<QString, FunctionModelItem> functionMap = item->functionMap();
  133. foreach (FunctionModelItem item, functionMap.values()) {
  134. writeOutFunction(s, item);
  135. }
  136. QHash<QString, ClassModelItem> typeMap = item->classMap();
  137. foreach (ClassModelItem item, typeMap.values()) {
  138. writeOutClass(s, item);
  139. }
  140. s.writeEndElement();
  141. }