annotate.cc 2.56 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * EMULAB-COPYRIGHT
 * Copyright (c) 2008 University of Utah and the Flux Group.
 * All rights reserved.
 */

/*
 * Implements the annotate methods which are independent of the type of file being annotated.
 */

static const char rcsid[] = "$Id: annotate.cc,v 1.2 2009-05-20 18:06:07 tarunp Exp $";

#ifdef WITH_XML

#include "annotate.h"
16 17
#include "xstr.h"
#include <string>
18 19 20 21 22 23 24 25 26 27 28

#include <xercesc/dom/DOM.hpp>
#include <xercesc/dom/DOMImplementation.hpp>
#include <xercesc/dom/DOMWriter.hpp>
#include <xercesc/framework/LocalFileFormatTarget.hpp>

XERCES_CPP_NAMESPACE_USE
using namespace std;

void annotate::write_annotated_file (const char* filename)
{
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 55 56 57
  // Get the current implementation
  DOMImplementation *impl = DOMImplementationRegistry::getDOMImplementation(NULL);
  
  // Construct the DOMWriter
  DOMWriter* writer = ((DOMImplementationLS*)impl)->createDOMWriter();
  
  // Make the output look pretty
  if (writer->canSetFeature(XMLUni::fgDOMWRTFormatPrettyPrint, true))
    writer->setFeature(XMLUni::fgDOMWRTFormatPrettyPrint, true);
  
  // Set the byte-order-mark feature      
  if (writer->canSetFeature(XMLUni::fgDOMWRTBOM, true))
    writer->setFeature(XMLUni::fgDOMWRTBOM, true);

  // Set the XML declaration feature
  if (writer->canSetFeature(XMLUni::fgDOMXMLDeclaration, true))
    writer->setFeature(XMLUni::fgDOMXMLDeclaration, true);

  // Construct the LocalFileFormatTarget
  XMLFormatTarget *outputFile = new xercesc::LocalFileFormatTarget(filename);
  
  // Serialize a DOMNode to the local file "<some-file-name>.xml"
  writer->writeNode(outputFile, *dynamic_cast<DOMNode*>(this->document));
  
  // Flush the buffer to ensure all contents are written
  outputFile->flush();
  
  // Release the memory
  writer->release();
58 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
string annotate::printXML (const DOMElement* input)
{
  DOMElement* tag = (DOMElement*)(input);

  // Get the current implementation
  DOMImplementation *impl = DOMImplementationRegistry::getDOMImplementation(NULL);
  
  // Construct the DOMWriter
  DOMWriter* writer = ((DOMImplementationLS*)impl)->createDOMWriter();
  
  // Make the output look pretty
  if (writer->canSetFeature(XMLUni::fgDOMWRTFormatPrettyPrint, true))
    writer->setFeature(XMLUni::fgDOMWRTFormatPrettyPrint, true);
  
  // Set the byte-order-mark feature      
  if (writer->canSetFeature(XMLUni::fgDOMWRTBOM, true))
    writer->setFeature(XMLUni::fgDOMWRTBOM, true);

  // Serialize a DOMNode to the local file "<some-file-name>.xml"
  string rv = XStr(writer->writeToString(*dynamic_cast<DOMNode*>(tag))).c();
  
  // Release the memory
  writer->release();

  return rv;
}


88
#endif