1 #include <samchon/library/XML.hpp> 6 #include <samchon/library/Math.hpp> 25 QuotePair(TYPE type,
size_t startIndex,
size_t endIndex)
28 this->startIndex = startIndex;
29 this->endIndex = endIndex;
51 for (
auto it = xml.begin(); it != xml.end(); it++)
53 if (it->second->empty() ==
true)
56 shared_ptr<XMLList> xmlList(
new XMLList());
57 xmlList->reserve(it->second->size());
59 for (
size_t i = 0; i < it->second->size(); it++)
60 xmlList->emplace_back(
new XML(*it->second->at(i)));
62 set(xmlList->at(0)->tag, xmlList);
69 value = move(xml.value);
77 if (wStr.
find(
'<') == std::string::npos)
81 std::string replacedStr;
82 if (wStr.
find(
"<!--") != std::string::npos)
84 queue<pair<size_t, size_t>> indexPairQueue;
85 size_t beginX = 0, endX;
88 replacedStr.reserve(wStr.
size());
89 while ((beginX = wStr.
find(
"<!--", beginX)) != std::string::npos)
91 indexPairQueue.push({ beginX, wStr.
find(
"-->", beginX + 1) + 3 });
97 while (indexPairQueue.empty() ==
false)
99 endX = indexPairQueue.front().first;
100 replacedStr.append(wStr.
substring(beginX, endX).str());
102 beginX = indexPairQueue.front().second;
103 indexPairQueue.
pop();
105 replacedStr.append(wStr.
substr(beginX).str());
112 if (wStr.
find(
"<?xml") != std::string::npos)
129 constructProperty(wStr);
130 if (constructValue(wStr) ==
true)
131 constructChildren(wStr);
135 size_t startX = wStr.
find(
"<") + 1;
140 wStr.
find(
' ', startX),
141 wStr.
find(
"\r\n", startX),
142 wStr.
find(
'\n', startX),
143 wStr.
find(
'\t', startX),
144 wStr.
find(
'>', startX),
145 wStr.
find(
'/', startX)
154 size_t i_begin = wStr.
find(
'<' +
tag) +
tag.size() + 1;
155 size_t i_endSlash = wStr.
rfind(
'/');
156 size_t i_endBlock = wStr.
find(
'>', i_begin);
158 size_t i_end = calcMinIndex({ i_endSlash, i_endBlock });
159 if (i_end == std::string::npos || i_begin >= i_end)
165 if (line.
find(
'=') == std::string::npos)
168 std::string label,
value;
169 vector<QuotePair*> helpers;
170 bool inQuote =
false;
171 QuotePair::TYPE type;
172 size_t startPoint, equalPoint;
175 for (i = 0; i < line.
size(); i++)
178 if (inQuote ==
false && (line[i] ==
'\'' || line[i] ==
'"'))
184 type = QuotePair::SINGLE;
185 else if (line[i] ==
'"')
186 type = QuotePair::DOUBLE;
192 (type == QuotePair::SINGLE && line[i] ==
'\'') ||
193 (type == QuotePair::DOUBLE && line[i] ==
'"')
197 helpers.push_back(
new QuotePair(type, startPoint, i));
201 for (i = 0; i < helpers.size(); i++)
205 equalPoint = (
long long)line.
find(
'=');
206 label = move( line.
substring(0, equalPoint).trim().str() );
210 equalPoint = line.
find(
'=', helpers[i - 1]->endIndex + 1);
211 label = line.
substring(helpers[i - 1]->endIndex + 1, equalPoint).trim().str();
221 helpers[i]->startIndex + 1,
230 for (i = 0; i < helpers.size(); i++)
233 auto XML::constructValue(
WeakString &wStr) ->
bool 235 size_t i_endSlash = wStr.
rfind(
'/');
236 size_t i_endBlock = wStr.
find(
'>');
238 if (i_endSlash < i_endBlock || i_endBlock + 1 == wStr.
rfind(
'<'))
246 size_t startX = i_endBlock + 1;
247 size_t endX = wStr.
rfind(
'<');
250 if (wStr.
find(
'<') == string::npos)
259 if (wStr.
find(
'<') == std::string::npos)
262 size_t startX = wStr.
find(
'<');
263 size_t endX = wStr.
rfind(
'>') + 1;
270 int blockStartCount = 0;
271 int blockEndCount = 0;
277 for (i = 0; i < wStr.
size(); i++)
279 if (wStr[i] ==
'<' && wStr.
substr(i, 2) !=
"</")
281 else if (wStr.
substr(i, 2) ==
"/>" || wStr.
substr(i, 2) ==
"</")
284 if (blockStartCount >= 1 && blockStartCount == blockEndCount)
287 end = wStr.
find(
'>', i);
292 shared_ptr<XML> xml(
new XML(
this, wStr.
substring(start, end + 1)));
441 if (str.
empty() ==
true)
445 auto it = find(xml->tag);
450 set(xml->tag, make_shared<XMLList>());
455 it->second->push_back(xml);
459 std::string &
tag = xml->tag;
460 if (this->
has(tag) ==
false)
461 set(
tag, make_shared<XMLList>());
473 for (
auto it = xml->propertyMap.begin(); it != xml->propertyMap.end(); it++)
582 auto XML::calcMinIndex(
const std::vector<size_t> &vec)
const ->
size_t 584 size_t val = std::string::npos;
585 for (
size_t i = 0; i < vec.size(); i++)
586 if (vec[i] != std::string::npos && vec[i] < val)
592 auto XML::encodeProperty(
const WeakString &str)
const -> std::string
594 static vector<pair<std::string, std::string>> pairArray =
608 auto XML::decodeProperty(
const WeakString &str)
const -> std::string
610 static vector<pair<std::string, std::string>> pairArray =
624 auto XML::encodeValue(
const WeakString &str)
const -> std::string
626 static vector<pair<std::string, std::string>> pairArray =
633 return str.
trim().replaceAll(pairArray);
635 auto XML::decodeValue(
const WeakString &str)
const -> std::string
637 static vector<pair<std::string, std::string>> pairArray =
650 string str = string(level,
'\t') +
"<" +
tag;
654 str +=
" " + it->first +
"=\"" + encodeProperty(it->second) +
"\"";
656 if (this->empty() ==
true)
659 if (
value.empty() ==
true)
662 str +=
">" + encodeValue(
value) +
"</" +
tag +
">";
669 for (
auto it = begin(); it != end(); it++)
670 for (
size_t i = 0; i < it->second->size(); i++)
671 str += it->second->at(i)->toString(level + 1);
673 str += string(level,
'\t') +
"</" +
tag +
">";
std::vector< std::shared_ptr< XML > > XMLList
A list of XML, tags are same.
std::string value
Value of the XML.
XML()
Default Constructor.
auto has(const std::string &key) const -> bool
Whether have the item or not.
auto pop(const Key &key) -> T
Pop item.
auto substr(size_t startIndex, size_t endIndex=SIZE_MAX) const -> WeakString
Generates a substring.
auto empty() const -> bool
Tests wheter string is emtpy.
auto size() const -> size_t
Returns size of the characters which are being referenced.
void eraseProperty(const std::string &)
Erase a property by its key.
void push_back(const WeakString &)
Add children xml objects by string representing them.
auto getPropertyMap() const -> const HashMap< std::string, std::string > &
Get propertyMap.
auto find(const WeakString &delim, size_t startIndex=NULL) const -> size_t
Finds first occurence in string.
void setTag(const std::string &)
Set tag (identifier) of the XML.
void clearProperties()
Remove all properties in the XML.
auto rfind(const WeakString &delim, size_t endIndex=SIZE_MAX) const -> size_t
Finds last occurence in string.
HashMap< std::string, std::string > propertyMap
Properties belongs to the XML.
auto toString(size_t level=0) const -> std::string
Get the string content.
auto trim(const std::vector< std::string > &delims) const -> WeakString
Removes all designated characters from the beginning and end of the specified string.
auto substring(size_t startIndex, size_t size=SIZE_MAX) const -> WeakString
Generates a substring.
auto replaceAll(const WeakString &before, const WeakString &after) const -> std::string
Returns a string specified word is replaced.
auto hasProperty(const std::string &) const -> bool
Test wheter a property exists or not.
auto getTag() const -> std::string
Get key; identifer of the XML.
XML is a class representing xml object.
void set(const Key &key, const T &val)
Set element.
auto between(const WeakString &start={}, const WeakString &end={}) const -> WeakString
Generates a substring.
void addAllProperty(const std::shared_ptr< XML >)
Add all properties from another XML.
Top level namespace of products built from samchon.
A string class only references characeters, reference only.