std::string XmlHandler::get_text_from_tag(const std::string &xpath) { Poco::XML::NodeIterator it(pDoc, Poco::XML::NodeFilter::SHOW_ELEMENT); Poco::XML::Node *pNode = it.nextNode(); Poco::XML::Node *detectorNode = pNode->getNodeByPath(xpath); std::string value; if (detectorNode) value = detectorNode->innerText(); return value; }
std::string XMLParserPoco::getFirstElementAtPathInnerText(std::string path) { if(_documentLoaded) { Poco::XML::Node* node = _document->getNodeByPath(path); if(node != nullptr) { return node->innerText(); } } return ""; }
/** * Build dictionary {string : string } of all tags in the dictionary * Composed tags: / replaced by _ * */ std::map<std::string, std::string> XmlHandler::get_metadata(const std::string &tag_to_ignore) { std::map<std::string, std::string> metadata; Poco::XML::NodeIterator it(pDoc, Poco::XML::NodeFilter::SHOW_ELEMENT); Poco::XML::Node *pNode = it.nextNode(); while (pNode) { if (pNode->childNodes()->length() == 1 && pNode->nodeName() != tag_to_ignore) { std::string key = pNode->parentNode()->nodeName() + "/" + pNode->nodeName(); std::string value = pNode->innerText(); metadata.emplace(key, value); } pNode = it.nextNode(); } return metadata; }
/** Create the dimension as a MDHistogram dimension. */ Mantid::Geometry::MDHistoDimension* IMDDimensionFactory::doCreate() const { using namespace Mantid::Geometry; if(m_dimensionXML == NULL) { throw std::runtime_error("Must provide dimension xml before creation"); } Poco::XML::NamedNodeMap* attributes = m_dimensionXML->attributes(); //First and only attribute is the dimension id. Poco::XML::Node* dimensionId = attributes->item(0); std::string id = dimensionId->innerText(); std::string name = m_dimensionXML->getChildElement("Name")->innerText(); Poco::XML::Element* unitsElement = m_dimensionXML->getChildElement("Units"); std::string units = "None"; if(NULL != unitsElement) { //Set units if they exist. units = unitsElement->innerText(); } double upperBounds = atof(m_dimensionXML->getChildElement("UpperBounds")->innerText().c_str()); double lowerBounds = atof(m_dimensionXML->getChildElement("LowerBounds")->innerText().c_str()); unsigned int nBins = atoi(m_dimensionXML->getChildElement("NumberOfBins")->innerText().c_str()); Poco::XML::Element* integrationXML = m_dimensionXML->getChildElement("Integrated"); if (NULL != integrationXML) { double upperLimit = atof(integrationXML->getChildElement("UpperLimit")->innerText().c_str()); double lowerLimit = atof(integrationXML->getChildElement("LowerLimit")->innerText().c_str()); //As it is not currently possible to set integration ranges on a MDDimension or MDGeometryDescription, boundaries become integration ranges. upperBounds = upperLimit; lowerBounds = lowerLimit; } return new MDHistoDimension(name, id, units, lowerBounds, upperBounds, nBins); }
/** Parse XML file and define the following internal variables: std::vector<detid_t> m_maskDetID; //spectrum id-s to unmask std::vector<detid_t> m_unMaskDetID; spectra mask provided std::vector<specnum_t> m_maskSpecID; spectra unmask provided NOT IMPLEMENTED std::vector<specnum_t> m_unMaskSpecID; std::vector<std::string> m_maskCompIDSingle; std::vector<std::string> m_uMaskCompIDSingle; // Supported xml Node names are: component: the name of an instrument component, containing detectors. ids : spectra numbers detids : detector numbers Full implementation needs unit tests verifying all these. Only detector id-s are currently implemented // There are also no current support for keyword, switching on un-masking */ void LoadMask::parseXML() { // 0. Check if (!m_pDoc) throw std::runtime_error("Call LoadMask::initialize() before parseXML."); // 1. Parse and create a structure Poco::AutoPtr<NodeList> pNL_type = m_pRootElem->getElementsByTagName("type"); g_log.information() << "Node Size = " << pNL_type->length() << '\n'; Poco::XML::NodeIterator it(m_pDoc, Poco::XML::NodeFilter::SHOW_ELEMENT); Poco::XML::Node *pNode = it.nextNode(); std::vector<specnum_t> singleSp, pairSp; std::vector<detid_t> maskSingleDet, maskPairDet; std::vector<detid_t> umaskSingleDet, umaskPairDet; bool tomask = true; bool ingroup = false; while (pNode) { const Poco::XML::XMLString value = pNode->innerText(); if (pNode->nodeName() == "group") { // Node "group" ingroup = true; tomask = true; } else if (pNode->nodeName() == "component") { // Node "component" if (ingroup) { parseComponent(value, tomask, m_maskCompIdSingle, m_uMaskCompIdSingle); } else { g_log.error() << "XML File hierarchical (component) error!\n"; } } else if (pNode->nodeName() == "ids") { // Node "ids" if (ingroup) { parseRangeText(value, singleSp, pairSp); } else { g_log.error() << "XML File (ids) hierarchical error!" << " Inner Text = " << pNode->innerText() << '\n'; } } else if (pNode->nodeName() == "detids") { // Node "detids" if (ingroup) { if (tomask) { parseRangeText(value, maskSingleDet, maskPairDet); } else { // NOTE -- currently never happens.TODO: NOT IMPLEMENTED parseRangeText(value, umaskSingleDet, umaskPairDet); } } else { g_log.error() << "XML File (detids) hierarchical error!\n"; } } else if (pNode->nodeName() == "detector-masking") { // Node "detector-masking". Check default value m_defaultToUse = true; } // END-IF-ELSE: pNode->nodeName() pNode = it.nextNode(); } // ENDWHILE convertToVector(singleSp, pairSp, m_maskSpecID); convertToVector(maskSingleDet, maskPairDet, m_maskDetID); // NOTE: -- TODO: NOT IMPLEMENTD -- if unmasking is implemented, should be // enabled // convertToVector(umaskSingleDet, umaskPairDet, m_unMaskDetID); }
/* * Parse XML file */ void LoadGroupXMLFile::parseXML() { // 0. Check if (!m_pDoc) throw std::runtime_error( "Call LoadDetectorsGroupingFile::initialize() before parseXML."); // 1. Parse and create a structure Poco::XML::NodeIterator it(m_pDoc, Poco::XML::NodeFilter::SHOW_ELEMENT); Poco::XML::Node *pNode = it.nextNode(); int curgroupid = m_startGroupID - 1; bool isfirstgroup = true; // Add flag to figure out it is automatic group ID or user-defined group ID bool autogroupid = true; // While loop to go over all nodes! while (pNode) { const Poco::XML::XMLString value = pNode->innerText(); if (pNode->nodeName().compare("detector-grouping") == 0) { // Node "detector-grouping" (first level) // Optional instrument name m_instrumentName = getAttributeValueByName(pNode, "instrument", m_userGiveInstrument); // Optional date for which is relevant m_date = getAttributeValueByName(pNode, "idf-date", m_userGiveDate); // Optional grouping description m_description = getAttributeValueByName(pNode, "description", m_userGiveDescription); } // "detector-grouping" else if (pNode->nodeName().compare("group") == 0) { // Group Node: get ID, set map // a) Find group ID bool foundid; std::string idstr = getAttributeValueByName(pNode, "ID", foundid); if (isfirstgroup && foundid) autogroupid = false; else if (!isfirstgroup && !autogroupid && foundid) autogroupid = false; else autogroupid = true; isfirstgroup = false; if (autogroupid) { curgroupid++; } else { curgroupid = atoi(idstr.c_str()); } // b) Set in map auto itc = m_groupComponentsMap.find(curgroupid); if (itc != m_groupComponentsMap.end()) { // Error! Duplicate Group ID defined in XML std::stringstream ss; ss << "Map (group ID, components) has group ID " << curgroupid << " already. Duplicate Group ID error!" << std::endl; throw std::invalid_argument(ss.str()); } else { // When group ID is sorted, check if user has specified a group name bool foundName; std::string name = getAttributeValueByName(pNode, "name", foundName); if (foundName) m_groupNamesMap[curgroupid] = name; // Set map std::vector<std::string> tempcomponents; std::vector<detid_t> tempdetids; std::vector<int> tempspectrumids; m_groupComponentsMap[curgroupid] = tempcomponents; m_groupDetectorsMap[curgroupid] = tempdetids; m_groupSpectraMap[curgroupid] = tempspectrumids; } } // "group" else if (pNode->nodeName().compare("component") == 0) { // Node "component" = value auto it = m_groupComponentsMap.find(curgroupid); if (it == m_groupComponentsMap.end()) { std::stringstream ss; ss << "XML File (component) heirachial error!" << " Inner Text = " << pNode->innerText() << std::endl; throw std::invalid_argument(ss.str()); } else { bool valfound; std::string val_value = this->getAttributeValueByName(pNode, "val", valfound); std::string finalvalue; if (valfound && value.size() > 0) finalvalue = value + ", " + val_value; else if (value.size() == 0) finalvalue = val_value; else finalvalue = value; it->second.push_back(finalvalue); } } // Component else if (pNode->nodeName().compare("detids") == 0) { // Node "detids" auto it = m_groupDetectorsMap.find(curgroupid); if (it == m_groupDetectorsMap.end()) { std::stringstream ss; ss << "XML File (detids) hierarchal error!" << " Inner Text = " << pNode->innerText() << std::endl; throw std::invalid_argument(ss.str()); } else { bool valfound; std::string val_value = this->getAttributeValueByName(pNode, "val", valfound); std::string finalvalue; if (valfound && value.size() > 0) finalvalue = value + ", " + val_value; else if (value.size() == 0) finalvalue = val_value; else finalvalue = value; std::vector<int> parsedRange = Strings::parseRange(finalvalue); it->second.insert(it->second.end(), parsedRange.begin(), parsedRange.end()); } } // "detids" else if (pNode->nodeName().compare("ids") == 0) { // Node ids: for spectrum number auto it = m_groupSpectraMap.find(curgroupid); if (it == m_groupSpectraMap.end()) { std::stringstream ss; ss << "XML File (ids) hierarchal error! " << " Inner Text = " << pNode->innerText() << std::endl; throw std::invalid_argument(ss.str()); } else { bool valfound; std::string val_value = this->getAttributeValueByName(pNode, "val", valfound); std::string finalvalue; if (valfound && value.size() > 0) finalvalue = value + ", " + val_value; else if (value.size() == 0) finalvalue = val_value; else finalvalue = value; std::vector<int> parsedRange = Strings::parseRange(finalvalue); it->second.insert(it->second.end(), parsedRange.begin(), parsedRange.end()); } } // Next Node! pNode = it.nextNode(); } // ENDWHILE return; }
/** Parse XML file */ void LoadMask::parseXML() { // 0. Check if (!m_pDoc) throw std::runtime_error("Call LoadMask::initialize() before parseXML."); // 1. Parse and create a structure Poco::AutoPtr<NodeList> pNL_type = m_pRootElem->getElementsByTagName("type"); g_log.information() << "Node Size = " << pNL_type->length() << std::endl; Poco::XML::NodeIterator it(m_pDoc, Poco::XML::NodeFilter::SHOW_ELEMENT); Poco::XML::Node *pNode = it.nextNode(); bool tomask = true; bool ingroup = false; while (pNode) { const Poco::XML::XMLString value = pNode->innerText(); if (pNode->nodeName().compare("group") == 0) { // Node "group" ingroup = true; tomask = true; /* // get type Poco::AutoPtr<Poco::XML::NamedNodeMap> att = pNode->attributes(); Poco::XML::Node* cNode = att->item(0); if (cNode->getNodeValue().compare("mask") == 0 || cNode->getNodeValue().compare("notuse") == 0){ tomask = true; } else if (cNode->getNodeValue().compare("unmask") == 0 || cNode->getNodeValue().compare("use") == 0){ tomask = false; } else { g_log.error() << "Type (" << cNode->localName() << ") = " << cNode->getNodeValue() << " is not supported!" << std::endl; } g_log.information() << "Node Group: child Node Name = " << cNode->localName() << ": " << cNode->getNodeValue() << "(always)"<< std::endl; */ } else if (pNode->nodeName().compare("component") == 0) { // Node "component" if (ingroup) { this->parseComponent(value, tomask); } else { g_log.error() << "XML File heirachial (component) error!" << std::endl; } // g_log.information() << "Component: " << value << std::endl; } else if (pNode->nodeName().compare("ids") == 0) { // Node "ids" if (ingroup) { this->parseSpectrumNos(value, tomask); } else { g_log.error() << "XML File (ids) heirachial error!" << " Inner Text = " << pNode->innerText() << std::endl; } // g_log.information() << "detids: " << value << std::endl; } else if (pNode->nodeName().compare("detids") == 0) { // Node "detids" if (ingroup) { this->parseDetectorIDs(value, tomask); } else { g_log.error() << "XML File (detids) heirachial error!" << std::endl; } } else if (pNode->nodeName().compare("detector-masking") == 0) { // Node "detector-masking". Check default value m_defaultToUse = true; /* Poco::AutoPtr<Poco::XML::NamedNodeMap> att = pNode->attributes(); if (att->length() > 0){ Poco::XML::Node* cNode = att->item(0); m_defaultToUse = true; if (cNode->localName().compare("default") == 0){ if (cNode->getNodeValue().compare("use") == 0){ m_defaultToUse = true; } else { m_defaultToUse = false; } } // if - att-length */ } // END-IF-ELSE: pNode->nodeName() pNode = it.nextNode(); } // ENDWHILE return; }