lilantha 2003/07/18 04:32:44
Modified: c/src/common BasicTypeSerializer.cpp BasicTypeSerializer.h
Log:
change and fix the inefficiency of the algo and overwriting strings
Revision Changes Path
1.6 +50 -63 xml-axis/c/src/common/BasicTypeSerializer.cpp
Index: BasicTypeSerializer.cpp
===================================================================
RCS file: /home/cvs/xml-axis/c/src/common/BasicTypeSerializer.cpp,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- BasicTypeSerializer.cpp 18 Jul 2003 06:16:05 -0000 1.5
+++ BasicTypeSerializer.cpp 18 Jul 2003 11:32:43 -0000 1.6
@@ -141,69 +141,56 @@
}
}
-string& BasicTypeSerializer::GetEntityReferenced(const string &str)
+/// Encode XML entities if any found in user data.
+string BasicTypeSerializer::GetEntityReferenced(const string &strVal)
{
- int pos;
- //replace "&" with "&" if any
- m_AuxStr = str;
- pos = 0;
- while (true)
- {
- pos = m_AuxStr.find('&', pos);
- if (pos < m_AuxStr.length())
- {
- m_AuxStr = m_AuxStr.replace(pos, 1, "&");
+ if (strVal.empty())
+ return strVal;
+
+ // Find entity reference characters and returns the first any of chars find
position
+ int nPos = strVal.find_first_of(XML_ENTITY_REFERENCE_CAHRS);
+
+ // Check for position validity
+ if (std::string::npos == nPos)
+ return strVal;
+
+ std::string strReturnVal;
+
+ int nOldIdx = 0; // Counter value
+ while(std::string::npos != nPos)
+ { // Get pointered character
+ switch(strVal.at(nPos))
+ {
+ case LESSER_THAN_CHAR : // Process < character
+ strReturnVal.append(strVal.substr(nOldIdx, nPos -
nOldIdx));
+ strReturnVal.append(ENCODED_LESSER_STR);
+ break;
+ case GREATOR_THAN_CHAR : // Process > character
+ strReturnVal.append(strVal.substr(nOldIdx, nPos -
nOldIdx));
+ strReturnVal.append(ENCODED_GREATOR_STR);
+ break;
+ case AMPERSAND_CHAR : // Process & character
+ strReturnVal.append(strVal.substr(nOldIdx, nPos -
nOldIdx));
+ strReturnVal.append(ENCODED_AMPERSAND_STR);
+ break;
+ case DOUBLE_QUOTE_CHAR : // Process " character
+ strReturnVal.append(strVal.substr(nOldIdx, nPos -
nOldIdx));
+ strReturnVal.append(ENCODED_DBL_QUOTE_STR);
+ break;
+ case SINGLE_QUOTE_CHAR : // Process ' character
+ strReturnVal.append(strVal.substr(nOldIdx, nPos -
nOldIdx));
+ strReturnVal.append(ENCODED_SGL_QUOTE_STR);
+ break;
}
- else break;
- pos++;
- }
- //replace "<" with "<" if any
- pos = 0;
- while (true)
- {
- pos = m_AuxStr.find('<', pos);
- if (pos < m_AuxStr.length())
- {
- m_AuxStr = m_AuxStr.replace(pos, 1, "<");
- }
- else break;
- pos++;
- }
- //replace ">" with ">" if any
- pos = 0;
- while (true)
- {
- pos = m_AuxStr.find('>', pos);
- if (pos < m_AuxStr.length())
- {
- m_AuxStr = m_AuxStr.replace(pos, 1, ">");
- }
- else break;
- pos++;
- }
- //replace "'" with "'" if any
- pos = 0;
- while (true)
- {
- pos = m_AuxStr.find('\'', pos);
- if (pos < m_AuxStr.length())
- {
- m_AuxStr = m_AuxStr.replace(pos, 1, "'");
- }
- else break;
- pos++;
- }
- //replace """ with """ if any
- pos = 0;
- while (true)
- {
- pos = m_AuxStr.find('"', pos);
- if (pos < m_AuxStr.length())
- {
- m_AuxStr = m_AuxStr.replace(pos, 1, """);
- }
- else break;
- pos++;
- }
- return m_AuxStr;
+ nOldIdx = ++nPos; // Get old position
+ // Find the next entity reference characters from previous found
position,
+ nPos = strVal.find_first_of(XML_ENTITY_REFERENCE_CAHRS, nPos);
+ }
+
+ int nDataLen = strVal.length(); // Get the length of the field value
+ int nLen = nDataLen - nOldIdx; // Get remaining number of characters
+ if (nLen > 0)
+ strReturnVal += strVal.substr(nOldIdx, nLen);// Apend the remaining
data
+
+ return strReturnVal;
}
1.6 +19 -1 xml-axis/c/src/common/BasicTypeSerializer.h
Index: BasicTypeSerializer.h
===================================================================
RCS file: /home/cvs/xml-axis/c/src/common/BasicTypeSerializer.h,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- BasicTypeSerializer.h 18 Jul 2003 05:29:10 -0000 1.5
+++ BasicTypeSerializer.h 18 Jul 2003 11:32:43 -0000 1.6
@@ -76,10 +76,17 @@
using namespace std;
+const char XML_ENTITY_REFERENCE_CAHRS[] = "<>&\"\'";// Entity reference
characters
+const char ENCODED_LESSER_STR[] = "<"; // Encoded
string for less than character
+const char ENCODED_GREATOR_STR[] = ">"; // Encoded string for
greator than character
+const char ENCODED_AMPERSAND_STR[] = "&"; // Encoded string for
ampersand character
+const char ENCODED_DBL_QUOTE_STR[] = """; // Encoded string for
single quote character
+const char ENCODED_SGL_QUOTE_STR[] = "'"; // Encoded string for
double quote character
+
class BasicTypeSerializer
{
public:
- static string& GetEntityReferenced(const string& str);
+ static string GetEntityReferenced(const string& str);
static string m_AuxStr;
static string& serialize(const string& sName, string& sValue, XSDTYPE
type=XSD_STRING);
static string& serialize(const string& sName, float fValue);
@@ -87,7 +94,18 @@
static const char* BasicTypeStr(XSDTYPE type);
BasicTypeSerializer();
virtual ~BasicTypeSerializer();
+
private:
+ enum
+ {
+ GREATOR_THAN_CHAR = '>', // Greator
than character
+ LESSER_THAN_CHAR = '<', // Less than
character
+ SINGLE_QUOTE_CHAR = '\'', // Single
quotation character
+ DOUBLE_QUOTE_CHAR = '\"', // Double
quotation character
+ AMPERSAND_CHAR = '&', // Ampersand
character
+ };
+private:
+
static void HelpSerialize(const string &sName, const string &sValue);
static string m_sSZ;
static char m_Buf[64]; //used for numeric to string conversions with sprintf