1
/*
2
 * This file is part of the API Extractor project.
3
 *
4
 * Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
5
 *
6
 * Contact: PySide team <contact@pyside.org>
7
 *
8
 * This program is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU General Public License
10
 * version 2 as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful, but
13
 * WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20
 * 02110-1301 USA
21
 *
22
 */
23
24
#include "asttoxml.h"
25
#include "parser/control.h"
26
#include "parser/parser.h"
27
#include "parser/binder.h"
28
29
30
#include <QtCore/QXmlStreamWriter>
31
#include <QtCore/QTextStream>
32
#include <QtCore/QTextCodec>
33
#include <QtCore/QFile>
34
35
void astToXML(QString name)
36
{
37
    QFile file(name);
38
39
    if (!file.open(QFile::ReadOnly))
40
        return;
41
42
    QTextStream stream(&file);
43
    stream.setCodec(QTextCodec::codecForName("UTF-8"));
44
    QByteArray contents = stream.readAll().toUtf8();
45
    file.close();
46
47
    Control control;
48
    Parser p(&control);
49
    pool __pool;
50
51
    TranslationUnitAST *ast = p.parse(contents, contents.size(), &__pool);
52
53
    CodeModel model;
54
    Binder binder(&model, p.location());
55
    FileModelItem dom = binder.run(ast);
56
57
    QFile outputFile;
58
    if (!outputFile.open(stdout, QIODevice::WriteOnly))
59
        return;
60
61
    QXmlStreamWriter s(&outputFile);
62
    s.setAutoFormatting(true);
63
64
    s.writeStartElement("code");
65
66
    QHash<QString, NamespaceModelItem> namespaceMap = dom->namespaceMap();
67
    foreach (NamespaceModelItem item, namespaceMap.values())
68
        writeOutNamespace(s, item);
69
70
    QHash<QString, ClassModelItem> typeMap = dom->classMap();
71
    foreach (ClassModelItem item, typeMap.values())
72
        writeOutClass(s, item);
73
74
    s.writeEndElement();
75
}
76
77
78
void writeOutNamespace(QXmlStreamWriter &s, NamespaceModelItem &item)
79
{
80
    s.writeStartElement("namespace");
81
    s.writeAttribute("name", item->name());
82
83
    QHash<QString, NamespaceModelItem> namespaceMap = item->namespaceMap();
84
    foreach (NamespaceModelItem item, namespaceMap.values())
85
        writeOutNamespace(s, item);
86
87
    QHash<QString, ClassModelItem> typeMap = item->classMap();
88
    foreach (ClassModelItem item, typeMap.values())
89
        writeOutClass(s, item);
90
91
    QHash<QString, EnumModelItem> enumMap = item->enumMap();
92
    foreach (EnumModelItem item, enumMap.values())
93
        writeOutEnum(s, item);
94
95
    s.writeEndElement();
96
}
97
98
void writeOutEnum(QXmlStreamWriter &s, EnumModelItem &item)
99
{
100
    QString qualifiedName = item->qualifiedName().join("::");
101
    s.writeStartElement("enum");
102
    s.writeAttribute("name", qualifiedName);
103
104
    EnumeratorList enumList = item->enumerators();
105
    for (int i = 0; i < enumList.size() ; i++) {
106
        s.writeStartElement("enumerator");
107
        if (!enumList[i]->value().isEmpty())
108
            s.writeAttribute("value", enumList[i]->value());
109
        s.writeCharacters(enumList[i]->name());
110
111
        s.writeEndElement();
112
    }
113
    s.writeEndElement();
114
}
115
116
void writeOutFunction(QXmlStreamWriter &s, FunctionModelItem &item)
117
{
118
    QString qualifiedName = item->qualifiedName().join("::");
119
    s.writeStartElement("function");
120
    s.writeAttribute("name", qualifiedName);
121
122
    ArgumentList arguments = item->arguments();
123
    for (int i = 0; i < arguments.size() ; i++) {
124
        s.writeStartElement("argument");
125
        s.writeAttribute("type",  arguments[i]->type().qualifiedName().join("::"));
126
        s.writeEndElement();
127
    }
128
    s.writeEndElement();
129
}
130
131
void writeOutClass(QXmlStreamWriter &s, ClassModelItem &item)
132
{
133
    QString qualifiedName = item->qualifiedName().join("::");
134
    s.writeStartElement("class");
135
    s.writeAttribute("name", qualifiedName);
136
137
    QHash<QString, EnumModelItem> enumMap = item->enumMap();
138
    foreach (EnumModelItem item, enumMap.values())
139
        writeOutEnum(s, item);
140
141
    QHash<QString, FunctionModelItem> functionMap = item->functionMap();
142
    foreach (FunctionModelItem item, functionMap.values())
143
        writeOutFunction(s, item);
144
145
    QHash<QString, ClassModelItem> typeMap = item->classMap();
146
    foreach (ClassModelItem item, typeMap.values())
147
        writeOutClass(s, item);
148
149
    s.writeEndElement();
150
}