comparison generator/asttoxml.cpp @ 1:e78566595089

initial import
author mandel
date Mon, 11 May 2009 16:01:50 +0000
parents
children
comparison
equal deleted inserted replaced
0:36fb74dc547d 1:e78566595089
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 **
37 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
38 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
39 **
40 ****************************************************************************/
41
42 #include "asttoxml.h"
43 #include "control.h"
44 #include "parser.h"
45 #include "binder.h"
46
47
48 #include <QXmlStreamWriter>
49 #include <QTextStream>
50 #include <QTextCodec>
51 #include <QFile>
52
53 void astToXML(QString name) {
54 QFile file(name);
55
56 if (!file.open(QFile::ReadOnly))
57 return;
58
59 QTextStream stream(&file);
60 stream.setCodec(QTextCodec::codecForName("UTF-8"));
61 QByteArray contents = stream.readAll().toUtf8();
62 file.close();
63
64 Control control;
65 Parser p(&control);
66 pool __pool;
67
68 TranslationUnitAST *ast = p.parse(contents, contents.size(), &__pool);
69
70 CodeModel model;
71 Binder binder(&model, p.location());
72 FileModelItem dom = binder.run(ast);
73
74 QFile outputFile;
75 if (!outputFile.open(stdout, QIODevice::WriteOnly))
76 {
77 return;
78 }
79
80 QXmlStreamWriter s( &outputFile);
81 s.setAutoFormatting( true );
82
83 s.writeStartElement("code");
84
85 QHash<QString, NamespaceModelItem> namespaceMap = dom->namespaceMap();
86 foreach (NamespaceModelItem item, namespaceMap.values()) {
87 writeOutNamespace(s, item);
88 }
89
90 QHash<QString, ClassModelItem> typeMap = dom->classMap();
91 foreach (ClassModelItem item, typeMap.values()) {
92 writeOutClass(s, item);
93 }
94 s.writeEndElement();
95 }
96
97
98 void writeOutNamespace(QXmlStreamWriter &s, NamespaceModelItem &item) {
99 s.writeStartElement("namespace");
100 s.writeAttribute("name", item->name());
101
102 QHash<QString, NamespaceModelItem> namespaceMap = item->namespaceMap();
103 foreach (NamespaceModelItem item, namespaceMap.values()) {
104 writeOutNamespace(s, item);
105 }
106
107 QHash<QString, ClassModelItem> typeMap = item->classMap();
108 foreach (ClassModelItem item, typeMap.values()) {
109 writeOutClass(s, item);
110 }
111
112 QHash<QString, EnumModelItem> enumMap = item->enumMap();
113 foreach (EnumModelItem item, enumMap.values()) {
114 writeOutEnum(s, item);
115 }
116
117 s.writeEndElement();
118 }
119
120 void writeOutEnum(QXmlStreamWriter &s, EnumModelItem &item) {
121 QString qualified_name = item->qualifiedName().join("::");
122 s.writeStartElement("enum");
123 s.writeAttribute("name", qualified_name);
124
125 EnumeratorList enumList = item->enumerators();
126 for(int i=0; i < enumList.size() ; i++) {
127 s.writeStartElement("enumerator");
128 if( !enumList[i]->value().isEmpty() )
129 s.writeAttribute("value", enumList[i]->value());
130 s.writeCharacters(enumList[i]->name());
131
132 s.writeEndElement();
133 }
134 s.writeEndElement();
135 }
136
137 void writeOutFunction(QXmlStreamWriter &s, FunctionModelItem &item) {
138 QString qualified_name = item->qualifiedName().join("::");
139 s.writeStartElement("function");
140 s.writeAttribute("name", qualified_name);
141
142 ArgumentList arguments = item->arguments();
143 for(int i=0; i < arguments.size() ; i++) {
144 s.writeStartElement("argument");
145 s.writeAttribute("type", arguments[i]->type().qualifiedName().join("::"));
146 s.writeEndElement();
147 }
148 s.writeEndElement();
149 }
150
151 void writeOutClass(QXmlStreamWriter &s, ClassModelItem &item) {
152 QString qualified_name = item->qualifiedName().join("::");
153 s.writeStartElement("class");
154 s.writeAttribute("name", qualified_name);
155
156 QHash<QString, EnumModelItem> enumMap = item->enumMap();
157 foreach (EnumModelItem item, enumMap.values()) {
158 writeOutEnum(s, item);
159 }
160
161 QHash<QString, FunctionModelItem> functionMap = item->functionMap();
162 foreach (FunctionModelItem item, functionMap.values()) {
163 writeOutFunction(s, item);
164 }
165
166 QHash<QString, ClassModelItem> typeMap = item->classMap();
167 foreach (ClassModelItem item, typeMap.values()) {
168 writeOutClass(s, item);
169 }
170 s.writeEndElement();
171 }