void parse_file( bool do_html, istream& input_stream, ostream& output_stream ) { DomParser parser; parser.set_substitute_entities( true ); parser.parse_stream( input_stream ); if ( parser ) { /* if succesfull create output */ const Element * rootNode = parser.get_document()->get_root_node(); if ( rootNode == NULL ) { throw runtime_error( "get_root_node() failed" ); } OutputBuilder* b; if ( do_html ) { b = new HtmlBuilder( output_stream ); } else { b = new LatexBuilder( output_stream ); } /* do stuff */ { const Element & root_in = dynamic_cast<const Element &>( *rootNode ); if ( root_in.get_name() != "document" ) { throw runtime_error( "root node must be document" ); } OutputState * s = b->create_root(); Node::NodeList list = root_in.get_children(); for ( Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter ) { if ( *iter != NULL ) { parse_node( **iter, * s ); } } s->finish(); delete s; } delete b; } }
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; }
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; }
bool OpenSearchResponseParser::parse(const ::Document *pResponseDoc, vector<DocumentInfo> &resultsList, unsigned int &totalResults, unsigned int &firstResultIndex) const { float pseudoScore = 100; unsigned int contentLen = 0; bool foundResult = false; if ((pResponseDoc == NULL) || (pResponseDoc->getData(contentLen) == NULL) || (contentLen == 0)) { return false; } // Make sure the response MIME type is sensible string mimeType = pResponseDoc->getType(); if ((mimeType.empty() == false) && (mimeType.find("xml") == string::npos)) { cerr << "OpenSearchResponseParser::parse: response is not XML" << endl; return false; } const char *pContent = pResponseDoc->getData(contentLen); try { bool loadFeed = false; // Parse the configuration file DomParser parser; parser.set_substitute_entities(true); parser.parse_memory_raw((const unsigned char *)pContent, (Parser::size_type)contentLen); xmlpp::Document *pDocument = parser.get_document(); if (pDocument == NULL) { return false; } Node *pNode = pDocument->get_root_node(); Element *pRootElem = dynamic_cast<Element *>(pNode); if (pRootElem == NULL) { return false; } // Check the top-level element is what we expect ustring rootNodeName = pRootElem->get_name(); if (m_rssResponse == true) { if (rootNodeName == "rss") { const Node::NodeList rssChildNodes = pRootElem->get_children(); for (Node::NodeList::const_iterator rssIter = rssChildNodes.begin(); rssIter != rssChildNodes.end(); ++rssIter) { Node *pRssNode = (*rssIter); Element *pRssElem = dynamic_cast<Element*>(pRssNode); if (pRssElem != NULL) { if (pRssElem->get_name() == "channel") { pRootElem = pRssElem; loadFeed = true; break; } } } } } else { if (rootNodeName != "feed") { return false; } loadFeed = true; } if (loadFeed == false) { #ifdef DEBUG cout << "OpenSearchResponseParser::parse: error on root node " << rootNodeName << endl; #endif return false; } // RSS ustring itemNode("item"); ustring descriptionNode("description"); if (m_rssResponse == false) { // Atom itemNode = "entry"; descriptionNode = "content"; } // Go through the subnodes const Node::NodeList childNodes = pRootElem->get_children(); for (Node::NodeList::const_iterator iter = childNodes.begin(); iter != childNodes.end(); ++iter) { Node *pChildNode = (*iter); ustring nodeName(pChildNode->get_name()); ustring nodeContent(getNodeContent(pChildNode)); // Is this an OpenSearch extension ? // FIXME: make sure namespace is opensearch if (nodeName == "totalResults") { if (nodeContent.empty() == false) { totalResults = min((unsigned int)atoi(nodeContent.c_str()), totalResults); #ifdef DEBUG cout << "OpenSearchResponseParser::parse: total results " << totalResults << endl; #endif } } else if (nodeName == "startIndex") { if (nodeContent.empty() == false) { firstResultIndex = (unsigned int)atoi(nodeContent.c_str()); #ifdef DEBUG cout << "OpenSearchResponseParser::parse: first result index " << firstResultIndex << endl; #endif } } if (nodeName != itemNode) { continue; } // Go through the item's subnodes ustring title, url, extract; const Node::NodeList itemChildNodes = pChildNode->get_children(); for (Node::NodeList::const_iterator itemIter = itemChildNodes.begin(); itemIter != itemChildNodes.end(); ++itemIter) { Node *pItemNode = (*itemIter); Element *pItemElem = dynamic_cast<Element*>(pItemNode); if (pItemElem == NULL) { continue; } ustring itemNodeName = pItemNode->get_name(); if (itemNodeName == "title") { title = getNodeContent(pItemNode); } else if (itemNodeName == "link") { if (m_rssResponse == true) { url = getNodeContent(pItemNode); } else { Attribute *pAttr = pItemElem->get_attribute("href"); if (pAttr != NULL) { url = pAttr->get_value(); } } } else if (itemNodeName == descriptionNode) { extract = getNodeContent(pItemNode); } } DocumentInfo result(title, url, "", ""); result.setExtract(extract); result.setScore(pseudoScore); resultsList.push_back(result); --pseudoScore; foundResult = true; if (resultsList.size() >= totalResults) { // Enough results break; } } } catch (const std::exception& ex) { #ifdef DEBUG cout << "OpenSearchResponseParser::parse: caught exception: " << ex.what() << endl; #endif foundResult = false; } return foundResult; }
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 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; }
iPackage* JispParser::parse(const FindFile::Found& defFile) { using namespace xmlpp; // parsujemy xmla z definicjami DomParser parser; parser.set_substitute_entities(); Node* rootNode; Node* metaNode; Node::NodeList icons; Node::NodeList nodes; Attribute* attrib; eMSet result; ifstream file(defFile.getFilePath().c_str()); try { parser.parse_stream(file); } catch (const xmlpp::exception& e) { throw XMLParserException(e.what()); } file.close(); rootNode = parser.get_document()->get_root_node(); if (rootNode->get_name() != "icondef") throw WrongFormat("Z³a nazwa korzenia dokumentu (z³y format ?)"); nodes = rootNode->get_children("meta"); if (nodes.size() != 1) throw WrongFormat("Nie ma dok³adnie jednego elementu 'meta'"); metaNode = *nodes.begin(); if (!metaNode) throw WrongFormat("Element 'meta' nie zawiera dzieci"); nodes = metaNode->get_children("name"); if (nodes.size() == 1 && dynamic_cast<Element*>(*nodes.begin())) { result.setName(dynamic_cast<Element*>(*nodes.begin())->get_child_text()->get_content().c_str()); } nodes = metaNode->get_children("version"); if (nodes.size() == 1 && dynamic_cast<Element*>(*nodes.begin())) { result.setVersion(dynamic_cast<Element*>(*nodes.begin())->get_child_text()->get_content().c_str()); } nodes = metaNode->get_children("description"); if (nodes.size() == 1 && dynamic_cast<Element*>(*nodes.begin())) { result.setDescription(dynamic_cast<Element*>(*nodes.begin())->get_child_text()->get_content().c_str()); } nodes = metaNode->get_children("author"); for (Node::NodeList::iterator it = nodes.begin(); it != nodes.end(); it++) { if (dynamic_cast<Element*>(*it)) { attrib = dynamic_cast<Element*>(*it)->get_attribute("jid"); result.addAuthor(eMAuthor(dynamic_cast<Element*>(*it)->get_child_text()->get_content().c_str(), attrib ? attrib->get_value().c_str() : "")); } } nodes = metaNode->get_children("creation"); if (nodes.size() == 1 && dynamic_cast<Element*>(*nodes.begin())) { // result.setCTime(dynamic_cast<Element*>(*nodes.begin())->get_child_text()->get_content().c_str()); } nodes = metaNode->get_children("home"); if (nodes.size() == 1 && dynamic_cast<Element*>(*nodes.begin())) { result.setUrl(dynamic_cast<Element*>(*nodes.begin())->get_child_text()->get_content().c_str()); } icons = rootNode->get_children("icon"); for (Node::NodeList::iterator it = icons.begin(); it != icons.end(); it++) { eM emot(true, false); string mime; nodes = (*it)->get_children("object"); for (Node::NodeList::iterator it = nodes.begin(); it != nodes.end(); it++) { if (dynamic_cast<Element*>(*it) && dynamic_cast<Element*>(*it)->get_attribute("mime")) { mime = dynamic_cast<Element*>(*it)->get_attribute("mime")->get_value(); if (mime == "image/png" || mime == "image/gif" || mime == "image/jpeg") { emot.setImgPath(defFile.getDirectory() + (string) dynamic_cast<Element*>(*it)->get_child_text()->get_content()); break; } mime.clear(); } } if (mime.empty()) continue; nodes = (*it)->get_children("text"); if (nodes.empty()) throw WrongFormat("Brak tekstu do zamiany"); for (Node::NodeList::iterator it = nodes.begin(); it != nodes.end(); it++) { if (dynamic_cast<Element*>(*it)) { emot.setText(dynamic_cast<Element*>(*it)->get_child_text()->get_content().c_str()); attrib = dynamic_cast<Element*>(*it)->get_attribute("regexp"); if (attrib) { emot.setPreg((bool) atoi(attrib->get_value().c_str())); } result.addEmot(emot); } } } return new eMSet(result); }