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;
}
示例#2
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 "";
}
示例#4
0
文件: msl2010.cpp 项目: timn/fawkes
/** 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");
  }

}
示例#5
0
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);
}