45#include <drain/Sprinter.h>
46#include <drain/FlexibleVariable.h>
51#include "ReferenceMap.h"
60class XML :
protected ReferenceMap2<FlexibleVariable> {
68 static const intval_t UNDEFINED = 0;
69 static const intval_t COMMENT = 1;
70 static const intval_t CTEXT = 2;
71 static const intval_t SCRIPT = 3;
72 static const intval_t STYLE = 4;
73 static const intval_t STYLE_SELECT = 5;
80 NONBREAKABLE_SPACE =
' ',
124 void setType(
const T &t){
125 const intval_t t2 =
static_cast<intval_t
>(t);
142 const intval_t & getType()
const {
149 return type != UNDEFINED;
156 template <
class T2,
class ...T3>
158 bool typeIs(
const T2 & arg,
const T3... args)
const {
159 if (type ==
static_cast<intval_t
>(arg)){
174 static int getCount(){
180 bool isUndefined()
const {
181 return type == UNDEFINED;
185 bool isComment()
const {
186 return type == COMMENT;
190 bool isCText()
const {
191 return type == CTEXT;
195 bool isStyle()
const {
196 return type == STYLE;
200 bool isScript()
const {
201 return type == SCRIPT;
247 template <
char C=
'\0',
typename ...TT>
265 template <
class ...T>
281 void setText(
const std::string & s);
283 template <
class ...T>
284 void setText(
const T & ...args) {
288 template <
class ...T>
289 void setTextSafe(
const T & ...args) {
297 const std::string & getText()
const {
302 const std::string & getUrl(){
307 void setUrl(
const std::string & s){
312 template <
class ...T>
314 void setName(
const T & ...args){
320 return (*
this)[
"data-name"];
328 return map_t::empty();
332 const map_t & getAttributes()
const {
338 map_t & getAttributes(){
358 V get(
const std::string & key,
const V & defaultValue)
const {
363 std::string get(
const std::string & key,
const char * defaultValue)
const {
371 (*this)[key] = value;
378 (*this)[key] = value;
385 (*this)[key] = value;
390 void removeAttribute(
const std::string & s){
392 if (it != this->end()){
406 intval_t type = XML::UNDEFINED;
428 const StyleXML & getStyle()
const {
433 void setStyle(
const StyleXML & s){
438 void setStyle(
const std::string & value){
440 if (type == UNDEFINED){
441 mout.reject<LOG_WARNING>(
"setting style for UNDEFINED elem: ", value);
442 mout.unimplemented<LOG_WARNING>(
"future option: set type to STYLE_SELECT");
444 else if (type == STYLE){
445 mout.reject<LOG_WARNING>(
"not setting style for STYLE elem: ", value);
453 void setStyle(
const char *value){
454 setStyle(std::string(value));
458 void setStyle(
const std::string & key,
const std::string & value){
460 if (type == UNDEFINED){
461 mout.reject<LOG_WARNING>(
"setting style for UNDEFINED elem: ", key,
'=', value);
462 mout.unimplemented<LOG_WARNING>(
"future option: set type to STYLE_SELECT");
464 else if (type == STYLE){
465 mout.reject<LOG_WARNING>(
"not setting style for STYLE elem: ", value);
468 this->style[key] = value;
482 void setStyle(
const std::string & key,
const std::initializer_list<V> &l){
488 this->style[key] = l;
498 void setStyle(
const std::string & key,
const V & value){
501 drain::Logger(__FILE__, __FUNCTION__).
reject<LOG_WARNING>(
"Setting style of STYLE: ", key,
"=", value);
504 this->style[key] = value;
509 void setStyle(
const std::initializer_list<std::pair<const char *,const drain::Variable> > &args){
510 drain::SmartMapTools::setValues(style, args);
519 ClassListXML classList;
523 const ClassListXML & getClasses()
const {
527 template <
typename ... TT>
530 classList.add(args...);
539 return classList.has(cls);
543 void removeClass(
const std::string & s) {
554 void specificAttributesToStream(std::ostream & ostr)
const;
557 enum tag_display_mode {
561 EMPTY_TAG = OPENING_TAG | CLOSING_TAG,
567 std::ostream & nodeToStream(std::ostream & ostr, tag_display_mode mode=EMPTY_TAG)
const = 0;
573 std::ostream &
toStream(std::ostream & ostr,
const TR & tree,
const std::string & defaultTag=
"ELEM",
int indent=0);
593 static const std::map<char,char> keyMap = {
602 static const std::map<char,std::string> valueMap = {
603 {entity_t::QUOTE,
"'"},
604 {entity_t::LESS_THAN,
"(("},
605 {entity_t::GREATER_THAN,
"))"},
621 template <
typename N>
628 if (dst.getType() != src.getType()){
631 dst.setType(src.getType());
633 dst.getAttributes().importMap(src.getAttributes());
634 dst.setStyle(src.getStyle());
636 dst.ctext = src.ctext;
667DRAIN_ENUM_OSTREAM(XML::entity_t);
670std::ostream &
XML::toStream(std::ostream & ostr,
const TR & tree,
const std::string & defaultTag,
int indent){
674 const typename TR::container_t & children = tree.getChildren();
677 const typename TR::node_data_t & data = tree.data;
679 tag_display_mode mode = EMPTY_TAG;
682 data.nodeToStream(ostr, mode);
687 if (!data.ctext.empty()){
689 if (data.isSingular()){
698 if (!children.empty()){
700 if (data.isSingular()){
701 mout.
warn(
"Singular (hence normally empty) element <", tree->getTag(),
" id='", data.getId(),
"' ...> has ", children.size(),
" children?");
706 if (data.isExplicit()){
709 else if (data.isSingular()){
716 std::string fill(2*indent,
' ');
718 data.nodeToStream(ostr, mode);
720 if (mode == EMPTY_TAG){
725 else if (data.isStyle()){
730 if (!data.ctext.empty()){
732 ostr << fill << data.ctext;
737 if (!data.getAttributes().empty()){
738 mout.
warn(
"STYLE elem ", data.getId(),
" contains attributes, probably meant as style: ", sprinter(data.getAttributes()));
739 ostr <<
"\n\t /" <<
"* <!-- DISCARDED attribs ";
741 ostr <<
" /--> *" <<
"/" <<
'\n';
744 if (!data.style.empty()){
748 for (
const auto & attr: data.style){
750 Sprinter::pairToStream(ostr, attr, StyleXML::styleRecordLayout);
764 for (
const auto & entry: tree.getChildren()){
766 if (entry.second->isComment()){
768 ostr << fill <<
"/* "<< entry.second->ctext <<
" */" <<
'\n';
773 if (!entry.second->ctext.empty()){
775 ostr << fill <<
" " << entry.first <<
" {" << entry.second->ctext <<
"} /* CTEXT */ \n";
778 if (!entry.second->getAttributes().empty()){
779 ostr << fill <<
" " << entry.first <<
" {\n";
780 for (
const auto & attr: entry.second->getAttributes()){
782 if (attr.first ==
"id"){
787 ostr << attr.first <<
':' << attr.second <<
';';
791 ostr << fill <<
" }\n";
807 if (data.isScript()){
817 bool ALL_CTEXT =
true;
819 for (
const auto & entry: children){
820 if (!entry.second->isCText()){
831 for (
const auto & entry: children){
848 for (
const auto & entry: children){
849 toStream(ostr, entry.second, entry.first, indent+1);
859 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:158
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:148
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:236
void setStyle(const std::string &key, const std::initializer_list< V > &l)
Set style of an element.
Definition XML.h:482
const std::string & getId() const
Returns ID of this element. Hopefully a unique ID...
Definition XML.h:255
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:377
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:589
void setId(const TT &...args)
Concatenates arguments to an id.
Definition XML.h:249
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:670
static N & xmlAssignNode(N &dst, const N &src)
Assign tree node (data) to another.
Definition XML.h:623
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:109
void setStyle(const std::string &key, const V &value)
For element/class/id, assign ...
Definition XML.h:498
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:267
void setAttribute(const std::string &key, const V &value)
"Final" implementation.
Definition XML.h:384
bool hasClass(const V &cls) const
Definition XML.h:538
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:242
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 EnumUtils.h:52
Default implementation.
Definition Type.h:541