MinitScript  0.9.31 PRE-BETA
XMLMethods.cpp
Go to the documentation of this file.
1 #include <span>
2 
8 
9 using std::span;
10 
12 
14 
17 
18 void XMLMethods::registerConstants(MinitScript* minitScript) {
19 }
20 
21 void XMLMethods::registerMethods(MinitScript* minitScript) {
22  // xml
23  {
24  //
25  class MethodXMLCreateTag: public MinitScript::Method {
26  private:
27  MinitScript* minitScript { nullptr };
28  public:
29  MethodXMLCreateTag(MinitScript* minitScript):
31  {
32  { .type = MinitScript::TYPE_STRING, .name = "name", .optional = false, .reference = false, .nullable = false },
33  { .type = MinitScript::TYPE_MAP, .name = "attributes", .optional = true, .reference = false, .nullable = false },
34  { .type = MinitScript::TYPE_STRING, .name = "innerXML", .optional = true, .reference = false, .nullable = false },
35  },
36  MinitScript::TYPE_STRING
37  ),
38  minitScript(minitScript) {}
39  const string getMethodName() override {
40  return "xml.createTag";
41  }
42  void executeMethod(span<MinitScript::Variable>& arguments, MinitScript::Variable& returnValue, const MinitScript::SubStatement& subStatement) override {
43  string name;
44  string innerXML;
45  if ((arguments.size() == 1 || arguments.size() == 2 || arguments.size() == 3) &&
46  MinitScript::getStringValue(arguments, 0, name) == true &&
47  (arguments.size() == 1 || arguments[1].getType() == MinitScript::TYPE_MAP) &&
48  MinitScript::getStringValue(arguments, 2, innerXML, true) == true) {
49  auto mapPtr = arguments[1].getMapPointer();
50  string xml;
51  xml+= "<" + name;
52  if (mapPtr != nullptr && mapPtr->empty() == false) {
53  for(const auto& [mapEntryName, mapEntryValue]: *mapPtr) {
54  xml+= " " + mapEntryName + "=\"" + escape(mapEntryValue->getValueAsString()) + "\"";
55  }
56  }
57  if (innerXML.empty() == true) {
58  xml+= "/>";
59  } else {
60  xml+= ">" + innerXML + "</" + name + ">";
61  }
62  returnValue.setValue(xml);
63  } else {
64  MINITSCRIPT_METHODUSAGE_COMPLAIN(getMethodName());
65  }
66  }
67  };
68  minitScript->registerMethod(new MethodXMLCreateTag(minitScript));
69  }
70 }
71 
72 inline const string XMLMethods::escape(const string& str) {
73  string result;
74  result = _StringTools::replace(str, "&", "&amp;");
75  result = _StringTools::replace(result, "\"", "&quot;");
76  result = _StringTools::replace(result, "'", "&#39;");
77  result = _StringTools::replace(result, "<", "&lt;");
78  result = _StringTools::replace(result, ">", "&gt;");
79  return result;
80 }
#define MINITSCRIPT_METHODUSAGE_COMPLAIN(methodName)
Definition: MinitScript.h:59
void setValue(const Variable &variable)
Set value from given variable into variable.
Definition: MinitScript.h:1618
void registerMethod(Method *method)
Register method.
MinitScript script xml methods.
Definition: XMLMethods.h:12
static const string replace(const string &str, const char what, const char by, int64_t beginIndex=0)
Replace char with another char.
Definition: StringTools.cpp:33