diff options
author | Fridrich Štrba <fridrich.strba@bluewin.ch> | 2013-11-29 10:17:01 +0100 |
---|---|---|
committer | Fridrich Štrba <fridrich.strba@bluewin.ch> | 2013-11-29 10:17:01 +0100 |
commit | f99cd12e87de9f5ae4449cd39f9e38b677cb3a86 (patch) | |
tree | 8be1023b2a839ba950b86581ead4ec8431f99606 | |
parent | 6bbd6b3193cf106a4b1edbbde9b5dc577d2f70d4 (diff) |
Dry-parse the AbiWord xml elements
-rw-r--r-- | src/conv/html/abw2html.cpp | 21 | ||||
-rw-r--r-- | src/conv/raw/abw2raw.cpp | 2 | ||||
-rw-r--r-- | src/conv/text/abw2text.cpp | 2 | ||||
-rw-r--r-- | src/lib/ABWCollector.cpp | 22 | ||||
-rw-r--r-- | src/lib/ABWCollector.h | 34 | ||||
-rw-r--r-- | src/lib/ABWParser.cpp | 114 | ||||
-rw-r--r-- | src/lib/ABWParser.h | 50 | ||||
-rw-r--r-- | src/lib/AbiDocument.cpp | 9 | ||||
-rw-r--r-- | src/lib/Makefile.am | 4 |
9 files changed, 235 insertions, 23 deletions
diff --git a/src/conv/html/abw2html.cpp b/src/conv/html/abw2html.cpp index bc76a3f..d74aecb 100644 --- a/src/conv/html/abw2html.cpp +++ b/src/conv/html/abw2html.cpp @@ -1,23 +1,10 @@ -/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ -/* libabw - * Version: MPL 2.0 / LGPLv2.1+ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* + * This file is part of the libabw project. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. - * - * Major Contributor(s): - * Copyright (C) 2002-2003 William Lachance (wrlach@gmail.com) - * Copyright (C) 2002-2004 Marc Maurer (uwog@uwog.net) - * - * For minor contributions see the git repository. - * - * Alternatively, the contents of this file may be used under the terms - * of the GNU Lesser General Public License Version 2.1 or later - * (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are - * applicable instead of those above. - * - * For further information visit http://libabw.sourceforge.net */ #include <stdio.h> @@ -92,4 +79,4 @@ int main(int argc, char *argv[]) return 0; } -/* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */ +/* vim:set shiftwidth=2 softtabstop=2 expandtab: */ diff --git a/src/conv/raw/abw2raw.cpp b/src/conv/raw/abw2raw.cpp index adb41e7..a23f34d 100644 --- a/src/conv/raw/abw2raw.cpp +++ b/src/conv/raw/abw2raw.cpp @@ -79,4 +79,4 @@ int main(int argc, char *argv[]) return 0; return 1; } -/* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */ +/* vim:set shiftwidth=2 softtabstop=2 expandtab: */ diff --git a/src/conv/text/abw2text.cpp b/src/conv/text/abw2text.cpp index bdb851d..968e774 100644 --- a/src/conv/text/abw2text.cpp +++ b/src/conv/text/abw2text.cpp @@ -84,4 +84,4 @@ int main(int argc, char *argv[]) return 0; } -/* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */ +/* vim:set shiftwidth=2 softtabstop=2 expandtab: */ diff --git a/src/lib/ABWCollector.cpp b/src/lib/ABWCollector.cpp new file mode 100644 index 0000000..06253af --- /dev/null +++ b/src/lib/ABWCollector.cpp @@ -0,0 +1,22 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* + * This file is part of the libabw project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include <librevenge/librevenge.h> +#include "ABWCollector.h" + +libabw::ABWCollector::ABWCollector(librevenge::RVNGTextInterface *iface) : + m_iface(iface) +{ +} + +libabw::ABWCollector::~ABWCollector() +{ +} + +/* vim:set shiftwidth=2 softtabstop=2 expandtab: */ diff --git a/src/lib/ABWCollector.h b/src/lib/ABWCollector.h new file mode 100644 index 0000000..a03775c --- /dev/null +++ b/src/lib/ABWCollector.h @@ -0,0 +1,34 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* + * This file is part of the libabw project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#ifndef __ABWCOLLECTOR_H__ +#define __ABWCOLLECTOR_H__ + +namespace libabw +{ + +class ABWCollector +{ +public: + ABWCollector(::librevenge::RVNGTextInterface *iface); + virtual ~ABWCollector(); + + // collector functions + +private: + ABWCollector(const ABWCollector &); + ABWCollector &operator=(const ABWCollector &); + + librevenge::RVNGTextInterface *m_iface; +}; + +} // namespace libabw + +#endif /* __ABWCOLLECTOR_H__ */ +/* vim:set shiftwidth=2 softtabstop=2 expandtab: */ diff --git a/src/lib/ABWParser.cpp b/src/lib/ABWParser.cpp new file mode 100644 index 0000000..13fff6b --- /dev/null +++ b/src/lib/ABWParser.cpp @@ -0,0 +1,114 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* + * This file is part of the libabw project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include <string.h> +#include <libxml/xmlIO.h> +#include <libxml/xmlstring.h> +#include <librevenge-stream/librevenge-stream.h> +#include <boost/algorithm/string.hpp> +#include "ABWParser.h" +#include "ABWCollector.h" +#include "libabw_internal.h" +#include "ABWXMLHelper.h" +#include "ABWXMLTokenMap.h" + + +libabw::ABWParser::ABWParser(librevenge::RVNGInputStream *input, librevenge::RVNGTextInterface *iface) + : m_input(input), m_iface(iface), m_collector(0) +{ +} + +libabw::ABWParser::~ABWParser() +{ +} + +bool libabw::ABWParser::parse() +{ + if (!m_input) + return false; + + try + { + ABWCollector collector(m_iface); + m_collector = &collector; + m_input->seek(0, librevenge::RVNG_SEEK_SET); + if (!processXmlDocument(m_input)) + return false; + + return true; + } + catch (...) + { + return false; + } +} + +bool libabw::ABWParser::processXmlDocument(librevenge::RVNGInputStream *input) +{ + if (!input) + return false; + + xmlTextReaderPtr reader = xmlReaderForStream(input, 0, 0, XML_PARSE_NOBLANKS|XML_PARSE_NOENT|XML_PARSE_NONET|XML_PARSE_RECOVER); + if (!reader) + return false; + int ret = xmlTextReaderRead(reader); + while (1 == ret) + { + processXmlNode(reader); + + ret = xmlTextReaderRead(reader); + } + xmlFreeTextReader(reader); + + return true; +} + +void libabw::ABWParser::processXmlNode(xmlTextReaderPtr reader) +{ + if (!reader) + return; + int tokenId = getElementToken(reader); + int tokenType = xmlTextReaderNodeType(reader); + switch (tokenId) + { + default: + break; + } + +#ifdef DEBUG + const xmlChar *name = xmlTextReaderConstName(reader); + const xmlChar *value = xmlTextReaderConstValue(reader); + int isEmptyElement = xmlTextReaderIsEmptyElement(reader); + + ABW_DEBUG_MSG(("%i %i %s", isEmptyElement, tokenType, name ? (const char *)name : "")); + if (xmlTextReaderNodeType(reader) == 1) + { + while (xmlTextReaderMoveToNextAttribute(reader)) + { + const xmlChar *name1 = xmlTextReaderConstName(reader); + const xmlChar *value1 = xmlTextReaderConstValue(reader); + printf(" %s=\"%s\"", name1, value1); + } + } + + if (!value) + ABW_DEBUG_MSG(("\n")); + else + { + ABW_DEBUG_MSG((" %s\n", value)); + } +#endif +} + +int libabw::ABWParser::getElementToken(xmlTextReaderPtr reader) +{ + return ABWXMLTokenMap::getTokenId(xmlTextReaderConstName(reader)); +} + +/* vim:set shiftwidth=2 softtabstop=2 expandtab: */ diff --git a/src/lib/ABWParser.h b/src/lib/ABWParser.h new file mode 100644 index 0000000..0df83b0 --- /dev/null +++ b/src/lib/ABWParser.h @@ -0,0 +1,50 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* + * This file is part of the libabw project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#ifndef __ABWPARSER_H__ +#define __ABWPARSER_H__ + +#include <librevenge/librevenge.h> +#include "ABWXMLHelper.h" + +namespace libabw +{ + +class ABWCollector; + +class ABWParser +{ +public: + explicit ABWParser(librevenge::RVNGInputStream *input, librevenge::RVNGTextInterface *iface); + virtual ~ABWParser(); + bool parse(); + +private: + ABWParser(); + ABWParser(const ABWParser &); + ABWParser &operator=(const ABWParser &); + + // Helper functions + + int getElementToken(xmlTextReaderPtr reader); + + // Functions to read the AWML document structure + + bool processXmlDocument(librevenge::RVNGInputStream *input); + void processXmlNode(xmlTextReaderPtr reader); + + librevenge::RVNGInputStream *m_input; + librevenge::RVNGTextInterface *m_iface; + ABWCollector *m_collector; +}; + +} // namespace libabw + +#endif // __ABWPARSER_H__ +/* vim:set shiftwidth=2 softtabstop=2 expandtab: */ diff --git a/src/lib/AbiDocument.cpp b/src/lib/AbiDocument.cpp index e299109..6b10bbe 100644 --- a/src/lib/AbiDocument.cpp +++ b/src/lib/AbiDocument.cpp @@ -9,6 +9,7 @@ #include <libabw/libabw.h> #include "ABWXMLHelper.h" +#include "ABWParser.h" #include "libabw_internal.h" /** @@ -102,10 +103,10 @@ was not, it indicates the reason of the error bool AbiDocument::parse(librevenge::RVNGInputStream *input, librevenge::RVNGTextInterface *textInterface) { ABW_DEBUG_MSG(("AbiDocument::parse\n")); - if (!input) - return false; - if (!textInterface) - return false; + input->seek(0, librevenge::RVNG_SEEK_SET); + libabw::ABWParser parser(input, textInterface); + if (parser.parse()) + return true; return false; } /* vim:set shiftwidth=2 softtabstop=2 expandtab: */ diff --git a/src/lib/Makefile.am b/src/lib/Makefile.am index 7de83b8..2a6830b 100644 --- a/src/lib/Makefile.am +++ b/src/lib/Makefile.am @@ -30,10 +30,14 @@ libabw_@ABW_MAJOR_VERSION@_@ABW_MINOR_VERSION@_la_LIBADD = $(REVENGE_LIBS) $(LI libabw_@ABW_MAJOR_VERSION@_@ABW_MINOR_VERSION@_la_DEPENDENCIES = @LIBABW_WIN32_RESOURCE@ libabw_@ABW_MAJOR_VERSION@_@ABW_MINOR_VERSION@_la_LDFLAGS = $(version_info) -export-dynamic $(no_undefined) libabw_@ABW_MAJOR_VERSION@_@ABW_MINOR_VERSION@_la_SOURCES = \ + ABWCollector.cpp \ + ABWParser.cpp \ ABWXMLHelper.cpp \ ABWXMLTokenMap.cpp \ AbiDocument.cpp \ \ + ABWCollector.h \ + ABWParser.h \ ABWXMLHelper.h \ ABWXMLTokenMap.h \ libabw_internal.h \ |