Commits

Christian Fischer committed 63f3b05

added tinyxml2 backend for the XML parser API

  • Participants
  • Parent commits a96cecc

Comments (0)

Files changed (6)

build/cmake/modules/wiesel-FindTinyXML2.cmake

+# -----------------------------------------------------------------------------
+# include the TinyXML2 library into a project and offers the ability
+# to download and compile the library
+#
+# Defines
+# TINYXML2_FOUND		- true when tinyml was found
+# TINYXML2_DEPENDENCY	- a dependency to add via add_dependencies, if defined
+# TINYXML2_INCLUDE_DIR	- include directory for the compiler target
+# TINYXML2_LIBRARY		- library to add to the compiler target
+
+
+# When enabled, the TinyXML2 library will be downloaded and compiled
+# otherwise, we try to find libTinyXML2 in the system 
+option(
+		WIESEL_BUILD_TINYXML
+		"Download and compile the TinyXML2 library from source instead of searching in the system for it."
+		OFF
+)
+
+
+if (WIESEL_BUILD_TINYXML2)
+	include(ExternalProject)
+	find_package(Git)
+
+	#set(WIESEL_BUILD_TINYXML2_DOWNLOAD_URL		"")
+	#set(WIESEL_BUILD_TINYXML2_DOWNLOAD_MD5		"")
+	set(WIESEL_BUILD_TINYXML2_GIT_CHECKOUT_URL	"https://github.com/leethomason/tinyxml2.git")
+	set(WIESEL_BUILD_TINYXML2_GIT_CHECKOUT_TAG	"HEAD")
+
+	set(
+		WIESEL_BUILD_TINYXML2_CMAKE_ARGS
+	)
+
+	# forward build type
+	if (DEFINED CMAKE_BUILD_TYPE)
+		set(
+			WIESEL_BUILD_TINYXML2_CMAKE_ARGS
+			${WIESEL_BUILD_TINYXML2_CMAKE_ARGS}
+			-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
+		)
+	endif(DEFINED CMAKE_BUILD_TYPE)
+
+	if (GIT_FOUND)
+		ExternalProject_Add(
+				libTinyXML2
+				PREFIX				libTinyXML2
+				GIT_REPOSITORY		${WIESEL_BUILD_TINYXML2_GIT_CHECKOUT_URL}
+				GIT_TAG				${WIESEL_BUILD_TINYXML2_GIT_CHECKOUT_TAG}
+
+				# cmake config
+				CMAKE_GENERATOR		"${CMAKE_GENERATOR}"
+				CMAKE_ARGS			${WIESEL_BUILD_TINYXML2_CMAKE_ARGS}
+
+				# skip installation
+				INSTALL_COMMAND 	""
+		)
+
+		# get build directories
+		ExternalProject_Get_Property(libTinyXML2 SOURCE_DIR)
+		ExternalProject_Get_Property(libTinyXML2 BINARY_DIR)
+
+		set(TINYXML2_LIBRARY_NAME	${CMAKE_SHARED_LIBRARY_PREFIX}tinyxml2${CMAKE_SHARED_LIBRARY_SUFFIX})
+
+		# set TinyXML2 include & library varialbles for usage in the application script
+		set(TINYXML2_FOUND			TRUE													)
+		set(TINYXML2_DEPENDENCY		libTinyXML2												)
+		set(TINYXML2_INCLUDE_DIR	${SOURCE_DIR}											)
+		set(TINYXML2_LIBRARIES		${CMAKE_CURRENT_BINARY_DIR}/${TINYXML2_LIBRARY_NAME}	)
+
+		# copy the created library into our build directory
+		add_custom_command(
+					TARGET			libTinyXML2		POST_BUILD
+					COMMAND			${CMAKE_COMMAND} -E copy_if_different ${BINARY_DIR}/${TINYXML2_LIBRARY_NAME} ${CMAKE_CURRENT_BINARY_DIR}/${TINYXML2_LIBRARY_NAME}
+					MAIN_DEPENDENCY	${BINARY_DIR}/${TINYXML2_LIBRARY_NAME}
+		)
+
+	else(GIT_FOUND)
+		message(
+				FATAL_ERROR 
+				"Missing git executable, which is required for downloading the TinyXML2 source."
+		)
+	endif(GIT_FOUND)
+
+else(WIESEL_BUILD_TINYXML2)
+	set(TINYXML2_FOUND			FALSE)
+endif(WIESEL_BUILD_TINYXML2)

libraries/desktop/wiesel-common/module.cmake

 wiesel_module_add_dependency(wiesel-common wiesel-core)
 
 
+# check if libpng is available
+include(FindPNG)
+
+if(PNG_FOUND)
+	wiesel_target_add_includes(wiesel-common ${PNG_INCLUDE_DIRS})
+	wiesel_target_add_libraries(wiesel-common ${PNG_LIBRARIES})
+	wiesel_target_add_compileflags(wiesel-common ${PNG_DEFINITIONS})
+
+	set(WIESEL_SUPPORTS_LIBPNG		TRUE)
+else()
+	set(WIESEL_SUPPORTS_LIBPNG		FALSE)
+endif()
+
+
 # add libxml2 dependency
 include(FindLibXml2)
 
 endif(LIBXML2_FOUND)
 
 
-# check if libpng is available
-include(FindPNG)
+# add tinyxml2 library
+include(wiesel-FindTinyXML2)
 
-if(PNG_FOUND)
-	wiesel_target_add_includes(wiesel-common ${PNG_INCLUDE_DIRS})
-	wiesel_target_add_libraries(wiesel-common ${PNG_LIBRARIES})
-	wiesel_target_add_compileflags(wiesel-common ${PNG_DEFINITIONS})
+if (TINYXML2_FOUND)
+	if (DEFINED TINYXML2_DEPENDENCY)
+		add_dependencies(wiesel-common ${TINYXML2_DEPENDENCY})
+	endif(DEFINED TINYXML2_DEPENDENCY)
 
-	set(WIESEL_SUPPORTS_LIBPNG		TRUE)
-else()
-	set(WIESEL_SUPPORTS_LIBPNG		FALSE)
-endif()
+	wiesel_target_add_includes(wiesel-common ${TINYXML2_INCLUDE_DIR})
+	wiesel_target_add_libraries(wiesel-common ${TINYXML2_LIBRARIES})
+	wiesel_target_add_compileflags(wiesel-common ${TINYXML2_DEFINITIONS})
+
+	set(WIESEL_SUPPORTS_LIBTINYXML2		TRUE)
+else(TINYXML2_FOUND)
+	set(WIESEL_SUPPORTS_LIBTINYXML2		FALSE)
+endif(TINYXML2_FOUND)
+
 
 
 # finally, create the config file

src/common/wiesel/util/tinyxml2_parser.cpp

+/**
+ * Copyright (C) 2012
+ * Christian Fischer
+ *
+ * https://bitbucket.org/baldur/wiesel/
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301 USA
+ */
+#include "tinyxml2_parser.h"
+
+#if WIESEL_SUPPORTS_LIBTINYXML2
+
+#include <wiesel/io/databuffer.h>
+#include <wiesel/io/datasource.h>
+#include <wiesel/io/file.h>
+#include <wiesel/util/log.h>
+
+#include <tinyxml2.h>
+
+using namespace wiesel;
+
+
+
+
+class LibTinyXml2ParserVisitor : public tinyxml2::XMLVisitor
+{
+private:
+	LibTinyXml2ParserVisitor() {
+		return;
+	}
+
+public:
+	LibTinyXml2ParserVisitor(XmlParserCallback *callback) {
+		this->parser = new XmlParser(callback);
+		this->parser->retain();
+	}
+
+	virtual ~LibTinyXml2ParserVisitor() {
+		safe_release(parser);
+	}
+
+public:
+	virtual bool VisitEnter(const tinyxml2::XMLDocument& doc) {
+		parser->start();
+		return true;
+	}
+
+	virtual bool VisitExit(const tinyxml2::XMLDocument& doc) {
+		parser->finish();
+		return true;
+	}
+
+	virtual bool VisitEnter(const tinyxml2::XMLElement& element, const tinyxml2::XMLAttribute* firstAttribute) {
+		const tinyxml2::XMLAttribute *current_attribute = firstAttribute;
+		XmlParser::Attributes attributes;
+
+		while(current_attribute) {
+			attributes[current_attribute->Name()] = current_attribute->Value();
+			current_attribute = current_attribute->Next();
+		}
+
+		parser->startElement(element.Name(), attributes);
+
+		return true;
+	}
+
+	virtual bool VisitExit(const tinyxml2::XMLElement& element) {
+		parser->closeElement(element.Name());
+		return true;
+	}
+
+	virtual bool Visit(const tinyxml2::XMLText& text) {
+		parser->addTextContent(text.Value());
+		return true;
+	}
+
+private:
+	XmlParser* parser;
+};
+
+
+
+
+
+LibTinyXml2Parser::LibTinyXml2Parser() {
+	return;
+}
+
+
+LibTinyXml2Parser::~LibTinyXml2Parser() {
+	return;
+}
+
+
+bool LibTinyXml2Parser::parse(DataSource *source, XmlParserCallback *callback) {
+	tinyxml2::XMLDocument xml_doc;
+	int result = tinyxml2::XML_ERROR_FILE_NOT_FOUND;
+
+	FileDataSource *fds = dynamic_cast<FileDataSource*>(source);
+	if (fds) {
+		// load the file with tinyxml
+		result = xml_doc.LoadFile(fds->getFile()->getNativePath().c_str());
+	}
+	
+	// when file loading failed or not possible, try loading from the buffer
+	if (result != tinyxml2::XML_SUCCESS) {
+		DataBuffer *buffer = source->getDataBuffer();
+
+		if (buffer) {
+			// we need to copy the XML data into a std::string,
+			// because the buffer is not null-terminated
+			std::string xml_content(buffer->getDataAsCharPtr(), buffer->getSize());
+
+			result = xml_doc.Parse(xml_content.c_str());
+		}
+	}
+
+	// when the source was parsed successfully, start parsing the result with our own parser
+	if (result == tinyxml2::XML_SUCCESS) {
+		LibTinyXml2ParserVisitor visitor(callback);
+		xml_doc.Accept(&visitor);
+
+		return true;
+	}
+
+	return false;
+}
+
+#endif /* WIESEL_SUPPORTS_LIBTINYXML2 */

src/common/wiesel/util/tinyxml2_parser.h

+/**
+ * Copyright (C) 2012
+ * Christian Fischer
+ *
+ * https://bitbucket.org/baldur/wiesel/
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301 USA
+ */
+#ifndef __WIESEL_UTIL_LIBTINYXML2_PARSER_H__
+#define __WIESEL_UTIL_LIBTINYXML2_PARSER_H__
+
+#include <wiesel/wiesel-common.def>
+#include <wiesel/wiesel-common-config.h>
+
+#include <wiesel/util/xml_parser.h>
+
+
+#if WIESEL_SUPPORTS_LIBTINYXML2
+
+namespace wiesel {
+
+	// predeclarations
+
+	class DataBuffer;
+	class DataSource;
+	class File;
+
+
+	class WIESEL_COMMON_EXPORT LibTinyXml2Parser : public IXmlParser
+	{
+	public:
+		LibTinyXml2Parser();
+		virtual ~LibTinyXml2Parser();
+
+		virtual bool parse(DataSource *source, XmlParserCallback *callback);
+	};
+
+} /* namespace wiesel */
+#endif /* WIESEL_SUPPORTS_LIBTINYXML2 */
+#endif /* __WIESEL_UTIL_LIBTINYXML2_PARSER_H__ */

src/common/wiesel/util/tinyxml2_parser.loadable_module

+
+#include <wiesel/module_registry.h>
+#include "tinyxml2_parser.h"
+
+#if WIESEL_SUPPORTS_LIBTINYXML2
+
+	// add the module to the module registry
+	namespace wiesel {
+		REGISTER_MODULE(
+				IXmlParser,
+				LibTinyXml2Parser,
+				&generic_create<LibTinyXml2Parser>,
+				"libTinyXML2",
+				0x01000000u,
+				IModuleLoader::PriorityNormal
+		)
+	}
+
+#endif // WIESEL_SUPPORTS_LIBTINYXML2

src/common/wiesel/wiesel-common-config.in

 // enables support for libxml2
 #cmakedefine01 WIESEL_SUPPORTS_LIBXML2
 
+// enables support for libtinyxml2
+#cmakedefine01 WIESEL_SUPPORTS_LIBTINYXML2
+
 
 #endif // __WIESEL_COMMON_CONFIG_H__