From ffaca765ed010aa6f367f417a97eaa99902870a1 Mon Sep 17 00:00:00 2001 From: Stef Date: Sun, 11 Jul 2004 22:07:43 +0000 Subject: Fixed grouping issues Consolidate footnotes to end Proper ref format Fixed 'removeDuplicate' bugs --- src/tags.h | 15 +---- src/xmlcomposer.cpp | 15 +---- src/xmlcomposer.h | 2 - src/xmlfixups.cpp | 164 +++++++++++++++++++++++++++++++++++----------------- src/xmlfixups.h | 1 + 5 files changed, 118 insertions(+), 79 deletions(-) (limited to 'src') diff --git a/src/tags.h b/src/tags.h index 33676f7..1b4b284 100644 --- a/src/tags.h +++ b/src/tags.h @@ -86,7 +86,7 @@ static const char* kAtOrdered = "ordered"; static const char* kAtStart = "start"; static const char* kAtId = "id"; static const char* kAtIndex = "id"; -static const char* kAtFootNote = "footnote"; +static const char* kAtTo = "to"; static const wchar_t* kValDisc = L"disc"; static const wchar_t* kValLowerAlpha = L"lower-alpha"; @@ -95,23 +95,12 @@ static const wchar_t* kValLowerRoman = L"lower-roman"; static const wchar_t* kValUpperRoman = L"upper-roman"; static const wchar_t* kValArabic = L"arabic"; static const wchar_t* kValNull = L""; +static const wchar_t* kValFootNote = L"footnote"; static const wchar_t* kValList = L"list"; static const wchar_t* kValPara = L"para"; static const wchar_t* kValTable = L"table"; -static const char* kNoDuplicates[] = - { kElB, kElU, kElI, kElColor, kElHide, kElColor, NULL }; - -static const char* kRemoveTags[] = - { kElDest, kElListdef, kElListtable, NULL }; - -static const char* kBlockTags[] = - { kElTable, kElPara, NULL }; - -static const char* kHideList[] = - { kAtId, kAtList, NULL }; - static const char* kNSPrefix = "xmlns"; #endif // __TAGS_H__ diff --git a/src/xmlcomposer.cpp b/src/xmlcomposer.cpp index 4091adb..5c29e65 100644 --- a/src/xmlcomposer.cpp +++ b/src/xmlcomposer.cpp @@ -104,6 +104,7 @@ void RtfParser::endDocument() // Cleanup the tree RtfFixups::removeDuplicates(m_document); + RtfFixups::consolidateEndTags(m_document); RtfFixups::breakTables(m_document); RtfFixups::breakTags(m_document, kElTable, kElRow); RtfFixups::breakTags(m_document, kElRow, kElCell); @@ -504,7 +505,8 @@ bool RtfParser::ParseAnalyser::processTextAutoContent(const string& cw, int flag int ac = m_parser->getAutoCount(AUTOCOUNT_FOOTNOTE); AN_ELEMENT(kElRef); - AN_ATTRIBUTE(kAtFootNote, ac); + AN_ATTRIBUTE(kAtType, kValFootNote); + AN_ATTRIBUTE(kAtTo, ac); dest->charData(formatInt(ac)); AN_POP_ELEMENT(); return true; @@ -953,18 +955,7 @@ ON_DONE(FootNote) m_parser->incrementAutoCount(AUTOCOUNT_FOOTNOTE); } -// TODO: Remove these (for debugging) -ON_GROUPSTART(FootNote) -{ - int ac = m_parser->getAutoCount(AUTOCOUNT_FOOTNOTE); - fprintf(stderr, "%d start\n", ac); -} -ON_GROUPEND(FootNote) -{ - int ac = m_parser->getAutoCount(AUTOCOUNT_FOOTNOTE); - fprintf(stderr, "%d end\n", ac); -} ON_CHARDATA(Raw) diff --git a/src/xmlcomposer.h b/src/xmlcomposer.h index fc40496..deba4ba 100644 --- a/src/xmlcomposer.h +++ b/src/xmlcomposer.h @@ -234,8 +234,6 @@ protected: ANALYSER(FootNote) INITIALIZE CONTROLWORD - GROUPSTART - GROUPEND DONE END_ANALYSER }; diff --git a/src/xmlfixups.cpp b/src/xmlfixups.cpp index 7201703..64d360d 100644 --- a/src/xmlfixups.cpp +++ b/src/xmlfixups.cpp @@ -41,6 +41,21 @@ #include "domhelpers.h" #include "tags.h" +static const char* kNoDuplicates[] = + { kElB, kElU, kElI, kElColor, kElHide, kElColor, kElSuper, kElSub, NULL }; + +static const char* kRemoveTags[] = + { kElDest, kElListdef, kElListtable, NULL }; + +static const char* kBlockTags[] = + { kElTable, kElPara, NULL }; + +static const char* kHideList[] = + { kAtId, kAtList, NULL }; + +static const char* kConsolidateEnd[] = + { kElFootNote, NULL }; + void RtfFixups::breakBreak(DOM::Document& doc, const string& contain, const string& tag) { @@ -668,72 +683,117 @@ void RtfFixups::fixBlocks(const DOM::Document doc) } } - /** - * Removes adjacent duplicate nodes of certain names + * Consolidates a certain tag types at the end of the document */ -void RtfFixups::removeDuplicates(const DOM::Document& doc) +void RtfFixups::consolidateEndTags(DOM::Document& doc) { - // Go through the list of nodes - for(const char** t = kNoDuplicates; *t = NULL; t++) - { - DOM::NodeList elements = doc.getElementsByTagName(*t); - if(elements != NULL) - { - int x = elements->getLength(); - for(int j = 0; j < elements->getLength(); j++) - { + DOM::Element top = doc.getDocumentElement(); + ASSERT(top != NULL); - // Make sure it's a valid element + for(const char** t = kConsolidateEnd; *t != NULL; t++) + { + DOM::NodeList elements = doc.getElementsByTagName(*t); + if(elements != NULL) + { + int x = elements->getLength(); + for(int j = 0; j < x; j++) + { + // Make sure it's a valid element DOM::Element element = (const DOM::Element&)elements->item(j); - if(element == NULL) + if(element == NULL) continue; - // Get neighbors - DOM::Node previous = element.getPreviousSibling(); - DOM::Node next = element.getNextSibling(); - - // Make sure it's still in the document, as we may have - // removed it on a previous loop - DOM::Node parent = element.getParentNode(); + DOM::Element parent = (const DOM::Element&)element.getParentNode(); if(parent == NULL) - continue; + continue; - // Combine previous if valid - if(previous != NULL && previous.getNodeType() == DOM::Node::ELEMENT_NODE && - DOMHelpers::isEqualElement((DOM::Element&)previous, element)) - { - while(previous.hasChildNodes()) + // Remove it from it's child + parent.removeChild(element); + + // And append it to the end of the document + top.appendChild(element); + } + } + } +} + +/** + * Removes adjacent duplicate nodes of certain names + */ +void RtfFixups::removeDuplicates(const DOM::Document& doc) +{ + bool found; + + do + { + found = false; + + // Go through the list of nodes + for(const char** t = kNoDuplicates; *t != NULL; t++) + { + DOM::NodeList elements = doc.getElementsByTagName(*t); + if(elements != NULL) + { + int x = elements->getLength(); + for(int j = 0; j < x; j++) + { + // Make sure it's a valid element + DOM::Element element = (const DOM::Element&)elements->item(j); + if(element == NULL) + continue; + + // Get neighbors + DOM::Node previous = element.getPreviousSibling(); + DOM::Node next = element.getNextSibling(); + + // Make sure it's still in the document, as we may have + // removed it on a previous loop + DOM::Node parent = element.getParentNode(); + if(parent == NULL) + continue; + + // Combine previous if valid + if(previous != NULL && previous.getNodeType() == DOM::Node::ELEMENT_NODE && + DOMHelpers::isEqualElement((DOM::Element&)previous, element)) { - DOM::Node child = previous.removeChild(previous.getLastChild()); - if(child != NULL) - { - if(element.hasChildNodes()) - element.insertBefore(child, element.getFirstChild()); - else - element.appendChild(child); + while(previous.hasChildNodes()) + { + DOM::Node child = previous.removeChild(previous.getLastChild()); + if(child != NULL) + { + if(element.hasChildNodes()) + element.insertBefore(child, element.getFirstChild()); + else + element.appendChild(child); + } } - } - // Remove duplicate node - parent.removeChild(previous); - } + // Remove duplicate node + parent.removeChild(previous); + found = true; + } - // Combine next if valid - if(next != NULL && next.getNodeType() == DOM::Node::ELEMENT_NODE && - DOMHelpers::isEqualElement((DOM::Element&)next, element)) - { - while(next.hasChildNodes()) + // Combine next if valid + if(next != NULL && next.getNodeType() == DOM::Node::ELEMENT_NODE && + DOMHelpers::isEqualElement((DOM::Element&)next, element)) { - DOM::Node child = next.removeChild(next.getFirstChild()); - if(child != NULL) - element.appendChild(child); + while(next.hasChildNodes()) + { + DOM::Node child = next.removeChild(next.getFirstChild()); + if(child != NULL) + element.appendChild(child); + } + + // Remove duplicate node + parent.removeChild(next); + found = true; } + } + } + } - // Remove duplicate node - parent.removeChild(next); - } - } - } - } + // Keep looping until no more duplicates found + } + while(found); } diff --git a/src/xmlfixups.h b/src/xmlfixups.h index 8cc9b82..2def770 100644 --- a/src/xmlfixups.h +++ b/src/xmlfixups.h @@ -57,6 +57,7 @@ public: static void wrapTags(DOM::Document& document, const string& tagName, const string& wrapName); static void removeTags(const DOM::Document& doc); static void removeDuplicates(const DOM::Document& doc); + static void consolidateEndTags(DOM::Document& doc); }; #endif // __RTFFIXUPS_H__ -- cgit v1.2.3