XdmfCoreReader.cpp 12.7 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 "boost/tokenizer.hpp"
28
#include <map>
29
#include <sstream>
30
#include <cstring>
31 32
#include <utility>
#include "XdmfArray.hpp"
33 34
#include "XdmfArrayType.hpp"
#include "XdmfHDF5Controller.hpp"
35 36
#include "XdmfCoreItemFactory.hpp"
#include "XdmfCoreReader.hpp"
37
#include "XdmfError.hpp"
38
#include "XdmfItem.hpp"
39
#include "XdmfSystemUtils.hpp"
40

41 42 43 44 45 46 47
/**
 * PIMPL
 */
class XdmfCoreReader::XdmfCoreReaderImpl {

public:

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

  ~XdmfCoreReaderImpl()
  {
  };

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

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

97 98 99 100
    mXPathContext = xmlXPtrNewContext(mDocument, NULL, NULL);
    mXPathMap.clear();
  }

101 102 103 104 105 106
  void
  parse(const std::string & lightData) 
  {
    mDocument = xmlParseDoc((const xmlChar*)lightData.c_str());
                               
    if(mDocument == NULL) {
107 108 109 110 111 112 113 114
      try {
        XdmfError::message(XdmfError::FATAL,
                           "xmlReadFile could not parse passed light data string"
                           " in XdmfCoreReader::XdmfCoreReaderImpl::parse");
      }
      catch (XdmfError e) {
        throw e;
      }
115 116
    }

117
    //    mDocuments.insert(std::make_pair((char*)mDocument->URL, mDocument));
118 119 120 121
    mXPathContext = xmlXPtrNewContext(mDocument, NULL, NULL);
    mXPathMap.clear();
  }

122 123 124 125 126
  /**
   * Constructs XdmfItems for all nodes in currNode's tree.
   * XdmfItems are constructed by recursively calling this function for all
   * children of currNode.
   */
127
  std::vector<shared_ptr<XdmfItem> >
128 129
  read(xmlNodePtr currNode)
  {
130
    std::vector<shared_ptr<XdmfItem> > myItems;
131 132 133 134 135

    while(currNode != NULL) {
      if(currNode->type == XML_ELEMENT_NODE) {
        if(xmlStrcmp(currNode->name, (xmlChar*)"include") == 0) {
          // Deal with proper reading of XIncludes
136 137 138 139

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

140 141 142 143 144
          xmlAttrPtr currAttribute = currNode->properties;
          while(currAttribute != NULL) {
            if(xmlStrcmp(currAttribute->name, (xmlChar*)"xpointer") == 0) {
              xpointer = currAttribute->children->content;
            }
145 146 147 148 149 150 151 152
            if(xmlStrcmp(currAttribute->name, (xmlChar*)"href") == 0) {
              href = currAttribute->children->content;
            }
            currAttribute = currAttribute->next;
          }

          xmlXPathContextPtr context = mXPathContext;
          if(href) {
153
	    xmlDocPtr document;
154
            xmlChar * filePath = xmlBuildURI(href, mDocument->URL);
155 156 157 158 159 160 161 162 163 164
	    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;
	    }

165
            context = xmlXPtrNewContext(document, NULL, NULL);           
166
          }
167

168
          if(xpointer) {
169 170 171 172
            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],
173 174 175
                                     myItems);
              }
            }
176 177 178 179 180
            xmlXPathFreeObject(result);
          }

          if(href) {
            xmlXPathFreeContext(context);
181
          }
182

183 184 185
        }
        else {
          // Normal reading
186 187 188 189 190 191
          try {
            this->readSingleNode(currNode, myItems);
          }
          catch (XdmfError e) {
            throw e;
          }
192 193 194 195 196 197 198 199 200 201 202 203 204 205
        }
      }
      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,
206
                 std::vector<shared_ptr<XdmfItem> > & myItems)
207
  {
208
    // Check to see if the node is already in the Xpath
209
    std::map<xmlNodePtr, shared_ptr<XdmfItem> >::const_iterator iter =
210
      mXPathMap.find(currNode);
211
    // If it is grab it from the previously stored items
212
    if(iter != mXPathMap.end()) {
213 214
      myItems.push_back(iter->second);
    }
215
    else {
216
      // Otherwise, generate it from the node
217
      std::map<std::string, std::string> itemProperties;
218 219

      xmlNodePtr childNode = currNode->children;
220 221 222 223 224
      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;
            
225
            // Determine if content is whitespace
226 227 228
            bool whitespace = true;
            
            const char * contentPtr = content;
229
            // Step through to end of pointer
230
            while(contentPtr != NULL) {
231
              // If not a whitespace character, break
232 233 234 235 236 237 238
              if(!isspace(*contentPtr++)) {
                whitespace = false;
                break;
              }
            }
            
            if(!whitespace) {
239 240 241
              itemProperties.insert(std::make_pair("Content", content));
              itemProperties.insert(std::make_pair("XMLDir", mXMLDir));
              break;
242 243
            }
          }
244
          childNode = childNode->next;
245
        }
246
      }
247
    
248 249
      xmlAttrPtr currAttribute = currNode->properties;
      while(currAttribute != NULL) {
250 251
        itemProperties.insert(std::make_pair((char *)currAttribute->name,
                                             (char *)currAttribute->children->content));
252 253
        currAttribute = currAttribute->next;
      }
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
      try {
        const std::vector<shared_ptr<XdmfItem> > childItems =
          this->read(currNode->children);
        shared_ptr<XdmfItem> newItem = 
          mItemFactory->createItem((const char *)currNode->name,
                                   itemProperties,
                                   childItems);
      
        if(newItem == NULL) {
          try {
            XdmfError::message(XdmfError::FATAL, 
                               "mItemFactory failed to createItem in "
                               "XdmfCoreReader::XdmfCoreReaderImpl::readSingleNode");
          }
          catch (XdmfError e) {
            throw e;
          }
        }
272
        if (newItem->getItemTag().compare((const char *)currNode->name) != 0) {
273 274 275
          newItem->populateItem(itemProperties,
                                std::vector<shared_ptr<XdmfItem> >(),
                                mCoreReader);
276 277
        }
        else {
278 279 280
          newItem->populateItem(itemProperties,
          childItems,
          mCoreReader);
281
        }
282 283 284 285 286
        myItems.push_back(newItem);
        mXPathMap.insert(std::make_pair(currNode, newItem));
      }
      catch (XdmfError e) {
        throw e;
287 288 289 290 291 292
      }
    }
  }

  void
  readPathObjects(const std::string & xPath,
293
                  std::vector<shared_ptr<XdmfItem> > & myItems)
294 295 296 297 298
  {
    xmlXPathObjectPtr xPathObject =
      xmlXPathEvalExpression((xmlChar*)xPath.c_str(), mXPathContext);
    if(xPathObject && xPathObject->nodesetval) {
      for(int i=0; i<xPathObject->nodesetval->nodeNr; ++i) {
299 300 301 302 303 304
        try {
          this->readSingleNode(xPathObject->nodesetval->nodeTab[i], myItems);
        }
        catch (XdmfError e) {
          throw e;
        }
305 306 307 308 309 310
      }
    }
    xmlXPathFreeObject(xPathObject);
  }

  xmlDocPtr mDocument;
311
  std::map<std::string, xmlDocPtr> mDocuments;
312
  const XdmfCoreReader * const mCoreReader;
313
  const shared_ptr<const XdmfCoreItemFactory> mItemFactory;
314 315
  std::string mXMLDir;
  xmlXPathContextPtr mXPathContext;
316
  std::map<xmlNodePtr, shared_ptr<XdmfItem> > mXPathMap;
317 318
};

319
XdmfCoreReader::XdmfCoreReader(const shared_ptr<const XdmfCoreItemFactory> itemFactory) :
320
  mImpl(new XdmfCoreReaderImpl(itemFactory, this))
321 322 323 324 325
{
}

XdmfCoreReader::~XdmfCoreReader()
{
326
  delete mImpl;
327 328
}

329 330 331
shared_ptr<XdmfItem >
XdmfCoreReader::parse(const std::string & lightData) const
{
332 333 334 335 336 337
  try {
    mImpl->parse(lightData);
  }
  catch (XdmfError e) {
    throw e;
  }
338
  const xmlNodePtr currNode = xmlDocGetRootElement(mImpl->mDocument);
339
  std::vector<shared_ptr<XdmfItem> > toReturn;
340 341 342 343 344 345 346 347 348
  try {
    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);
    }
349
  }
350 351
  catch (XdmfError e) {
    throw e;
352
  }
353 354 355 356
  mImpl->closeFile();
  return(toReturn[0]);
}

357
std::vector<shared_ptr<XdmfItem> >
358
XdmfCoreReader::readItems(const std::string & filePath) const
359
{
360 361 362 363 364 365 366 367 368 369 370 371
  try {
    mImpl->openFile(filePath);
    const xmlNodePtr currNode = xmlDocGetRootElement(mImpl->mDocument);
    const std::vector<shared_ptr<XdmfItem> > toReturn =
      mImpl->read(currNode->children);
    mImpl->closeFile();
    return toReturn;
  }
  catch (XdmfError e) {
    throw e;
  }

372 373
}

374
shared_ptr<XdmfItem>
375
XdmfCoreReader::read(const std::string & filePath) const
376
{
377 378 379 380 381 382 383 384 385
  try {
    const std::vector<shared_ptr<XdmfItem> > toReturn = readItems(filePath);
    if (toReturn.size() == 0) {
      return(shared_ptr<XdmfItem>());
    }
    return(toReturn[0]);
  }
  catch (XdmfError e) {
    throw e;
386
  }
387
}
388

389
std::vector<shared_ptr<XdmfItem> >
390 391
XdmfCoreReader::read(const std::string & filePath,
                     const std::string & xPath) const
392
{
393 394 395 396 397 398 399 400 401
  try {
    mImpl->openFile(filePath);
    std::vector<shared_ptr<XdmfItem> > toReturn = this->readPathObjects(xPath);
    mImpl->closeFile();
    return toReturn;
  }
  catch (XdmfError e) {
    throw e;
  }
402
}
403

404
std::vector<shared_ptr<XdmfItem> >
405
XdmfCoreReader::readPathObjects(const std::string & xPath) const
406
{
407
  std::vector<shared_ptr<XdmfItem> > toReturn;
408 409 410 411 412 413
  try {
    mImpl->readPathObjects(xPath, toReturn);
  }
  catch (XdmfError e) {
    throw e;
  }
414
  return toReturn;
415 416
}