XdmfCoreReader.cpp 11.1 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/uri.h>
25
#include <libxml/xpointer.h>
26
#include <libxml/xmlreader.h>
27
#include <map>
28
#include <sstream>
29
#include <cstring>
30 31
#include <utility>
#include "XdmfArray.hpp"
32 33
#include "XdmfCoreItemFactory.hpp"
#include "XdmfCoreReader.hpp"
34
#include "XdmfError.hpp"
35
#include "XdmfItem.hpp"
36
#include "XdmfSystemUtils.hpp"
37

38 39 40 41 42 43 44
/**
 * PIMPL
 */
class XdmfCoreReader::XdmfCoreReaderImpl {

public:

45
  XdmfCoreReaderImpl(const shared_ptr<const XdmfCoreItemFactory> itemFactory,
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
                     const XdmfCoreReader * const coreReader) :
    mCoreReader(coreReader),
    mItemFactory(itemFactory)
  {
  };

  ~XdmfCoreReaderImpl()
  {
  };

  void
  closeFile()
  {
    mXPathMap.clear();
    xmlXPathFreeContext(mXPathContext);
61 62 63 64 65 66
    for(std::map<std::string, xmlDocPtr>::const_iterator iter = 
	  mDocuments.begin(); iter != mDocuments.end(); ++iter) {
      xmlFreeDoc(iter->second);
    }
    mDocuments.clear();
    
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
    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) {
82 83 84
      XdmfError::message(XdmfError::FATAL,
                         "xmlReadFile could not read " + filePath +
                         " in XdmfCoreReader::XdmfCoreReaderImpl::openFile");
85 86
    }

87 88
    mDocuments.insert(std::make_pair((char*)mDocument->URL, mDocument));

89 90 91 92
    mXPathContext = xmlXPtrNewContext(mDocument, NULL, NULL);
    mXPathMap.clear();
  }

93 94 95 96 97 98 99 100 101 102 103
  void
  parse(const std::string & lightData) 
  {
    mDocument = xmlParseDoc((const xmlChar*)lightData.c_str());
                               
    if(mDocument == NULL) {
      XdmfError::message(XdmfError::FATAL,
                         "xmlReadFile could not parse passed light data string"
                         " in XdmfCoreReader::XdmfCoreReaderImpl::parse");
    }

104
    //    mDocuments.insert(std::make_pair((char*)mDocument->URL, mDocument));
105 106 107 108
    mXPathContext = xmlXPtrNewContext(mDocument, NULL, NULL);
    mXPathMap.clear();
  }

109 110 111 112 113
  /**
   * Constructs XdmfItems for all nodes in currNode's tree.
   * XdmfItems are constructed by recursively calling this function for all
   * children of currNode.
   */
114
  std::vector<shared_ptr<XdmfItem> >
115 116
  read(xmlNodePtr currNode)
  {
117
    std::vector<shared_ptr<XdmfItem> > myItems;
118 119 120 121 122

    while(currNode != NULL) {
      if(currNode->type == XML_ELEMENT_NODE) {
        if(xmlStrcmp(currNode->name, (xmlChar*)"include") == 0) {
          // Deal with proper reading of XIncludes
123 124 125 126

          xmlChar * xpointer = NULL;
          xmlChar * href = NULL;

127 128 129 130 131
          xmlAttrPtr currAttribute = currNode->properties;
          while(currAttribute != NULL) {
            if(xmlStrcmp(currAttribute->name, (xmlChar*)"xpointer") == 0) {
              xpointer = currAttribute->children->content;
            }
132 133 134 135 136 137 138 139
            if(xmlStrcmp(currAttribute->name, (xmlChar*)"href") == 0) {
              href = currAttribute->children->content;
            }
            currAttribute = currAttribute->next;
          }

          xmlXPathContextPtr context = mXPathContext;
          if(href) {
140
	    xmlDocPtr document;
141
            xmlChar * filePath = xmlBuildURI(href, mDocument->URL);
142 143 144 145 146 147 148 149 150 151
	    std::map<std::string, xmlDocPtr>::const_iterator iter = 
	      mDocuments.find((char*)filePath);
	    if(iter == mDocuments.end()) {
	      document = xmlReadFile((char*)filePath, NULL, 0);
	      mDocuments.insert(std::make_pair((char*)document->URL, document));
	    }
	    else {
	      document = iter->second;
	    }

152
            context = xmlXPtrNewContext(document, NULL, NULL);           
153
          }
154

155
          if(xpointer) {
156 157 158 159
            xmlXPathObjectPtr result = xmlXPtrEval(xpointer, context);
            if(result && !xmlXPathNodeSetIsEmpty(result->nodesetval)) {
              for(int i=0; i<result->nodesetval->nodeNr; ++i) {
                this->readSingleNode(result->nodesetval->nodeTab[i],
160 161 162
                                     myItems);
              }
            }
163 164 165 166 167
            xmlXPathFreeObject(result);
          }

          if(href) {
            xmlXPathFreeContext(context);
168
          }
169

170 171 172 173 174 175 176 177
        }
        else {
          // Normal reading
          this->readSingleNode(currNode, myItems);
        }
      }
      currNode = currNode->next;
    }
178

179 180 181 182 183 184 185 186 187 188
    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,
189
                 std::vector<shared_ptr<XdmfItem> > & myItems)
190
  {
191
    std::map<xmlNodePtr, shared_ptr<XdmfItem> >::const_iterator iter =
192 193 194 195 196 197
      mXPathMap.find(currNode);
    if(iter != mXPathMap.end()) {
      myItems.push_back(iter->second);
    }
    else {
      std::map<std::string, std::string> itemProperties;
198 199

      xmlNodePtr childNode = currNode->children;
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
      if (XdmfArray::ItemTag.compare((char *)currNode->name) == 0) {
        while(childNode != NULL) {
          if(childNode->type == XML_TEXT_NODE && childNode->content) {
            const char * content = (char*)childNode->content;
            
            // determine if content is whitespace
            bool whitespace = true;
            
            const char * contentPtr = content;
            while(contentPtr != NULL) {
              if(!isspace(*contentPtr++)) {
                whitespace = false;
                break;
              }
            }
            
            if(!whitespace) {
              itemProperties.insert(std::make_pair("Content", content));
              itemProperties.insert(std::make_pair("XMLDir", mXMLDir));
219 220 221
              break;
            }
          }
222
          childNode = childNode->next;
223
        }
224
      }
225
     
226 227
      xmlAttrPtr currAttribute = currNode->properties;
      while(currAttribute != NULL) {
228 229
        itemProperties.insert(std::make_pair((char *)currAttribute->name,
                                             (char *)currAttribute->children->content));
230 231 232
        currAttribute = currAttribute->next;
      }

233
      const std::vector<shared_ptr<XdmfItem> > childItems =
234
        this->read(currNode->children);
235
      shared_ptr<XdmfItem> newItem =
236 237 238 239
        mItemFactory->createItem((const char *)currNode->name,
                                 itemProperties,
                                 childItems);
      if(newItem == NULL) {
240 241 242
        XdmfError::message(XdmfError::FATAL, 
                           "mItemFactory failed to createItem in "
                           "XdmfCoreReader::XdmfCoreReaderImpl::readSingleNode");
243 244 245 246
      }

      newItem->populateItem(itemProperties, childItems, mCoreReader);
      myItems.push_back(newItem);
247
      mXPathMap.insert(std::make_pair(currNode, newItem));
248 249 250 251 252
    }
  }

  void
  readPathObjects(const std::string & xPath,
253
                  std::vector<shared_ptr<XdmfItem> > & myItems)
254 255 256 257 258 259 260 261 262 263 264 265
  {
    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;
266
  std::map<std::string, xmlDocPtr> mDocuments;
267
  const XdmfCoreReader * const mCoreReader;
268
  const shared_ptr<const XdmfCoreItemFactory> mItemFactory;
269 270
  std::string mXMLDir;
  xmlXPathContextPtr mXPathContext;
271
  std::map<xmlNodePtr, shared_ptr<XdmfItem> > mXPathMap;
272 273
};

274
XdmfCoreReader::XdmfCoreReader(const shared_ptr<const XdmfCoreItemFactory> itemFactory) :
275
  mImpl(new XdmfCoreReaderImpl(itemFactory, this))
276 277 278 279 280
{
}

XdmfCoreReader::~XdmfCoreReader()
{
281
  delete mImpl;
282 283
}

284 285 286 287 288
shared_ptr<XdmfItem >
XdmfCoreReader::parse(const std::string & lightData) const
{
  mImpl->parse(lightData);
  const xmlNodePtr currNode = xmlDocGetRootElement(mImpl->mDocument);
289 290 291 292 293 294 295 296 297
  std::vector<shared_ptr<XdmfItem> > toReturn;
  if(mImpl->mItemFactory->createItem((const char*)currNode->name,
                                     std::map<std::string, std::string>(),
                                     std::vector<shared_ptr<XdmfItem> >()) == NULL) {
    toReturn = mImpl->read(currNode->children);
  }
  else {
    toReturn = mImpl->read(currNode);
  }
298 299 300 301
  mImpl->closeFile();
  return(toReturn[0]);
}

302
std::vector<shared_ptr<XdmfItem> >
303
XdmfCoreReader::readItems(const std::string & filePath) const
304
{
305 306
  mImpl->openFile(filePath);
  const xmlNodePtr currNode = xmlDocGetRootElement(mImpl->mDocument);
307
  const std::vector<shared_ptr<XdmfItem> > toReturn =
308 309 310
    mImpl->read(currNode->children);
  mImpl->closeFile();
  return toReturn;
311 312
}

313
shared_ptr<XdmfItem>
314
XdmfCoreReader::read(const std::string & filePath) const
315
{
316
  const std::vector<shared_ptr<XdmfItem> > toReturn = readItems(filePath);
317
  if (toReturn.size() == 0) {
318
    return(shared_ptr<XdmfItem>());
319 320
  }
  return(toReturn[0]);
321
}
322

323
std::vector<shared_ptr<XdmfItem> >
324 325
XdmfCoreReader::read(const std::string & filePath,
                     const std::string & xPath) const
326
{
327
  mImpl->openFile(filePath);
328
  std::vector<shared_ptr<XdmfItem> > toReturn = this->readPathObjects(xPath);
329 330
  mImpl->closeFile();
  return toReturn;
331
}
332

333
std::vector<shared_ptr<XdmfItem> >
334
XdmfCoreReader::readPathObjects(const std::string & xPath) const
335
{
336
  std::vector<shared_ptr<XdmfItem> > toReturn;
337 338
  mImpl->readPathObjects(xPath, toReturn);
  return toReturn;
339 340
}