1 /******************************************************************************
6 * Copyright (C) 1997-2014 by Dimitri van Heesch.
8 * Permission to use, copy, modify, and distribute this software and its
9 * documentation under the terms of the GNU General Public License is hereby
10 * granted. No representations are made about the suitability of this software
11 * for any purpose. It is provided "as is" without express or implied warranty.
12 * See the GNU General Public License for more details.
14 * Documents produced by Doxygen are derivative works derived from the
15 * input used in their production; they are not affected by this license.
19 #include <qfileinfo.h>
21 #include "xmldocvisitor.h"
22 #include "docparser.h"
25 #include "outputgen.h"
30 #include "parserintf.h"
33 #include "htmlentity.h"
35 XmlDocVisitor::XmlDocVisitor(FTextStream &t,CodeOutputInterface &ci)
36 : DocVisitor(DocVisitor_XML), m_t(t), m_ci(ci), m_insidePre(FALSE), m_hide(FALSE)
40 //--------------------------------------
41 // visitor functions for leaf nodes
42 //--------------------------------------
44 void XmlDocVisitor::visit(DocWord *w)
50 void XmlDocVisitor::visit(DocLinkedWord *w)
53 startLink(w->ref(),w->file(),w->anchor());
58 void XmlDocVisitor::visit(DocWhiteSpace *w)
71 void XmlDocVisitor::visit(DocSymbol *s)
74 const char *res = HtmlEntityMapper::instance()->xml(s->symbol());
81 err("XML: non supported HTML-entity found: %s\n",HtmlEntityMapper::instance()->html(s->symbol(),TRUE));
85 void XmlDocVisitor::visit(DocURL *u)
88 m_t << "<ulink url=\"";
89 if (u->isEmail()) m_t << "mailto:";
96 void XmlDocVisitor::visit(DocLineBreak *)
99 m_t << "<linebreak/>\n";
102 void XmlDocVisitor::visit(DocHorRuler *)
105 m_t << "<hruler/>\n";
108 void XmlDocVisitor::visit(DocStyleChange *s)
113 case DocStyleChange::Bold:
114 if (s->enable()) m_t << "<bold>"; else m_t << "</bold>";
116 case DocStyleChange::Italic:
117 if (s->enable()) m_t << "<emphasis>"; else m_t << "</emphasis>";
119 case DocStyleChange::Code:
120 if (s->enable()) m_t << "<computeroutput>"; else m_t << "</computeroutput>";
122 case DocStyleChange::Subscript:
123 if (s->enable()) m_t << "<subscript>"; else m_t << "</subscript>";
125 case DocStyleChange::Superscript:
126 if (s->enable()) m_t << "<superscript>"; else m_t << "</superscript>";
128 case DocStyleChange::Center:
129 if (s->enable()) m_t << "<center>"; else m_t << "</center>";
131 case DocStyleChange::Small:
132 if (s->enable()) m_t << "<small>"; else m_t << "</small>";
134 case DocStyleChange::Preformatted:
137 m_t << "<preformatted>";
142 m_t << "</preformatted>";
146 case DocStyleChange::Div: /* HTML only */ break;
147 case DocStyleChange::Span: /* HTML only */ break;
151 void XmlDocVisitor::visit(DocVerbatim *s)
154 QCString lang = m_langExt;
155 if (!s->language().isEmpty()) // explicit language setting
157 lang = s->language();
159 SrcLangExt langExt = getLanguageFromFileName(lang);
162 case DocVerbatim::Code: // fall though
163 m_t << "<programlisting>";
164 Doxygen::parserManager->getParser(lang)
165 ->parseCode(m_ci,s->context(),s->text(),langExt,
166 s->isExample(),s->exampleFile());
167 m_t << "</programlisting>";
169 case DocVerbatim::Verbatim:
172 m_t << "</verbatim>";
174 case DocVerbatim::HtmlOnly:
177 m_t << "</htmlonly>";
179 case DocVerbatim::RtfOnly:
184 case DocVerbatim::ManOnly:
189 case DocVerbatim::LatexOnly:
190 m_t << "<latexonly>";
192 m_t << "</latexonly>";
194 case DocVerbatim::XmlOnly:
197 case DocVerbatim::DocbookOnly:
198 m_t << "<docbookonly>";
200 m_t << "</docbookonly>";
202 case DocVerbatim::Dot:
207 case DocVerbatim::Msc:
215 void XmlDocVisitor::visit(DocAnchor *anc)
218 m_t << "<anchor id=\"" << anc->file() << "_1" << anc->anchor() << "\"/>";
221 void XmlDocVisitor::visit(DocInclude *inc)
224 SrcLangExt langExt = getLanguageFromFileName(inc->extension());
227 case DocInclude::IncWithLines:
229 m_t << "<programlisting>";
230 QFileInfo cfi( inc->file() );
231 FileDef fd( cfi.dirPath().utf8(), cfi.fileName().utf8() );
232 Doxygen::parserManager->getParser(inc->extension())
233 ->parseCode(m_ci,inc->context(),
237 inc->exampleFile(), &fd);
238 m_t << "</programlisting>";
241 case DocInclude::Include:
242 m_t << "<programlisting>";
243 Doxygen::parserManager->getParser(inc->extension())
244 ->parseCode(m_ci,inc->context(),
249 m_t << "</programlisting>";
251 case DocInclude::DontInclude:
253 case DocInclude::HtmlInclude:
256 m_t << "</htmlonly>";
258 case DocInclude::LatexInclude:
259 m_t << "<latexonly>";
261 m_t << "</latexonly>";
263 case DocInclude::VerbInclude:
266 m_t << "</verbatim>";
268 case DocInclude::Snippet:
269 m_t << "<programlisting>";
270 Doxygen::parserManager->getParser(inc->extension())
273 extractBlock(inc->text(),inc->blockId()),
278 m_t << "</programlisting>";
283 void XmlDocVisitor::visit(DocIncOperator *op)
285 //printf("DocIncOperator: type=%d first=%d, last=%d text=`%s'\n",
286 // op->type(),op->isFirst(),op->isLast(),op->text().data());
291 m_t << "<programlisting>";
296 SrcLangExt langExt = getLanguageFromFileName(m_langExt);
297 if (op->type()!=DocIncOperator::Skip)
302 Doxygen::parserManager->getParser(m_langExt)
303 ->parseCode(m_ci,op->context(),
304 op->text(),langExt,op->isExample(),
313 if (!m_hide) m_t << "</programlisting>";
317 if (!m_hide) m_t << endl;
321 void XmlDocVisitor::visit(DocFormula *f)
324 m_t << "<formula id=\"" << f->id() << "\">";
329 void XmlDocVisitor::visit(DocIndexEntry *ie)
332 m_t << "<indexentry>"
335 m_t << "</primaryie>"
336 "<secondaryie></secondaryie>"
340 void XmlDocVisitor::visit(DocSimpleSectSep *sep)
342 if (sep->parent() && sep->parent()->kind()==DocNode::Kind_SimpleSect)
344 visitPost((DocSimpleSect*)sep->parent()); // end current section
345 visitPre((DocSimpleSect*)sep->parent()); // start new section
349 void XmlDocVisitor::visit(DocCite *cite)
352 if (!cite->file().isEmpty()) startLink(cite->ref(),cite->file(),cite->anchor());
353 filter(cite->text());
354 if (!cite->file().isEmpty()) endLink();
357 //--------------------------------------
358 // visitor functions for compound nodes
359 //--------------------------------------
361 void XmlDocVisitor::visitPre(DocAutoList *l)
366 m_t << "<orderedlist>\n";
370 m_t << "<itemizedlist>\n";
374 void XmlDocVisitor::visitPost(DocAutoList *l)
379 m_t << "</orderedlist>\n";
383 m_t << "</itemizedlist>\n";
387 void XmlDocVisitor::visitPre(DocAutoListItem *)
393 void XmlDocVisitor::visitPost(DocAutoListItem *)
396 m_t << "</listitem>";
399 void XmlDocVisitor::visitPre(DocPara *)
405 void XmlDocVisitor::visitPost(DocPara *)
411 void XmlDocVisitor::visitPre(DocRoot *)
413 //m_t << "<hr><h4><font color=\"red\">New parser:</font></h4>\n";
416 void XmlDocVisitor::visitPost(DocRoot *)
418 //m_t << "<hr><h4><font color=\"red\">Old parser:</font></h4>\n";
421 void XmlDocVisitor::visitPre(DocSimpleSect *s)
424 m_t << "<simplesect kind=\"";
427 case DocSimpleSect::See:
429 case DocSimpleSect::Return:
430 m_t << "return"; break;
431 case DocSimpleSect::Author:
432 m_t << "author"; break;
433 case DocSimpleSect::Authors:
434 m_t << "authors"; break;
435 case DocSimpleSect::Version:
436 m_t << "version"; break;
437 case DocSimpleSect::Since:
438 m_t << "since"; break;
439 case DocSimpleSect::Date:
440 m_t << "date"; break;
441 case DocSimpleSect::Note:
442 m_t << "note"; break;
443 case DocSimpleSect::Warning:
444 m_t << "warning"; break;
445 case DocSimpleSect::Pre:
447 case DocSimpleSect::Post:
448 m_t << "post"; break;
449 case DocSimpleSect::Copyright:
450 m_t << "copyright"; break;
451 case DocSimpleSect::Invar:
452 m_t << "invariant"; break;
453 case DocSimpleSect::Remark:
454 m_t << "remark"; break;
455 case DocSimpleSect::Attention:
456 m_t << "attention"; break;
457 case DocSimpleSect::User:
459 case DocSimpleSect::Rcs:
461 case DocSimpleSect::Unknown: break;
466 void XmlDocVisitor::visitPost(DocSimpleSect *)
469 m_t << "</simplesect>\n";
472 void XmlDocVisitor::visitPre(DocTitle *)
478 void XmlDocVisitor::visitPost(DocTitle *)
484 void XmlDocVisitor::visitPre(DocSimpleList *)
487 m_t << "<itemizedlist>\n";
490 void XmlDocVisitor::visitPost(DocSimpleList *)
493 m_t << "</itemizedlist>\n";
496 void XmlDocVisitor::visitPre(DocSimpleListItem *)
502 void XmlDocVisitor::visitPost(DocSimpleListItem *)
505 m_t << "</listitem>\n";
508 void XmlDocVisitor::visitPre(DocSection *s)
511 m_t << "<sect" << s->level() << " id=\"" << s->file();
512 if (!s->anchor().isEmpty()) m_t << "_1" << s->anchor();
513 m_t << "\">" << endl;
515 filter(convertCharEntitiesToUTF8(s->title()));
516 m_t << "</title>" << endl;
519 void XmlDocVisitor::visitPost(DocSection *s)
521 m_t << "</sect" << s->level() << ">\n";
524 void XmlDocVisitor::visitPre(DocHtmlList *s)
527 if (s->type()==DocHtmlList::Ordered)
528 m_t << "<orderedlist>\n";
530 m_t << "<itemizedlist>\n";
533 void XmlDocVisitor::visitPost(DocHtmlList *s)
536 if (s->type()==DocHtmlList::Ordered)
537 m_t << "</orderedlist>\n";
539 m_t << "</itemizedlist>\n";
542 void XmlDocVisitor::visitPre(DocHtmlListItem *)
545 m_t << "<listitem>\n";
548 void XmlDocVisitor::visitPost(DocHtmlListItem *)
551 m_t << "</listitem>\n";
554 void XmlDocVisitor::visitPre(DocHtmlDescList *)
557 m_t << "<variablelist>\n";
560 void XmlDocVisitor::visitPost(DocHtmlDescList *)
563 m_t << "</variablelist>\n";
566 void XmlDocVisitor::visitPre(DocHtmlDescTitle *)
569 m_t << "<varlistentry><term>";
572 void XmlDocVisitor::visitPost(DocHtmlDescTitle *)
575 m_t << "</term></varlistentry>\n";
578 void XmlDocVisitor::visitPre(DocHtmlDescData *)
584 void XmlDocVisitor::visitPost(DocHtmlDescData *)
587 m_t << "</listitem>\n";
590 void XmlDocVisitor::visitPre(DocHtmlTable *t)
593 m_t << "<table rows=\"" << t->numRows()
594 << "\" cols=\"" << t->numColumns() << "\">" ;
597 void XmlDocVisitor::visitPost(DocHtmlTable *)
603 void XmlDocVisitor::visitPre(DocHtmlRow *)
609 void XmlDocVisitor::visitPost(DocHtmlRow *)
615 void XmlDocVisitor::visitPre(DocHtmlCell *c)
618 if (c->isHeading()) m_t << "<entry thead=\"yes\">"; else m_t << "<entry thead=\"no\">";
621 void XmlDocVisitor::visitPost(DocHtmlCell *)
627 void XmlDocVisitor::visitPre(DocHtmlCaption *)
633 void XmlDocVisitor::visitPost(DocHtmlCaption *)
636 m_t << "</caption>\n";
639 void XmlDocVisitor::visitPre(DocInternal *)
645 void XmlDocVisitor::visitPost(DocInternal *)
648 m_t << "</internal>" << endl;
651 void XmlDocVisitor::visitPre(DocHRef *href)
654 m_t << "<ulink url=\"" << href->url() << "\">";
657 void XmlDocVisitor::visitPost(DocHRef *)
663 void XmlDocVisitor::visitPre(DocHtmlHeader *header)
666 m_t << "<heading level=\"" << header->level() << "\">";
669 void XmlDocVisitor::visitPost(DocHtmlHeader *)
672 m_t << "</heading>\n";
675 void XmlDocVisitor::visitPre(DocImage *img)
678 m_t << "<image type=\"";
681 case DocImage::Html: m_t << "html"; break;
682 case DocImage::Latex: m_t << "latex"; break;
683 case DocImage::Rtf: m_t << "rtf"; break;
687 QCString baseName=img->name();
689 if ((i=baseName.findRev('/'))!=-1 || (i=baseName.findRev('\\'))!=-1)
691 baseName=baseName.right(baseName.length()-i-1);
693 m_t << " name=\"" << baseName << "\"";
694 if (!img->width().isEmpty())
697 filter(img->width());
700 else if (!img->height().isEmpty())
703 filter(img->height());
708 // copy the image to the output dir
709 QFile inImage(img->name());
710 QFile outImage(Config_getString("XML_OUTPUT")+"/"+baseName.data());
711 if (inImage.open(IO_ReadOnly))
713 if (outImage.open(IO_WriteOnly))
715 char *buffer = new char[inImage.size()];
716 inImage.readBlock(buffer,inImage.size());
717 outImage.writeBlock(buffer,inImage.size());
724 void XmlDocVisitor::visitPost(DocImage *)
727 m_t << "</image>" << endl;
730 void XmlDocVisitor::visitPre(DocDotFile *df)
733 m_t << "<dotfile name=\"" << df->file() << "\">";
736 void XmlDocVisitor::visitPost(DocDotFile *)
739 m_t << "</dotfile>" << endl;
742 void XmlDocVisitor::visitPre(DocMscFile *df)
745 m_t << "<mscfile name=\"" << df->file() << "\">";
748 void XmlDocVisitor::visitPost(DocMscFile *)
751 m_t << "</mscfile>" << endl;
754 void XmlDocVisitor::visitPre(DocDiaFile *df)
757 m_t << "<diafile name=\"" << df->file() << "\">";
760 void XmlDocVisitor::visitPost(DocDiaFile *)
763 m_t << "</diafile>" << endl;
766 void XmlDocVisitor::visitPre(DocLink *lnk)
769 startLink(lnk->ref(),lnk->file(),lnk->anchor());
772 void XmlDocVisitor::visitPost(DocLink *)
778 void XmlDocVisitor::visitPre(DocRef *ref)
781 if (!ref->file().isEmpty())
783 startLink(ref->ref(),ref->file(),ref->isSubPage() ? QCString() : ref->anchor());
785 if (!ref->hasLinkText()) filter(ref->targetTitle());
788 void XmlDocVisitor::visitPost(DocRef *ref)
791 if (!ref->file().isEmpty()) endLink();
795 void XmlDocVisitor::visitPre(DocSecRefItem *ref)
798 m_t << "<tocitem id=\"" << ref->file() << "_1" << ref->anchor() << "\">";
801 void XmlDocVisitor::visitPost(DocSecRefItem *)
804 m_t << "</tocitem>" << endl;
807 void XmlDocVisitor::visitPre(DocSecRefList *)
810 m_t << "<toclist>" << endl;
813 void XmlDocVisitor::visitPost(DocSecRefList *)
816 m_t << "</toclist>" << endl;
819 //void XmlDocVisitor::visitPre(DocLanguage *l)
821 // if (m_hide) return;
822 // m_t << "<language langid=\"" << l->id() << "\">";
825 //void XmlDocVisitor::visitPost(DocLanguage *)
827 // if (m_hide) return;
828 // m_t << "</language>" << endl;
831 void XmlDocVisitor::visitPre(DocParamSect *s)
834 m_t << "<parameterlist kind=\"";
837 case DocParamSect::Param:
838 m_t << "param"; break;
839 case DocParamSect::RetVal:
840 m_t << "retval"; break;
841 case DocParamSect::Exception:
842 m_t << "exception"; break;
843 case DocParamSect::TemplateParam:
844 m_t << "templateparam"; break;
851 void XmlDocVisitor::visitPost(DocParamSect *)
854 m_t << "</parameterlist>" << endl;
857 void XmlDocVisitor::visitPre(DocParamList *pl)
860 m_t << "<parameteritem>" << endl;
861 m_t << "<parameternamelist>" << endl;
862 //QStrListIterator li(pl->parameters());
864 QListIterator<DocNode> li(pl->parameters());
866 for (li.toFirst();(param=li.current());++li)
868 if (pl->paramTypes().count()>0)
870 QListIterator<DocNode> li(pl->paramTypes());
872 for (li.toFirst();(type=li.current());++li)
874 m_t << "<parametertype>";
875 if (type->kind()==DocNode::Kind_Word)
877 visit((DocWord*)type);
879 else if (type->kind()==DocNode::Kind_LinkedWord)
881 visit((DocLinkedWord*)type);
883 m_t << "</parametertype>" << endl;
886 m_t << "<parametername";
887 if (pl->direction()!=DocParamSect::Unspecified)
889 m_t << " direction=\"";
890 if (pl->direction()==DocParamSect::In)
894 else if (pl->direction()==DocParamSect::Out)
898 else if (pl->direction()==DocParamSect::InOut)
905 if (param->kind()==DocNode::Kind_Word)
907 visit((DocWord*)param);
909 else if (param->kind()==DocNode::Kind_LinkedWord)
911 visit((DocLinkedWord*)param);
913 m_t << "</parametername>" << endl;
915 m_t << "</parameternamelist>" << endl;
916 m_t << "<parameterdescription>" << endl;
919 void XmlDocVisitor::visitPost(DocParamList *)
922 m_t << "</parameterdescription>" << endl;
923 m_t << "</parameteritem>" << endl;
926 void XmlDocVisitor::visitPre(DocXRefItem *x)
929 if (x->title().isEmpty()) return;
930 m_t << "<xrefsect id=\"";
931 m_t << x->file() << "_1" << x->anchor();
933 m_t << "<xreftitle>";
935 m_t << "</xreftitle>";
936 m_t << "<xrefdescription>";
939 void XmlDocVisitor::visitPost(DocXRefItem *x)
942 if (x->title().isEmpty()) return;
943 m_t << "</xrefdescription>";
944 m_t << "</xrefsect>";
947 void XmlDocVisitor::visitPre(DocInternalRef *ref)
950 startLink(0,ref->file(),ref->anchor());
953 void XmlDocVisitor::visitPost(DocInternalRef *)
960 void XmlDocVisitor::visitPre(DocCopy *c)
963 m_t << "<copydoc link=\"" << convertToXML(c->link()) << "\">";
966 void XmlDocVisitor::visitPost(DocCopy *)
969 m_t << "</copydoc>" << endl;
972 void XmlDocVisitor::visitPre(DocText *)
976 void XmlDocVisitor::visitPost(DocText *)
980 void XmlDocVisitor::visitPre(DocHtmlBlockQuote *)
983 m_t << "<blockquote>";
986 void XmlDocVisitor::visitPost(DocHtmlBlockQuote *)
989 m_t << "</blockquote>";
992 void XmlDocVisitor::visitPre(DocVhdlFlow *)
996 void XmlDocVisitor::visitPost(DocVhdlFlow *)
1000 void XmlDocVisitor::visitPre(DocParBlock *)
1003 m_t << "<parblock>";
1006 void XmlDocVisitor::visitPost(DocParBlock *)
1009 m_t << "</parblock>";
1013 void XmlDocVisitor::filter(const char *str)
1015 m_t << convertToXML(str);
1018 void XmlDocVisitor::startLink(const QCString &ref,const QCString &file,const QCString &anchor)
1020 //printf("XmlDocVisitor: file=%s anchor=%s\n",file.data(),anchor.data());
1021 m_t << "<ref refid=\"" << file;
1022 if (!anchor.isEmpty()) m_t << "_1" << anchor;
1023 m_t << "\" kindref=\"";
1024 if (!anchor.isEmpty()) m_t << "member"; else m_t << "compound";
1026 if (!ref.isEmpty()) m_t << " external=\"" << ref << "\"";
1030 void XmlDocVisitor::endLink()
1035 void XmlDocVisitor::pushEnabled()
1037 m_enabled.push(new bool(m_hide));
1040 void XmlDocVisitor::popEnabled()
1042 bool *v=m_enabled.pop();