Example #1
0
void ThingTypeManager::loadXml(const std::string& file)
{
    try {
        if(!isOtbLoaded())
            stdext::throw_exception("OTB must be loaded before XML");

        TiXmlDocument doc;
        doc.Parse(g_resources.readFileContents(file).c_str());
        if(doc.Error())
            stdext::throw_exception(stdext::format("failed to parse '%s': '%s'", file, doc.ErrorDesc()));

        TiXmlElement* root = doc.FirstChildElement();
        if(!root || root->ValueTStr() != "items")
            stdext::throw_exception("invalid root tag name");

        for(TiXmlElement *element = root->FirstChildElement(); element; element = element->NextSiblingElement()) {
            if(unlikely(element->ValueTStr() != "item"))
                continue;

            uint16 id = element->readType<uint16>("id");
            if(id != 0) {
                std::vector<std::string> s_ids = stdext::split(element->Attribute("id"), ";");
                for(const std::string& s : s_ids) {
                    std::vector<int32> ids = stdext::split<int32>(s, "-");
                    if(ids.size() > 1) {
                        int32 i = ids[0];
                        while(i <= ids[1])
                            parseItemType(i++, element);
                    } else
                        parseItemType(atoi(s.c_str()), element);
                }
            } else {
                std::vector<int32> begin = stdext::split<int32>(element->Attribute("fromid"), ";");
                std::vector<int32> end   = stdext::split<int32>(element->Attribute("toid"), ";");
                if(begin[0] && begin.size() == end.size()) {
                    size_t size = begin.size();
                    for(size_t i = 0; i < size; ++i)
                        while(begin[i] <= end[i])
                            parseItemType(begin[i]++, element);
                }
            }
        }

        doc.Clear();
        m_xmlLoaded = true;
        g_logger.debug("items.xml read successfully.");
    } catch(std::exception& e) {
        g_logger.error(stdext::format("Failed to load '%s' (XML file): %s", file, e.what()));
    }
}
Example #2
0
bool loadXMLFile (TiXmlDocument &pXMLDoc, std::string XMLFilename, std::map<int,
                  std::string> * pMapXmlStrings, bool isSourceFile)
{
  if (!pXMLDoc.LoadFile(XMLFilename.c_str()))
  {
    printf ("%s %s\n", pXMLDoc.ErrorDesc(), XMLFilename.c_str());
    return false;
  }
  if (isSourceFile) GetEncoding(&pXMLDoc, sourceXMLEncoding);
    else GetEncoding(&pXMLDoc, foreignXMLEncoding);

  TiXmlElement* pRootElement = pXMLDoc.RootElement();
  if (!pRootElement || pRootElement->NoChildren() || pRootElement->ValueTStr()!="strings")
  {
    printf ("error: No root element called: \"strings\" or no child found in input XML file: %s\n",
            XMLFilename.c_str());
    return false;
  }

  if (isSourceFile) GetComment(pRootElement->FirstChild(), -1);

  const TiXmlElement *pChildElement = pRootElement->FirstChildElement("string");
  const char* pAttrId = NULL;
  const char* pValue = NULL;
  std::string valueString;
  int id;

  while (pChildElement)
  {
    pAttrId=pChildElement->Attribute("id");
    if (pAttrId && !pChildElement->NoChildren())
    {
      id = atoi(pAttrId);
      if (pMapXmlStrings->find(id) == pMapXmlStrings->end())
      {
        pValue = pChildElement->FirstChild()->Value();
        valueString = EscapeLF(pValue);
        if (isSourceFile)
          multimapSourceXmlStrings.insert(std::pair<std::string,int>( valueString,id));

        (*pMapXmlStrings)[id] = valueString;

        if (pChildElement && isSourceFile) GetComment(pChildElement->NextSibling(), id);
      }
    }
    pChildElement = pChildElement->NextSiblingElement("string");
  }
  // Free up the allocated memory for the XML file
  pXMLDoc.Clear();
  return true;
}
Example #3
0
my_shared_ptr<Geometry> parseGeometry(TiXmlElement *g)
{
  my_shared_ptr<Geometry> geom;
  if (!g) return geom;

  TiXmlElement *shape = g->FirstChildElement();
  if (!shape)
  {
    logError("Geometry tag contains no child element.");
    return geom;
  }

  const std::string type_name = shape->ValueTStr().c_str();
  if (type_name == "sphere")
  {
    Sphere *s = new Sphere();
    geom.reset(s);
    if (parseSphere(*s, shape))
      return geom;
  }
  else if (type_name == "box")
  {
    Box *b = new Box();
    geom.reset(b);
    if (parseBox(*b, shape))
      return geom;
  }
  else if (type_name == "cylinder")
  {
    Cylinder *c = new Cylinder();
    geom.reset(c);
    if (parseCylinder(*c, shape))
      return geom;
  }
  else if (type_name == "mesh")
  {
    Mesh *m = new Mesh();
    geom.reset(m);
    if (parseMesh(*m, shape))
      return geom;    
  }
  else
  {
    logError("Unknown geometry type '%s'", type_name.c_str());
    return geom;
  }
  
  return my_shared_ptr<Geometry>();
}
//读取Rss文件的信息。
//传入参数 本地暂存文件的绝对路径。
bool ReadXmlFile(TCHAR* lpszTempRssXmlPath, string& rssFeedTitle, map<string, RssContent>& mXML)
{
	string Title;
	RssContent Con;
	memset(&Con, 0, sizeof(Con));
	char* cszTempRssXmlPath = FileEncode::UnicodeToAnsi((char*)lpszTempRssXmlPath);
	TiXmlDocument *myDocument = new TiXmlDocument(cszTempRssXmlPath);
	free(cszTempRssXmlPath);
	myDocument->LoadFile();
	TiXmlElement *RootElement = myDocument->RootElement();
	TiXmlElement *FirstElement = RootElement->FirstChildElement();
	TiXmlElement *MyElement = FirstElement->FirstChildElement();
	rssFeedTitle = MyElement->GetText();
	while (MyElement != NULL)
	{
		while ((MyElement->ValueTStr()) != "item")
		{
			MyElement = MyElement->NextSiblingElement();
		}
		TiXmlElement *MyElement_1 = MyElement->FirstChildElement();
		while (MyElement_1->ValueTStr() != "title")
		{
			MyElement_1 = MyElement_1->NextSiblingElement();
		}
		Title = MyElement_1->GetText();
		while (MyElement_1->ValueTStr() != "link")
		{
			MyElement_1 = MyElement_1->NextSiblingElement();
		}
		Con.Web = MyElement_1->GetText();
		while (MyElement_1->ValueTStr() != "pubDate")
		{
			MyElement_1 = MyElement_1->NextSiblingElement();
		}
		Con.Date = MyElement_1->GetText();
		while (MyElement_1->ValueTStr() != "description")
		{
			MyElement_1 = MyElement_1->NextSiblingElement();
		}
		Con.Description = MyElement_1->GetText();
		mXML.insert(pair<string, RssContent>(Title, Con));
		MyElement = MyElement->NextSiblingElement();
	}
	return true;
}
Example #5
0
bool UrdfParser::parseGeometry(UrdfGeometry& geom, TiXmlElement* g, ErrorLogger* logger)
{
	btAssert(g);
		
	TiXmlElement *shape = g->FirstChildElement();
	if (!shape)
	{
		logger->reportError("Geometry tag contains no child element.");
		return false;
	}
		
	const std::string type_name = shape->ValueTStr().c_str();
	if (type_name == "sphere")
	{
		geom.m_type = URDF_GEOM_SPHERE;
		if (!shape->Attribute("radius"))
		{
			logger->reportError("Sphere shape must have a radius attribute");
			return false;
		} else
		{
			geom.m_sphereRadius = urdfLexicalCast<double>(shape->Attribute("radius"));
		}
	}	
	else if (type_name == "box")
	{
		geom.m_type = URDF_GEOM_BOX;
        if (m_parseSDF)
        {
            TiXmlElement* size = shape->FirstChildElement("size");
            if (0==size)
            {
                logger->reportError("box requires a size child element");
                return false;
            }
            parseVector3(geom.m_boxSize,size->GetText(),logger);
        }
        else
        {
              if (!shape->Attribute("size"))
              {
                  logger->reportError("box requires a size attribute");
                  return false;
              } else
              {
                  parseVector3(geom.m_boxSize,shape->Attribute("size"),logger);
              }
        }
	}
	else if (type_name == "cylinder")
	{
		geom.m_type = URDF_GEOM_CYLINDER;
		if (!shape->Attribute("length") ||
			!shape->Attribute("radius"))
	  {
		  logger->reportError("Cylinder shape must have both length and radius attributes");
		  return false;
	  }
		geom.m_cylinderRadius = urdfLexicalCast<double>(shape->Attribute("radius"));
		geom.m_cylinderLength = urdfLexicalCast<double>(shape->Attribute("length"));
		
	}
	
  else if (type_name == "mesh")
  {
	  geom.m_type = URDF_GEOM_MESH;
      if (m_parseSDF)
      {
          TiXmlElement* scale = shape->FirstChildElement("scale");
          if (0==scale)
          {
              geom.m_meshScale.setValue(1,1,1);
          }
          else
          {
              parseVector3(geom.m_meshScale,scale->GetText(),logger);
          }
          
          TiXmlElement* filename = shape->FirstChildElement("uri");
          geom.m_meshFileName = filename->GetText();
      }
      else
      {
          if (!shape->Attribute("filename")) {
              logger->reportError("Mesh must contain a filename attribute");
              return false;
          }
          
          geom.m_meshFileName = shape->Attribute("filename");
		  geom.m_meshScale.setValue(1,1,1);

		  if (shape->Attribute("scale"))
          {
              if (!parseVector3(geom.m_meshScale,shape->Attribute("scale"),logger))
			  {
				  logger->reportWarning("scale should be a vector3, not single scalar. Workaround activated.\n");
				  std::string scalar_str = shape->Attribute("scale");
				  double scaleFactor = urdfLexicalCast<double>(scalar_str.c_str());
				  if (scaleFactor)
				  {
					  geom.m_meshScale.setValue(scaleFactor,scaleFactor,scaleFactor);
				  }
			  }
          } else
          {
          }
      }
  }
  else
  {
      if (this->m_parseSDF)
      {
          if (type_name == "plane")
          {
              geom.m_type = URDF_GEOM_PLANE;
             
              TiXmlElement *n = shape->FirstChildElement("normal");
              TiXmlElement *s = shape->FirstChildElement("size");

              if ((0==n)||(0==s))
              {
                  logger->reportError("Plane shape must have both normal and size attributes");
                  return false;
              }
            
              parseVector3(geom.m_planeNormal,n->GetText(),logger);
          }
      } else
      {
          logger->reportError("Unknown geometry type:");
          logger->reportError(type_name.c_str());
          return false;
      }
  }
  
	return true;
}