XdmfCoreReader.cpp 8.59 KB
Newer Older
Kenneth Leiter's avatar
Kenneth Leiter committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*****************************************************************************/
/*                                    XDMF                                   */
/*                       eXtensible Data Model and Format                    */
/*                                                                           */
/*  Id : XdmfCoreReader.cpp                                                  */
/*                                                                           */
/*  Author:                                                                  */
/*     Kenneth Leiter                                                        */
/*     kenneth.leiter@arl.army.mil                                           */
/*     US Army Research Laboratory                                           */
/*     Aberdeen Proving Ground, MD                                           */
/*                                                                           */
/*     Copyright @ 2011 US Army Research Laboratory                          */
/*     All Rights Reserved                                                   */
/*     See Copyright.txt for details                                         */
/*                                                                           */
/*     This software is distributed WITHOUT ANY WARRANTY; without            */
/*     even the implied warranty of MERCHANTABILITY or FITNESS               */
/*     FOR A PARTICULAR PURPOSE.  See the above copyright notice             */
/*     for more information.                                                 */
/*                                                                           */
/*****************************************************************************/
23

24
#include <libxml/xpointer.h>
25
#include <libxml/xmlreader.h>
26
#include <map>
27
#include <sstream>
28
#include <cstring>
29 30 31
#include "XdmfCoreItemFactory.hpp"
#include "XdmfCoreReader.hpp"
#include "XdmfItem.hpp"
32
#include "XdmfSystemUtils.hpp"
33
#include "XdmfError.hpp"
34 35 36 37 38 39 40 41

/**
 * PIMPL
 */
class XdmfCoreReader::XdmfCoreReaderImpl {

public:

42
  XdmfCoreReaderImpl(const shared_ptr<const XdmfCoreItemFactory> itemFactory,
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
                     const XdmfCoreReader * const coreReader) :
    mCoreReader(coreReader),
    mItemFactory(itemFactory)
  {
  };

  ~XdmfCoreReaderImpl()
  {
  };

  void
  closeFile()
  {
    mXPathMap.clear();
    xmlXPathFreeContext(mXPathContext);
    xmlFreeDoc(mDocument);
    xmlCleanupParser();
  }

  void
  openFile(const std::string & filePath)
  {
    mXMLDir = XdmfSystemUtils::getRealPath(filePath);
    size_t index = mXMLDir.find_last_of("/\\");
    if(index != std::string::npos) {
      mXMLDir = mXMLDir.substr(0, index + 1);
    }

    mDocument = xmlReadFile(filePath.c_str(), NULL, 0);

    if(mDocument == NULL) {
74 75 76
      XdmfError::message(XdmfError::FATAL,
                         "xmlReadFile could not read " + filePath +
                         " in XdmfCoreReader::XdmfCoreReaderImpl::openFile");
77 78 79 80 81 82 83 84 85 86 87
    }

    mXPathContext = xmlXPtrNewContext(mDocument, NULL, NULL);
    mXPathMap.clear();
  }

  /**
   * Constructs XdmfItems for all nodes in currNode's tree.
   * XdmfItems are constructed by recursively calling this function for all
   * children of currNode.
   */
88
  std::vector<shared_ptr<XdmfItem> >
89 90
  read(xmlNodePtr currNode)
  {
91
    std::vector<shared_ptr<XdmfItem> > myItems;
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 129 130 131 132 133

    while(currNode != NULL) {
      if(currNode->type == XML_ELEMENT_NODE) {
        xmlChar * xpointer = NULL;
        if(xmlStrcmp(currNode->name, (xmlChar*)"include") == 0) {
          // Deal with proper reading of XIncludes
          xmlAttrPtr currAttribute = currNode->properties;
          while(currAttribute != NULL) {
            if(xmlStrcmp(currAttribute->name, (xmlChar*)"xpointer") == 0) {
              xpointer = currAttribute->children->content;
              break;
            }
          }
          if(xpointer) {
            xmlXPathObjectPtr xPathObject =
              xmlXPtrEval(xpointer, mXPathContext);
            if(xPathObject && xPathObject->nodesetval) {
              for(int i=0; i<xPathObject->nodesetval->nodeNr; ++i) {
                this->readSingleNode(xPathObject->nodesetval->nodeTab[i],
                                     myItems);
              }
            }
            xmlXPathFreeObject(xPathObject);
          }
        }
        else {
          // Normal reading
          this->readSingleNode(currNode, myItems);
        }
      }
      currNode = currNode->next;
    }
    return myItems;
  }

  /**
   * Reads a single xmlNode into an XdmfItem object in memory. The constructed
   * XdmfItem is added to myItems and an entry is added mapping the xmlNodePtr
   * to the new XdmfItem in the mXPathMap.
   */
  void
  readSingleNode(const xmlNodePtr currNode,
134
                 std::vector<shared_ptr<XdmfItem> > & myItems)
135
  {
136
    std::map<xmlNodePtr, shared_ptr<XdmfItem> >::const_iterator iter =
137 138 139 140 141 142
      mXPathMap.find(currNode);
    if(iter != mXPathMap.end()) {
      myItems.push_back(iter->second);
    }
    else {
      std::map<std::string, std::string> itemProperties;
143 144 145

      xmlNodePtr childNode = currNode->children;
      while(childNode != NULL) {
146 147 148 149
        if(childNode->type == XML_TEXT_NODE && childNode->content) {
          const char * content = (char*)childNode->content;

          // determine if content is whitespace
150
          const size_t contentSize = std::strlen(content);
151 152 153 154 155 156 157 158 159 160 161 162 163 164
          bool whitespace = true;

          for(int i=0; i<contentSize; ++i) {
            if(!isspace(content[i])) {
              whitespace = false;
              break;
            }
          }

          if(!whitespace) {
            itemProperties["Content"] = content;
            itemProperties["XMLDir"] = mXMLDir;
            break;
          }
165 166
        }
        childNode = childNode->next;
167 168 169 170 171 172 173 174
      }
      xmlAttrPtr currAttribute = currNode->properties;
      while(currAttribute != NULL) {
        itemProperties[(const char *)currAttribute->name] =
          (const char *)currAttribute->children->content;
        currAttribute = currAttribute->next;
      }

175
      std::vector<shared_ptr<XdmfItem> > childItems =
176
        this->read(currNode->children);
177
      shared_ptr<XdmfItem> newItem =
178 179 180 181
        mItemFactory->createItem((const char *)currNode->name,
                                 itemProperties,
                                 childItems);
      if(newItem == NULL) {
182 183 184
        XdmfError::message(XdmfError::FATAL, 
                           "mItemFactory failed to createItem in "
                           "XdmfCoreReader::XdmfCoreReaderImpl::readSingleNode");
185 186 187 188 189 190 191 192 193 194
      }

      newItem->populateItem(itemProperties, childItems, mCoreReader);
      myItems.push_back(newItem);
      mXPathMap[currNode] = newItem;
    }
  }

  void
  readPathObjects(const std::string & xPath,
195
                  std::vector<shared_ptr<XdmfItem> > & myItems)
196 197 198 199 200 201 202 203 204 205 206 207 208
  {
    xmlXPathObjectPtr xPathObject =
      xmlXPathEvalExpression((xmlChar*)xPath.c_str(), mXPathContext);
    if(xPathObject && xPathObject->nodesetval) {
      for(int i=0; i<xPathObject->nodesetval->nodeNr; ++i) {
        this->readSingleNode(xPathObject->nodesetval->nodeTab[i], myItems);
      }
    }
    xmlXPathFreeObject(xPathObject);
  }

  xmlDocPtr mDocument;
  const XdmfCoreReader * const mCoreReader;
209
  const shared_ptr<const XdmfCoreItemFactory> mItemFactory;
210 211
  std::string mXMLDir;
  xmlXPathContextPtr mXPathContext;
212
  std::map<xmlNodePtr, shared_ptr<XdmfItem> > mXPathMap;
213 214
};

215
XdmfCoreReader::XdmfCoreReader(const shared_ptr<const XdmfCoreItemFactory> itemFactory) :
216
  mImpl(new XdmfCoreReaderImpl(itemFactory, this))
217 218 219 220 221
{
}

XdmfCoreReader::~XdmfCoreReader()
{
222
  delete mImpl;
223 224
}

225
std::vector<shared_ptr<XdmfItem> >
226
XdmfCoreReader::readItems(const std::string & filePath) const
227
{
228 229
  mImpl->openFile(filePath);
  const xmlNodePtr currNode = xmlDocGetRootElement(mImpl->mDocument);
230
  const std::vector<shared_ptr<XdmfItem> > toReturn =
231 232 233
    mImpl->read(currNode->children);
  mImpl->closeFile();
  return toReturn;
234 235
}

236
shared_ptr<XdmfItem>
237
XdmfCoreReader::read(const std::string & filePath) const
238
{
239
  const std::vector<shared_ptr<XdmfItem> > toReturn = readItems(filePath);
240
  if (toReturn.size() == 0) {
241
    return(shared_ptr<XdmfItem>());
242 243
  }
  return(toReturn[0]);
244
}
245

246
std::vector<shared_ptr<XdmfItem> >
247 248
XdmfCoreReader::read(const std::string & filePath,
                     const std::string & xPath) const
249
{
250
  mImpl->openFile(filePath);
251
  std::vector<shared_ptr<XdmfItem> > toReturn = this->readPathObjects(xPath);
252 253
  mImpl->closeFile();
  return toReturn;
254
}
255

256
std::vector<shared_ptr<XdmfItem> >
257
XdmfCoreReader::readPathObjects(const std::string & xPath) const
258
{
259
  std::vector<shared_ptr<XdmfItem> > toReturn;
260 261
  mImpl->readPathObjects(xPath, toReturn);
  return toReturn;
262 263
}