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;
	}
}
bool PinotSettings::loadLabels(const Element *pElem)
{
	if (pElem == NULL)
	{
		return false;
	}

	Node::NodeList childNodes = pElem->get_children();
	if (childNodes.empty() == true)
	{
		return false;
	}

	// Load the label's properties
	for (Node::NodeList::iterator iter = childNodes.begin(); iter != childNodes.end(); ++iter)
	{
		Node *pNode = (*iter);
		Element *pElem = dynamic_cast<Element*>(pNode);
		if (pElem == NULL)
		{
			continue;
		}

		string nodeName = pElem->get_name();
		string nodeContent = getElementContent(pElem);

		if (nodeName == "name")
		{
			m_labels.insert(nodeContent);
		}
		// Labels used to have colours...
	}

	return true;
}
bool PinotSettings::loadFilePatterns(const Element *pElem)
{
	if (pElem == NULL)
	{
		return false;
	}

	Node::NodeList childNodes = pElem->get_children();
	if (childNodes.empty() == true)
	{
		return false;
	}

	// Load the file patterns list
	for (Node::NodeList::iterator iter = childNodes.begin(); iter != childNodes.end(); ++iter)
	{
		Node *pNode = (*iter);
		Element *pElem = dynamic_cast<Element*>(pNode);
		if (pElem == NULL)
		{
			continue;
		}

		string nodeName = pElem->get_name();
		string nodeContent = getElementContent(pElem);

		if (nodeName == "pattern")
		{
			m_filePatternsBlackList.insert(nodeContent);
		}
	}

	return true;
}
void CharacterList::fromXML(const xmlpp::Element &root)
{
    using namespace xmlpp;

    clear();
    Node::NodeList node = root.get_children("character");
    for (Node::NodeList::const_iterator it = node.begin(); it != node.end(); it++)
    {
        Element *elem = dynamic_cast<Element*>(*it);
        string name;
        Attribute *attr = elem->get_attribute("name");
        if (attr != NULL)
        {
            name = attr->get_value();
        }
        string playerName="";
        attr = elem->get_attribute("playername");
        if (attr != NULL)
        {
            playerName = attr->get_value();
        }
        Character character = Character(name,playerName);
        character.fromXML(*elem);
        vCharacters.push_back(character);
    }        
}
bool PinotSettings::loadUi(const Element *pElem)
{
	if (pElem == NULL)
	{
		return false;
	}

	Node::NodeList childNodes = pElem->get_children();
	if (childNodes.empty() == true)
	{
		return false;
	}

	for (Node::NodeList::iterator iter = childNodes.begin(); iter != childNodes.end(); ++iter)
	{
		Node *pNode = (*iter);
		Element *pElem = dynamic_cast<Element*>(pNode);
		if (pElem == NULL)
		{
			continue;
		}

		string nodeName = pElem->get_name();
		string nodeContent = getElementContent(pElem);
		if (nodeName == "xpos")
		{
			m_xPos = atoi(nodeContent.c_str());
		}
		else if (nodeName == "ypos")
		{
			m_yPos = atoi(nodeContent.c_str());
		}
		else if (nodeName == "width")
		{
			m_width = atoi(nodeContent.c_str());
		}
		else if (nodeName == "height")
		{
			m_height = atoi(nodeContent.c_str());
		}
		else if (nodeName == "panepos")
		{
			m_panePos = atoi(nodeContent.c_str());
		}
		else if (nodeName == "expandqueries")
		{
			if (nodeContent == "YES")
			{
				m_expandQueries = true;
			}
			else
			{
				m_expandQueries = false;
			}
		}
	}

	return true;
}
bool PinotSettings::loadMailAccounts(const Element *pElem)
{
	if (pElem == NULL)
	{
		return false;
	}

	Node::NodeList childNodes = pElem->get_children();
	if (childNodes.empty() == true)
	{
		return false;
	}

	MailAccount mailAccount;

	// Load the mail account's properties
	for (Node::NodeList::iterator iter = childNodes.begin(); iter != childNodes.end(); ++iter)
	{
		Node *pNode = (*iter);
		Element *pElem = dynamic_cast<Element*>(pNode);
		if (pElem == NULL)
		{
			continue;
		}

		string nodeName = pElem->get_name();
		string nodeContent = getElementContent(pElem);

		if (nodeName == "name")
		{
			mailAccount.m_name = nodeContent;
		}
		else if (nodeName == "type")
		{
			mailAccount.m_type = nodeContent;
		}
		else if (nodeName == "mtime")
		{
			mailAccount.m_modTime = (time_t)atoi(nodeContent.c_str());
		}
		else if (nodeName == "mindate")
		{
			mailAccount.m_lastMessageTime = (time_t)atoi(nodeContent.c_str());
		}
		else if (nodeName == "size")
		{
			mailAccount.m_size = (off_t)atoi(nodeContent.c_str());
		}
	}

	if (mailAccount.m_name.empty() == false)
	{
		m_mailAccounts.insert(mailAccount);
	}

	return true;
}
bool PinotSettings::loadLabels(const Element *pElem)
{
	if (pElem == NULL)
	{
		return false;
	}

	Node::NodeList childNodes = pElem->get_children();
	if (childNodes.empty() == true)
	{
		return false;
	}

	Label label;

	// Load the label's properties
	for (Node::NodeList::iterator iter = childNodes.begin(); iter != childNodes.end(); ++iter)
	{
		Node *pNode = (*iter);
		Element *pElem = dynamic_cast<Element*>(pNode);
		if (pElem == NULL)
		{
			continue;
		}

		string nodeName = pElem->get_name();
		string nodeContent = getElementContent(pElem);

		if (nodeName == "name")
		{
			label.m_name = nodeContent;
		}
		else if (nodeName == "red")
		{
			label.m_red = (unsigned short)atoi(nodeContent.c_str());
		}
		else if (nodeName == "green")
		{
			label.m_green = (unsigned short)atoi(nodeContent.c_str());
		}
		else if (nodeName == "blue")
		{
			label.m_blue = (unsigned short)atoi(nodeContent.c_str());
		}
	}

	if (label.m_name.empty() == false)
	{
		m_labels.insert(label);
	}

	return true;
}
Exemple #8
0
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;
}
bool PinotSettings::loadProxy(const Element *pElem)
{
	if (pElem == NULL)
	{
		return false;
	}

	Node::NodeList childNodes = pElem->get_children();
	if (childNodes.empty() == true)
	{
		return false;
	}

	for (Node::NodeList::iterator iter = childNodes.begin(); iter != childNodes.end(); ++iter)
	{
		Node *pNode = (*iter);
		Element *pChildElem = dynamic_cast<Element*>(pNode);
		if (pChildElem == NULL)
		{
			continue;
		}

		string nodeName(pChildElem->get_name());
		string nodeContent(getElementContent(pChildElem));

		if (nodeName == "address")
		{
			m_proxyAddress = nodeContent;
		}
		else if (nodeName == "port")
		{
			m_proxyPort = (unsigned int)atoi(nodeContent.c_str());
		}
		else if (nodeName == "type")
		{
			m_proxyType = nodeContent;
		}
		else if (nodeName == "enable")
		{
			if (nodeContent == "YES")
			{
				m_proxyEnabled = true;
			}
			else
			{
				m_proxyEnabled = false;
			}
		}	
	}

	return true;
}
bool PinotSettings::loadIndexableLocations(const Element *pElem)
{
	if (pElem == NULL)
	{
		return false;
	}

	Node::NodeList childNodes = pElem->get_children();
	if (childNodes.empty() == true)
	{
		return false;
	}

	IndexableLocation location;

	// Load the indexable location's properties
	for (Node::NodeList::iterator iter = childNodes.begin(); iter != childNodes.end(); ++iter)
	{
		Node *pNode = (*iter);
		Element *pElem = dynamic_cast<Element*>(pNode);
		if (pElem == NULL)
		{
			continue;
		}

		string nodeName = pElem->get_name();
		string nodeContent = getElementContent(pElem);

		if (nodeName == "name")
		{
			location.m_name = nodeContent;
		}
		else if (nodeName == "monitor")
		{
			if (nodeContent == "YES")
			{
				location.m_monitor = true;
			}
			else
			{
				location.m_monitor = false;
			}
		}
	}

	if (location.m_name.empty() == false)
	{
		m_indexableLocations.insert(location);
	}

	return true;
}
Exemple #11
0
void Character::fromXML(const xmlpp::Element &root)
{
    using namespace xmlpp;

    clearSkills();
    Node::NodeList list = root.get_children("skill");
    for (Node::NodeList::const_iterator it = list.begin(); it != list.end(); it++)
    {
        Element *elem = dynamic_cast<Element *>(*it);
        string value;
        Attribute *attr = elem->get_attribute("value");
        if (attr != NULL)
        {
            value = attr->get_value();
        }
        vSkills.push_back(value);
    }
}
Exemple #12
0
void Character::fromXML(const IOConfig &config, const xmlpp::Element &root)
{
    using namespace xmlpp;

    clearProperties();
    Node::NodeList list = root.get_children(config.propertyName());
    for (Node::NodeList::const_iterator it = list.begin(); it != list.end(); it++)
    {
        Element *elem = dynamic_cast<Element *>(*it);
        string value;
        Attribute *attr = elem->get_attribute("value");
        if (attr)
        {
            value = attr->get_value();
        }
        vProperties.push_back(value);
    }
}
bool PinotSettings::loadIndexes(const Element *pElem)
{
	if (pElem == NULL)
	{
		return false;
	}

	Node::NodeList childNodes = pElem->get_children();
	if (childNodes.empty() == true)
	{
		return false;
	}

	string indexName, indexLocation;

	for (Node::NodeList::iterator iter = childNodes.begin(); iter != childNodes.end(); ++iter)
	{
		Node *pNode = (*iter);
		Element *pElem = dynamic_cast<Element*>(pNode);
		if (pElem == NULL)
		{
			continue;
		}

		string nodeName = pElem->get_name();
		string nodeContent = getElementContent(pElem);
		if (nodeName == "name")
		{
			indexName = nodeContent;
		}
		else if (nodeName == "location")
		{
			indexLocation = nodeContent;
		}
	}

	if ((indexName.empty() == false) &&
		(indexLocation.empty() == false))
	{
		addIndex(indexName, indexLocation);
	}

	return true;
}
bool PinotSettings::loadColour(const Element *pElem)
{
	if (pElem == NULL)
	{
		return false;
	}

	Node::NodeList childNodes = pElem->get_children();
	if (childNodes.empty() == true)
	{
		return false;
	}

	// Load the colour RGB components
	for (Node::NodeList::iterator iter = childNodes.begin(); iter != childNodes.end(); ++iter)
	{
		Node *pNode = (*iter);
		Element *pElem = dynamic_cast<Element*>(pNode);
		if (pElem == NULL)
		{
			continue;
		}

		string nodeName = pElem->get_name();
		string nodeContent = getElementContent(pElem);
		gushort value = (gushort)atoi(nodeContent.c_str());

		if (nodeName == "red")
		{
			m_newResultsColourRed = value;
		}
		else if (nodeName == "green")
		{
			m_newResultsColourGreen = value;
		}
		else if (nodeName == "blue")
		{
			m_newResultsColourBlue = value;
		}
	}

	return true;
}
bool PinotSettings::loadResults(const Element *pElem)
{
	if (pElem == NULL)
	{
		return false;
	}

	Node::NodeList childNodes = pElem->get_children();
	if (childNodes.empty() == true)
	{
		return false;
	}

	for (Node::NodeList::iterator iter = childNodes.begin(); iter != childNodes.end(); ++iter)
	{
		Node *pNode = (*iter);
		Element *pElem = dynamic_cast<Element*>(pNode);
		if (pElem == NULL)
		{
			continue;
		}

		string nodeName = pElem->get_name();
		string nodeContent = getElementContent(pElem);
		if (nodeName == "viewmode")
		{
			if (nodeContent == "SOURCE")
			{
				m_browseResults = false;
			}
			else
			{
				m_browseResults = true;
			}
		}
		else if (nodeName == "browser")
		{
			m_browserCommand = nodeContent;
		}
	}

	return true;
}
Exemple #16
0
void FileItem::fromXML(const xmlpp::Element &root) throw(xmlpp::exception, invalid_argument, overflow_error)
{
    using namespace xmlpp;
    
    Node::NodeList list = root.get_children("file");
    string name = "";
    if (list.size()==0)
    {
        throw xmlpp::exception("Missing file name");
    }
    else
    {
        Element *tmp = dynamic_cast<Element*>(list.front());
        Attribute *attr = tmp->get_attribute("name");
        if (attr!=NULL)
        {
            name = attr->get_value();
        }
    }
    setFileName(name);
}
static ustring getNodeContent(const Node *pNode)
{
	if (pNode == NULL)
	{
		return "";
	}

	// Is it an element ?
	const Element *pElem = dynamic_cast<const Element*>(pNode);
	if (pElem != NULL)
	{
#ifdef HAS_LIBXMLPP026
		const TextNode *pText = pElem->get_child_content();
#else
		const TextNode *pText = pElem->get_child_text();
#endif
		if (pText == NULL)
		{
			// Maybe the text is given as CDATA
			const Node::NodeList childNodes = pNode->get_children();
			if (childNodes.size() == 1)
			{
				// Is it CDATA ?
				const CdataNode *pContent = dynamic_cast<const CdataNode*>(*childNodes.begin());
				if (pContent != NULL)
				{
					return pContent->get_content();
				}
			}

			return "";
		}

		return pText->get_content();
	}

	return "";
}
string XmlReader::getTextFromElement(const Element* elementNode, const string& childName, bool required) const	{
    string text;
    const Node::NodeList children = elementNode->get_children(childName);
    if (children.size() == 1)	{
        const Element *elementChild = castToElement(children.front());
        for (const Node *child : elementChild->get_children())	{
            const TextNode* textNode = dynamic_cast<const TextNode*>(child);
            if (textNode != nullptr)	{
                if (!textNode->is_white_space())
                    text += textNode->get_content();
            } else {
                throw InvalidDatasetFile(caller(), "Invalid cast to 'TextNode*' type!", (child != nullptr ? child->get_line() : -1));
            }
        }
    } else if (children.size() > 1)	{
        throw InvalidDatasetFile(caller(), "Only from one child the text can be retrieved!", elementNode->get_line());
    } else if (children.empty() && required)	{
        string msg = "Cannot find the '"+string(childName)+"' element!\n";
        throw InvalidDatasetFile(caller(), msg+"Invalid input xml file!", elementNode->get_line());
    }

    return text;
}
bool PinotSettings::loadEngineChannels(const Element *pElem)
{
	if (pElem == NULL)
	{
		return false;
	}

	Node::NodeList childNodes = pElem->get_children();
	if (childNodes.empty() == true)
	{
		return false;
	}

	for (Node::NodeList::iterator iter = childNodes.begin(); iter != childNodes.end(); ++iter)
	{
		Node *pNode = (*iter);
		Element *pElem = dynamic_cast<Element*>(pNode);
		if (pElem == NULL)
		{
			continue;
		}

		string nodeName = pElem->get_name();
		string nodeContent = getElementContent(pElem);
		if (nodeName == "name")
		{
			std::map<string, bool>::iterator channelIter = m_engineChannels.find(nodeContent);

			if (channelIter != m_engineChannels.end())
			{
				channelIter->second = false;
			}
		}
	}

	return true;
}
Exemple #20
0
/** Process received string. */
void
Msl2010RefBoxProcessor::process_string(char *buf, size_t len)
{
  __logger->log_info(__name, "Received\n *****\n %s \n *****", buf);

  std::istringstream iss( std::string(buf), std::istringstream::in);

  dom = new DomParser();
  //dom->set_validate();
  dom->set_substitute_entities();
  dom->parse_stream(iss);
  root = dom->get_document()->get_root_node();

  //printf( " root node:\n%s\n", root->get_name().data() );

  const Element * el = dynamic_cast<const Element *>(root);

  if ( el ) {
    /// valid element
    //printf("Is valid Element\n");
    __logger->log_info(__name, "root-element name is '%s'", el->get_name().data() );

    const Node::NodeList nl = el->get_children();

    if( nl.size() == 0 ) {
      __logger->log_info(__name, "root has NO children!");
    }
    else {
      //printf("root has %u children!\n", nl.size());

      for (Node::NodeList::const_iterator it = nl.begin(); it != nl.end(); ++it) {
	const Node* node = *it;
	__logger->log_info(__name, "1st level child name is '%s'", node->get_name().data() );

	//if( node->get_name().data() == REFBOX_GAMEINFO ) {
	//
	//}
	//else if( node->get_name().data() == REFBOX_EVENT ) {
	//
	//}
	//else {
	//  printf(" unhandled RefboxMessage-type '%s'!\n", node->get_name().data() );
	//}
	
	const Node::NodeList cnl = node->get_children();

	if( cnl.size() == 0 ) {
	  __logger->log_info(__name, "child has NO children!");
	}
	else {
	  //printf("child has %u children!\n", nl.size());
	  
	  for (Node::NodeList::const_iterator cit = cnl.begin(); cit != cnl.end(); ++cit) {
	    const Node*  cnode = *cit;
	    const Element* cel = dynamic_cast<const Element *>(cnode);
	    std::string cnodename(cnode->get_name().data());

	    __logger->log_info(__name, "2nd level child name is '%s'", cnode->get_name().data() );

	    const Attribute* cattr;
	    std::string cteamcolor;
	    //std::string cgoalcolor;
	    //std::string ccardcolor;
	    std::string cstagetype;

	    if( cnodename == REFBOX_KICKOFF      || cnodename == REFBOX_FREEKICK     ||
		cnodename == REFBOX_GOALKICK     || cnodename == REFBOX_THROWIN      ||
		cnodename == REFBOX_CORNER       || cnodename == REFBOX_PENALTY      ||
		cnodename == REFBOX_GOAL_AWARDED || cnodename == REFBOX_GOAL_REMOVED ||
		cnodename == REFBOX_CARD_AWARDED || cnodename == REFBOX_CARD_REMOVED ||		
		cnodename == REFBOX_PLAYER_OUT   || cnodename == REFBOX_PLAYER_IN    ||
		cnodename == REFBOX_SUBSTITUTION ) 
	      {
		cattr = cel->get_attribute("team");
		cteamcolor = std::string( cattr->get_value().data() );
	      }

 	    if( cnodename == REFBOX_CANCEL ) {
 	      // refbox canceled last command
	      __logger->log_info(__name, "RefBox cancelled last command");
 	    }
 	    else if( cnodename == REFBOX_GAMESTOP ) {
 	      _rsh->set_gamestate(GS_FROZEN, TEAM_BOTH);
 	    }
 	    else if( cnodename == REFBOX_GAMESTART ) {
 	      _rsh->set_gamestate(GS_PLAY, TEAM_BOTH);
 	    }
	    else if( cnodename == REFBOX_DROPPEDBALL ) {
	      _rsh->set_gamestate(GS_DROP_BALL, TEAM_BOTH);
	    }
	    else if( cnodename == REFBOX_GOAL_AWARDED ) {
	      // increment according to color
	      if( cteamcolor == REFBOX_TEAMCOLOR_CYAN ) {
		_rsh->set_score(++__score_cyan, __score_magenta);
	      } 
	      else if ( cteamcolor == REFBOX_TEAMCOLOR_MAGENTA ) {
		_rsh->set_score(__score_cyan, ++__score_magenta);
	      }
	      _rsh->set_gamestate(GS_FROZEN, TEAM_BOTH);
	    }
	    else if( cnodename == REFBOX_KICKOFF ) {
	      if( cteamcolor == REFBOX_TEAMCOLOR_CYAN ) {
		_rsh->set_gamestate(GS_KICK_OFF, TEAM_CYAN);
	      } 
	      else if ( cteamcolor == REFBOX_TEAMCOLOR_MAGENTA ) {
		_rsh->set_gamestate(GS_KICK_OFF, TEAM_MAGENTA);
	      }
	    }
	    else if( cnodename == REFBOX_PENALTY ) {
	      if( cteamcolor == REFBOX_TEAMCOLOR_CYAN ) {
		_rsh->set_gamestate(GS_PENALTY, TEAM_CYAN);
	      } 
	      else if ( cteamcolor == REFBOX_TEAMCOLOR_MAGENTA ) {
		_rsh->set_gamestate(GS_PENALTY, TEAM_MAGENTA);
	      }
	    }
	    else if( cnodename == REFBOX_CORNER ) {
	      if( cteamcolor == REFBOX_TEAMCOLOR_CYAN ) {
		_rsh->set_gamestate(GS_CORNER_KICK, TEAM_CYAN);
	      } 
	      else if ( cteamcolor == REFBOX_TEAMCOLOR_MAGENTA ) {
		_rsh->set_gamestate(GS_CORNER_KICK, TEAM_MAGENTA);
	      }
	    }
	    else if( cnodename == REFBOX_THROWIN ) {
	      if( cteamcolor == REFBOX_TEAMCOLOR_CYAN ) {
		_rsh->set_gamestate(GS_THROW_IN, TEAM_CYAN);
	      } 
	      else if ( cteamcolor == REFBOX_TEAMCOLOR_MAGENTA ) {
		_rsh->set_gamestate(GS_THROW_IN, TEAM_MAGENTA);
	      }
	    }
	    else if( cnodename == REFBOX_FREEKICK ) {
	      if( cteamcolor == REFBOX_TEAMCOLOR_CYAN ) {
		_rsh->set_gamestate(GS_FREE_KICK, TEAM_CYAN);
	      } 
	      else if ( cteamcolor == REFBOX_TEAMCOLOR_MAGENTA ) {
		_rsh->set_gamestate(GS_FREE_KICK, TEAM_MAGENTA);
	      }
	    }
	    else if( cnodename == REFBOX_GOALKICK ) {
	      if( cteamcolor == REFBOX_TEAMCOLOR_CYAN ) {
		_rsh->set_gamestate(GS_GOAL_KICK, TEAM_CYAN);
	      } 
	      else if ( cteamcolor == REFBOX_TEAMCOLOR_MAGENTA ) {
		_rsh->set_gamestate(GS_GOAL_KICK, TEAM_MAGENTA);
	      }
	    }
	    else if( cnodename == REFBOX_STAGE_CHANGED ) {
	      cattr = cel->get_attribute("newStage");
	      cstagetype = std::string( cattr->get_value().data() );
	      if( cstagetype == REFBOX_STAGETYPE_PREGAME ) {
		//
	      } else if( cstagetype == REFBOX_STAGETYPE_FIRSTHALF ) {
		_rsh->set_half(HALF_FIRST);
	      } else if( cstagetype == REFBOX_STAGETYPE_HALFTIME ) {
		_rsh->set_gamestate(GS_HALF_TIME, TEAM_BOTH);
	      } else if( cstagetype == REFBOX_STAGETYPE_SECONDHALF ) {
		_rsh->set_half(HALF_SECOND);
	      } else if( cstagetype == REFBOX_STAGETYPE_SHOOTOUT ) {
		//
	      } else if( cstagetype == REFBOX_STAGETYPE_ENDGAME ) {
		//
	      }

	    }

	  } // end-for "child-node children list iteration"
	} // end-if "child-node has children"
      } // end-for "root children list iteration"
    } // end-if "root has children"
  }
  else {
    // throw RefBoxParserException("root is not an element");
    __logger->log_info(__name, "root is NOT a valid element");
  }

}
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;
}
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;
}
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);
}
Exemple #24
0
  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::loadQueries(const Element *pElem)
{
	if (pElem == NULL)
	{
		return false;
	}

	Node::NodeList childNodes = pElem->get_children();
	if (childNodes.empty() == true)
	{
		return false;
	}

	QueryProperties queryProps;
	Date minDate, maxDate;
	string freeQuery;
	bool enableMinDate = false, enableMaxDate = false;

	// Load the query's properties
	for (Node::NodeList::iterator iter = childNodes.begin(); iter != childNodes.end(); ++iter)
	{
		Node *pNode = (*iter);
		Element *pChildElem = dynamic_cast<Element*>(pNode);
		if (pChildElem == NULL)
		{
			continue;
		}

		string nodeName(pChildElem->get_name());
		string nodeContent(getElementContent(pChildElem));
		if (nodeName == "name")
		{
			queryProps.setName(nodeContent);
		}
		else if (nodeName == "sortorder")
		{
			if (nodeContent == "DATE")
			{
				queryProps.setSortOrder(QueryProperties::DATE);
			}
			else
			{
				queryProps.setSortOrder(QueryProperties::RELEVANCE);
			}
		}
		else if (nodeName == "text")
		{
			freeQuery = nodeContent;
		}
		else if ((nodeName == "and") &&
			(nodeContent.empty() == false))
		{
			if (freeQuery.empty() == false)
			{
				freeQuery += " ";
			}
			freeQuery += nodeContent;
		}
		else if ((nodeName == "phrase") &&
			(nodeContent.empty() == false))
		{
			if (freeQuery.empty() == false)
			{
				freeQuery += " ";
			}
			freeQuery += "\"";
			freeQuery += nodeContent;
			freeQuery += "\"";
		}
		else if ((nodeName == "any") &&
			(nodeContent.empty() == false))
		{
			// FIXME: don't be lazy and add those correctly
			if (freeQuery.empty() == false)
			{
				freeQuery += " ";
			}
			freeQuery += nodeContent;
		}
		else if ((nodeName == "not") &&
			(nodeContent.empty() == false))
		{
			if (freeQuery.empty() == false)
			{
				freeQuery += " ";
			}
			freeQuery += "-(";
			freeQuery += nodeContent;
			freeQuery += ")";
		}
		else if ((nodeName == "language") &&
			(nodeContent.empty() == false))
		{
			if (freeQuery.empty() == false)
			{
				freeQuery += " ";
			}
			freeQuery += "lang:";
			freeQuery += nodeContent;
		}
		else if ((nodeName == "stemlanguage") &&
			(nodeContent.empty() == false))
		{
			queryProps.setStemmingLanguage(Languages::toLocale(nodeContent));
		}
		else if ((nodeName == "hostfilter") &&
			(nodeContent.empty() == false))
		{
			if (freeQuery.empty() == false)
			{
				freeQuery += " ";
			}
			freeQuery += "site:";
			freeQuery += nodeContent;
		}
		else if ((nodeName == "filefilter") &&
			(nodeContent.empty() == false))
		{
			if (freeQuery.empty() == false)
			{
				freeQuery += " ";
			}
			freeQuery += "file:";
			freeQuery += nodeContent;
		}
		else if ((nodeName == "labelfilter") &&
			(nodeContent.empty() == false))
		{
			if (freeQuery.empty() == false)
			{
				freeQuery += " ";
			}
			freeQuery += "label:";
			freeQuery += nodeContent;
		}
		else if (nodeName == "maxresults")
		{
			int count = atoi(nodeContent.c_str());
			queryProps.setMaximumResultsCount((unsigned int)max(count, 10));
		}
		else if (nodeName == "enablemindate")
		{
			if (nodeContent == "YES")
			{
				enableMinDate = true;
			}
		}
		else if (nodeName == "mindate")
		{
			minDate.set_parse(nodeContent);
		}
		else if (nodeName == "enablemaxdate")
		{
			if (nodeContent == "YES")
			{
				enableMaxDate = true;
			}
		}
		else if (nodeName == "maxdate")
		{
			maxDate.set_parse(nodeContent);
		}
		else if (nodeName == "index")
		{
			if (nodeContent == "NEW")
			{
				queryProps.setIndexResults(QueryProperties::NEW_RESULTS);
			}
			else if (nodeContent == "ALL")
			{
				queryProps.setIndexResults(QueryProperties::ALL_RESULTS);
			}
			else
			{
				queryProps.setIndexResults(QueryProperties::NOTHING);
			}
		}
		else if (nodeName == "label")
		{
			queryProps.setLabelName(nodeContent);
		}
		else if (nodeName == "modified")
		{
			if (nodeContent == "YES")
			{
				queryProps.setModified(true);
			}
		}
	}

	// Are pre-0.80 dates specified ?
	if ((enableMinDate == true) ||
		(enableMaxDate == true))
	{
		// Provide reasonable defaults
		if (enableMinDate == false)
		{
			minDate.set_day(1);
			minDate.set_month(Date::JANUARY);
			minDate.set_year(1970);
		}
		if (enableMaxDate == false)
		{
			maxDate.set_day(31);
			maxDate.set_month(Date::DECEMBER);
			maxDate.set_year(2099);
		}

		ustring minDateStr(minDate.format_string("%Y%m%d"));
		ustring maxDateStr(maxDate.format_string("%Y%m%d"));

#ifdef DEBUG
		cout << "PinotSettings::loadQueries: date range " << minDateStr << " to " << maxDateStr << endl;
#endif
		freeQuery += " ";
		freeQuery += minDateStr;
		freeQuery += "..";
		freeQuery += maxDateStr;
	}

	// We need at least a name
	if (queryProps.getName().empty() == false)
	{
		if (freeQuery.empty() == false)
		{
			queryProps.setFreeQuery(freeQuery);
		}
		m_queries[queryProps.getName()] = queryProps;
	}

	return true;
}
void parse_node( const Node& in_node, OutputState& state )
{
	if ( dynamic_cast<const ContentNode*>( &in_node ) != NULL )
	{
		const ContentNode & content = dynamic_cast<const ContentNode &>( in_node );
		state.put_text( content.get_content() );
	}
	else
	{
		if ( dynamic_cast<const TextNode*>( &in_node ) != NULL )
		{
			const TextNode & text = dynamic_cast<const TextNode &>( in_node );
			if ( text.is_white_space() ) {
				return;
			}
		} else if ( dynamic_cast<const Element*>( &in_node ) != NULL )
		{
			const Element & element = dynamic_cast<const Element&>( in_node );
			OutputState* out = NULL;
			string name = element.get_name();
			if ( name == "b" )
			{
				out = state.bold();
			} else if ( name == "math" )
			{
				out = state.math();
			} else if ( name == "plot" )
			{
				out = state.plot();
			} else if ( ( name == "section" ) || ( name == "subsection" ) || ( name == "subsubsection" ) )
			{
				string section_name = element.get_attribute_value( "name" );
				if ( section_name.length() == 0 )
				{
					throw runtime_error( "Sections must have a name attribute" );
				}
				unsigned int level;
				if ( name == "subsection" )
				{
					level = 1;
				} else if ( name == "subsubsection" )
				{
					level = 2;
				} else
				{
					level = 0;
				}
				out = state.section( section_name, level );
			}
			else
			{
				stringstream ss;
				ss << "Unknown element with name \"" << name << "\" found!" << endl;
				throw runtime_error( ss.str().c_str() );
			}
			Node::NodeList list = in_node.get_children();
			for ( Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter )
			{
				if ( *iter != NULL )
				{
					parse_node( **iter, *out );
				}
			}
			out->finish();
			delete out;
		}
	}
}
bool PinotSettings::loadCacheProviders(const Element *pElem)
{
	if (pElem == NULL)
	{
		return false;
	}

	Node::NodeList childNodes = pElem->get_children();
	if (childNodes.empty() == true)
	{
		return false;
	}

	CacheProvider cacheProvider;

	// Load the cache provider's properties
	for (Node::NodeList::iterator iter = childNodes.begin(); iter != childNodes.end(); ++iter)
	{
		Node *pNode = (*iter);
		Element *pElem = dynamic_cast<Element*>(pNode);
		if (pElem == NULL)
		{
			continue;
		}

		string nodeName = pElem->get_name();
		string nodeContent = getElementContent(pElem);

		if (nodeName == "name")
		{
			cacheProvider.m_name = nodeContent;
		}
		else if (nodeName == "location")
		{
			cacheProvider.m_location = nodeContent;
		}
		else if (nodeName == "protocols")
		{
			nodeContent += ",";

			ustring::size_type previousPos = 0, commaPos = nodeContent.find(",");
			while (commaPos != ustring::npos)
			{
				string protocol(nodeContent.substr(previousPos,
                                        commaPos - previousPos));

				StringManip::trimSpaces(protocol);
				cacheProvider.m_protocols.insert(protocol);

				// Next
				previousPos = commaPos + 1;
				commaPos = nodeContent.find(",", previousPos);
			}
		}
	}

	if ((cacheProvider.m_name.empty() == false) &&
		(cacheProvider.m_location.empty() == false))
	{
		m_cacheProviders.push_back(cacheProvider);

		// Copy the list of protocols supported by this cache provider
		copy(cacheProvider.m_protocols.begin(), cacheProvider.m_protocols.end(),
			inserter(m_cacheProtocols, m_cacheProtocols.begin()));
	}

	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;
}
Exemple #29
0
void Scenario::fromFile(const std::string &fileName, bool checkFiles) throw(xmlpp::exception, invalid_argument)
{
    using namespace xmlpp;
    using namespace Poco;
    string xmlFile, fileType;
    bool isArchive = false;

    clear();
    if (pDetector)
    {
       fileType = pDetector->typeOfFile(fileName);
    }
    if (fileType == "text/xml" || fileType == "application/xml")
    {
        xmlFile = fileName;
    }
    else if (!pDetector || fileType == "application/zip")
    {
        // attempt to unzip
        FileInputStream input(fileName.c_str());
        if (input.good())
        {
            // creating new temporary directory and extracting into it
            sTempDir = TemporaryFile::tempName();
            Zip::Decompress dec(input, sTempDir);
            TemporaryFile::registerForDeletion(sTempDir);
            // valid extraction
            isArchive = false;
            try
            {
                dec.decompressAllFiles();
                isArchive = true;
            }
            catch (Poco::Exception)
            {
                if (!pDetector)
                {
                    xmlFile = fileName;
                }
                else
                {
                    throw xmlpp::exception("Bad file format");
                }
            }
            if (isArchive)
            {
                Zip::Decompress::ZipMapping mapping = dec.mapping();
                try
                {
                    xmlFile = mapping.at("scenario.xml").makeAbsolute(sTempDir).toString();
                }
                catch(out_of_range &e)
                {
                    throw xmlpp::exception("No scenario in file " + fileName);
                }
            }
        }
        else
        {
            throw xmlpp::exception("Unable to open the file " + fileName);
        }
    }
    else
    {
        throw xmlpp::exception("Unrecognized file format");
    }
    ioConfig = IOConfig::detect(xmlFile, isArchive);
    DomParser parser(xmlFile);
    Document *document = parser.get_document();
    Element *root = document->get_root_node();
    if (root->get_name() != ioConfig.rootName())
    {
        throw xmlpp::exception("Bad document content type: " + ioConfig.rootName() + " expected");
    }
    // getting the user interface
    try
    {
        Attribute *attr = root->get_attribute("interface");
        if (attr)
        {
            uiInterface = stringToInterface(attr->get_value());
        }
        else
        {
            uiInterface = uiFull;
        }
    }
    catch (invalid_argument)
    {
        throw xmlpp::exception("Bad user interface");
    }
    // now loading the different parts of the game
    Node::NodeList node;
    if (ioConfig.hasMetadata())
    {
        node = root->get_children("metadata");
        if (!node.empty())
        {
            mMetadata.fromXML(*dynamic_cast<Element*>(node.front()));
        }
    }
    node = root->get_children(ioConfig.plotName());
    if (node.empty())
    {
        throw xmlpp::exception("Missing \"" + ioConfig.plotName() + "\" section");
    }
    else
    {
        tPlot.fromXML(ioConfig, *dynamic_cast<Element*>(node.front()), checkFiles);
    }
    node = root->get_children("notes");
    if (node.empty())
    {
        throw xmlpp::exception("Missing \"notes\" section");
    }
    else
    {
        Element *elem = dynamic_cast<Element*>(node.front());
        string mainText = "";
        if (elem->has_child_text())
        {
            mainText = elem->get_child_text()->get_content();
        }
        nMain = Note("", mainText);
    }
    node = root->get_children(ioConfig.propertiesName());
    if (node.empty())
    {
        throw xmlpp::exception("Missing \"" + ioConfig.propertiesName() + "\" section");
    }
    else
    {
        lProperties.fromXML(ioConfig, *dynamic_cast<Element*>(node.front()));
    }
    node = root->get_children("characters");
    if (node.empty())
    {
        throw xmlpp::exception("Missing \"characters\" section");
    }
    else
    {
        lCharacters.fromXML(ioConfig, *dynamic_cast<Element*>(node.front()));
    }
    node = root->get_children("history");
    if (node.empty())
    {
        throw xmlpp::exception("Missing \"history\" section");
    }
    else
    {
        tHistory.fromXML(ioConfig, *dynamic_cast<Element*>(node.front()), checkFiles);
    }
    node = root->get_children("music");
    if (node.empty())
    {
        throw xmlpp::exception("Missing \"music\" section");
    }
    else
    {
        tMusic.fromXML(ioConfig, *dynamic_cast<Element*>(node.front()), checkFiles);
    }
    node = root->get_children("effects");
    if (node.empty())
    {
        throw xmlpp::exception("Missing \"effects\" section");
    }
    else
    {
        tEffects.fromXML(ioConfig, *dynamic_cast<Element*>(node.front()), checkFiles);
    }
}
bool PinotSettings::loadQueries(const Element *pElem)
{
	if (pElem == NULL)
	{
		return false;
	}

	Node::NodeList childNodes = pElem->get_children();
	if (childNodes.empty() == true)
	{
		return false;
	}

	QueryProperties queryProps;

	// Load the query's properties
	for (Node::NodeList::iterator iter = childNodes.begin(); iter != childNodes.end(); ++iter)
	{
		Node *pNode = (*iter);
		Element *pElem = dynamic_cast<Element*>(pNode);
		if (pElem == NULL)
		{
			continue;
		}

		string nodeName = pElem->get_name();
		string nodeContent = getElementContent(pElem);
		if (nodeName == "name")
		{
			queryProps.setName(nodeContent);
		}
		else if (nodeName == "and")
		{
			queryProps.setAndWords(nodeContent);
		}
		else if (nodeName == "phrase")
		{
			queryProps.setPhrase(nodeContent);
		}
		else if (nodeName == "any")
		{
			queryProps.setAnyWords(nodeContent);
		}
		else if (nodeName == "not")
		{
			queryProps.setNotWords(nodeContent);
		}
		else if (nodeName == "language")
		{
			queryProps.setLanguage(Languages::toLocale(nodeContent));
		}
		else if (nodeName == "hostfilter")
		{
			queryProps.setHostFilter(nodeContent);
		}
		else if (nodeName == "filefilter")
		{
			queryProps.setFileFilter(nodeContent);
		}
		else if (nodeName == "labelfilter")
		{
			queryProps.setLabelFilter(nodeContent);
		}
		else if (nodeName == "maxresults")
		{
			int count = atoi(nodeContent.c_str());
			queryProps.setMaximumResultsCount((unsigned int)max(count, 10));
		}
		else if (nodeName == "index")
		{
			if (nodeContent == "ALL")
			{
				queryProps.setIndexResults(true);
			}
			else
			{
				queryProps.setIndexResults(false);
			}
		}
		else if (nodeName == "label")
		{
			queryProps.setLabelName(nodeContent);
		}
	}

	// We need at least a name
	if (queryProps.getName().empty() == false)
	{
		m_queries[queryProps.getName()] = queryProps;
	}

	return true;
}