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 =
' ',
120 void setType(
const T &t){
121 const intval_t t2 =
static_cast<intval_t
>(t);
138 const intval_t & getType()
const {
145 return type != UNDEFINED;
152 template <
class T2,
class ...T3>
154 bool typeIs(
const T2 & arg,
const T3... args)
const {
155 if (type ==
static_cast<intval_t
>(arg)){
170 static int getCount(){
176 bool isUndefined()
const {
177 return type == UNDEFINED;
181 bool isComment()
const {
182 return type == COMMENT;
186 bool isCText()
const {
187 return type == CTEXT;
191 bool isStyle()
const {
192 return type == STYLE;
196 bool isScript()
const {
197 return type == SCRIPT;
243 template <
char C=
'\0',
typename ...TT>
261 template <
class ...T>
277 void setText(
const std::string & s);
279 template <
class ...T>
280 void setText(
const T & ...args) {
284 template <
class ...T>
285 void setTextSafe(
const T & ...args) {
293 const std::string & getText()
const {
298 const std::string & getUrl(){
303 void setUrl(
const std::string & s){
308 template <
class ...T>
310 void setName(
const T & ...args){
316 return (*
this)[
"data-name"];
324 return map_t::empty();
328 const map_t & getAttributes()
const {
334 map_t & getAttributes(){
354 V get(
const std::string & key,
const V & defaultValue)
const {
359 std::string get(
const std::string & key,
const char * defaultValue)
const {
367 (*this)[key] = value;
374 (*this)[key] = value;
381 (*this)[key] = value;
386 void removeAttribute(
const std::string & s){
388 if (it != this->end()){
402 intval_t type = XML::UNDEFINED;
424 const StyleXML & getStyle()
const {
429 void setStyle(
const StyleXML & s){
434 void setStyle(
const std::string & value){
436 if (type == UNDEFINED){
437 mout.reject<LOG_WARNING>(
"setting style for UNDEFINED elem: ", value);
438 mout.unimplemented<LOG_WARNING>(
"future option: set type to STYLE_SELECT");
440 else if (type == STYLE){
441 mout.reject<LOG_WARNING>(
"not setting style for STYLE elem: ", value);
449 void setStyle(
const char *value){
450 setStyle(std::string(value));
454 void setStyle(
const std::string & key,
const std::string & value){
456 if (type == UNDEFINED){
457 mout.reject<LOG_WARNING>(
"setting style for UNDEFINED elem: ", key,
'=', value);
458 mout.unimplemented<LOG_WARNING>(
"future option: set type to STYLE_SELECT");
460 else if (type == STYLE){
461 mout.reject<LOG_WARNING>(
"not setting style for STYLE elem: ", value);
464 this->style[key] = value;
478 void setStyle(
const std::string & key,
const std::initializer_list<V> &l){
484 this->style[key] = l;
494 void setStyle(
const std::string & key,
const V & value){
497 drain::Logger(__FILE__, __FUNCTION__).
reject<LOG_WARNING>(
"Setting style of STYLE: ", key,
"=", value);
500 this->style[key] = value;
505 void setStyle(
const std::initializer_list<std::pair<const char *,const drain::Variable> > &args){
515 ClassListXML classList;
519 const ClassListXML & getClasses()
const {
523 template <
typename ... TT>
526 classList.add(args...);
535 return classList.has(cls);
539 void removeClass(
const std::string & s) {
550 void specificAttributesToStream(std::ostream & ostr)
const;
553 enum tag_display_mode {
557 EMPTY_TAG = OPENING_TAG | CLOSING_TAG,
563 std::ostream & nodeToStream(std::ostream & ostr, tag_display_mode mode=EMPTY_TAG)
const = 0;
570 std::ostream &
toStream(std::ostream & ostr,
const TR & tree,
const std::string & defaultTag=
"ELEM",
int indent=0);
589 static const std::map<char,char> keyMap = {
598 static const std::map<char,std::string> valueMap = {
599 {entity_t::QUOTE,
"'"},
600 {entity_t::LESS_THAN,
"(("},
601 {entity_t::GREATER_THAN,
"))"},
614 template <
typename T>
637 template <
typename TX>
639 TX &
xmlAssign(TX & dst,
const typename TX::xml_node_t & src){
659 template <
typename N>
666 if (dst.getType() != src.getType()){
669 dst.setType(src.getType());
673 dst.getAttributes().importMap(src.getAttributes());
674 dst.setStyle(src.getStyle());
676 dst.ctext = src.ctext;
686 template <
typename T,
typename V>
697 template <
typename T>
700 T &
xmlAssign(T & tree, std::initializer_list<std::pair<const char *,const Variable> > l){
703 switch (tree->getType()){
705 for (
const auto & entry: l){
706 T & elem = tree[entry.first];
707 elem->setType(STYLE_SELECT);
712 tree->setType(STYLE_SELECT);
730 template <
typename TX>
733 if (tree->isUndefined()){
734 tree->setType(CTEXT);
740 template <
typename TX>
742 TX & xmlAppendString(TX & tree,
const std::string & s){
743 if (tree->isCText()){
747 else if (tree->isUndefined()){
748 tree->setType(CTEXT);
755 TX & child = tree.addChild();
756 child->setType(CTEXT);
767 template <
typename TX>
769 TX &
xmlSetType(TX & tree,
const typename TX::node_data_t::xml_tag_t & type){
780 template <
typename T>
783 typename T::node_data_t::xml_tag_t type = xmlRetrieveDefaultType(tree.data);
786 return tree[key](type);
793 k << tree.getChildren().size();
794 return tree[k.str()](type);
798 template <
typename N>
800 typename N::xml_tag_t xmlRetrieveDefaultType(
const N & parentNode){
801 typedef typename N::xml_default_elem_map_t map_t;
802 const typename map_t::const_iterator it = N::xml_default_elems.find(parentNode.getNativeType());
803 if (it != N::xml_default_elems.end()){
807 return static_cast<typename N::xml_tag_t
>(0);
833DRAIN_ENUM_OSTREAM(XML::entity_t);
836std::ostream &
XML::toStream(std::ostream & ostr,
const TR & tree,
const std::string & defaultTag,
int indent){
840 const typename TR::container_t & children = tree.getChildren();
843 const typename TR::node_data_t & data = tree.data;
845 tag_display_mode mode = EMPTY_TAG;
848 data.nodeToStream(ostr, mode);
853 if (!data.ctext.empty()){
855 if (data.isSingular()){
864 if (!children.empty()){
866 if (data.isSingular()){
867 mout.
warn(
"Singular (hence normally empty) element <", tree->getTag(),
" id='", data.getId(),
"' ...> has ", children.size(),
" children?");
872 if (data.isExplicit()){
875 else if (data.isSingular()){
882 std::string fill(2*indent,
' ');
884 data.nodeToStream(ostr, mode);
886 if (mode == EMPTY_TAG){
891 else if (data.isStyle()){
896 if (!data.ctext.empty()){
898 ostr << fill << data.ctext;
903 if (!data.getAttributes().empty()){
904 mout.
warn(
"STYLE elem ", data.getId(),
" contains attributes, probably meant as style: ", sprinter(data.getAttributes()));
905 ostr <<
"\n\t /" <<
"* <!-- DISCARDED attribs ";
907 ostr <<
" /--> *" <<
"/" <<
'\n';
910 if (!data.style.empty()){
914 for (
const auto & attr: data.style){
916 Sprinter::pairToStream(ostr, attr, StyleXML::styleRecordLayout);
930 for (
const auto & entry: tree.getChildren()){
932 if (entry.second->isComment()){
934 ostr << fill <<
"/* "<< entry.second->ctext <<
" */" <<
'\n';
939 if (!entry.second->ctext.empty()){
941 ostr << fill <<
" " << entry.first <<
" {" << entry.second->ctext <<
"} /* CTEXT */ \n";
944 if (!entry.second->getAttributes().empty()){
945 ostr << fill <<
" " << entry.first <<
" {\n";
946 for (
const auto & attr: entry.second->getAttributes()){
948 ostr << attr.first <<
':' << attr.second <<
';';
951 ostr << fill <<
" }\n";
967 if (data.isScript()){
977 bool ALL_CTEXT =
true;
979 for (
const auto & entry: children){
980 if (!entry.second->isCText()){
991 for (
const auto & entry: children){
1008 for (
const auto & entry: children){
1009 toStream(ostr, entry.second, entry.first, indent+1);
1019 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:312
Logger & warn(const TT &... args)
Possible error, but execution can continue.
Definition Log.h:430
Logger & reject(const TT &... args)
Some input has been rejected, for example by a syntax.
Definition Log.h:610
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:420
Definition StringBuilder.h:58
static void commentToStream(std::ostream &ostr, const T &v)
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:186
bool typeIs(const T2 &arg, const T3... args) const
Return true, if type is any of the arguments.
Definition XML.h:154
virtual bool isExplicit() const
Tell if this element should always have an explicit closing tag even when empty, like <STYLE></STYLE>
Definition XML.cpp:147
bool typeIsSet() const
Return true, if type is any of the arguments.
Definition XML.h:144
static const std::map< char, std::string > & getKeyConversionMap()
Characters that must be avoided in XML attribute keys: space, tab, =.
Definition XML.cpp:82
virtual bool isSingular() const
Tell if this element should always have an explicit closing tag even when empty, like <STYLE></STYLE>
Definition XML.cpp:141
void addClass(const TT &... args)
Style class.
Definition TreeXML.h:220
static T & xmlAssign(T &dst, const T &src)
Assign another tree structure to another.
Definition XML.h:616
void setId()
Makes ID a visible attribute.
Definition XML.h:232
void setStyle(const std::string &key, const std::initializer_list< V > &l)
Set style of an element.
Definition XML.h:478
const std::string & getId() const
Returns ID of this element. Hopefully a unique ID...
Definition XML.h:251
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:373
static TX & xmlAssignString(TX &tree, const std::string &s)
When assigning a string, create new element unless the element itself is of type CTEXT.
Definition XML.h:732
static const std::map< char, std::string > & getAttributeConversionMap()
Characters that must be avoided in XML attribute values: ".
Definition XML.cpp:97
static const std::map< char, std::string > & getCTextConversionMap()
Characters to avoid in XML free text: <, >, {, },.
Definition XML.cpp:109
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:585
void setId(const TT &...args)
Concatenates arguments to an id.
Definition XML.h:245
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 TX & xmlSetType(TX &tree, const typename TX::node_data_t::xml_tag_t &type)
Definition XML.h:769
static std::ostream & toStream(std::ostream &ostr, const TR &tree, const std::string &defaultTag="ELEM", int indent=0)
Definition XML.h:836
static T & xmlAddChild(T &tree, const std::string &key)
Definition XML.h:782
static N & xmlAssignNode(N &dst, const N &src)
Assign tree node (data) to another.
Definition XML.h:661
static TX & xmlAssign(TX &dst, const typename TX::xml_node_t &src)
Copy node data to tree.
Definition XML.h:639
void reset()
Keep the element type but clear style, class and string data.
Definition XML.cpp:134
ReferenceMap2< FlexibleVariable > map_t
User may optionally filter attributes and CTEST with this using StringTools::replace(XML::encodingMap...
Definition XML.h:105
void setStyle(const std::string &key, const V &value)
For element/class/id, assign ...
Definition XML.h:494
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:152
static T & xmlAssign(T &tree, std::initializer_list< std::pair< const char *, const Variable > > l)
Tree.
Definition XML.h:700
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:263
void setAttribute(const std::string &key, const V &value)
"Final" implementation.
Definition XML.h:380
static T & xmlAssign(T &tree, const V &arg)
Assign property to a XML tree node.
Definition XML.h:688
bool hasClass(const V &cls) const
Definition XML.h:534
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:238
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