Ejemplo n.º 1
0
void XMLUtils::LoadXMLFile( ticpp::Document& doc, bool condenseWhiteSpace, const wxString& path )
{
	try
	{
		if ( path.empty() )
		{
			THROW_WXFBEX( _("LoadXMLFile needs a path") )
		}

		if ( !::wxFileExists( path ) )
		{
			THROW_WXFBEX( _("The file does not exist.\nFile: ") << path )
		}
		TiXmlBase::SetCondenseWhiteSpace( condenseWhiteSpace );
		doc.SetValue( std::string( path.mb_str( wxConvFile ) ) );
		doc.LoadFile();
	}
	catch ( ticpp::Exception& )
	{
		// Ask user to all wxFB to convert the file to UTF-8 and add the XML declaration
		wxString msg = _("This xml file could not be loaded. This could be the result of an unsupported encoding.\n");
		msg 		+= _("Would you like wxFormBuilder to backup the file and convert it to UTF-8\?\n");
		msg			+= _("You will be prompted for the original encoding.\n\n");
		msg			+= _("Path: ");
		msg			+= path;
		if ( wxNO == wxMessageBox( msg, _("Unable to load file"), wxICON_QUESTION | wxYES_NO | wxYES_DEFAULT, wxTheApp->GetTopWindow() ) )
		{
			// User declined, give up
			THROW_WXFBEX( _("Unable to load file: ") << path );
		}

		// User accepted, convert the file
		wxFontEncoding chosenEncoding = StringUtils::GetEncodingFromUser( _("Please choose the original encoding.") );
		if ( wxFONTENCODING_MAX == chosenEncoding )
		{
			THROW_WXFBEX( _("Unable to load file: ") << path );
		}

		ConvertAndAddDeclaration( path, chosenEncoding );

		LoadXMLFile( doc, condenseWhiteSpace, path );
	}

	ticpp::Declaration* declaration;
	try
	{
		ticpp::Node* firstChild = doc.FirstChild();
		declaration = firstChild->ToDeclaration();
	}
	catch( ticpp::Exception& )
	{
		declaration = NULL;
	}

	LoadXMLFileImp( doc, condenseWhiteSpace, path, declaration );
}
Ejemplo n.º 2
0
bool CXmlUtil::LoadXMLFile(XMLDOMDocumentPtr& doc,
                           const wchar_t* filename,
                           XMLDOMElementPtr& root,
                           const wchar_t* pszRootName,
                           IXmlFileCrypt* pCryptHandler)
{
    root = NULL;
    if (LoadXMLFile(doc, filename, pCryptHandler))
    {
        if (!GetRoot(root, doc, pszRootName))
            doc = NULL;
    }
    return doc != NULL;
}
Ejemplo n.º 3
0
CCObject * LoadPlist(const char * szPlistFile)
{
  TiXmlDocument doc;
  if (!LoadXMLFile(szPlistFile, doc))
  {
    return NULL;
  }

  TiXmlElement * pRoot = doc.RootElement();
  if (!pRoot)
    return NULL;

  CCDictionary * pRootDict = new CCDictionary;
  Recurse(XMLHelper::SubNode(pRoot,"dict",false), pRootDict);
  
  return pRootDict;
}
Ejemplo n.º 4
0
////////////////////////////////////////
//		PRIVATE UTILITY FUNCTIONS
////////////////////////////////////////
bool LevelLoader::LoadInitialData(string filename)
{
	string filePath("resource/data/levels/");
	filePath += filename + ".xml";
	xml_document doc;
	
	if(!LoadXMLFile(doc,filePath))
		return false;

	// Start reading level names and corresponding filenames in
	for( xml_node levelset = doc.child("levelset"); levelset; levelset = levelset.next_sibling("levelset"))
	{
		for( xml_node level = levelset.child("level"); level; level = level.next_sibling("level"))
		{			
			m_levelList.push_back(level.attribute("fileName").as_string());
			m_levelNames.push_back(level.attribute("name").as_string());			
		}
	}


	return true;
}
Ejemplo n.º 5
0
CKADmerge::CKADmerge(string sFileName)
{
    m_bIsDirty = FALSE;

	HRESULT hRes = CoInitialize(NULL);
    if(FAILED(hRes))
    {
        throw string("CoInitialize() failed");
    }
    m_sFileName = sFileName;

    LoadXMLFile(sFileName, &m_pXMLKad);

    // init
    int i = 0;
    /*for(i = 0; pPrimaryKeys[i].Element != End; i++)
    {
        m_PrimaryKeys[pPrimaryKeys[i].Element] = pPrimaryKeys[i].PrimKey;
    }*/
    for(i = 0; pOverwriteable[i] != End; i++)
    {
        m_Overwriteable[pOverwriteable[i]] = TRUE;
    }
}
Ejemplo n.º 6
0
bool LevelLoader::LoadLevel(std::string filename)
{
	string filePath("resource/data/levels/");
	filePath += filename + ".xml";
	xml_document doc;

	if(!LoadXMLFile(doc,filePath))
		return false;

	bool anythingLoaded = false;

	for(xml_node entityNode = doc.child("entity"); entityNode; entityNode = entityNode.next_sibling("entity"))
	{  
		Entity* entity = nullptr;

		// Get the number of components
		unsigned int componentCount = std::distance(entityNode.children("component").begin(),
			entityNode.children("component").end());

		// Throws on bad allocation and if we pass something wrong to it
		try
		{
			entity = new Entity(m_nextEntityID++,entityNode.attribute("name").as_string(),
				componentCount);
		}
		catch(std::invalid_argument& ia)
		{
			LOG("A new entity could not be allocated. invalid_argument caught: " << ia.what());
			continue;
		}
		catch(std::bad_alloc& ba)
		{
			LOG("A new entity could not be allocated. bad_alloc caught: " << ba.what());
			continue;
		}

		entity->IsActive(entityNode.attribute("active").as_bool());
		entity->SetHP(entityNode.attribute("hp").as_int());
		entity->SetLayer(entityNode.attribute("layer").as_int());

		vec3<float> entityPos(entityNode.attribute("posX").as_float(), 
			entityNode.attribute("posY").as_float(), 0.0f);

		vec3<float> entityVel(entityNode.attribute("velX").as_float(),
			entityNode.attribute("velY").as_float(), 0.0f);

		entity->SetPosition(entityPos);
		entity->SetVelocity(entityVel);			

		for(xml_node componentNode = entityNode.child("component"); componentNode; componentNode = componentNode.next_sibling("component"))
		{
			ComponentAttribute compAt;
			std::string componentName = componentNode.attribute("type").as_string();
						
			if(componentName == "")
			{
				LOG("Tried to load invalid component type on Entity: " << entityNode.attribute("name").as_string() <<
					" Invalid Component Type: " << componentNode.attribute("type").as_string());
				continue;
			}
			
			// Grab all attributes for this component and give to the component factory to construct
			// Iteration is starting at second attribute since first is type and we already have that
			xml_attribute attributeNode = componentNode.first_attribute();
			attributeNode = attributeNode.next_attribute();

			std::vector<xml_attribute> compAttributes;

			for(; attributeNode; attributeNode = attributeNode.next_attribute())
			{
				compAttributes.push_back(attributeNode);
			}
			
			ComponentFactory::GetInstance().AddComponentToEntity(entity, componentName, &compAttributes);
		}

		entity->RegisterForMessages();

		m_worldMan->AddEntity(entity);
		anythingLoaded = true;
	}

	if(!anythingLoaded)
	{
		LOG("No valid entities to load in : " << filePath);
		return false;
	}

	return true;
}
Ejemplo n.º 7
0
HRESULT CKADmerge::Merge(string sAddOnFileName, BOOL bOverwrite, string sLogFile)
{
    HRESULT hRes = 0;

	IXMLDOMDocument * pXMLAddOn = NULL;

    if(sLogFile != "")
    {
        // open log file
        OpenLog(sLogFile, sDescription + " " + m_sFileName + " with " + sAddOnFileName);
    }

    try
	{
        // load AddOn file
        hRes = LoadXMLFile(sAddOnFileName, &pXMLAddOn);

        IXMLDOMElement * pKadRoot = NULL;
        hRes = GetRootElement(m_pXMLKad, &pKadRoot);
        if(hRes == S_FALSE)
        {
            // create root element
            hRes = GetRootElement(pXMLAddOn, &pKadRoot);
            if(hRes == S_FALSE)
            {
                throw string("ERROR: could not get addon kad root element: " + sAddOnFileName);
            }

            _bstr_t bTagName(GetName(pKadRoot).c_str());
            hRes = m_pXMLKad->createElement(bTagName, &pKadRoot);
            hRes = m_pXMLKad->putref_documentElement(pKadRoot);

            // log changes
            Log("Create Root-Element: " + GetName(pKadRoot) );

            m_bIsDirty = TRUE;
        }

        if(pKadRoot)
            pKadRoot->Release();

	    IXMLDOMNode * pXMLKadNode = NULL;
	    IXMLDOMNode * pXMLAddOnNode = NULL;

        hRes = GetRootElement(m_pXMLKad, &pXMLKadNode);
        hRes = GetRootElement(pXMLAddOn, &pXMLAddOnNode);

        // copy nodes
        hRes = CopyNode(&pXMLKadNode, &pXMLAddOnNode, bOverwrite, "");

	    if(pXMLKadNode != NULL)
		    pXMLKadNode->Release();
	    if(pXMLAddOnNode != NULL)
		    pXMLAddOnNode->Release();

        if(m_bIsDirty)
        {
            hRes = SaveXMLFile(m_sFileName, m_pXMLKad);
            m_bIsDirty = FALSE;
        }
	}
	catch(string str)
	{
        Log(str);
        hRes = S_FALSE;
	}

	if(pXMLAddOn != NULL)
		pXMLAddOn->Release();

    // close log file
    CloseLog();

    return hRes;
}
Ejemplo n.º 8
0
		void LoadXMLFileImp( T& doc, bool condenseWhiteSpace, const wxString& path, U* declaration )
	{
		if ( NULL == declaration )
		{
			// Ask user to all wxFB to convert the file to UTF-8 and add the XML declaration
			wxString msg = _("This xml file has no declaration.\n");
			msg 		+= _("Would you like wxFormBuilder to backup the file and convert it to UTF-8\?\n");
			msg			+= _("You will be prompted for an encoding.\n\n");
			msg			+= _("Path: ");
			msg			+= path;
			int result = wxMessageBox( msg, _("Missing Declaration"), wxICON_QUESTION | wxYES_NO | wxYES_DEFAULT, wxTheApp->GetTopWindow() );
			if ( wxNO == result )
			{
				// User declined, give up
				THROW_WXFBEX( _("Missing Declaration on XML File: ") << path );
			}

			// User accepted, convert the file
			wxFontEncoding chosenEncoding = StringUtils::GetEncodingFromUser( _("Please choose the original encoding.") );
			if ( wxFONTENCODING_MAX == chosenEncoding )
			{
				THROW_WXFBEX( _("Missing Declaration on XML File: ") << path );
			}

			ConvertAndAddDeclaration( path, chosenEncoding );

			// Reload
			LoadXMLFile( doc, condenseWhiteSpace, path );
			return;
		}

		// The file will have a declaration at this point
		wxString version = _WXSTR( declaration->Version() );
		if ( version.empty() )
		{
			version = wxT("1.0");
		}

		wxString standalone = _WXSTR( declaration->Standalone() );
		if ( standalone.empty() )
		{
			standalone = wxT("yes");
		}

		wxString encodingName = _WXSTR( declaration->Encoding() );
		if ( encodingName.empty() )
		{
			// Ask user to all wxFB to convert the file to UTF-8 and add the XML declaration
			wxString msg = _("This xml file has no encoding specified.\n");
			msg 		+= _("Would you like wxFormBuilder to backup the file and convert it to UTF-8\?\n");
			msg			+= _("You will be prompted for an encoding.\n\n");
			msg			+= _("Path: ");
			msg			+= path;
			if ( wxNO == wxMessageBox( msg, _("Unknown Encoding"), wxICON_QUESTION | wxYES_NO | wxYES_DEFAULT, wxTheApp->GetTopWindow() ) )
			{
				// User declined, give up
				THROW_WXFBEX( _("Unknown Encoding for XML File: ") << path );
			}

			// User accepted, convert the file
			wxFontEncoding chosenEncoding = StringUtils::GetEncodingFromUser( _("Please choose the original encoding.") );
			if ( wxFONTENCODING_MAX == chosenEncoding )
			{
				THROW_WXFBEX( _("Unknown Encoding for XML File: ") << path );
			}
			ConvertAndChangeDeclaration( path, version, standalone, chosenEncoding );

			// Reload
			LoadXMLFile( doc, condenseWhiteSpace, path );
			return;
		}

		// The file will have an encoding at this point
		wxFontEncoding encoding = wxFontMapperBase::GetEncodingFromName( encodingName.MakeLower() );
		if ( wxFONTENCODING_UTF8 == encoding )
		{
			// This is what we want
			return;
		}
		else if ( wxFONTENCODING_MAX == encoding )
		{
			wxString msg = wxString::Format( _("The encoding of this xml file is not supported.\n\nFile: %s\nEncoding: %s\nSupported Encodings:\n\n%s"),
							path.c_str(),
							encodingName.c_str(),
							StringUtils::GetSupportedEncodings().c_str() );
			wxMessageBox( msg, wxString::Format( _("Unsupported Encoding: %s"), encodingName.c_str() ) );
			THROW_WXFBEX( _("Unsupported encoding for XML File: ") << path );
		}
		else
		{
			// Ask user to all wxFB to convert the file to UTF-8 and add the XML declaration
			wxString msg = wxString::Format( _("This xml file has specified encoding %s. wxFormBuilder only works with UTF-8.\n"),
							wxFontMapper::GetEncodingDescription( encoding ).c_str() );
			msg 		+= _("Would you like wxFormBuilder to backup the file and convert it to UTF-8\?\n\n");
			msg			+= _("Path: ");
			msg			+= path;
			if ( wxNO == wxMessageBox( msg, _("Not UTF-8"), wxICON_QUESTION | wxYES_NO | wxYES_DEFAULT, wxTheApp->GetTopWindow() ) )
			{
				// User declined, give up
				THROW_WXFBEX( _("Wrong Encoding for XML File: ") << path );
			}

			// User accepted, convert the file
			ConvertAndChangeDeclaration( path, version, standalone, encoding );

			// Reload
			LoadXMLFile( doc, condenseWhiteSpace, path );
			return;
		}
	}