~ubuntu-branches/ubuntu/precise/qtscriptgenerator/precise

« back to all changes in this revision

Viewing changes to generator/asttoxml.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Modestas Vainius
  • Date: 2009-04-25 16:16:02 UTC
  • Revision ID: james.westby@ubuntu.com-20090425161602-vrlxapa3fbo2k3x7
Tags: upstream-0.1.0
ImportĀ upstreamĀ versionĀ 0.1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved.
 
4
**
 
5
** This file is part of the Qt Script Generator project on Trolltech Labs.
 
6
**
 
7
** This file may be used under the terms of the GNU General Public
 
8
** License version 2.0 as published by the Free Software Foundation
 
9
** and appearing in the file LICENSE.GPL included in the packaging of
 
10
** this file.  Please review the following information to ensure GNU
 
11
** General Public Licensing requirements will be met:
 
12
** http://www.trolltech.com/products/qt/opensource.html
 
13
**
 
14
** If you are unsure which license is appropriate for your use, please
 
15
** review the following information:
 
16
** http://www.trolltech.com/products/qt/licensing.html or contact the
 
17
** sales department at sales@trolltech.com.
 
18
**
 
19
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 
20
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 
21
**
 
22
****************************************************************************/
 
23
 
 
24
#include "asttoxml.h"
 
25
#include "control.h"
 
26
#include "parser.h"
 
27
#include "binder.h"
 
28
 
 
29
 
 
30
#include <QXmlStreamWriter>
 
31
#include <QTextStream>
 
32
#include <QTextCodec>
 
33
#include <QFile>
 
34
 
 
35
void astToXML(QString name) {
 
36
    QFile file(name);
 
37
 
 
38
    if (!file.open(QFile::ReadOnly))
 
39
        return;
 
40
 
 
41
    QTextStream stream(&file);
 
42
    stream.setCodec(QTextCodec::codecForName("UTF-8"));
 
43
    QByteArray contents = stream.readAll().toUtf8();
 
44
    file.close();
 
45
 
 
46
    Control control;
 
47
    Parser p(&control);
 
48
    pool __pool;
 
49
 
 
50
    TranslationUnitAST *ast = p.parse(contents, contents.size(), &__pool);
 
51
 
 
52
    CodeModel model;
 
53
    Binder binder(&model, p.location());
 
54
    FileModelItem dom = binder.run(ast);
 
55
 
 
56
    QFile outputFile;
 
57
    if (!outputFile.open(stdout, QIODevice::WriteOnly))
 
58
        {
 
59
            return;
 
60
        }
 
61
 
 
62
    QXmlStreamWriter s( &outputFile);
 
63
    s.setAutoFormatting( true );
 
64
 
 
65
    s.writeStartElement("code");
 
66
 
 
67
    QHash<QString, NamespaceModelItem> namespaceMap = dom->namespaceMap();
 
68
    foreach (NamespaceModelItem item, namespaceMap.values()) {
 
69
        writeOutNamespace(s, item);
 
70
    }
 
71
 
 
72
    QHash<QString, ClassModelItem> typeMap = dom->classMap();
 
73
    foreach (ClassModelItem item, typeMap.values()) {
 
74
        writeOutClass(s, item);
 
75
    }
 
76
    s.writeEndElement();
 
77
}
 
78
 
 
79
 
 
80
void writeOutNamespace(QXmlStreamWriter &s, NamespaceModelItem &item) {
 
81
    s.writeStartElement("namespace");
 
82
    s.writeAttribute("name", item->name());
 
83
 
 
84
    QHash<QString, NamespaceModelItem> namespaceMap = item->namespaceMap();
 
85
    foreach (NamespaceModelItem namespaceItem, namespaceMap.values()) {
 
86
        writeOutNamespace(s, namespaceItem);
 
87
    }
 
88
 
 
89
    QHash<QString, ClassModelItem> typeMap = item->classMap();
 
90
    foreach (ClassModelItem classItem, typeMap.values()) {
 
91
        writeOutClass(s, classItem);
 
92
    }
 
93
 
 
94
    QHash<QString, EnumModelItem> enumMap = item->enumMap();
 
95
    foreach (EnumModelItem enumItem, enumMap.values()) {
 
96
        writeOutEnum(s, enumItem);
 
97
    }
 
98
 
 
99
    s.writeEndElement();
 
100
}
 
101
 
 
102
void writeOutEnum(QXmlStreamWriter &s, EnumModelItem &item) {
 
103
    QString qualified_name = item->qualifiedName().join("::");
 
104
    s.writeStartElement("enum");
 
105
    s.writeAttribute("name", qualified_name);
 
106
 
 
107
    EnumeratorList enumList = item->enumerators();
 
108
    for(int i=0; i < enumList.size() ; i++) {
 
109
        s.writeStartElement("enumerator");
 
110
        if( !enumList[i]->value().isEmpty() )
 
111
            s.writeAttribute("value", enumList[i]->value());
 
112
        s.writeCharacters(enumList[i]->name());
 
113
 
 
114
        s.writeEndElement();
 
115
    }
 
116
    s.writeEndElement();
 
117
}
 
118
 
 
119
void writeOutFunction(QXmlStreamWriter &s, FunctionModelItem &item) {
 
120
    QString qualified_name = item->qualifiedName().join("::");
 
121
    s.writeStartElement("function");
 
122
    s.writeAttribute("name", qualified_name);
 
123
 
 
124
    ArgumentList arguments = item->arguments();
 
125
    for(int i=0; i < arguments.size() ; i++) {
 
126
        s.writeStartElement("argument");
 
127
        s.writeAttribute("type",  arguments[i]->type().qualifiedName().join("::"));
 
128
        s.writeEndElement();
 
129
    }
 
130
    s.writeEndElement();
 
131
}
 
132
 
 
133
void writeOutClass(QXmlStreamWriter &s, ClassModelItem &item) {
 
134
    QString qualified_name = item->qualifiedName().join("::");
 
135
    s.writeStartElement("class");
 
136
    s.writeAttribute("name", qualified_name);
 
137
 
 
138
    QHash<QString, EnumModelItem> enumMap = item->enumMap();
 
139
    foreach (EnumModelItem enumItem, enumMap.values()) {
 
140
        writeOutEnum(s, enumItem);
 
141
    }
 
142
 
 
143
    QHash<QString, FunctionModelItem> functionMap = item->functionMap();
 
144
    foreach (FunctionModelItem funcItem, functionMap.values()) {
 
145
        writeOutFunction(s, funcItem);
 
146
    }
 
147
 
 
148
    QHash<QString, ClassModelItem> typeMap = item->classMap();
 
149
    foreach (ClassModelItem classItem, typeMap.values()) {
 
150
        writeOutClass(s, classItem);
 
151
    }
 
152
    s.writeEndElement();
 
153
}