45#include <drain/Sprinter.h>
46#include <drain/FlexibleVariable.h>
51#include "ReferenceMap.h"
58class XML :
protected ReferenceMap2<FlexibleVariable> {
66 static const intval_t UNDEFINED = 0;
67 static const intval_t COMMENT = 1;
68 static const intval_t CTEXT = 2;
69 static const intval_t SCRIPT = 3;
70 static const intval_t STYLE = 4;
71 static const intval_t STYLE_SELECT = 5;
78 NONBREAKABLE_SPACE =
' ',
122 void setType(
const T &t){
123 const intval_t t2 =
static_cast<intval_t
>(t);
140 const intval_t & getType()
const {
147 return type != UNDEFINED;
154 template <
class T2,
class ...T3>
156 bool typeIs(
const T2 & arg,
const T3... args)
const {
157 if (type ==
static_cast<intval_t
>(arg)){
172 static int getCount(){
178 bool isUndefined()
const {
179 return type == UNDEFINED;
183 bool isComment()
const {
184 return type == COMMENT;
188 bool isCText()
const {
189 return type == CTEXT;
193 bool isStyle()
const {
194 return type == STYLE;
198 bool isScript()
const {
199 return type == SCRIPT;
245 template <
char C=
'\0',
typename ...TT>
263 template <
class ...T>
279 void setText(
const std::string & s);
281 template <
class ...T>
282 void setText(
const T & ...args) {
286 template <
class ...T>
287 void setTextSafe(
const T & ...args) {
295 const std::string & getText()
const {
300 const std::string & getUrl(){
305 void setUrl(
const std::string & s){
310 template <
class ...T>
312 void setName(
const T & ...args){
318 return (*
this)[
"data-name"];
326 return map_t::empty();
330 const map_t & getAttributes()
const {
336 map_t & getAttributes(){
356 V get(
const std::string & key,
const V & defaultValue)
const {
361 std::string get(
const std::string & key,
const char * defaultValue)
const {
369 (*this)[key] = value;
376 (*this)[key] = value;
383 (*this)[key] = value;
388 void removeAttribute(
const std::string & s){
390 if (it != this->end()){
404 intval_t type = XML::UNDEFINED;
426 const StyleXML & getStyle()
const {
431 void setStyle(
const StyleXML & s){
436 void setStyle(
const std::string & value){
438 if (type == UNDEFINED){
439 mout.reject<LOG_WARNING>(
"setting style for UNDEFINED elem: ", value);
440 mout.unimplemented<LOG_WARNING>(
"future option: set type to STYLE_SELECT");
442 else if (type == STYLE){
443 mout.reject<LOG_WARNING>(
"not setting style for STYLE elem: ", value);
451 void setStyle(
const char *value){
452 setStyle(std::string(value));
456 void setStyle(
const std::string & key,
const std::string & value){
458 if (type == UNDEFINED){
459 mout.reject<LOG_WARNING>(
"setting style for UNDEFINED elem: ", key,
'=', value);
460 mout.unimplemented<LOG_WARNING>(
"future option: set type to STYLE_SELECT");
462 else if (type == STYLE){
463 mout.reject<LOG_WARNING>(
"not setting style for STYLE elem: ", value);
466 this->style[key] = value;
480 void setStyle(
const std::string & key,
const std::initializer_list<V> &l){
486 this->style[key] = l;
496 void setStyle(
const std::string & key,
const V & value){
499 drain::Logger(__FILE__, __FUNCTION__).
reject<LOG_WARNING>(
"Setting style of STYLE: ", key,
"=", value);
502 this->style[key] = value;
507 void setStyle(
const std::initializer_list<std::pair<const char *,const drain::Variable> > &args){
517 ClassListXML classList;
521 const ClassListXML & getClasses()
const {
525 template <
typename ... TT>
528 classList.add(args...);
537 return classList.has(cls);
541 void removeClass(
const std::string & s) {
552 void specificAttributesToStream(std::ostream & ostr)
const;
555 enum tag_display_mode {
559 EMPTY_TAG = OPENING_TAG | CLOSING_TAG,
565 std::ostream & nodeToStream(std::ostream & ostr, tag_display_mode mode=EMPTY_TAG)
const = 0;
571 std::ostream &
toStream(std::ostream & ostr,
const TR & tree,
const std::string & defaultTag=
"ELEM",
int indent=0);
590 static const std::map<char,char> keyMap = {
599 static const std::map<char,std::string> valueMap = {
600 {entity_t::QUOTE,
"'"},
601 {entity_t::LESS_THAN,
"(("},
602 {entity_t::GREATER_THAN,
"))"},
649 template <
typename N>
656 if (dst.getType() != src.getType()){
659 dst.setType(src.getType());
661 dst.getAttributes().importMap(src.getAttributes());
662 dst.setStyle(src.getStyle());
664 dst.ctext = src.ctext;
831DRAIN_ENUM_OSTREAM(XML::entity_t);
834std::ostream &
XML::toStream(std::ostream & ostr,
const TR & tree,
const std::string & defaultTag,
int indent){
838 const typename TR::container_t & children = tree.getChildren();
841 const typename TR::node_data_t & data = tree.data;
843 tag_display_mode mode = EMPTY_TAG;
846 data.nodeToStream(ostr, mode);
851 if (!data.ctext.empty()){
853 if (data.isSingular()){
862 if (!children.empty()){
864 if (data.isSingular()){
865 mout.
warn(
"Singular (hence normally empty) element <", tree->getTag(),
" id='", data.getId(),
"' ...> has ", children.size(),
" children?");
870 if (data.isExplicit()){
873 else if (data.isSingular()){
880 std::string fill(2*indent,
' ');
882 data.nodeToStream(ostr, mode);
884 if (mode == EMPTY_TAG){
889 else if (data.isStyle()){
894 if (!data.ctext.empty()){
896 ostr << fill << data.ctext;
901 if (!data.getAttributes().empty()){
902 mout.
warn(
"STYLE elem ", data.getId(),
" contains attributes, probably meant as style: ", sprinter(data.getAttributes()));
903 ostr <<
"\n\t /" <<
"* <!-- DISCARDED attribs ";
905 ostr <<
" /--> *" <<
"/" <<
'\n';
908 if (!data.style.empty()){
912 for (
const auto & attr: data.style){
914 Sprinter::pairToStream(ostr, attr, StyleXML::styleRecordLayout);
928 for (
const auto & entry: tree.getChildren()){
930 if (entry.second->isComment()){
932 ostr << fill <<
"/* "<< entry.second->ctext <<
" */" <<
'\n';
937 if (!entry.second->ctext.empty()){
939 ostr << fill <<
" " << entry.first <<
" {" << entry.second->ctext <<
"} /* CTEXT */ \n";
942 if (!entry.second->getAttributes().empty()){
943 ostr << fill <<
" " << entry.first <<
" {\n";
944 for (
const auto & attr: entry.second->getAttributes()){
946 if (attr.first ==
"id"){
951 ostr << attr.first <<
':' << attr.second <<
';';
955 ostr << fill <<
" }\n";
971 if (data.isScript()){
981 bool ALL_CTEXT =
true;
983 for (
const auto & entry: children){
984 if (!entry.second->isCText()){
995 for (
const auto & entry: children){
1012 for (
const auto & entry: children){
1013 toStream(ostr, entry.second, entry.first, indent+1);
1023 data.nodeToStream(ostr, CLOSING_TAG);
Two-way mapping between strings and objects of template class T.
Definition Dictionary.h:63
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition Log.h:313
Logger & warn(const TT &... args)
Possible error, but execution can continue.
Definition Log.h:431
Logger & reject(const TT &... args)
Some input has been rejected, for example by a syntax.
Definition Log.h:611
A map of references to base type scalars, arrays or std::string; changing values in either are equiva...
Definition ReferenceMap.h:69
ref_t & link(const std::string &key, F &x)
Associates a map entry with a variable.
Definition ReferenceMap.h:84
map_t::iterator iterator
Needed?
Definition SmartMap.h:80
std::string get(const std::string &key, const std::string &defaultValue) const
Retrieves a value, or default value if value is unset.
Definition SmartMap.h:127
static std::ostream & toStream(std::ostream &ostr, const std::initializer_list< T > &x, const SprinterLayout &layout=defaultLayout)
New (experimental)
Definition Sprinter.h:424
Definition StringBuilder.h:58
static void commentToStream(std::ostream &ostr, const TT... args)
Practical utility, helps in adding C++ code commenting...
Definition StyleXML.h:69
VariableT is a final class applied through typedefs Variable, Reference and FlexibleVariable.
Definition VariableT.h:87
Base class for XML "nodes", to be data elements T for drain::Tree<T>
Definition TreeXML.h:80
static const std::map< char, std::string > & getEntityMap()
Handy map for converting characters to XML entities. Example: '&' -> "&".
Definition XML.cpp:189
bool typeIs(const T2 &arg, const T3... args) const
Return true, if type is any of the arguments.
Definition XML.h:156
virtual bool isExplicit() const
Tell if this element should always have an explicit closing tag even when empty, like <STYLE></STYLE>
Definition XML.cpp:150
bool typeIsSet() const
Return true, if type is any of the arguments.
Definition XML.h:146
static const std::map< char, std::string > & getKeyConversionMap()
Characters that must be avoided in XML attribute keys: space, tab, =.
Definition XML.cpp:85
virtual bool isSingular() const
Tell if this element should always have an explicit closing tag even when empty, like <STYLE></STYLE>
Definition XML.cpp:144
void addClass(const TT &... args)
Style class.
Definition TreeXML.h:220
void setId()
Makes ID a visible attribute.
Definition XML.h:234
void setStyle(const std::string &key, const std::initializer_list< V > &l)
Set style of an element.
Definition XML.h:480
const std::string & getId() const
Returns ID of this element. Hopefully a unique ID...
Definition XML.h:253
virtual void setAttribute(const std::string &key, const char *value)
Default implementation. Needed for handling units in strings, like "50%" or "640px".
Definition XML.h:375
static const std::map< char, std::string > & getAttributeConversionMap()
Characters that must be avoided in XML attribute values: ".
Definition XML.cpp:100
static const std::map< char, std::string > & getCTextConversionMap()
Characters to avoid in XML free text: <, >, {, },.
Definition XML.cpp:112
static void xmlAttribToStream(std::ostream &ostr, const std::string &key, const V &value)
Handy map for converting characters to XML entities. Example: '&' -> "&".
Definition XML.h:586
void setId(const TT &...args)
Concatenates arguments to an id.
Definition XML.h:247
virtual void setAttribute(const std::string &key, const std::string &value)
Default implementation. Needed for handling units in strings, like "50%" or "640px".
Definition TreeXML.h:166
static std::ostream & toStream(std::ostream &ostr, const TR &tree, const std::string &defaultTag="ELEM", int indent=0)
Definition XML.h:834
static N & xmlAssignNode(N &dst, const N &src)
Assign another tree structure to another.
Definition XML.h:651
void reset()
Keep the element type but clear style, class and string data.
Definition XML.cpp:137
ReferenceMap2< FlexibleVariable > map_t
User may optionally filter attributes and CTEST with this using StringTools::replace(XML::encodingMap...
Definition XML.h:107
void setStyle(const std::string &key, const V &value)
For element/class/id, assign ...
Definition XML.h:496
virtual void setText(const std::string &s)
Assign the text content of this node. If the node type is undefined, set it to CTEXT.
Definition XML.cpp:155
void setComment(const T &...args)
Make this node a comment. Contained tree will not be deleted. In current version, attributes WILL be ...
Definition XML.h:265
void setAttribute(const std::string &key, const V &value)
"Final" implementation.
Definition XML.h:382
bool hasClass(const V &cls) const
Definition XML.h:536
std::string id
Some general-purpose.
Definition TreeXML.h:95
void setId(const std::string &s)
Makes ID a visible attribute, with a given value.
Definition XML.h:240
Definition DataSelector.cpp:1277
VariableT< VariableInitializer< ReferenceT< VariableBase > > > FlexibleVariable
Value container supporting dynamic type with own memory and optional linking (referencing) of externa...
Definition FlexibleVariable.h:67
A container for a static dictionary of enumeration values.
Definition EnumFlags.h:69
Default implementation.
Definition Type.h:541