compoundhandler.cpp 8.29 KB
Newer Older
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1 2 3 4 5
/******************************************************************************
 *
 * $Id$
 *
 *
6
 * Copyright (C) 1997-2002 by Dimitri van Heesch.
Dimitri van Heesch's avatar
Dimitri van Heesch committed
7 8 9 10 11 12 13 14 15
 *
 * 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.
 *
 */

Dimitri van Heesch's avatar
Dimitri van Heesch committed
16 17 18
#include "mainhandler.h"
#include "compoundhandler.h"
#include "dochandler.h"
19
#include "debug.h"
20 21
#include "graphhandler.h"
#include "sectionhandler.h"
Dimitri van Heesch's avatar
Dimitri van Heesch committed
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
class CompoundErrorHandler : public QXmlErrorHandler
{
    public:
      virtual ~CompoundErrorHandler() {}
      bool warning( const QXmlParseException & )
      {
        return FALSE;
      }
      bool error( const QXmlParseException & )
      {
        return FALSE;
      }
      bool fatalError( const QXmlParseException &exception )
      {
        debug(1,"Fatal error at line %d column %d: %s\n",
            exception.lineNumber(),exception.columnNumber(),
            exception.message().data());
        return FALSE;
      }
      QString errorString() { return ""; }

    private:
      QString errorMsg;
};

//----------------------------------------------------------------------------

class CompoundTypeMap
{
  public:
    CompoundTypeMap()
    {
      m_map.setAutoDelete(TRUE);
      m_map.insert("class",new int(ICompound::Class));
      m_map.insert("struct",new int(ICompound::Struct));
      m_map.insert("union",new int(ICompound::Union));
      m_map.insert("interface",new int(ICompound::Interface));
      m_map.insert("exception",new int(ICompound::Exception));
      m_map.insert("namespace",new int(ICompound::Namespace));
      m_map.insert("file",new int(ICompound::File));
      m_map.insert("group",new int(ICompound::Group));
      m_map.insert("page",new int(ICompound::Page));
      m_map.insert("package",new int(ICompound::Package));
    }
    virtual ~CompoundTypeMap()
    {
    }
    ICompound::CompoundKind map(const QString &s)
    {
      int *val = m_map.find(s);
      if (val==0) 
      {
        debug(1,"Warning: `%s' is an invalid compound type\n",s.data());
        return ICompound::Invalid;
      }
      else return (ICompound::CompoundKind)*val;
    }
  private: 
    QDict<int> m_map;
};

static CompoundTypeMap *s_typeMap;

void compoundhandler_init()
{
  s_typeMap = new CompoundTypeMap;
}

void compoundhandler_exit()
{
  delete s_typeMap;
}

//----------------------------------------------------------------------------

CompoundHandler::CompoundHandler(const QString &xmlDir) 
  : m_brief(0), m_detailed(0), m_programListing(0),
    m_xmlDir(xmlDir), m_refCount(1), m_memberDict(257), m_memberNameDict(257),
    m_mainHandler(0)
Dimitri van Heesch's avatar
Dimitri van Heesch committed
102 103
{
  m_superClasses.setAutoDelete(TRUE);
104 105
  m_subClasses.setAutoDelete(TRUE);
  m_sections.setAutoDelete(TRUE);
106
  m_memberNameDict.setAutoDelete(TRUE);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
107

108 109 110 111
  addStartHandler("doxygen");
  addEndHandler("doxygen");
  
  addStartHandler("compounddef",this,&CompoundHandler::startCompound);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
  addEndHandler("compounddef",this,&CompoundHandler::endCompound);

  addStartHandler("compoundname");
  addEndHandler("compoundname",this,&CompoundHandler::endCompoundName);

  addStartHandler("derivedcompoundref",this,&CompoundHandler::addSubClass);
  addEndHandler("derivedcompoundref");

  addStartHandler("basecompoundref",this,&CompoundHandler::addSuperClass);
  addEndHandler("basecompoundref");

  addStartHandler("briefdescription",this,&CompoundHandler::startBriefDesc);

  addStartHandler("detaileddescription",this,&CompoundHandler::startDetailedDesc);
  
  addStartHandler("sectiondef",this,&CompoundHandler::startSection);
128 129 130
  
  addStartHandler("location",this,&CompoundHandler::startLocation);
  addEndHandler("location");
131 132

  addStartHandler("programlisting",this,&CompoundHandler::startProgramListing);
133

134 135 136 137
  addStartHandler("inheritancegraph",this,&CompoundHandler::startInheritanceGraph);

  addStartHandler("collaborationgraph",this,&CompoundHandler::startCollaborationGraph);

Dimitri van Heesch's avatar
Dimitri van Heesch committed
138 139 140 141
}

CompoundHandler::~CompoundHandler()
{
142
  debug(2,"CompoundHandler::~CompoundHandler()\n");
143 144
  delete m_brief;
  delete m_detailed;
145
  delete m_programListing;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
}

void CompoundHandler::startSection(const QXmlAttributes& attrib)
{
  SectionHandler *sectHandler = new SectionHandler(this);
  sectHandler->startSection(attrib);
  m_sections.append(sectHandler);
}

void CompoundHandler::startBriefDesc(const QXmlAttributes& attrib)
{
  DocHandler *docHandler = new DocHandler(this);
  docHandler->startDoc(attrib);
  m_brief = docHandler;
}

void CompoundHandler::startDetailedDesc(const QXmlAttributes& attrib)
{
  DocHandler *docHandler = new DocHandler(this);
  docHandler->startDoc(attrib);
  m_detailed = docHandler;
}

169 170 171 172 173 174 175
void CompoundHandler::startProgramListing(const QXmlAttributes& attrib)
{
  ProgramListingHandler *plHandler = new ProgramListingHandler(this);
  plHandler->startProgramListing(attrib);
  m_programListing = plHandler;
}

Dimitri van Heesch's avatar
Dimitri van Heesch committed
176 177
void CompoundHandler::startCompound(const QXmlAttributes& attrib)
{
178
  m_id = attrib.value("id");
179 180 181
  m_kindString = attrib.value("kind");
  m_kind = s_typeMap->map(m_kindString);
  debug(2,"startCompound(id=`%s' type=`%s')\n",m_id.data(),m_kindString.data());
182 183
}

184
void CompoundHandler::endCompound()
185
{
186
   debug(2,"endCompound()\n");
Dimitri van Heesch's avatar
Dimitri van Heesch committed
187 188
}

189
void CompoundHandler::startLocation(const QXmlAttributes& attrib)
Dimitri van Heesch's avatar
Dimitri van Heesch committed
190
{
191 192
  m_defFile = attrib.value("file");
  m_defLine = attrib.value("line").toInt();
Dimitri van Heesch's avatar
Dimitri van Heesch committed
193 194 195 196 197
}

void CompoundHandler::endCompoundName()
{
  m_name = m_curString.stripWhiteSpace();
198
  debug(2,"Compound name `%s'\n",m_name.data());
Dimitri van Heesch's avatar
Dimitri van Heesch committed
199 200 201 202
}

void CompoundHandler::addSuperClass(const QXmlAttributes& attrib)
{
203
  RelatedClass *sc=new RelatedClass(
Dimitri van Heesch's avatar
Dimitri van Heesch committed
204
          attrib.value("refid"),
Dimitri van Heesch's avatar
Dimitri van Heesch committed
205 206 207
          attrib.value("prot"),
          attrib.value("virt")
         );
208
  debug(2,"super class id=`%s' prot=`%s' virt=`%s'\n",
Dimitri van Heesch's avatar
Dimitri van Heesch committed
209 210 211 212 213 214 215 216
      sc->m_id.data(),
      sc->m_protection.data(),
      sc->m_virtualness.data());
  m_superClasses.append(sc);
}

void CompoundHandler::addSubClass(const QXmlAttributes& attrib)
{
217
  RelatedClass *sc = new RelatedClass(
Dimitri van Heesch's avatar
Dimitri van Heesch committed
218
          attrib.value("refid"),
Dimitri van Heesch's avatar
Dimitri van Heesch committed
219 220 221
          attrib.value("prot"),
          attrib.value("virt")
         );
222
  debug(2,"sub class id=`%s' prot=`%s' virt=`%s'\n",
Dimitri van Heesch's avatar
Dimitri van Heesch committed
223 224 225 226 227 228
      sc->m_id.data(),
      sc->m_protection.data(),
      sc->m_virtualness.data());
  m_subClasses.append(sc);
}

229 230 231 232 233 234 235 236 237 238 239 240 241 242
bool CompoundHandler::parseXML(const QString &compId)
{
  QFile xmlFile(m_xmlDir+"/"+compId+".xml");
  if (!xmlFile.exists()) return FALSE;
  CompoundErrorHandler errorHandler;
  QXmlInputSource source( xmlFile );
  QXmlSimpleReader reader;
  reader.setContentHandler( this );
  reader.setErrorHandler( &errorHandler );
  reader.parse( source );
  return TRUE;
}

void CompoundHandler::initialize(MainHandler *mh)
Dimitri van Heesch's avatar
Dimitri van Heesch committed
243
{
244
  m_mainHandler = mh;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
245 246 247 248
  QListIterator<ISection> msi(m_sections);
  SectionHandler *sec;
  for (;(sec=(SectionHandler *)msi.current());++msi)
  {
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
    sec->initialize(this);
  }
}

void CompoundHandler::insertMember(MemberHandler *mh)
{
  m_memberDict.insert(mh->id(),mh);
  QList<MemberHandler> *mhl = m_memberNameDict.find(mh->id());
  if (mhl==0)
  {
    mhl = new QList<MemberHandler>;
    m_memberNameDict.insert(mh->name(),mhl);
  }
  mhl->append(mh);
}

void CompoundHandler::release()
{ 
  debug(2,"CompoundHandler::release() %d->%d\n",m_refCount,m_refCount-1);
  if (--m_refCount<=0)
  {
    m_mainHandler->unloadCompound(this);
    delete this; 
Dimitri van Heesch's avatar
Dimitri van Heesch committed
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
ISectionIterator *CompoundHandler::sections() const 
{ 
  return new SectionIterator(m_sections); 
}
    
IMemberIterator *CompoundHandler::memberByName(const QString &name) const
{ 
  QList<MemberHandler> *ml = m_memberNameDict[name]; 
  if (ml==0) return 0;
  return new MemberIterator(*ml);
}

void CompoundHandler::startInheritanceGraph(const QXmlAttributes &attrib)
{
  m_inheritanceGraph = new GraphHandler(this,"inheritancegraph");
  m_inheritanceGraph->startGraph(attrib);
}

void CompoundHandler::startCollaborationGraph(const QXmlAttributes &attrib)
{
  m_collaborationGraph = new GraphHandler(this,"collaborationgraph");
  m_collaborationGraph->startGraph(attrib);
}

IDocRoot *CompoundHandler::briefDescription() const 
{ 
  return m_brief; 
}

IDocRoot *CompoundHandler::detailedDescription() const 
{ 
  return m_detailed; 
}

IMember *CompoundHandler::memberById(const QString &id) const 
{ 
  return m_memberDict[id]; 
}