summaryrefslogtreecommitdiffstats
path: root/addon/xmlgen/xml.cpp
blob: a62442681ee37598174413f206730ca695e031a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
/******************************************************************************
 *
 * $Id$
 *
 *
 * Copyright (C) 1997-2001 by Dimitri van Heesch.
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation under the terms of the GNU General Public License is hereby 
 * granted. No representations are made about the suitability of this software 
 * for any purpose. It is provided "as is" without express or implied warranty.
 * See the GNU General Public License for more details.
 *
 * Documents produced by Doxygen are derivative works derived from the
 * input used in their production; they are not affected by this license.
 *
 */

#include <stdlib.h>

#include "qtbc.h"
#include "xml.h"
#include "doxygen.h"
#include "message.h"
#include "config.h"
#include "classlist.h"
#include "util.h"
#include "defargs.h"

#include <qdir.h>
#include <qfile.h>
#include <qtextstream.h>

const char dtd_data[]=
#include "xml_dtd.h"
;

class TextGeneratorXMLImpl : public TextGeneratorIntf
{
  public:
    TextGeneratorXMLImpl(QTextStream &t): m_t(t) {}
    void writeString(const char *s) const
    {
      writeXMLString(m_t,s); 
    }
    void writeBreak() const {}
    void writeLink(const char *extRef,const char *file,
                   const char *anchor,const char *text
                  ) const
    {
      if (extRef==0) 
      { writeXMLLink(m_t,file,anchor,text); } 
      else // external references are not supported for XML
      { writeXMLString(m_t,text); }
    }
  private:
    QTextStream &m_t;
};


void generateDTD()
{
  QCString fileName=Config::instance()->getString("OUTPUT_DIRECTORY")+"/xml/doxygen.dtd";
  QFile f(fileName);
  if (!f.open(IO_WriteOnly))
  {
    err("Cannot open file %s for writing!\n",fileName.data());
    return;
  }
  QTextStream t(&f);
  t << dtd_data;
}

void writeXMLString(QTextStream &t,const char *s)
{
  t << convertToXML(s);
}

void writeXMLLink(QTextStream &t,const char *compoundId,const char *memId,
                  const char *text)
{
  if (memId==0)
  {
    t << "<compoundref idref=\"" << compoundId << "\">";
    writeXMLString(t,text);
    t << "</compoundref>";
  }
  else
  {
    t << "<memberref idref=\"" << compoundId << ":" << memId << "\">";
    writeXMLString(t,text);
    t << "</memberref>";
  }
}

void generateXMLForMember(MemberDef *md,QTextStream &t,Definition *def)
{
  if (md->memberType()==MemberDef::EnumValue) return;

  QCString scopeName;
  if (md->getClassDef()) 
    scopeName=md->getClassDef()->name();
  else if (md->getNamespaceDef()) 
    scopeName=md->getNamespaceDef()->name();
    
  t << "            <";
  enum { define_t,variable_t,typedef_t,enum_t,function_t } xmlType = function_t;
  switch (md->memberType())
  {
    case MemberDef::Define:      t << "definedef";   xmlType=define_t;   break;
    case MemberDef::EnumValue:   // fall through
    case MemberDef::Property:    // fall through
    case MemberDef::Variable:    t << "variabledef"; xmlType=variable_t; break;
    case MemberDef::Typedef:     t << "typedef";     xmlType=typedef_t;  break;
    case MemberDef::Enumeration: t << "enumdef";     xmlType=enum_t;     break;
    case MemberDef::Function:    // fall through
    case MemberDef::Signal:      // fall through
    case MemberDef::Prototype:   // fall through
    case MemberDef::Friend:      // fall through
    case MemberDef::DCOP:        // fall through
    case MemberDef::Slot:        t << "functiondef"; xmlType=function_t; break;
  }
  t << " id=\"";
  t << def->getOutputFileBase()
    << "__"      // can we change this to a non ID char? 
                 // : do not seem allowed for some parsers!
    << md->anchor();
  t << "\"";
  if (xmlType==function_t && md->virtualness()!=Normal) 
    // functions has an extra "virt" attribute
  {
    t << " virt=\"";
    switch (md->virtualness())
    {
      case Virtual: t << "virtual";      break;
      case Pure:    t << "pure-virtual"; break;
      default: ASSERT(0);
    }
    t << "\"";
  }
  t << ">" << endl;
  
  if (xmlType!=define_t && xmlType!=enum_t &&    // These don't have types.
      (xmlType!=function_t || md->typeString()!=0) // Type is optional here.
     )
  {
    QCString typeStr = replaceAnonymousScopes(md->typeString());
    if (xmlType==typedef_t && typeStr.left(8)=="typedef ") 
      typeStr=typeStr.right(typeStr.length()-8);
    if (xmlType==function_t && typeStr.left(8)=="virtual ") 
      typeStr=typeStr.right(typeStr.length()-8);
    t << "              <type>";
    linkifyText(TextGeneratorXMLImpl(t),scopeName,md->name(),typeStr);
    t << "</type>" << endl;
  }

  t << "              <name>";
  writeXMLString(t,md->name());
  t << "</name>" << endl;
  if (xmlType==function_t) //function
  {
    t << "              <paramlist>" << endl;
    ArgumentList *declAl = new ArgumentList;
    ArgumentList *defAl = md->argumentList();
    stringToArgumentList(md->argsString(),declAl);
    if (declAl->count()>0)
    {
      ArgumentListIterator declAli(*declAl);
      ArgumentListIterator defAli(*defAl);
      Argument *a;
      for (declAli.toFirst();(a=declAli.current());++declAli)
      {
        Argument *defArg = defAli.current();
        t << "                <param>" << endl;
        if (!a->attrib.isEmpty())
        {
          t << "                  <attributes>";
          writeXMLString(t,a->attrib);
          t << "</attributes>" << endl;
        }
        if (!a->type.isEmpty())
        {
          t << "                  <type>";
          linkifyText(TextGeneratorXMLImpl(t),scopeName,md->name(),a->type);
          t << "</type>" << endl;
        }
        if (!a->name.isEmpty())
        {
          t << "                  <declname>";
          writeXMLString(t,a->name); 
          t << "</declname>" << endl;
        }
        if (defArg && !defArg->name.isEmpty() && defArg->name!=a->name)
        {
          t << "                  <defname>";
          writeXMLString(t,defArg->name);
          t << "</defname>" << endl;
        }
        if (!a->array.isEmpty())
        {
          t << "                  <array>"; 
          writeXMLString(t,a->array); 
          t << "</array>" << endl;
        }
        if (!a->defval.isEmpty())
        {
          t << "                  <defval>";
          linkifyText(TextGeneratorXMLImpl(t),scopeName,md->name(),a->defval);
          t << "</defval>" << endl;
        }
        t << "                </param>" << endl;
        if (defArg) ++defAli;
      }
    }
    delete declAl;
    t << "              </paramlist>" << endl;
  }
  else if (xmlType==define_t && md->argsString()!=0) // define
  {
    t << "              <defparamlist>" << endl;
    ArgumentListIterator ali(*md->argumentList());
    Argument *a;
    for (ali.toFirst();(a=ali.current());++ali)
    {
      t << "                <defarg>" << a->type << "</defarg>" << endl;
    }
    t << "              </defparamlist>" << endl;
    if (!md->initializer().isEmpty())
    {
      t << "             <initializer>";
      linkifyText(TextGeneratorXMLImpl(t),scopeName,md->name(),md->initializer());
      t << "</initializer>" << endl;
    }
  }
  else if (xmlType==enum_t) // enum
  {
    t << "              <enumvaluelist>" << endl;
    if (md->enumFieldList())
    {
      MemberListIterator emli(*md->enumFieldList());
      MemberDef *emd;
      for (emli.toFirst();(emd=emli.current());++emli)
      {
        t << "                <enumvalue>" << endl;
        t << "                  <name>";
        writeXMLString(t,emd->name());
        t << "</name>" << endl;
        if (!emd->initializer().isEmpty())
        {
          t << "                  <initializer>";
          writeXMLString(t,emd->initializer());
          t << "</initializer>" << endl;
        }
        t << "                </enumvalue>" << endl;
      }
    }
    t << "              </enumvaluelist>" << endl;
  }
  t << "            </";
  switch (md->memberType())
  {
    case MemberDef::Define:      t << "definedef";   break;
    case MemberDef::EnumValue:   // fall through
    case MemberDef::Property:    // fall through
    case MemberDef::Variable:    t << "variabledef"; break;
    case MemberDef::Typedef:     t << "typedef";     break;
    case MemberDef::Enumeration: t << "enumdef";     break;
    case MemberDef::Function:    // fall through
    case MemberDef::Signal:      // fall through
    case MemberDef::Prototype:   // fall through
    case MemberDef::Friend:      // fall through
    case MemberDef::DCOP:        // fall through
    case MemberDef::Slot:        t << "functiondef"; break;
  }
  t << ">" << endl;
}


void generateXMLClassSection(ClassDef *cd,QTextStream &t,MemberList *ml,const char *type)
{
  if (ml->count()>0)
  {
    t << "        <sectiondef type=\"" << type << "\">" << endl;
    t << "          <memberlist>" << endl;
    MemberListIterator mli(*ml);
    MemberDef *md;
    for (mli.toFirst();(md=mli.current());++mli)
    {
      generateXMLForMember(md,t,cd);
    }
    t << "          </memberlist>" << endl;
    t << "        </sectiondef>" << endl;
  }
}

void generateXMLForClass(ClassDef *cd,QTextStream &t)
{
  if (cd->name().find('@')!=-1) return; // skip anonymous compounds
  t << "    <compounddef id=\"" 
    << cd->getOutputFileBase() << "\" type=\"" 
    << cd->compoundTypeString() << "\">" << endl;
  t << "      <compoundname>"; 
  writeXMLString(t,cd->name()); 
  t << "</compoundname>" << endl;
  if (cd->baseClasses()->count()>0)
  {
    t << "      <basecompoundlist>" << endl;
    BaseClassListIterator bcli(*cd->baseClasses());
    BaseClassDef *bcd;
    for (bcli.toFirst();(bcd=bcli.current());++bcli)
    {
      t << "        <basecompoundref idref=\"" 
        << bcd->classDef->getOutputFileBase()
        << "\" prot=\"";
      switch (bcd->prot)
      {
        case Public:    t << "public";    break;
        case Protected: t << "protected"; break;
        case Private:   t << "private";   break;
      }
      t << "\" virt=\"";
      switch(bcd->virt)
      {
        case Normal:  t << "non-virtual";  break;
        case Virtual: t << "virtual";      break;
        case Pure:    t <<"pure-virtual"; break;
      }
      t << "\"/>" << endl;
    }
    t << "      </basecompoundlist>" << endl;
  }
  if (cd->subClasses()->count()>0)
  {
    t << "      <derivedcompoundlist>" << endl;
    BaseClassListIterator bcli(*cd->subClasses());
    BaseClassDef *bcd;
    for (bcli.toFirst();(bcd=bcli.current());++bcli)
    {
      t << "        <derivedcompoundref idref=\"" 
        << bcd->classDef->getOutputFileBase()
        << "\" prot=\"";
      switch (bcd->prot)
      {
        case Public:    t << "public";    break;
        case Protected: t << "protected"; break;
        case Private:   t << "private";   break;
      }
      t << "\" virt=\"";
      switch(bcd->virt)
      {
        case Normal:  t << "non-virtual";  break;
        case Virtual: t << "virtual";      break;
        case Pure:    t << "pure-virtual"; break;
      }
      t << "\"/>" << endl;
    }
    t << "      </derivedcompoundlist>" << endl;
  }
  int numMembers = 
    cd->pubTypes.count()+cd->pubMembers.count()+cd->pubAttribs.count()+
    cd->pubSlots.count()+cd->signals.count()+cd->dcopMethods.count()+
    cd->pubStaticMembers.count()+
    cd->pubStaticAttribs.count()+cd->proTypes.count()+cd->proMembers.count()+
    cd->proAttribs.count()+cd->proSlots.count()+cd->proStaticMembers.count()+
    cd->proStaticAttribs.count()+cd->priTypes.count()+cd->priMembers.count()+
    cd->priAttribs.count()+cd->priSlots.count()+cd->priStaticMembers.count()+
    cd->priStaticAttribs.count()+cd->friends.count()+cd->related.count();
  if (numMembers>0)
  {
    t << "      <sectionlist>" << endl;
    generateXMLClassSection(cd,t,&cd->pubTypes,"public-type");
    generateXMLClassSection(cd,t,&cd->pubMembers,"public-func");
    generateXMLClassSection(cd,t,&cd->pubAttribs,"public-attrib");
    generateXMLClassSection(cd,t,&cd->pubSlots,"public-slot");
    generateXMLClassSection(cd,t,&cd->signals,"signal");
    generateXMLClassSection(cd,t,&cd->dcopMethods,"dcop-func");
    generateXMLClassSection(cd,t,&cd->properties,"property");
    generateXMLClassSection(cd,t,&cd->pubStaticMembers,"public-static-func");
    generateXMLClassSection(cd,t,&cd->pubStaticAttribs,"public-static-attrib");
    generateXMLClassSection(cd,t,&cd->proTypes,"protected-type");
    generateXMLClassSection(cd,t,&cd->proMembers,"protected-func");
    generateXMLClassSection(cd,t,&cd->proAttribs,"protected-attrib");
    generateXMLClassSection(cd,t,&cd->proSlots,"protected-slot");
    generateXMLClassSection(cd,t,&cd->proStaticMembers,"protected-static-func");
    generateXMLClassSection(cd,t,&cd->proStaticAttribs,"protected-static-attrib");
    generateXMLClassSection(cd,t,&cd->priTypes,"private-type");
    generateXMLClassSection(cd,t,&cd->priMembers,"private-func");
    generateXMLClassSection(cd,t,&cd->priAttribs,"private-attrib");
    generateXMLClassSection(cd,t,&cd->priSlots,"private-slot");
    generateXMLClassSection(cd,t,&cd->priStaticMembers,"private-static-func");
    generateXMLClassSection(cd,t,&cd->priStaticAttribs,"private-static-attrib");
    generateXMLClassSection(cd,t,&cd->friends,"signal");
    generateXMLClassSection(cd,t,&cd->related,"related");
    t << "      </sectionlist>" << endl;
  }
  t << "    </compounddef>" << endl;
}

void generateXMLFileSection(FileDef *fd,QTextStream &t,MemberList *ml,const char *type)
{
  if (ml->count()>0)
  {
    t << "        <sectiondef type=\"" << type << "\">" << endl;
    t << "          <memberlist>" << endl;
    MemberListIterator mli(*ml);
    MemberDef *md;
    for (mli.toFirst();(md=mli.current());++mli)
    {
      generateXMLForMember(md,t,fd);
    }
    t << "          </memberlist>" << endl;
    t << "        </sectiondef>" << endl;
  }
}

void generateXMLForFile(FileDef *fd,QTextStream &t)
{
  t << "    <compounddef id=\"" 
    << fd->getOutputFileBase() << "\" type=\"file\">" << endl;
  t << "      <compoundname>";
  writeXMLString(t,fd->name());
  t << "</compoundname>" << endl;
  int numMembers = fd->defineMembers.count()+fd->protoMembers.count()+
                   fd->typedefMembers.count()+fd->enumMembers.count()+
                   fd->funcMembers.count()+fd->varMembers.count();
  if (numMembers>0)
  {
    t << "      <sectionlist>" << endl;
    generateXMLFileSection(fd,t,&fd->defineMembers,"define");
    generateXMLFileSection(fd,t,&fd->protoMembers,"prototype");
    generateXMLFileSection(fd,t,&fd->typedefMembers,"typedef");
    generateXMLFileSection(fd,t,&fd->enumMembers,"enum");
    generateXMLFileSection(fd,t,&fd->funcMembers,"func");
    generateXMLFileSection(fd,t,&fd->varMembers,"var");
    t << "      </sectionlist>" << endl;
  }
  t << "    </compounddef>" << endl;
}


void generateXML()
{
  if (Config::instance()->getString("OUTPUT_DIRECTORY").isEmpty())
    Config::instance()->getString("OUTPUT_DIRECTORY")=QDir::currentDirPath();
  else
  {
    QDir dir(Config::instance()->getString("OUTPUT_DIRECTORY"));
    if (!dir.exists())
    {
      dir.setPath(QDir::currentDirPath());
      if (!dir.mkdir(Config::instance()->getString("OUTPUT_DIRECTORY")))
      {
        err("Error: tag OUTPUT_DIRECTORY: Output directory `%s' does not "
            "exist and cannot be created\n",Config::instance()->getString("OUTPUT_DIRECTORY").data());
        exit(1);
      }
      else if (!Config::instance()->getBool("QUIET"))
      {
        err("Notice: Output directory `%s' does not exist. "
            "I have created it for you.\n", Config::instance()->getString("OUTPUT_DIRECTORY").data());
      }
      dir.cd(Config::instance()->getString("OUTPUT_DIRECTORY"));
    }
    Config::instance()->getString("OUTPUT_DIRECTORY")=dir.absPath();
  }

  QDir dir(Config::instance()->getString("OUTPUT_DIRECTORY"));
  if (!dir.exists())
  {
    dir.setPath(QDir::currentDirPath());
    if (!dir.mkdir(Config::instance()->getString("OUTPUT_DIRECTORY")))
    {
      err("Cannot create directory %s\n",Config::instance()->getString("OUTPUT_DIRECTORY").data());
      return;
    }
  }
  QDir xmlDir(Config::instance()->getString("OUTPUT_DIRECTORY")+"/xml");
  if (!xmlDir.exists() && !xmlDir.mkdir(Config::instance()->getString("OUTPUT_DIRECTORY")+"/xml"))
  {
    err("Could not create xml directory in %s\n",Config::instance()->getString("OUTPUT_DIRECTORY").data());
    return;
  }
  generateDTD();
  
  QCString fileName=Config::instance()->getString("OUTPUT_DIRECTORY")+"/xml/doxygen.xml";
  QFile f(fileName);
  if (!f.open(IO_WriteOnly))
  {
    err("Cannot open file %s for writing!\n",fileName.data());
    return;
  }
  QTextStream t(&f);
  t << "<?xml version='1.0' encoding='ISO-8859-1' standalone='no'?>" << endl;
  t << "<!DOCTYPE doxygen SYSTEM \"doxygen.dtd\">" << endl;
  t << "<doxygen>" << endl;
  if (Doxygen::classList.count()+Doxygen::inputNameList.count()>0)
  {
    t << "  <compoundlist>" << endl;
    ClassListIterator cli(Doxygen::classList);
    ClassDef *cd;
    for (cli.toFirst();(cd=cli.current());++cli)
    {
      generateXMLForClass(cd,t);
    }
    FileNameListIterator fnli(Doxygen::inputNameList);
    FileName *fn;
    for (;(fn=fnli.current());++fnli)
    {
      FileNameIterator fni(*fn);
      FileDef *fd;
      for (;(fd=fni.current());++fni)
      {
        generateXMLForFile(fd,t);
      }
    }
    t << "  </compoundlist>" << endl;
  }
  t << "</doxygen>" << endl;
}