oopapidocs  2.0
docbookwalker.cpp
00001 #include "docbookwalker.h"
00002 #include <QDebug>
00003 
00004 DocbookWalker::DocbookWalker(DocbookDoc doc) 
00005     :  m_document(doc), m_chapt(0), m_appendix(0), m_visited(0) {
00006 
00007 }
00008   
00009 void DocbookWalker::newSection(Element e, int depth) {
00010     m_currentSection = e;
00011     while (m_sectNum.size() < depth+2)
00012         m_sectNum << 0;
00013     ++m_sectNum[depth];
00014     m_sectNum[depth+1] = 0;
00015 }
00016 
00017 
00018 QString DocbookWalker::heading(Element sect) const {
00019     if (m_chapt == 0) return QString();
00020     QStringList sl;
00021     for (int i=0; i<m_sectNum.size(); ++i) {
00022         if (m_sectNum[i] > 0) 
00023             sl << QString("%1").arg(m_sectNum[i]);
00024     }
00025     QString header = sect.tagName();  
00026     if (header == "chapter") {
00027         sl.clear();
00028         sl << QString("%1").arg(m_chapt);
00029     }
00030     else if (header == "appendix") {
00031         QChar appendix('A' + (char) m_appendix);
00032         sl.insert(0, QString("%1").arg(appendix)); 
00033     }
00034 
00035     header[0] = header[0].toUpper();
00036     QString retval = header + " " + sl.join(".");
00037     QString titleString = m_document.getTitle(sect);
00038     retval += ": " + titleString;
00039     return retval;
00040 }
00041 
00042 
00043 QString DocbookWalker::currentHeading() const {
00044     return heading(m_currentSection);
00045 }
00046 
00047 void DocbookWalker::newChapter(Element e) {
00048     ++m_chapt;
00049     m_sectNum.clear();
00050     m_sectNum << m_chapt;
00051     m_currentChapter = e;
00052     m_currentSection = e;
00053 }
00054 
00055 void DocbookWalker::newAppendix(Element e) {
00056     ++m_appendix;
00057     m_sectNum.clear();
00058     m_currentChapter = e;
00059     m_currentSection = e;
00060     
00061 }
00062 
00063 bool DocbookWalker::visit(int depth, Element e) 
00064 {
00065     QString name = e.tagName();
00066     if (name == "appendix") {
00067         newAppendix(e);
00068     }
00069     if (name == "chapter") {
00070         newChapter(e);
00071     }
00072     if (name == "section") {
00073         newSection(e, depth);
00074     }
00075     return true;
00076 }
00077 
00078 
00079 QDomNode DocbookWalker::walkTree(int depth, QDomNode current) {
00080     
00081     static int count = 0;
00082     ++count;
00083     if (count % 1000 == 0) qDebug() << "." << flush;
00084     bool checkChildren = false;    
00085     if (current.nodeType() == QDomNode::ElementNode) {
00086         Element ce = current.toElement(); 
00087         checkChildren = visit(depth, ce);
00088         ++m_visited;
00089     }
00090     if (checkChildren) {
00091         Element child = current.firstChildElement();
00092         while (!child.isNull()) {
00093             walkTree(depth+1, child); 
00094             child = child.nextSiblingElement();
00095         }
00096     }
00097     return current;
00098 }
00099 
00100 
00101 QDomDocument DocbookWalker::transform() {
00102     walkTree(0, m_document.documentElement());
00103     return m_document;
00104 }
 All Classes Namespaces Functions Enumerations