Sfoglia il codice sorgente

Upgraded Utility::ReplaceXmlAttribute to ignore elements in comments.


git-svn-id: https://svn.microneil.com/svn/SNFUtility/trunk@53 aa37657e-1934-4a5f-aa6d-2d8eab27ff7c
master
adeniz 12 anni fa
parent
commit
7eea9f0cd3
2 ha cambiato i file con 78 aggiunte e 14 eliminazioni
  1. 33
    7
      Common/Utility.cpp
  2. 45
    7
      CommonTests/TestUtility.cpp

+ 33
- 7
Common/Utility.cpp Vedi File

@@ -317,18 +317,44 @@ Utility::MkDir(std::string &Dir) {
void
Utility::ReplaceXmlAttribute(std::string *Content, std::string ElementName, std::string AttributeName, std::string AttributeValue) {
bool FoundElement;
FoundElement = false;
std::string::size_type ElementContentBegin; // Index of start of the element content.
std::string::size_type ElementContentEnd; // One past the end of the element content.
ElementContentBegin = Content->find("<" + ElementName); // Get indices of element content.
ElementContentEnd = Content->find("</" + ElementName);
ElementContentBegin = 0;
while (!FoundElement) {
if (std::string::npos == ElementContentBegin) {
std::string Temp;
ElementContentBegin = Content->find("<" + ElementName, ElementContentBegin); // Get indices of element content.
ElementContentEnd = Content->find("</" + ElementName, ElementContentBegin);
Temp = "Unable to find the start of element '" + ElementName;
Temp += "'.";
throw std::runtime_error(Temp);
if (std::string::npos == ElementContentBegin) {
std::string Temp;
Temp = "Unable to find the start of element '" + ElementName;
Temp += "'.";
throw std::runtime_error(Temp);
}
// Check whether the element is in a comment.
std::string::size_type PrevCommentBegin;
std::string::size_type PrevCommentEnd;
PrevCommentBegin = Content->rfind("<!--", ElementContentBegin);
PrevCommentEnd = Content->rfind("-->", ElementContentBegin);
if ( (PrevCommentBegin == std::string::npos) ||
(PrevCommentEnd < ElementContentBegin) ) {
FoundElement = true;
break; // Not in comment; continue processing.
}
ElementContentBegin++; // In comment; continue search.
}

+ 45
- 7
CommonTests/TestUtility.cpp Vedi File

@@ -208,6 +208,50 @@ TestReplaceXmlAttribute() {
}
// Element in comment.
Content = "<!-- License file created by SNFIdentity\n"
"<TestElement TestAttribute = \"testmode\" authentication='setuptestingonly'/>\n"
"-->\n"
"<snf>\n"
"<TestElement TestAttribute = \"testmode\" authentication='setuptestingonly'/>\n"
"</snf>\n";
OriginalContent = Content;
ExpectedContent = "<!-- License file created by SNFIdentity\n"
"<TestElement TestAttribute = \"testmode\" authentication='setuptestingonly'/>\n"
"-->\n"
"<snf>\n"
"<TestElement TestAttribute = \"NewValue\" authentication='setuptestingonly'/>\n"
"</snf>\n";
try {
Utility::ReplaceXmlAttribute(&Content, ElementName, AttributeName, AttributeNewValue);
} catch (std::exception &e) {
std::string Temp;
Temp = "TestReplaceXmlAttribute: Unexpected exception thrown with valid input: ";
Temp += e.what();
Temp += "\n\nOriginal Content:\n\n";
Temp += OriginalContent + "\n";
Error(Temp);
return false;
}
if (Content != ExpectedContent) {
std::string Temp;
Temp = "TestReplaceXmlAttribute: Incorrect result with valid input. Content after replacement:\n\n";
Temp += Content;
Temp += "\n\nExpected:\n\n";
Temp += ExpectedContent + "\n";
Error(Temp);
return false;
}
// Test with invalid input.
// Duplicate element.
Content = "<!-- License file created by SNFIdentity-->\n"
@@ -228,7 +272,7 @@ TestReplaceXmlAttribute() {
return false;
} catch (std::exception &e) {
std::cout << "Duplicate TestElement: " << e.what() << "\n\n";
}
// Missing element.
@@ -248,7 +292,6 @@ TestReplaceXmlAttribute() {
return false;
} catch (std::exception &e) {
std::cout << "Missing TestElement: " << e.what() << "\n\n";
}
@@ -270,7 +313,6 @@ TestReplaceXmlAttribute() {
return false;
} catch (std::exception &e) {
std::cout << "No closing to TestElement: " << e.what() << "\n\n";
}
@@ -292,7 +334,6 @@ TestReplaceXmlAttribute() {
return false;
} catch (std::exception &e) {
std::cout << "Duplicate attribute: " << e.what() << "\n\n";
}
@@ -316,7 +357,6 @@ TestReplaceXmlAttribute() {
return false;
} catch (std::exception &e) {
std::cout << "TestAttribute in another element: " << e.what() << "\n\n";
}
@@ -338,7 +378,6 @@ TestReplaceXmlAttribute() {
return false;
} catch (std::exception &e) {
std::cout << "Missing attribute: " << e.what() << "\n\n";
}
@@ -361,7 +400,6 @@ TestReplaceXmlAttribute() {
return false;
} catch (std::exception &e) {
std::cout << "No closing to attribute: " << e.what() << "\n\n";
}

Loading…
Annulla
Salva