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)){
166 bool typeIs()
const {
170 intval_t type = XML::UNDEFINED;
189 static int getCount(){
195 bool isUndefined()
const {
196 return type == UNDEFINED;
200 bool isComment()
const {
201 return type == COMMENT;
205 bool isCText()
const {
206 return type == CTEXT;
210 bool isStyle()
const {
211 return type == STYLE;
215 bool isScript()
const {
216 return type == SCRIPT;
262 template <
char C=
'\0',
typename ...TT>
280 template <
class ...T>
296 void setText(
const std::string & s);
298 template <
class ...T>
299 void setText(
const T & ...args) {
303 template <
class ...T>
304 void setTextSafe(
const T & ...args) {
312 const std::string & getText()
const {
317 const std::string & getUrl(){
322 void setUrl(
const std::string & s){
327 template <
class ...T>
329 void setName(
const T & ...args){
338 return map_t::empty();
342 const map_t & getAttributes()
const {
348 map_t & getAttributes(){
368 V get(
const std::string & key,
const V & defaultValue)
const {
373 std::string get(
const std::string & key,
const char * defaultValue)
const {
381 (*this)[key] = value;
388 (*this)[key] = value;
395 (*this)[key] = value;
400 void removeAttribute(
const std::string & s){
402 if (it != this->end()){
417 const StyleXML & getStyle()
const {
422 void setStyle(
const StyleXML & s){
427 void setStyle(
const std::string & value){
429 if (type == UNDEFINED){
430 mout.reject<LOG_WARNING>(
"setting style for UNDEFINED elem: ", value);
431 mout.unimplemented<LOG_WARNING>(
"future option: set type to STYLE_SELECT");
433 else if (type == STYLE){
434 mout.reject<LOG_WARNING>(
"not setting style for STYLE elem: ", value);
442 void setStyle(
const char *value){
443 setStyle(std::string(value));
447 void setStyle(
const std::string & key,
const std::string & value){
449 if (type == UNDEFINED){
450 mout.reject<LOG_WARNING>(
"setting style for UNDEFINED elem: ", key,
'=', value);
451 mout.unimplemented<LOG_WARNING>(
"future option: set type to STYLE_SELECT");
453 else if (type == STYLE){
454 mout.reject<LOG_WARNING>(
"not setting style for STYLE elem: ", value);
457 this->style[key] = value;
471 void setStyle(
const std::string & key,
const std::initializer_list<V> &l){
477 this->style[key] = l;
487 void setStyle(
const std::string & key,
const V & value){
490 drain::Logger(__FILE__, __FUNCTION__).
reject<LOG_WARNING>(
"Setting style of STYLE: ", key,
"=", value);
493 this->style[key] = value;
498 void setStyle(
const std::initializer_list<std::pair<const char *,const drain::Variable> > &args){
508 ClassListXML classList;
512 const ClassListXML & getClasses()
const {
516 template <
typename ... TT>
519 classList.add(args...);
528 return classList.has(cls);
532 void removeClass(
const std::string & s) {
543 void specificAttributesToStream(std::ostream & ostr)
const;
546 enum tag_display_mode {
550 EMPTY_TAG = OPENING_TAG | CLOSING_TAG,
556 std::ostream & nodeToStream(std::ostream & ostr, tag_display_mode mode=EMPTY_TAG)
const = 0;
563 std::ostream &
toStream(std::ostream & ostr,
const TR & tree,
const std::string & defaultTag=
"ELEM",
int indent=0);
582 static const std::map<char,char> keyMap = {
591 static const std::map<char,std::string> valueMap = {
592 {entity_t::QUOTE,
"'"},
593 {entity_t::LESS_THAN,
"(("},
594 {entity_t::GREATER_THAN,
"))"},
607 template <
typename T>
630 template <
typename TX>
632 TX &
xmlAssign(TX & dst,
const typename TX::xml_node_t & src){
652 template <
typename N>
659 if (dst.getType() != src.getType()){
662 dst.setType(src.getType());
666 dst.getAttributes().importMap(src.getAttributes());
667 dst.setStyle(src.getStyle());
669 dst.ctext = src.ctext;
679 template <
typename T,
typename V>
690 template <
typename T>
693 T &
xmlAssign(T & tree, std::initializer_list<std::pair<const char *,const Variable> > l){
696 switch (tree->getType()){
698 for (
const auto & entry: l){
699 T & elem = tree[entry.first];
700 elem->setType(STYLE_SELECT);
705 tree->setType(STYLE_SELECT);
723 template <
typename TX>
726 if (tree->isUndefined()){
727 tree->setType(CTEXT);
733 template <
typename TX>
735 TX & xmlAppendString(TX & tree,
const std::string & s){
736 if (tree->isCText()){
740 else if (tree->isUndefined()){
741 tree->setType(CTEXT);
748 TX & child = tree.addChild();
749 child->setType(CTEXT);
760 template <
typename TX>
762 TX &
xmlSetType(TX & tree,
const typename TX::node_data_t::xml_tag_t & type){
773 template <
typename T>
776 typename T::node_data_t::xml_tag_t type = xmlRetrieveDefaultType(tree.data);
779 return tree[key](type);
786 k << tree.getChildren().size();
787 return tree[k.str()](type);
791 template <
typename N>
793 typename N::xml_tag_t xmlRetrieveDefaultType(
const N & parentNode){
794 typedef typename N::xml_default_elem_map_t map_t;
795 const typename map_t::const_iterator it = N::xml_default_elems.find(parentNode.getNativeType());
796 if (it != N::xml_default_elems.end()){
800 return static_cast<typename N::xml_tag_t
>(0);
826DRAIN_ENUM_OSTREAM(XML::entity_t);
829std::ostream &
XML::toStream(std::ostream & ostr,
const TR & tree,
const std::string & defaultTag,
int indent){
833 const typename TR::container_t & children = tree.getChildren();
836 const typename TR::node_data_t & data = tree.data;
838 tag_display_mode mode = EMPTY_TAG;
841 data.nodeToStream(ostr, mode);
846 if (!data.ctext.empty()){
848 if (data.isSingular()){
857 if (!children.empty()){
859 if (data.isSingular()){
860 mout.
warn(
"Singular (hence normally empty) element <", tree->getTag(),
" id='", data.getId(),
"' ...> has ", children.size(),
" children?");
865 if (data.isExplicit()){
868 else if (data.isSingular()){
875 std::string fill(2*indent,
' ');
877 data.nodeToStream(ostr, mode);
879 if (mode == EMPTY_TAG){
884 else if (data.isStyle()){
889 if (!data.ctext.empty()){
891 ostr << fill << data.ctext;
896 if (!data.getAttributes().empty()){
897 mout.
warn(
"STYLE elem ", data.getId(),
" contains attributes, probably meant as style: ", sprinter(data.getAttributes()));
898 ostr <<
"\n\t /" <<
"* <!-- DISCARDED attribs ";
900 ostr <<
" /--> *" <<
"/" <<
'\n';
903 if (!data.style.empty()){
907 for (
const auto & attr: data.style){
909 Sprinter::pairToStream(ostr, attr, StyleXML::styleRecordLayout);
923 for (
const auto & entry: tree.getChildren()){
925 if (entry.second->isComment()){
927 ostr << fill <<
"/* "<< entry.second->ctext <<
" */" <<
'\n';
932 if (!entry.second->ctext.empty()){
934 ostr << fill <<
" " << entry.first <<
" {" << entry.second->ctext <<
"} /* CTEXT */ \n";
937 if (!entry.second->getAttributes().empty()){
938 ostr << fill <<
" " << entry.first <<
" {\n";
939 for (
const auto & attr: entry.second->getAttributes()){
941 ostr << attr.first <<
':' << attr.second <<
';';
944 ostr << fill <<
" }\n";
960 if (data.isScript()){
970 bool ALL_CTEXT =
true;
972 for (
const auto & entry: children){
973 if (!entry.second->isCText()){
984 for (
const auto & entry: children){
1001 for (
const auto & entry: children){
1002 toStream(ostr, entry.second, entry.first, indent+1);
1012 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:195
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:156
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:91
virtual bool isSingular() const
Tell if this element should always have an explicit closing tag even when empty, like <STYLE></STYLE>
Definition XML.cpp:150
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:609
void setId()
Makes ID a visible attribute.
Definition XML.h:251
void setStyle(const std::string &key, const std::initializer_list< V > &l)
Set style of an element.
Definition XML.h:471
const std::string & getId() const
Returns ID of this element. Hopefully a unique ID...
Definition XML.h:270
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:387
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:725
static const std::map< char, std::string > & getAttributeConversionMap()
Characters that must be avoided in XML attribute values: ".
Definition XML.cpp:106
static const std::map< char, std::string > & getCTextConversionMap()
Characters to avoid in XML free text: <, >, {, },.
Definition XML.cpp:118
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:578
void setId(const TT &...args)
Concatenates arguments to an id.
Definition XML.h:264
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:762
static std::ostream & toStream(std::ostream &ostr, const TR &tree, const std::string &defaultTag="ELEM", int indent=0)
Definition XML.h:829
static T & xmlAddChild(T &tree, const std::string &key)
Definition XML.h:775
static N & xmlAssignNode(N &dst, const N &src)
Assign tree node (data) to another.
Definition XML.h:654
static TX & xmlAssign(TX &dst, const typename TX::xml_node_t &src)
Copy node data to tree.
Definition XML.h:632
void reset()
Keep the element type but clear style, class and string data.
Definition XML.cpp:143
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:487
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:161
static T & xmlAssign(T &tree, std::initializer_list< std::pair< const char *, const Variable > > l)
Tree.
Definition XML.h:693
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:282
void setAttribute(const std::string &key, const V &value)
"Final" implementation.
Definition XML.h:394
static T & xmlAssign(T &tree, const V &arg)
Assign property to a XML tree node.
Definition XML.h:681
bool hasClass(const V &cls) const
Definition XML.h:527
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:257
Definition DataSelector.cpp:1277
A container for a static dictionary of enumeration values.
Definition EnumFlags.h:69