Exemplo n.º 1
0
int main(int argc, char** argv)
{
	// build a DOM document and write it to standard output.

	AutoPtr<Document> pDoc = new Document;
	
	AutoPtr<Element> pRoot = pDoc->createElement("root");
	pDoc->appendChild(pRoot);

	AutoPtr<Element> pChild1 = pDoc->createElement("child1");
	AutoPtr<Text> pText1 = pDoc->createTextNode("text1");
	pChild1->appendChild(pText1);
	pRoot->appendChild(pChild1);

	AutoPtr<Element> pChild2 = pDoc->createElement("child2");
	AutoPtr<Text> pText2 = pDoc->createTextNode("text2");
	pChild2->appendChild(pText2);
	pRoot->appendChild(pChild2);
	
	DOMWriter writer;
	writer.setNewLine("\n");
	writer.setOptions(XMLWriter::PRETTY_PRINT);
	writer.writeNode(std::cout, pDoc);
	
	return 0;
}
Exemplo n.º 2
0
/**
 * Save grouping to the XML file specified.
 *
 * @param        g :: Struct with grouping information
 * @param filename :: XML filename where information will be saved
 */
void saveGroupingToXML(const Grouping& g, const std::string& filename)
{
  std::ofstream outFile(filename.c_str());
  if (!outFile)
    throw Mantid::Kernel::Exception::FileError("Unable to open output file", filename);

  DOMWriter writer;
  writer.setNewLine("\n");
  writer.setOptions(XMLWriter::PRETTY_PRINT);

  Poco::AutoPtr<Poco::XML::Document> mDoc = new Document();

  // Create root element with a description
  Poco::AutoPtr<Element> rootElem = mDoc->createElement("detector-grouping");
  rootElem->setAttribute("description", g.description);
  mDoc->appendChild(rootElem);

  // Create group elements
  for (size_t gi = 0; gi < g.groups.size(); gi++)
  {
    Poco::AutoPtr<Element> gElem = mDoc->createElement("group");
    gElem->setAttribute("name", g.groupNames[gi]);
    rootElem->appendChild(gElem);

    Poco::AutoPtr<Element> idsElem = mDoc->createElement("ids");
    idsElem->setAttribute("val", g.groups[gi]);
    gElem->appendChild(idsElem);
  }

  // Create pair elements
  for (size_t pi = 0; pi < g.pairs.size(); pi++)
  {
    Poco::AutoPtr<Element> gElem = mDoc->createElement("pair");
    gElem->setAttribute("name", g.pairNames[pi]);
    rootElem->appendChild(gElem);

    Poco::AutoPtr<Element> fwElem = mDoc->createElement("forward-group");
    fwElem->setAttribute("val", g.groupNames[g.pairs[pi].first]);
    gElem->appendChild(fwElem);

    Poco::AutoPtr<Element> bwElem = mDoc->createElement("backward-group");
    bwElem->setAttribute("val", g.groupNames[g.pairs[pi].second]);
    gElem->appendChild(bwElem);

    Poco::AutoPtr<Element> alphaElem = mDoc->createElement("alpha");
    alphaElem->setAttribute("val", boost::lexical_cast<std::string>(g.pairAlphas[pi]));
    gElem->appendChild(alphaElem);
  } 

  // Create default group/pair name element
  Poco::AutoPtr<Element> gElem = mDoc->createElement("default");
  gElem->setAttribute("name", g.defaultName);
  rootElem->appendChild(gElem);

  writer.writeNode(outFile, mDoc);
}
/** Put the parameters into one workspace
  * @param column :: [input] column of the output table workspace
  * @param ws :: [input/output] the group workspace parameters are to be put in
  * @param nProf :: the PROF Number, which is used to determine fitting function
 * for the parameters.
  * @param parameterXMLString :: string where the XML document filename is
 * stored
  */
void LoadFullprofResolution::putParametersIntoWorkspace(
    API::Column_const_sptr column, API::MatrixWorkspace_sptr ws, int nProf,
    std::string &parameterXMLString) {

  // Get instrument name from matrix workspace
  std::string instrumentName = ws->getInstrument()->getName();

  // Convert table workspace column into DOM XML document
  //   Set up writer to Paremeter file
  DOMWriter writer;
  writer.setNewLine("\n");
  writer.setOptions(XMLWriter::PRETTY_PRINT);

  //   Get current time
  Kernel::DateAndTime date = Kernel::DateAndTime::getCurrentTime();
  std::string ISOdate = date.toISO8601String();
  std::string ISOdateShort =
      ISOdate.substr(0, 19); // Remove fraction of seconds

  //   Create document
  AutoPtr<Document> mDoc = new Document();
  AutoPtr<Element> rootElem = mDoc->createElement("parameter-file");
  rootElem->setAttribute("date", ISOdateShort);
  mDoc->appendChild(rootElem);

  //   Add instrument
  AutoPtr<Element> instrumentElem = mDoc->createElement("component-link");
  instrumentElem->setAttribute("name", instrumentName);
  rootElem->appendChild(instrumentElem);
  if (nProf == 9) // put parameters into BackToBackExponential function
  {
    addBBX_S_Parameters(column, mDoc, instrumentElem);
    addBBX_A_Parameters(column, mDoc, instrumentElem);
    addBBX_B_Parameters(column, mDoc, instrumentElem);
  } else // Assume IkedaCarpenter PV
  {
    addALFBEParameter(column, mDoc, instrumentElem, "Alph0");
    addALFBEParameter(column, mDoc, instrumentElem, "Beta0");
    addALFBEParameter(column, mDoc, instrumentElem, "Alph1");
    addALFBEParameter(column, mDoc, instrumentElem, "Beta1");
    addSigmaParameters(column, mDoc, instrumentElem);
    addGammaParameters(column, mDoc, instrumentElem);
  }

  // Convert DOM XML document into string
  std::ostringstream outFile;
  writer.writeNode(outFile, mDoc);
  parameterXMLString = outFile.str();

  // Useful code for testing upgrades commented out for production use
  // std::ofstream outfileDebug("C:/Temp/test4_fullprof.xml");
  // outfileDebug << parameterXMLString;
  // outfileDebug.close();
}
Exemplo n.º 4
0
 /**
  * Write the XML to the file
  */	  
 void vtkGeometryCacheWriter::write()
 {
   DOMWriter writer;
   writer.setNewLine("\n");
   writer.setOptions(XMLWriter::PRETTY_PRINT);
   std::ofstream file;
   try
   {
     file.open(mFileName.c_str(),std::ios::trunc);
     writer.writeNode(file, mDoc);
     file.close();
   }
   catch(...)
   {
     PLog.error("Geometry Cache file writing exception");
   }
 }
Exemplo n.º 5
0
void SaveDetectorsGrouping::printToXML(std::map<int, std::vector<detid_t> > groupdetidrangemap, std::string xmlfilename) {

    // 1. Get Instrument information
    Geometry::Instrument_const_sptr instrument = mGroupWS->getInstrument();
    std::string name = instrument->getName();
    g_log.debug() << "Instrument " << name << std::endl;

    // 2. Start document (XML)
    AutoPtr<Document> pDoc = new Document;
    AutoPtr<Element> pRoot = pDoc->createElement("detector-grouping");
    pDoc->appendChild(pRoot);
    pRoot->setAttribute("instrument", name);

    // Set description if was specified by user
    if(mGroupWS->run().hasProperty("Description"))
    {
        std::string description = mGroupWS->run().getProperty("Description")->value();
        pRoot->setAttribute("description", description);
    }

    // 3. Append Groups
    for (std::map<int, std::vector<detid_t> >::iterator it = groupdetidrangemap.begin();
            it != groupdetidrangemap.end(); ++it) {

        // a) Group Node
        int groupid = it->first;
        std::stringstream sid;
        sid << groupid;

        AutoPtr<Element> pChildGroup = pDoc->createElement("group");
        pChildGroup->setAttribute("ID",  sid.str());
        // Set name if was specified by user
        std::string groupNameProp = "GroupName_" + sid.str();
        if(mGroupWS->run().hasProperty(groupNameProp))
        {
            std::string groupName = mGroupWS->run().getProperty(groupNameProp)->value();
            pChildGroup->setAttribute("name", groupName);
        }

        pRoot->appendChild(pChildGroup);

        g_log.debug() << "Group ID = " << groupid << std::endl;

        // b) Detector ID Child Nodes
        std::stringstream ss;

        for (size_t i = 0; i < it->second.size()/2; i ++)
        {
            // i. Generate text value

            bool writedata = true;
            detid_t ist = it->second[i*2];
            detid_t ied = it->second[i*2+1];
            // "a-b" or "a"
            if (ist < ied) {
                ss << ist << "-" << ied;
            } else if (ist == ied) {
                ss << ist;
            } else {
                writedata = false;
                g_log.error() << "Impossible to have this situation!" << std::endl;
                throw std::invalid_argument("Impossible to have this sitaution!");
            }
            // add ","
            if (writedata && i < it->second.size()/2-1) {
                ss << ",";
            }

            g_log.debug() << "Detectors:  " << it->second[i*2] << ", " << it->second[i*2+1] << std::endl;
        } // FOREACH Detectors Range Set


        std::string textvalue = ss.str();

        g_log.debug() << "Detector IDs Node: " << textvalue << std::endl;

        // c) Create element
        AutoPtr<Element> pDetid = pDoc->createElement("detids");
        AutoPtr<Text> pText1 = pDoc->createTextNode(textvalue);
        pDetid->appendChild(pText1);
        pChildGroup->appendChild(pDetid);

    } // FOREACH GroupID

    // 4. Write file
    DOMWriter writer;
    writer.setNewLine("\n");
    writer.setOptions(XMLWriter::PRETTY_PRINT);

    std::ofstream ofs;
    ofs.open(xmlfilename.c_str(), std::fstream::out);

    ofs << "<?xml version=\"1.0\"?>\n";

    writer.writeNode(std::cout, pDoc);
    writer.writeNode(ofs, pDoc);
    ofs.close();

}
Exemplo n.º 6
0
/**
 * save XML grouping file
 */
void saveGroupingTabletoXML(Ui::MuonAnalysis& m_uiForm, const std::string& filename)
{
  std::ofstream outFile(filename.c_str());
  if (!outFile)
  {
    throw Mantid::Kernel::Exception::FileError("Unable to open file:", filename);
  }

  DOMWriter writer;
  writer.setNewLine("\n");
  writer.setOptions(XMLWriter::PRETTY_PRINT);

  Poco::XML::Document* mDoc = new Document();
  Element* rootElem = mDoc->createElement("detector-grouping");
  rootElem->setAttribute("description", m_uiForm.groupDescription->text().toStdString());
  mDoc->appendChild(rootElem);

  // loop over groups in table

  std::vector<int> groupToRow;
  whichGroupToWhichRow(m_uiForm, groupToRow);

  int num = static_cast<int>(groupToRow.size());
  for (int i = 0; i < num; i++)
  {
    Element* gElem = mDoc->createElement("group");
    gElem->setAttribute("name", m_uiForm.groupTable->item(groupToRow[i],0)->text().toStdString());
    rootElem->appendChild(gElem);
    Element* idsElem = mDoc->createElement("ids");
    idsElem->setAttribute("val", m_uiForm.groupTable->item(groupToRow[i],1)->text().toStdString());
    gElem->appendChild(idsElem);
  }

  // loop over pairs in pair table

  num = m_uiForm.pairTable->rowCount();
  for (int i = 0; i < num; i++)
  {
    QTableWidgetItem *itemName = m_uiForm.pairTable->item(i,0);
    if (!itemName)
      break;
    if ( itemName->text().isEmpty() )
      break;
    QTableWidgetItem *itemAlpha = m_uiForm.pairTable->item(i,3);
    if (!itemAlpha)
      break;
    if ( itemAlpha->text().isEmpty() )
      break;

    QComboBox* qw1 = static_cast<QComboBox*>(m_uiForm.pairTable->cellWidget(i,1));
    QComboBox* qw2 = static_cast<QComboBox*>(m_uiForm.pairTable->cellWidget(i,2));

    Element* gElem = mDoc->createElement("pair");
    gElem->setAttribute("name", itemName->text().toStdString());
    rootElem->appendChild(gElem);
    Element* fwElem = mDoc->createElement("forward-group");
    fwElem->setAttribute("val", qw1->currentText().toStdString());
    gElem->appendChild(fwElem);
    Element* bwElem = mDoc->createElement("backward-group");
    bwElem->setAttribute("val", qw2->currentText().toStdString());
    gElem->appendChild(bwElem);
    Element* alphaElem = mDoc->createElement("alpha");
    alphaElem->setAttribute("val", itemAlpha->text().toStdString());
    gElem->appendChild(alphaElem);
  } 

  // save default

  Element* gElem = mDoc->createElement("default");
  gElem->setAttribute("name", m_uiForm.frontGroupGroupPairComboBox->currentText().toStdString());
  rootElem->appendChild(gElem);

  writer.writeNode(outFile, mDoc);
}