void ApplicationSettings::write_ini(const string& fname) { Node *root; Document *doc; // { DomParser parser; // parser.set_validate(); parser.set_substitute_entities(); parser.parse_file(fname); doc = parser.get_document(); // } root = doc->get_root_node(); string_set_xpath(doc, root, "gate_lib", gateLibFile); string_set_xpath(doc, root, "help_file", helpFile); string_set_xpath(doc, root, "font_file", textFontFile); uint_set_xpath(doc, root, "frame/width", mainFrameWidth); uint_set_xpath(doc, root, "frame/height", mainFrameHeight); int_set_xpath(doc, root, "frame/top", mainFrameTop); int_set_xpath(doc, root, "frame/left", mainFrameLeft); uint_set_xpath(doc, root, "time_per_step", timePerStep); int_set_xpath(doc, root, "refresh_rate", refreshRate); float_set_xpath(doc, root, "wire_conn_radius", wireConnRadius); bool_set_xpath(doc, root, "wire_conn_visible", wireConnVisible); bool_set_xpath(doc, root, "grid_line_visible", gridlineVisible); string_set_xpath(doc, root, "last_directory", lastDir); doc->write_to_file_formatted(fname); cerr << "Wrote to file " << fname << endl; }
intg pascal_dataset<Tdata>::count_samples() { total_difficult = 0; total_truncated = 0; total_occluded = 0; total_ignored = 0; total_samples = 0; std::string xmlpath; boost::filesystem::path p(annroot); if (!boost::filesystem::exists(p)) eblthrow("Annotation path " << annroot << " does not exist."); std::cout << "Counting number of samples in " << annroot << " ..." << std::endl; // find all xml files recursively std::list<std::string> *files = find_fullfiles(annroot, XML_PATTERN, NULL, false, true); if (!files || files->size() == 0) eblthrow("no xml files found in " << annroot << " using file pattern " << XML_PATTERN); std::cout << "Found " << files->size() << " xml files." << std::endl; for (std::list<std::string>::iterator i = files->begin(); i != files->end(); ++i) { xmlpath = *i; // parse xml DomParser parser; parser.parse_file(xmlpath); if (parser) { // initialize root node and list const Node* pNode = parser.get_document()->get_root_node(); Node::NodeList list = pNode->get_children(); // parse all objects in image for(Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter) { if (!strcmp((*iter)->get_name().c_str(), "object")) { // check for difficult flag in object node Node::NodeList olist = (*iter)->get_children(); count_sample(olist); } } } } if (files) delete files; std::cout << "Found: " << total_samples << " samples, including "; std::cout << total_difficult << " difficult, " << total_truncated; std::cout << " truncated and " << total_occluded << " occluded." << std::endl; ignore_difficult ? std::cout << "Ignoring" : std::cout << "Using"; std::cout << " difficult samples." << std::endl; ignore_truncated ? std::cout << "Ignoring" : std::cout << "Using"; std::cout << " truncated samples." << std::endl; ignore_occluded ? std::cout << "Ignoring" : std::cout << "Using"; std::cout << " occluded samples." << std::endl; total_samples = total_samples - total_ignored; return total_samples; }
void ApplicationSettings::parse_ini(const string& fname) { DomParser parser; Node *root; // parser.set_validate(); parser.set_substitute_entities(); parser.parse_file(fname); root = parser.get_document()->get_root_node(); xpath_set_string(root, "gate_lib", gateLibFile); xpath_set_string(root, "help_file", helpFile); xpath_set_string(root, "font_file", textFontFile); xpath_set_uint(root, "frame/width", mainFrameWidth); xpath_set_uint(root, "frame/height", mainFrameHeight); xpath_set_int(root, "frame/top", mainFrameTop); xpath_set_int(root, "frame/left", mainFrameLeft); xpath_set_uint(root, "time_per_step", timePerStep); xpath_set_int(root, "refresh_rate", refreshRate); xpath_set_float(root, "wire_conn_radius", wireConnRadius); xpath_set_bool(root, "wire_conn_visible", wireConnVisible); xpath_set_bool(root, "grid_line_visible", gridlineVisible); xpath_set_string(root, "last_directory", lastDir); cerr << format("Parsed %s") % fname << endl; }
ResponseParserInterface *OpenSearchParser::parse(SearchPluginProperties &properties, bool extractSearchParams) { struct stat fileStat; bool rssResponse = true, success = true; if ((m_fileName.empty() == true) || (stat(m_fileName.c_str(), &fileStat) != 0) || (!S_ISREG(fileStat.st_mode))) { return NULL; } try { // Parse the configuration file DomParser parser; parser.set_substitute_entities(true); parser.parse_file(m_fileName); xmlpp::Document *pDocument = parser.get_document(); if (pDocument == NULL) { return NULL; } Node *pNode = pDocument->get_root_node(); Element *pRootElem = dynamic_cast<Element *>(pNode); if (pRootElem == NULL) { return NULL; } // Check the top-level element is what we expect // MozSearch is very much like OpenSearch Description ustring rootNodeName = pRootElem->get_name(); if ((rootNodeName != "OpenSearchDescription") && (rootNodeName != "SearchPlugin")) { #ifdef DEBUG cout << "OpenSearchParser::parse: wrong root node " << rootNodeName << endl; #endif return NULL; } // Go through the subnodes const Node::NodeList childNodes = pRootElem->get_children(); if (childNodes.empty() == false) { for (Node::NodeList::const_iterator iter = childNodes.begin(); iter != childNodes.end(); ++iter) { Node *pChildNode = (*iter); Element *pElem = dynamic_cast<Element*>(pChildNode); if (pElem == NULL) { continue; } ustring nodeName(pChildNode->get_name()); ustring nodeContent(getNodeContent(pChildNode)); if (nodeName == "ShortName") { properties.m_name = nodeContent; } else if (nodeName == "Description") { properties.m_description = nodeContent; } else if (nodeName == "Url") { ustring url, type; SearchPluginProperties::Response response = SearchPluginProperties::RSS_RESPONSE; bool getMethod = true; // Parse Query Syntax Element::AttributeList attributes = pElem->get_attributes(); for (Element::AttributeList::const_iterator iter = attributes.begin(); iter != attributes.end(); ++iter) { Attribute *pAttr = (*iter); if (pAttr != NULL) { ustring attrName = pAttr->get_name(); ustring attrContent = pAttr->get_value(); if (attrName == "template") { url = attrContent; } else if (attrName == "type") { type = attrContent; } else if (attrName == "method") { // GET is the default method if (StringManip::toLowerCase(attrContent) != "get") { getMethod = false; } } } } // Did we get the URL ? if (url.empty() == true) { // It's probably provided as content, v1.0 style url = nodeContent; } if (getMethod == true) { string::size_type startPos = 0, pos = url.find("?"); // Do we support that type ? if (type == "application/atom+xml") { response = SearchPluginProperties::ATOM_RESPONSE; rssResponse = false; } else if ((type.empty() == false) && (type != "application/rss+xml")) { response = SearchPluginProperties::UNKNOWN_RESPONSE; #ifdef DEBUG cout << "OpenSearchParser::parse: unsupported response type " << type << endl; #endif continue; } // Break the URL down into base and parameters if (pos != string::npos) { string params(url.substr(pos + 1)); // URL properties.m_baseUrl = url.substr(0, pos); #ifdef DEBUG cout << "OpenSearchParser::parse: URL is " << url << endl; #endif // Split this into the actual parameters params += "&"; pos = params.find("&"); while (pos != string::npos) { string parameter(params.substr(startPos, pos - startPos)); string::size_type equalPos = parameter.find("="); if (equalPos != string::npos) { string paramName(parameter.substr(0, equalPos)); string paramValue(parameter.substr(equalPos + 1)); SearchPluginProperties::Parameter param = SearchPluginProperties::UNKNOWN_PARAM; if (paramValue == "{searchTerms}") { param = SearchPluginProperties::SEARCH_TERMS_PARAM; } else if (paramValue == "{count}") { param = SearchPluginProperties::COUNT_PARAM; } else if (paramValue == "{startIndex}") { param = SearchPluginProperties::START_INDEX_PARAM; } else if (paramValue == "{startPage}") { param = SearchPluginProperties::START_PAGE_PARAM; } else if (paramValue == "{language}") { param = SearchPluginProperties::LANGUAGE_PARAM; } else if (paramValue == "{outputEncoding}") { param = SearchPluginProperties::OUTPUT_ENCODING_PARAM; } else if (paramValue == "{inputEncoding}") { param = SearchPluginProperties::INPUT_ENCODING_PARAM; } if (param != SearchPluginProperties::UNKNOWN_PARAM) { properties.m_parameters[param] = paramName; } else { // Append to the remainder if (properties.m_parametersRemainder.empty() == false) { properties.m_parametersRemainder += "&"; } properties.m_parametersRemainder += paramName; properties.m_parametersRemainder += "="; properties.m_parametersRemainder += paramValue; } } // Next startPos = pos + 1; pos = params.find_first_of("&", startPos); } } // Method properties.m_method = SearchPluginProperties::GET_METHOD; // Output type properties.m_outputType = type; // Response properties.m_response = response; } // We ignore Param as we only support GET } else if (nodeName == "Tags") { // This is supposed to be a space-delimited list, but use the whole thing as channel properties.m_channel = nodeContent; } else if (nodeName == "LongName") { properties.m_longName = nodeContent; } else if (nodeName == "Language") { properties.m_languages.insert(nodeContent); } else if (nodeName == "OutputEncoding") { properties.m_outputEncodings.insert(nodeContent); } else if (nodeName == "InputEncoding") { properties.m_inputEncodings.insert(nodeContent); } } } } catch (const std::exception& ex) { #ifdef DEBUG cout << "OpenSearchParser::parse: caught exception: " << ex.what() << endl; #endif success = false; } if (success == false) { return NULL; } // Scrolling properties.m_nextIncrement = 1; properties.m_nextBase = 1; if (properties.m_parameters.find(SearchPluginProperties::START_PAGE_PARAM) != properties.m_parameters.end()) { properties.m_scrolling = SearchPluginProperties::PER_PAGE; } else { properties.m_scrolling = SearchPluginProperties::PER_INDEX; } return new OpenSearchResponseParser(rssResponse); }
bool pascalfull_dataset<Tdata>::process_xml(const string &xmlfile) { string image_filename; string image_fullname; string obj_classname; // parse xml file try { DomParser parser; // parser.set_validate(); parser.parse_file(xmlfile); if (parser) { // initialize root node and list const Node* pNode = parser.get_document()->get_root_node(); Node::NodeList list = pNode->get_children(); // get image filename for(Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter) { if (!strcmp((*iter)->get_name().c_str(), "filename")) { xml_get_string(*iter, image_filename); iter = list.end(); iter--; // stop loop } } image_fullname = imgroot; image_fullname += image_filename; // parse all objects in image for(Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter) { if (!strcmp((*iter)->get_name().c_str(), "object")) { Node::NodeList olist = (*iter)->get_children(); for(Node::NodeList::iterator oiter = olist.begin(); oiter != olist.end(); ++oiter) { if (!strcmp((*oiter)->get_name().c_str(), "name")) { xml_get_string(*oiter, obj_classname); // if object's name matches an excluded class, stop this xml if (find(exclude.begin(), exclude.end(), obj_classname) != exclude.end()) return false; } } } } } } catch (const std::exception& ex) { cerr << "error: Xml exception caught: " << ex.what() << endl; return false; } catch (const char *err) { cerr << "error: " << err << endl; return false; } // copy image into output directory ostringstream cmd; ostringstream tgt; tgt << outdir << "/" << image_filename; cmd << "cp " << image_fullname << " " << tgt.str(); if (std::system(cmd.str().c_str())) cerr << "warning: failed to execute: " << cmd.str() << endl; else { cout << data_cnt << ": copied " << tgt.str() << endl; data_cnt++; } return true; }
bool PinotSettings::loadConfiguration(const std::string &fileName, bool isGlobal) { struct stat fileStat; bool success = true; if ((stat(fileName.c_str(), &fileStat) != 0) || (!S_ISREG(fileStat.st_mode))) { cerr << "Couldn' open configuration file " << fileName << endl; return false; } try { // Parse the configuration file DomParser parser; parser.set_substitute_entities(true); parser.parse_file(fileName); xmlpp::Document *pDocument = parser.get_document(); if (pDocument == NULL) { return false; } Element *pRootElem = pDocument->get_root_node(); if (pRootElem == NULL) { return false; } // Check the top-level element is what we expect ustring rootNodeName = pRootElem->get_name(); if (rootNodeName != "pinot") { return false; } // Go through the subnodes const Node::NodeList childNodes = pRootElem->get_children(); if (childNodes.empty() == false) { for (Node::NodeList::const_iterator iter = childNodes.begin(); iter != childNodes.end(); ++iter) { Node *pNode = (*iter); // All nodes should be elements Element *pElem = dynamic_cast<Element*>(pNode); if (pElem == NULL) { continue; } string nodeName(pElem->get_name()); string nodeContent(getElementContent(pElem)); if (isGlobal == true) { if (nodeName == "cache") { loadCacheProviders(pElem); } else { // Unsupported element continue; } } else if (nodeName == "googleapikey") { m_googleAPIKey = nodeContent; } else if (nodeName == "ui") { loadUi(pElem); } else if (nodeName == "extraindex") { loadIndexes(pElem); } else if (nodeName == "channel") { loadEngineChannels(pElem); } else if (nodeName == "storedquery") { loadQueries(pElem); } else if (nodeName == "label") { loadLabels(pElem); } else if (nodeName == "robots") { if (nodeContent == "IGNORE") { m_ignoreRobotsDirectives = true; } else { m_ignoreRobotsDirectives = false; } } else if (nodeName == "suggestterms") { if (nodeContent == "YES") { m_suggestQueryTerms = true; } else { m_suggestQueryTerms = false; } } else if (nodeName == "newresults") { loadColour(pElem); } else if (nodeName == "mailaccount") { loadMailAccounts(pElem); } else if (nodeName == "indexable") { loadIndexableLocations(pElem); } } } } catch (const std::exception& ex) { cerr << "Couldn't parse configuration file: " << ex.what() << endl; success = false; } return success; }
bool PinotSettings::loadConfiguration(const std::string &fileName) { struct stat fileStat; bool success = true; if ((stat(fileName.c_str(), &fileStat) != 0) || (!S_ISREG(fileStat.st_mode))) { return false; } try { // Parse the configuration file DomParser parser; parser.set_substitute_entities(true); parser.parse_file(fileName); xmlpp::Document *pDocument = parser.get_document(); if (pDocument == NULL) { return false; } Element *pRootElem = pDocument->get_root_node(); if (pRootElem == NULL) { return false; } // Check the top-level element is what we expect ustring rootNodeName = pRootElem->get_name(); if (rootNodeName != "pinot") { return false; } // Go through the subnodes const Node::NodeList childNodes = pRootElem->get_children(); if (childNodes.empty() == false) { for (Node::NodeList::const_iterator iter = childNodes.begin(); iter != childNodes.end(); ++iter) { Node *pNode = (*iter); // All nodes should be elements Element *pElem = dynamic_cast<Element*>(pNode); if (pElem == NULL) { continue; } string nodeName = pElem->get_name(); string nodeContent = getElementContent(pElem); if (nodeName == "googleapikey") { m_googleAPIKey = nodeContent; } else if (nodeName == "ui") { if (loadUi(pElem) == false) { cerr << _("Couldn't load ui block") << endl; } } else if (nodeName == "extraindex") { if (loadIndexes(pElem) == false) { cerr << _("Couldn't load extraindex block") << endl; } } else if (nodeName == "storedquery") { if (loadQueries(pElem) == false) { cerr << _("Couldn't load storedquery block") << endl; } } else if (nodeName == "results") { if (loadResults(pElem) == false) { cerr << _("Couldn't load results block") << endl; } } else if (nodeName == "label") { if (loadLabels(pElem) == false) { cerr << _("Couldn't load label block") << endl; } } else if (nodeName == "robots") { if (nodeContent == "IGNORE") { m_ignoreRobotsDirectives = true; } else { m_ignoreRobotsDirectives = false; } } else if (nodeName == "mailaccount") { if (loadMailAccounts(pElem) == false) { cerr << _("Couldn't load mailaccount block") << endl; } } } } } catch (const std::exception& ex) { cerr << _("Couldn't parse configuration file") << ": " << ex.what() << endl; success = false; } return success; }