Loading...
Searching...
No Matches
Public Types | Public Member Functions | Static Public Member Functions | Public Attributes | Static Public Attributes | Protected Member Functions | Protected Attributes | Static Protected Attributes | List of all members
XML Class Referenceabstract

Base class for XML "nodes", to be data elements T for drain::Tree<T> More...

#include <XML.h>

Inheritance diagram for XML:
Inheritance graph
[legend]
Collaboration diagram for XML:
Collaboration graph
[legend]

Public Types

enum  entity_t {
  AMPERSAND = '&' , LESS_THAN = '<' , EQUAL_TO = '=' , GREATER_THAN = '>' ,
  NONBREAKABLE_SPACE = ' ' , QUOTE = '"' , CURLY_LEFT = '{' , CURLY_RIGHT = '}' ,
  TAB = '\t' , NEWLINE = '\n' , DEGREE = 176
}
 
enum  tag_display_mode {
  FLEXIBLE_TAG = 0 , OPENING_TAG = 1 , CLOSING_TAG = 2 , EMPTY_TAG = OPENING_TAG | CLOSING_TAG ,
  NON_EMPTY_TAG , NO_TAG
}
 
typedef int intval_t
 
typedef std::map< std::string, FlexibleVariable > map_t
 

Public Member Functions

map_t & getMap ()
 
const map_t & getMap () const
 
 XML ()
 User may optionally filter attributes and CTEST with this using StringTools::replace(XML::encodingMap);.
 
 XML (const XML &)
 
virtual const std::string & getTag () const =0
 
template<class T >
void setType (const T &t)
 
void setType (intval_t t)
 
const intval_t & getType () const
 
bool typeIsSet () const
 Return true, if type is any of the arguments.
 
template<class T2 , class ... T3>
bool typeIs (const T2 &arg, const T3... args) const
 Return true, if type is any of the arguments.
 
const std::string & setId ()
 Makes ID a visible attribute.
 
const std::string & setId (const std::string &s)
 Makes ID a visible attribute, with a given value.
 
template<char C = '\0', typename ... TT>
const std::string & setId (const TT &...args)
 Concatenates arguments to an id.
 
const std::string & getId () const
 Returns ID of this element. Hopefully a unique ID...
 
bool isUndefined () const
 
bool isComment () const
 
bool isCText () const
 
bool isStyle () const
 
bool isScript () const
 
bool isScopePlain () const
 
bool isScopeJS () const
 
bool isScope () const
 
virtual bool isSingular () const
 Tell if this element should always have an explicit closing tag even when empty, like <STYLE></STYLE>
 
virtual bool isExplicit () const
 Tell if this element should always have an explicit closing tag even when empty, like <STYLE></STYLE>
 
void reset ()
 Keep the element type but clear style, class and string data.
 
virtual void swapXML (XML &node)
 
template<class ... T>
void setComment (const T &...args)
 Make this node a comment. Contained tree will not be deleted. In current version, attributes WILL be rendered.
 
template<class ... T>
void setCommentSafe (const T &...args)
 Sets XML style comment <--...--> of concatenated arguments.
 
virtual void setText (const std::string &s)
 Assign the text content of this node. If the node type is undefined, set it to CTEXT.
 
template<class ... T>
void setText (const T &...args)
 
template<class ... T>
void setTextSafe (const T &...args)
 Sets slightly converted text, ensuring some XML entities.
 
template<class ... T>
void setProgramComment (const T &...args)
 Sets text surrounded by C language comment marks.
 
virtual const std::string & getText () const
 
const std::string & getUrl ()
 
virtual void setUrl (const std::string &s)
 
template<class ... T>
void setName (const T &...args)
 
const FlexibleVariable & getName () const
 
virtual bool empty () const
 
bool hasAttribute (const std::string &key)
 
bool hasUserAttribute (const std::string &key)
 
const map_t & getAttributes () const
 
map_t & getAttributes ()
 
const drain::FlexibleVariable & getAttribute (const std::string &key) const
 
drain::FlexibleVariable & getAttribute (const std::string &key)
 
template<class V >
V getAttribute (const std::string &key, const V &defaultValue) const
 
std::string getAttribute (const std::string &key, const char *defaultValue) const
 
const drain::FlexibleVariable & getUserAttribute (const std::string &key) const
 
drain::FlexibleVariable & getUserAttribute (const std::string &key)
 
template<class V >
V getUserAttribute (const std::string &key, const V &defaultValue) const
 
std::string getUserAttribute (const std::string &key, const char *defaultValue) const
 
virtual void setAttribute (const std::string &key, const std::string &value)
 Default implementation. Needed for handling units in strings, like "50%" or "640px".
 
virtual void setAttribute (const std::string &key, const char *value)
 Default implementation. Needed for handling units in strings, like "50%" or "640px".
 
template<class ... TT>
void setAttribute (const std::string &key, const TT &... args)
 "Final" implementation.
 
template<class ... TT>
void setUserAttribute (const std::string &key, const TT &... args)
 
void removeAttribute (const std::string &s)
 
const StyleXML & getStyle () const
 
Variable & getStyle (const std::string &key)
 
template<class T >
T getStyle (const std::string &key, const T &defaultValue) const
 
void setStyle (const StyleXML &s)
 
void setStyle (const std::string &value)
 
void setStyle (const char *value)
 
void setStyle (const std::string &key, const std::string &value)
 
template<class V >
void setStyle (const std::string &key, const std::initializer_list< V > &l)
 Set style of an element.
 
template<class ... TT>
void setStyle (const std::string &key, const TT &... args)
 For element/class/id, assign ...
 
void setStyle (const std::initializer_list< std::pair< const char *, const drain::Variable > > &args)
 
template<class V >
void setStyle (const std::map< std::string, V > &args)
 
const ClassListXML & getClasses () const
 
template<typename ... TT>
void addClass (const TT &... args)
 
void addClass (const ClassListXML &clist)
 
template<class V >
bool hasClass (const V &cls) const
 
template<class V >
void removeClass (const V &cls)
 
void clearClasses ()
 
virtual void specificAttributesToStream (std::ostream &ostr) const
 
virtual std::ostream & nodeToStream (std::ostream &ostr, tag_display_mode mode=EMPTY_TAG) const =0
 

Static Public Member Functions

static const std::map< char, std::string > & getKeyConversionMap ()
 Characters that must be avoided in XML attribute keys: space, tab, =.
 
static const std::map< char, std::string > & getAttributeConversionMap ()
 Characters that must be avoided in XML attribute values: ".
 
static const std::map< char, std::string > & getCTextConversionMap ()
 Characters to avoid in XML free text: <, >, {, },.
 
static int getCount ()
 
static int getNewIndex ()
 
template<class N >
static std::ostream & toStream (std::ostream &ostr, const UnorderedMultiTree< N > &tree, int indent=0)
 
template<class N >
static std::ostream & styleToStream (std::ostream &ostr, const UnorderedMultiTree< N > &tree, int indent=0)
 
template<class N >
static std::ostream & textToStream (std::ostream &ostr, const UnorderedMultiTree< N > &tree, int indent=0)
 
static const std::map< char, std::string > & getEntityMap ()
 Handy map for converting characters to XML entities. Example: '&' -> "&amp;".
 
template<class V >
static void xmlAttribToStream (std::ostream &ostr, const std::string &key, const V &value)
 Handy map for converting characters to XML entities. Example: '&' -> "&amp;".
 
template<typename N >
static N & xmlAssignNode (N &dst, const N &src)
 Assign tree node (data) to another.
 

Public Attributes

std::string ctext
 

Static Public Attributes

static const intval_t UNDEFINED = 0
 
static const intval_t COMMENT = 1
 
static const intval_t CTEXT = 2
 
static const intval_t SCRIPT = 3
 
static const intval_t STYLE = 4
 
static const intval_t STYLE_SELECT = 5
 
static const intval_t JAVASCRIPT = 6
 
static const intval_t SCOPE = 8
 
static const intval_t SCOPE_CURLY = SCOPE|CURLY
 
static const intval_t XMLBASE = 10
 
static const std::string USER_ATTR_PREFIX = "data-"
 In XML variants, like SVG, it is possible to use any user defined attributes using prefix "data-" .
 
static int nextID = 0
 

Protected Member Functions

bool typeIs () const
 
virtual void handleType ()
 

Protected Attributes

intval_t type = XML::UNDEFINED
 
std::string id
 
std::string url
 
StyleXML style
 
ClassListXML classList
 

Static Protected Attributes

static const intval_t XML_LIKE = 0
 
static const intval_t CURLY = 1
 
static const intval_t PARENTHESES = 2
 
static const intval_t BRACES = 3
 
static const StyleXML curlyStyleXML
 
static const std::string SCOPE_BEGIN
 
static const std::string SCOPE_END
 
static const std::string COMMENT_BEGIN
 
static const std::string COMMENT_END
 

Detailed Description

Base class for XML "nodes", to be data elements T for drain::Tree<T>

In derived classes, the values will be typically of enumeration type. The derived classes often reuse basic types, especially UNDEFINED, COMMENT, and CTEXT. In that case, the first enum values should "inherit" the integer values from this basic class with explicit enum value assigment. The first 10 values are recommended to be reserved for this purpose, allowing future extensions in this base class.

Member Typedef Documentation

◆ intval_t

typedef int intval_t

In derived classes, the values will be typically of enumeration type. The derived classes often reuse basic types, especially UNDEFINED, COMMENT, and CTEXT. In that case, the first enum values should "inherit" the integer values from this basic class with explicit enum value assigment. The first 10 values are recommended to be reserved for this purpose, allowing future extensions in this base class.

Member Function Documentation

◆ getTag()

virtual const std::string & getTag ( ) const
pure virtual

All the derived classes must have a method returning a tag associated with a supported element type.

Implemented in NodeXML< T >, NodeXML< GDAL::tag_t >, NodeXML< Html::tag_t >, NodeXML< SLD::tag_t >, and NodeXML< svg::tag_t >.

◆ hasClass()

template<class V >
bool hasClass ( const V &  cls) const
inline
Template Parameters
V– string or enum type

◆ isExplicit()

bool isExplicit ( ) const
virtual

Tell if this element should always have an explicit closing tag even when empty, like <STYLE></STYLE>

Tell if this element should always have an explicit closing tag even when empty, like <STYLE></STYLE>

Reimplemented in NodeHTML.

◆ isSingular()

bool isSingular ( ) const
virtual

Tell if this element should always have an explicit closing tag even when empty, like <STYLE></STYLE>

Reimplemented in NodeSLD, NodeSVG, and NodeHTML.

◆ nodeToStream()

virtual std::ostream & nodeToStream ( std::ostream &  ostr,
tag_display_mode  mode = EMPTY_TAG 
) const
pure virtual

◆ reset()

void reset ( )

Keep the element type but clear style, class and string data.

Clears style, classList, cdata.

virtual void clear(); Clear type, style, class and string data.

◆ setAttribute() [1/2]

virtual void setAttribute ( const std::string &  key,
const char *  value 
)
inlinevirtual

Default implementation. Needed for handling units in strings, like "50%" or "640px".

Reimplemented in NodeSLD.

◆ setAttribute() [2/2]

virtual void setAttribute ( const std::string &  key,
const std::string &  value 
)
inlinevirtual

Default implementation. Needed for handling units in strings, like "50%" or "640px".

Reimplemented in NodeSLD.

◆ setId()

const std::string & setId ( )
inline

Makes ID a visible attribute.

Also a terminal function for

◆ setProgramComment()

template<class ... T>
void setProgramComment ( const T &...  args)
inline

Sets text surrounded by C language comment marks.

Does not change type of this node.

This comment style is applicable in

  • Cascaded Style Sheets (CSS)
  • C code
  • JavaScript code

◆ setStyle() [1/2]

template<class V >
void setStyle ( const std::string &  key,
const std::initializer_list< V > &  l 
)
inline

Set style of an element.

rect.setStyle("dash-array",{2,5,3});

◆ setStyle() [2/2]

template<class ... TT>
void setStyle ( const std::string &  key,
const TT &...  args 
)
inline

For element/class/id, assign ...

template <typename ... TT> inline void malli(const TT &... args) { something(args...); } Assing multiple values to a style. Examples:

node.setStyle("font-size", 20, "px");

TODO: handle units so that they become directly appended.

◆ setText()

void setText ( const std::string &  s)
virtual

Assign the text content of this node. If the node type is undefined, set it to CTEXT.

Parameters
s- assigned to text content.

TODO: strict/exclusive, i.e. non-element behavior

◆ setTextSafe()

template<class ... T>
void setTextSafe ( const T &...  args)
inline

Sets slightly converted text, ensuring some XML entities.

At least these characters become encoded:

  • entity_t::LESS_THAN
  • entity_t::GREATER_THAN
  • entity_t::NONBREAKABLE_SPACE

Future implementation may add more.

◆ setType() [1/2]

template<class T >
void setType ( const T &  t)
inline

Recommended way to mark a node out is to change it to a comment.

◆ setType() [2/2]

void setType ( intval_t  t)

Initializes element accordind to the type

◆ setUrl()

virtual void setUrl ( const std::string &  s)
inlinevirtual

Virtual, because derived classes have different attribute names for the url.

◆ specificAttributesToStream()

void specificAttributesToStream ( std::ostream &  ostr) const
virtual

Reimplemented in NodeSVG.

◆ swapXML()

void swapXML ( XML &  node)
virtual

inline void reset(){ type = UNDEFINED; clear(); }

◆ toStream()

template<class N >
std::ostream & toStream ( std::ostream &  ostr,
const UnorderedMultiTree< N > &  tree,
int  indent = 0 
)
static

NEW

iterate children - note the use of default tag

◆ typeIsSet()

bool typeIsSet ( ) const
inline

Return true, if type is any of the arguments.

Overlapping function: isUndefined() - ?

◆ xmlAssignNode()

template<typename N >
static N & xmlAssignNode ( N &  dst,
const N &  src 
)
inlinestatic

Assign tree node (data) to another.

Template Parameters
N- xml node (e.g. NodeXML, NodeSVG, NodeHTML)
See also
clear()

Member Data Documentation

◆ curlyStyleXML

const StyleXML curlyStyleXML
staticprotected
Initial value:
= {
{SCOPE_BEGIN, "{"},
{SCOPE_END, "}"},
{COMMENT_BEGIN, "<!-- "},
{COMMENT_END, " -->"}
}

The documentation for this class was generated from the following files: