Exemple #1
0
void SINQHMListener::loadDimensions() {
  std::istream &istr = httpRequest("/sinqhm.xml");
  std::stringstream oss;
  Poco::StreamCopier::copyStream(istr, oss);

  DOMParser xmlParser;
  Poco::AutoPtr<Document> doc;
  try {
    doc = xmlParser.parseString(oss.str());
  } catch (...) {
    throw std::runtime_error("Unable to parse sinqhm.xml");
  }
  Element *root = doc->documentElement();
  Poco::AutoPtr<NodeList> bankList = root->getElementsByTagName("bank");
  /**
   * TODO: There may be multiple banks but I only
   *       look at the first
   */
  Element *bank = dynamic_cast<Element *>(bankList->item(0));
  std::string rankt = bank->getAttribute("rank");
  rank = std::stoi(rankt);

  Poco::AutoPtr<NodeList> axisList = bank->getElementsByTagName("axis");
  for (unsigned int i = 0; i < axisList->length(); i++) {
    Element *axis = dynamic_cast<Element *>(axisList->item(i));
    std::string sdim = axis->getAttribute("length");
    dim[i] = std::stoi(sdim);
  }

  doSpecialDim();
}
    ImplicitFunctionParser* ImplicitFunctionParserFactoryImpl::createImplicitFunctionParserFromXML(const std::string& functionXML) const
    {
      using namespace Poco::XML;
      DOMParser pParser;
      AutoPtr<Document> pDoc = pParser.parseString(functionXML);
      Element* pRootElem = pDoc->documentElement();

      return createImplicitFunctionParserFromXML(pRootElem);
    }
void LoadParameterFile::execManually(bool useString, std::string filename, std::string parameterXML,  Mantid::API::ExperimentInfo_sptr localWorkspace)
{
  // TODO: Refactor to remove the need for the const cast (ticket #8521)
  Instrument_sptr instrument = boost::const_pointer_cast<Instrument>(localWorkspace->getInstrument()->baseInstrument());

  // Set up the DOM parser and parse xml file
  DOMParser pParser;
  AutoPtr<Document> pDoc;

  if(useString){
    try
    {
      pDoc = pParser.parseString(parameterXML);
    }
    catch(Poco::Exception& exc)
    {
      throw Kernel::Exception::FileError (exc.displayText() + ". Unable to parse parameter XML string","ParameterXML");
    }
    catch(...)
    {
      throw Kernel::Exception::FileError("Unable to parse parameter XML string","ParameterXML");
    }
  } 
  else
  {
    try
    {
      pDoc = pParser.parse(filename);
    }
    catch(Poco::Exception& exc)
    {
      throw Kernel::Exception::FileError(exc.displayText() + ". Unable to parse File:", filename);
    }
    catch(...)
    {
      throw Kernel::Exception::FileError("Unable to parse File:" , filename);
    }
  }

  // Get pointer to root element
  Element* pRootElem = pDoc->documentElement();
  if ( !pRootElem->hasChildNodes() )
  {
    throw Kernel::Exception::InstrumentDefinitionError("No root element in XML Parameter file", filename);
  }

  // 
  InstrumentDefinitionParser loadInstr;
  loadInstr.setComponentLinks(instrument, pRootElem);

  // populate parameter map of workspace 
  localWorkspace->populateInstrumentParameters();

}
    Mantid::Geometry::MDImplicitFunction* ImplicitFunctionFactoryImpl::createUnwrapped(const std::string& processXML) const
    {
      using namespace Poco::XML;
      DOMParser pParser;
      Poco::AutoPtr<Document> pDoc = pParser.parseString(processXML);
      Element* pInstructionsXML = pDoc->documentElement();

      boost::scoped_ptr<ImplicitFunctionParser> funcParser(ImplicitFunctionParserFactory::Instance().createImplicitFunctionParserFromXML(processXML));

      boost::scoped_ptr<ImplicitFunctionBuilder> functionBuilder(funcParser->createFunctionBuilder(pInstructionsXML));
      return functionBuilder->create();
    }
Exemple #5
0
/** Initalize Poco XML Parser
* @param filename  -- name of the xml file to process.
 */
void LoadMask::initializeXMLParser(const std::string &filename) {
  // const std::string instName
  std::cout << "Load File " << filename << '\n';
  const std::string xmlText = Kernel::Strings::loadFile(filename);
  std::cout << "Successfully Load XML File \n";

  // Set up the DOM parser and parse xml file
  DOMParser pParser;
  try {
    m_pDoc = pParser.parseString(xmlText);
  } catch (Poco::Exception &exc) {
    throw Kernel::Exception::FileError(
        exc.displayText() + ". Unable to parse File:", filename);
  } catch (...) {
    throw Kernel::Exception::FileError("Unable to parse File:", filename);
  }
  // Get pointer to root element
  m_pRootElem = m_pDoc->documentElement();
  if (!m_pRootElem->hasChildNodes()) {
    g_log.error("XML file: " + filename + "contains no root element.");
    throw Kernel::Exception::InstrumentDefinitionError(
        "No root element in XML instrument file", filename);
  }
}
Exemple #6
0
/**
 * Parse incomming message (from server).
 * @param str Incomming server message
 * @return Message in Command structure
 */
Command XMLTool::parseXML(string str) {
	Command cmd;

	try  {
		DOMParser parser = DOMParser(0);
		AutoPtr<Document> pDoc = parser.parseString(str);
		NodeIterator it(pDoc, NodeFilter::SHOW_ELEMENT);
		Node* pNode = it.nextNode();
		NamedNodeMap* attributes = NULL;
		Node* attribute = NULL;

		while (pNode)  {
			if (pNode->nodeName().compare("server_adapter") == 0) {
				if (pNode->hasAttributes()) {
					attributes = pNode->attributes();
					for(unsigned int i = 0; i < attributes->length(); i++) {
						attribute = attributes->item(i);
						if (attribute->nodeName().compare("protocol_version") == 0) {
							cmd.protocol_version = attribute->nodeValue();
						}

						else if (attribute->nodeName().compare("state") == 0) {
							cmd.state = attribute->nodeValue();
						}
						// FIXME - id attribute is here only for backward compatibility, it should be removed in Q1/2016
						else if (attribute->nodeName().compare("euid") == 0 || attribute->nodeName().compare("id") == 0) {
							cmd.euid = stoull(attribute->nodeValue(), nullptr, 0);
						}

						else if (attribute->nodeName().compare("device_id") == 0) {
							cmd.device_id = atoll(attribute->nodeValue().c_str());
						}

						else if (attribute->nodeName().compare("time") == 0) {
							cmd.time = atoll(attribute->nodeValue().c_str());
						}

						else {
							log.error("Unknow attribute for SERVER_ADAPTER : " + fromXMLString(attribute->nodeName()));
						}
					}
					attributes->release();
				}
			}

			else if (pNode->nodeName().compare("value") == 0) {
				if(cmd.state == "getparameters" || cmd.state == "parameters"){
					string inner = pNode->innerText();
					string device_id = "";

					if (pNode->hasAttributes()) {
						attributes = pNode->attributes();
						string device_id = "";
						for(unsigned int i = 0; i < attributes->length(); i++) {
							attribute = attributes->item(i);
							if (attribute->nodeName().compare("device_id") == 0) {
								device_id = toNumFromString(attribute->nodeValue());
							}
						}
						attributes->release();
					}
					cmd.params.value.push_back({inner, device_id});
				}
				else {
					float val = atof(pNode->innerText().c_str());

					if (pNode->hasAttributes()) {
						int module_id = 0;
						attributes = pNode->attributes();
						for(unsigned int i = 0; i < attributes->length(); i++) {
							attribute = attributes->item(i);
							if (attribute->nodeName().compare("module_id") == 0) {
								module_id = toNumFromString(attribute->nodeValue());
							}
						}
						cmd.values.push_back({module_id, val});  //TODO Hex number is processed wrongly
						attributes->release();
					}
				}
			}
			else if (pNode->nodeName().compare("parameter") == 0) {
				if (pNode->hasAttributes()) {
					attributes = pNode->attributes();
					for(unsigned int i = 0; i < attributes->length(); i++) {
						attribute = attributes->item(i);
						if (attribute->nodeName().compare("param_id") == 0 || attribute->nodeName().compare("id") == 0) {
							cmd.params.param_id = toNumFromString(attribute->nodeValue());
						}
						else if (attribute->nodeName().compare("euid") == 0) {
							cmd.params.euid = toNumFromString(attribute->nodeValue());
						}
					}
					attributes->release();
				}
			}
			pNode = it.nextNode();
		}
	}
	catch (Poco::Exception& e) {
		log.error("Invalid format of incoming message!" + e.displayText());
		cmd.state = "error";
	}
	return cmd;
}
/* Reads the parameter correction file and if a correction is needed output the
*parameterfile needed
*  and whether it is to be appended.
* @param correction_file :: path nsame of correction file as returned by
*getParameterCorrectionFile()
* @param date :: IS8601 date string applicable: Must be full timestamp (timezone
*optional)
* @param parameter_file :: output parameter file to use or "" if none
* @param append :: output whether the parameters from parameter_file should be
*appended.
*
*  @throw FileError Thrown if unable to parse XML file
*/
void LoadIDFFromNexus::readParameterCorrectionFile(
    const std::string &correction_file, const std::string &date,
    std::string &parameter_file, bool &append) {

  // Set output arguments to default
  parameter_file = "";
  append = false;

  // Check the date.
  if (date.empty()) {
    g_log.notice() << "No date is supplied for parameter correction file "
                   << correction_file << ". Correction file is ignored.\n";
    return;
  }

  // Get contents of correction file
  const std::string xmlText = Kernel::Strings::loadFile(correction_file);

  // Set up the DOM parser and parse xml file
  DOMParser pParser;
  Document *pDoc;
  try {
    pDoc = pParser.parseString(xmlText);
  } catch (Poco::Exception &exc) {
    throw Kernel::Exception::FileError(
        exc.displayText() + ". Unable to parse parameter correction file:",
        correction_file);
  } catch (...) {
    throw Kernel::Exception::FileError(
        "Unable to parse parameter correction file:", correction_file);
  }
  // Get pointer to root element
  Element *pRootElem = pDoc->documentElement();
  if (!pRootElem->hasChildNodes()) {
    g_log.error("Parameter correction file: " + correction_file +
                "contains no XML root element.");
    throw Kernel::Exception::InstrumentDefinitionError(
        "No root element in XML parameter correction file", correction_file);
  }

  // Convert date to Mantid object
  g_log.notice() << "Date for correction file " << date << "\n";
  DateAndTime externalDate(date);

  // Examine the XML structure obtained by parsing
  Poco::AutoPtr<NodeList> correctionNodeList =
      pRootElem->getElementsByTagName("correction");
  for (unsigned long i = 0; i < correctionNodeList->length(); ++i) {
    // For each correction element
    Element *corr = dynamic_cast<Element *>(correctionNodeList->item(i));
    if (corr) {
      DateAndTime start(corr->getAttribute("valid-from"));
      DateAndTime end(corr->getAttribute("valid-to"));
      if (start <= externalDate && externalDate <= end) {
        parameter_file = corr->getAttribute("file");
        append = (corr->getAttribute("append") == "true");
        break;
      }
    } else {
      g_log.error("Parameter correction file: " + correction_file +
                  "contains an invalid correction element.");
      throw Kernel::Exception::InstrumentDefinitionError(
          "Invalid element in XML parameter correction file", correction_file);
    }
  }
}