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;
83 template <
class T2,
class ...T3>
85 bool typeIs(
const T2 & arg,
const T3... args)
const {
86 if (type ==
static_cast<intval_t
>(arg)){
103 intval_t type = XML::UNDEFINED;
116 static int getCount(){
122 bool isUndefined()
const {
123 return type == UNDEFINED;
127 bool isComment()
const {
128 return type == COMMENT;
132 bool isCText()
const {
133 return type == CTEXT;
137 bool isStyle()
const {
138 return type == STYLE;
184 template <
char C=
'\0',
typename ...TT>
202 template <
class ...T>
218 void setText(
const std::string & s);
220 template <
class ...T>
221 void setText(
const T & ...args) {
226 const std::string & getText()
const {
231 const std::string & getUrl(){
236 void setUrl(
const std::string & s){
241 template <
class ...T>
243 void setName(
const T & ...args){
252 return map_t::empty();
256 const map_t & getAttributes()
const {
262 map_t & getAttributes(){
282 V get(
const std::string & key,
const V & defaultValue)
const {
287 std::string get(
const std::string & key,
const char * defaultValue)
const {
295 (*this)[key] = value;
302 (*this)[key] = value;
309 (*this)[key] = value;
314 void removeAttribute(
const std::string & s){
316 if (it != this->end()){
331 const StyleXML & getStyle()
const {
336 void setStyle(
const StyleXML & s){
341 void setStyle(
const std::string & value){
343 if (type == UNDEFINED){
344 mout.reject<LOG_WARNING>(
"setting style for UNDEFINED elem: ", value);
345 mout.unimplemented<LOG_WARNING>(
"future option: set type to STYLE_SELECT");
347 else if (type == STYLE){
348 mout.reject<LOG_WARNING>(
"not setting style for STYLE elem: ", value);
356 void setStyle(
const char *value){
357 setStyle(std::string(value));
361 void setStyle(
const std::string & key,
const std::string & value){
363 if (type == UNDEFINED){
364 mout.reject<LOG_WARNING>(
"setting style for UNDEFINED elem: ", key,
'=', value);
365 mout.unimplemented<LOG_WARNING>(
"future option: set type to STYLE_SELECT");
367 else if (type == STYLE){
368 mout.reject<LOG_WARNING>(
"not setting style for STYLE elem: ", value);
371 this->style[key] = value;
385 void setStyle(
const std::string & key,
const std::initializer_list<V> &l){
391 this->style[key] = l;
401 void setStyle(
const std::string & key,
const V & value){
404 drain::Logger(__FILE__, __FUNCTION__).
reject<LOG_WARNING>(
"Setting style of STYLE: ", key,
"=", value);
407 this->style[key] = value;
412 void setStyle(
const std::initializer_list<std::pair<const char *,const drain::Variable> > &args){
436 ClassListXML classList;
440 const ClassListXML & getClasses()
const {
444 template <
typename ... TT>
447 classList.add(args...);
456 return classList.has(cls);
460 void removeClass(
const std::string & s) {
471 void specificAttributesToStream(std::ostream & ostr)
const;
474 enum tag_display_mode {
478 EMPTY_TAG = OPENING_TAG | CLOSING_TAG,
484 std::ostream & nodeToStream(std::ostream & ostr, tag_display_mode mode=EMPTY_TAG)
const = 0;
491 std::ostream &
toStream(std::ostream & ostr,
const TR & tree,
const std::string & defaultTag=
"ELEM",
int indent=0);
496 void xmlAttribToStream(std::ostream &ostr,
const std::string & key,
const V &value){
511 template <
typename TX>
518 dst->setType(src->getType());
519 dst->setText(src->ctext);
520 dst->getAttributes() = src->getAttributes();
530 template <
typename TX>
532 TX &
xmlAssign(TX & dst,
const typename TX::xml_node_t & src){
552 template <
typename X>
559 dst.setType(src.getType());
560 dst.getAttributes().importMap(src.getAttributes());
561 dst.setStyle(src.getStyle());
563 dst.ctext = src.ctext;
573 template <
typename TX,
typename V>
584 template <
typename TX>
587 TX &
xmlAssign(TX & tree, std::initializer_list<std::pair<const char *,const Variable> > l){
589 switch (
static_cast<intval_t
>(tree->getType())){
591 for (
const auto & entry: l){
592 TX & elem = tree[entry.first];
593 elem->setType(STYLE_SELECT);
598 tree->setType(STYLE_SELECT);
616 template <
typename TX>
619 if (tree->isUndefined()){
620 tree->setType(CTEXT);
650 template <
typename TX>
652 TX & xmlAppendString(TX & tree,
const std::string & s){
653 if (tree->isCText()){
657 else if (tree->isUndefined()){
658 tree->setType(CTEXT);
665 TX & child = tree.addChild();
666 child->setType(CTEXT);
677 template <
typename TX>
679 TX &
xmlSetType(TX & tree,
const typename TX::node_data_t::xml_tag_t & type){
690 template <
typename T>
693 typename T::node_data_t::xml_tag_t type = xmlRetrieveDefaultType(tree.data);
696 return tree[key](type);
703 k << tree.getChildren().size();
704 return tree[k.str()](type);
708 template <
typename N>
710 typename N::xml_tag_t xmlRetrieveDefaultType(
const N & parentNode){
711 typedef typename N::xml_default_elem_map_t map_t;
712 const typename map_t::const_iterator it = N::xml_default_elems.find(parentNode.getNativeType());
713 if (it != N::xml_default_elems.end()){
717 return static_cast<typename N::xml_tag_t
>(0);
741std::ostream &
XML::toStream(std::ostream & ostr,
const TR & tree,
const std::string & defaultTag,
int indent){
745 const typename TR::container_t & children = tree.getChildren();
748 const typename TR::node_data_t & data = tree.data;
750 tag_display_mode mode = EMPTY_TAG;
753 data.nodeToStream(ostr, mode);
758 if (!data.ctext.empty()){
760 if (data.isSingular()){
769 if (!children.empty()){
771 if (data.isSingular()){
772 mout.
warn(
"Singular (hence normally empty) element <", tree->getTag(),
" id='", data.getId(),
"' ...> has ", children.size(),
" children?");
777 if (data.isExplicit()){
780 else if (data.isSingular()){
787 std::string fill(2*indent,
' ');
789 data.nodeToStream(ostr, mode);
791 if (mode == EMPTY_TAG){
796 else if (data.isStyle()){
801 if (!data.ctext.empty()){
803 ostr << fill << data.ctext;
808 if (!data.getAttributes().empty()){
809 mout.
warn(
"STYLE elem ", data.getId(),
" contains attributes, probably meant as style: ", sprinter(data.getAttributes()));
810 ostr <<
"\n\t /" <<
"* <!-- DISCARDED attribs ";
812 ostr <<
" /--> *" <<
"/" <<
'\n';
815 if (!data.style.empty()){
819 for (
const auto & attr: data.style){
821 Sprinter::pairToStream(ostr, attr, StyleXML::styleRecordLayout);
835 for (
const auto & entry: tree.getChildren()){
837 if (entry.second->isComment()){
839 ostr << fill <<
"/* "<< entry.second->ctext <<
" */" <<
'\n';
844 if (!entry.second->ctext.empty()){
846 ostr << fill <<
" " << entry.first <<
" {" << entry.second->ctext <<
"} /* CTEXT */ \n";
849 if (!entry.second->getAttributes().empty()){
850 ostr << fill <<
" " << entry.first <<
" {\n";
851 for (
const auto & attr: entry.second->getAttributes()){
853 ostr << attr.first <<
':' << attr.second <<
';';
856 ostr << fill <<
" }\n";
875 bool ALL_CTEXT =
true;
877 for (
const auto & entry: children){
878 if (!entry.second->isCText()){
889 for (
const auto & entry: children){
905 for (
const auto & entry: children){
906 toStream(ostr, entry.second, entry.first, indent+1);
916 data.nodeToStream(ostr, CLOSING_TAG);
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
bool typeIs(const T2 &arg, const T3... args) const
Return true, if type is any of the arguments.
Definition XML.h:85
virtual bool isExplicit() const
Tell if this element should always have an explicit closing tag even when empty, like <STYLE></STYLE>
Definition XML.cpp:85
static TX & xmlAssign(TX &tree, const V &arg)
Assign another tree structure to another.
Definition XML.h:575
virtual bool isSingular() const
Tell if this element should always have an explicit closing tag even when empty, like <STYLE></STYLE>
Definition XML.cpp:79
void addClass(const TT &... args)
Style class.
Definition TreeXML.h:220
void setId()
Makes ID a visible attribute.
Definition XML.h:173
void setStyle(const std::string &key, const std::initializer_list< V > &l)
Set style of an element.
Definition XML.h:385
const std::string & getId() const
Returns ID of this element. Hopefully a unique ID...
Definition XML.h:192
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:301
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:618
static const std::map< char, std::string > encodingMap
User may optionally filter attributes and CTEST with this using StringTools::replace(XML::encodingMap...
Definition XML.h:75
void setId(const TT &...args)
Concatenates arguments to an id.
Definition XML.h:186
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:679
static std::ostream & toStream(std::ostream &ostr, const TR &tree, const std::string &defaultTag="ELEM", int indent=0)
Definition XML.h:741
static T & xmlAddChild(T &tree, const std::string &key)
Definition XML.h:692
static TX & xmlAssign(TX &tree, std::initializer_list< std::pair< const char *, const Variable > > l)
Tree.
Definition XML.h:587
static TX & xmlAssign(TX &dst, const typename TX::xml_node_t &src)
Assign another tree structure to another.
Definition XML.h:532
virtual void clear()
Clear style, class and string data but keep the element type.
Definition XML.cpp:73
void reset()
Clear style, class and string data as well as the element type.
Definition XML.h:163
void setStyle(const std::string &key, const V &value)
For element/class/id, assign ...
Definition XML.h:401
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:97
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:204
void setAttribute(const std::string &key, const V &value)
"Final" implementation.
Definition XML.h:308
static X & xmlAssignNode(X &dst, const X &src)
Assign another tree structure to another.
Definition XML.h:554
static TX & xmlAssign(TX &dst, const TX &src)
Assign another tree structure to another.
Definition XML.h:513
bool hasClass(const V &cls) const
Definition XML.h:455
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:179
Definition DataSelector.cpp:1277