XdmfReader.cpp 2.87 KB
Newer Older
1 2 3 4 5 6 7 8
// Kenneth Leiter
// Xdmf Smart Pointer Test

#include "XdmfArray.hpp"
#include "XdmfAttribute.hpp"
#include "XdmfDomain.hpp"
#include "XdmfGeometry.hpp"
#include "XdmfGrid.hpp"
9
#include "XdmfSet.hpp"
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
#include "XdmfTopology.hpp"


#include <libxml/xmlreader.h>
#include <map>
#include "XdmfReader.hpp"

/**
 * PIMPL
 */
class XdmfReader::XdmfReaderImpl {

public:

	XdmfReaderImpl()
	{
	};

	~XdmfReaderImpl()
	{
	};

	boost::shared_ptr<XdmfItem> constructItem(const std::string & itemTag, const std::map<std::string, std::string> & itemProperties, std::vector<boost::shared_ptr<XdmfItem> > & childItems) const
	{
		boost::shared_ptr<XdmfItem> newItem;
		if(itemTag.compare(XdmfArray::ItemTag) == 0)
		{
			newItem = XdmfArray::New();
		}
		else if(itemTag.compare(XdmfAttribute::ItemTag) == 0)
		{
			newItem = XdmfAttribute::New();
		}
		else if(itemTag.compare(XdmfDomain::ItemTag) == 0)
		{
			newItem = XdmfDomain::New();
		}
		else if(itemTag.compare(XdmfGeometry::ItemTag) == 0)
		{
			newItem = XdmfGeometry::New();
		}
		else if(itemTag.compare(XdmfGrid::ItemTag) == 0)
		{
			newItem = XdmfGrid::New();
		}
55 56 57 58
		else if(itemTag.compare(XdmfSet::ItemTag) == 0)
		{
			newItem = XdmfSet::New();
		}
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
		else if(itemTag.compare(XdmfTopology::ItemTag) == 0)
		{
			newItem = XdmfTopology::New();
		}
		else
		{
			assert(false);
		}
		newItem->populateItem(itemProperties, childItems);
		return newItem;
	}

	std::vector<boost::shared_ptr<XdmfItem> > read(xmlNodePtr currNode) const
	{
		std::vector<boost::shared_ptr<XdmfItem> > myItems;

		while(currNode != NULL)
		{
			if(currNode->type == XML_ELEMENT_NODE)
			{
				std::map<std::string, std::string> itemProperties;
				itemProperties["Content"] = (const char *)currNode->children->content;
				xmlAttrPtr currAttribute = currNode->properties;
				while(currAttribute != NULL)
				{
					itemProperties[(const char *)currAttribute->name] = (const char *)currAttribute->children->content;
					currAttribute = currAttribute->next;
				}
				std::vector<boost::shared_ptr<XdmfItem> > childItems = this->read(currNode->children);
				myItems.push_back(this->constructItem((const char *)currNode->name, itemProperties, childItems));
			}
			currNode = currNode->next;
		}
		return myItems;
	}
};

XdmfReader::XdmfReader() :
	mImpl(new XdmfReaderImpl())
{
	std::cout << "Created XdmfReader " << this << std::endl;
}

XdmfReader::~XdmfReader()
{
	delete mImpl;
	std::cout << "Deleted XdmfReader " << this << std::endl;
}

boost::shared_ptr<XdmfDomain> XdmfReader::read(const std::string & filePath) const
{
	xmlDocPtr document;
	xmlNodePtr currNode;

	document = xmlReadFile(filePath.c_str(), NULL, 0);
	if(document == NULL)
	{
		assert(false);
	}
	currNode = xmlDocGetRootElement(document);

	std::vector<boost::shared_ptr<XdmfItem> > toReturn = mImpl->read(currNode->children);

	xmlFreeDoc(document);
	xmlCleanupParser();

	return boost::shared_dynamic_cast<XdmfDomain>(toReturn[0]);
}