diff --git a/trunk/ConsoleDataMigration/sources/ConsoleRestore/OMakefile b/trunk/ConsoleDataMigration/sources/ConsoleRestore/OMakefile index edb5158..998ed67 100644 --- a/trunk/ConsoleDataMigration/sources/ConsoleRestore/OMakefile +++ b/trunk/ConsoleDataMigration/sources/ConsoleRestore/OMakefile @@ -31,6 +31,7 @@ SOURCES[] = TitleDownloader.cpp Shop.cpp RegionIdModifier.cpp + SimpleXmlPreprocessor.cpp ../common/Util.cpp ../common/DrawSystemState.cpp ../common/FileTransfer.cpp diff --git a/trunk/ConsoleDataMigration/sources/ConsoleRestore/SimpleXmlPreprocessor.cpp b/trunk/ConsoleDataMigration/sources/ConsoleRestore/SimpleXmlPreprocessor.cpp new file mode 100644 index 0000000..cde1130 --- /dev/null +++ b/trunk/ConsoleDataMigration/sources/ConsoleRestore/SimpleXmlPreprocessor.cpp @@ -0,0 +1,176 @@ +/*---------------------------------------------------------------------------* + Project: Horizon + File: SimpleXmlPreprocessor.cpp + + Copyright 2009-2011 Nintendo. All rights reserved. + + These coded instructions, statements, and computer programs contain + proprietary information of Nintendo of America Inc. and/or Nintendo + Company Ltd., and are protected by Federal copyright law. They may + not be disclosed to third parties or copied or duplicated in any form, + in whole or in part, without the prior written consent of Nintendo. + + $Rev$ + *---------------------------------------------------------------------------*/ + +#include "SimpleXmlPreprocessor.h" +#include + +namespace ConsoleRestore +{ + +bool SimpleXmlPreprocessor::Canonicalize(std::string& str) +{ + if(!EraseXmlDeclaration(str)) + { + NN_LOG("invalid xml!\n"); + return false; + } + + if(!EraseEmptyTag(str)) + { + NN_LOG("can't erase empty tag"); + return false; + } + + if(!EraseAttribute(str)) + { + NN_LOG("can't erase attribute\n"); + return false; + } + + if(!EraseNsTagEnd(str)) + { + NN_LOG("can't erase ns end\n"); + return false; + } + + if(!EraseNsTagStart(str)) + { + NN_LOG("can't erase ns start\n"); + return false; + } + + return true; +} + +bool SimpleXmlPreprocessor::EraseXmlDeclaration(std::string& str) +{ + std::string::size_type start = str.find(""); + if (end == std::string::npos) + { + return false; + } + + str.erase(start, end + 1 - start); + + return true; +} + +bool SimpleXmlPreprocessor::EraseEmptyTag(std::string& str) +{ + // XMLとしては正しい前提 + std::string::size_type end = str.find("/>"); + if (end == std::string::npos) + { + return true; + } + + std::string::size_type start = str.rfind("<", end); + if (start == std::string::npos) + { + return false; + } + + str.erase(start, end + 2 - start); + + return true; +} + +bool SimpleXmlPreprocessor::EraseNsTagEnd(std::string& str) +{ + std::string::size_type start = str.rfind("", start); + if (end == std::string::npos) + { + // 不正なXML + return false; + } + + std::string::size_type colon = str.substr(start, end - start).find(":"); + if (colon == std::string::npos) + { + // 名前空間無し + start = str.rfind("", start); + if (end == std::string::npos) + { + // 不正なXML + return false; + } + + std::string::size_type colon = str.substr(start, end - start).find(":"); + if (colon == std::string::npos) + { + // 名前空間無し + start = str.find("<", end); + continue; + } + + str.erase(start + 1, colon); + start = str.find("<", end - colon); + } + + return true; +} + +bool SimpleXmlPreprocessor::EraseAttribute(std::string& str) +{ + std::string::size_type start = str.find("<"); + while (start != std::string::npos) + { + std::string::size_type end = str.find(">", start); + if (end == std::string::npos) + { + // 不正なXML + return false; + } + + std::string::size_type space = str.substr(start, end - start).find(" "); + if (space == std::string::npos) + { + // 属性なし + start = str.find("<", end); + continue; + } + + str.erase(start + space, end - (start + space)); + start = str.find("<", start + space); + } + + return true; +} + +} /* namespace ConsoleRestore */ diff --git a/trunk/ConsoleDataMigration/sources/ConsoleRestore/SimpleXmlPreprocessor.h b/trunk/ConsoleDataMigration/sources/ConsoleRestore/SimpleXmlPreprocessor.h new file mode 100644 index 0000000..11f2ffa --- /dev/null +++ b/trunk/ConsoleDataMigration/sources/ConsoleRestore/SimpleXmlPreprocessor.h @@ -0,0 +1,52 @@ +/*---------------------------------------------------------------------------* + Project: Horizon + File: SimpleXmlPreprocessor.h + + Copyright 2009-2011 Nintendo. All rights reserved. + + These coded instructions, statements, and computer programs contain + proprietary information of Nintendo of America Inc. and/or Nintendo + Company Ltd., and are protected by Federal copyright law. They may + not be disclosed to third parties or copied or duplicated in any form, + in whole or in part, without the prior written consent of Nintendo. + + $Rev$ + *---------------------------------------------------------------------------*/ + +#ifndef SIMPLEXMLPREPROCESSOR_H_ +#define SIMPLEXMLPREPROCESSOR_H_ + +#include + +namespace ConsoleRestore +{ + +class SimpleXmlPreprocessor +{ +public: + SimpleXmlPreprocessor() {}; + ~SimpleXmlPreprocessor() {}; + + // SimpleXmlParserで読めるように変換します + static bool Canonicalize(std::string& str); + +private: + // の除去 + static bool EraseXmlDeclaration(std::string& str); + + // 空タグの除去 + static bool EraseEmptyTag(std::string& str); + + // 属性の削除 + static bool EraseAttribute(std::string& str); + + // 終了タグの名前空間の削除 + static bool EraseNsTagEnd(std::string& str); + + // 開始タグの名前空間の削除 + static bool EraseNsTagStart(std::string& str); + +}; + +} /* namespace ConsoleRestore */ +#endif /* SIMPLEXMLPREPROCESSOR_H_ */