XdmfReader.cpp 3.21 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 "XdmfGridCollection.hpp"
10
#include "XdmfSet.hpp"
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
#include "XdmfTopology.hpp"

#include <libxml/xmlreader.h>
#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)
		{
52 53 54 55 56 57 58 59 60
			std::map<std::string, std::string>::const_iterator gridType = itemProperties.find("GridType");
			if(gridType->second.compare("Collection") == 0)
			{
				newItem = XdmfGridCollection::New();
			}
			else
			{
				newItem = XdmfGrid::New();
			}
61
		}
62 63 64 65
		else if(itemTag.compare(XdmfSet::ItemTag) == 0)
		{
			newItem = XdmfSet::New();
		}
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
		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;
87 88 89 90 91 92 93 94 95
				std::cout << currNode->name << std::endl;
				if(currNode->children != NULL)
				{
					itemProperties["Content"] = (const char *)currNode->children->content;
				}
				else
				{
					itemProperties["Content"] = "";
				}
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
				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;
}

123
boost::shared_ptr<XdmfItem> XdmfReader::read(const std::string & filePath) const
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
{
	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();

140
	return toReturn[0];
141 142 143
}