74 class PCL_CLASS XMLDocument;
75 class PCL_CLASS XMLElement;
116 template <
typename T>
119 return c == 0x20 || c == 9;
126 template <
typename T>
129 return c == 0x0A || c == 0x0D;
137 template <
typename T>
140 return IsWhiteSpaceChar( c ) || IsLineBreakChar( c );
148 template <
typename T>
151 return c >= T(
'a' ) && c <= T(
'z' )
152 || c >= T(
'A' ) && c <= T(
'Z' )
155 || c >= 0xC0 && c <= 0xD6
156 || c >= 0xD8 && c <= 0xF6
157 || c >= 0xF8 && c <= 0x2FF
158 || c >= 0x370 && c <= 0x37D
159 || c >= 0x37F && c <= 0x1FFF
160 || c >= 0x200C && c <= 0x200D
161 || c >= 0x2070 && c <= 0x218F
162 || c >= 0x2C00 && c <= 0x2FEF
163 || c >= 0x3001 && c <= 0xD7FF
164 || c >= 0xF900 && c <= 0xFDCF
165 || c >= 0xFDF0 && c <= 0xFFFD
174 template <
typename T>
177 return IsNameStartChar( c )
178 || c >= T(
'0' ) && c <= T(
'9' )
182 || c >= 0x0300 && c <= 0x036F
183 || c >= 0x203F && c <= 0x2040;
191 template <
typename T>
194 return c >= 0x00 && c <= 0x08
195 || c >= 0x0B && c <= 0x0C
196 || c >= 0x0E && c <= 0x1F
197 || c >= 0x7F && c <= 0x84
198 || c >= 0x86 && c <= 0x9F;
210 if ( IsNameStartChar( *name ) )
213 if ( ++i == name.
End() )
215 if ( !IsNameChar( *i ) )
280 return EncodedText(
String( i, j ), apos );
318 return ReferenceValue( reference.
Begin(), reference.
End() );
371 return m_parent ==
nullptr;
401 namespace XMLNodeType
405 Undefined = 0x00000000,
406 ChildNode = 0x80000000,
407 Unknown = 0x10000000,
408 Element = 0x00000001,
411 ProcessingInstructions = 0x00000008,
514 using node_type = XMLNodeType::mask_type;
534 : m_type( x.NodeType() )
535 , m_location( x.m_location )
545 m_location = x.m_location;
561 return m_type.IsFlagSet( XMLNodeType::ChildNode );
578 return NodeType() == XMLNodeType::Element;
587 return NodeType() == XMLNodeType::Text;
596 return NodeType() == XMLNodeType::Comment;
637 virtual void Serialize(
IsoString& text,
bool autoFormat,
char indentChar,
unsigned indentSize,
unsigned level )
const = 0;
653 Serialize( text, autoFormat, indentChar, indentSize, level );
718 :
Error( whileDoing + node.Location().ToString() +
": " + whatHappened )
739 :
Error( whileDoing + where.ToString() +
": " + whatHappened )
839 return m_name == x.m_name;
851 return m_name < x.m_name;
927 return int( m_list.Length() );
935 return m_list.IsEmpty();
954 return m_list.Begin();
966 #ifndef __PCL_NO_STL_COMPATIBLE_ITERATORS
990 return m_list.Contains( name );
1001 return (a != m_list.End()) ? a->Value() :
String();
1023 iterator a = m_list.Search( name );
1024 if ( a == m_list.End() )
1027 a->SetValue( value );
1040 iterator a = m_list.Search( attribute );
1041 if ( a == m_list.End() )
1042 m_list.Add( attribute );
1055 SetAttribute( attribute );
1076 for (
auto a : list )
1087 SetAttributes( list );
1098 iterator a = m_list.Search( name );
1099 if ( a != m_list.End() )
1200 :
XMLNode( XMLNodeType::Element )
1209 :
XMLNode( XMLNodeType::Element )
1211 , m_attributes( attributes )
1221 :
XMLNode( XMLNodeType::Element )
1223 , m_attributes( attributes )
1233 , m_name( x.m_name )
1234 , m_attributes( x.m_attributes )
1235 , m_childTypes( x.m_childTypes )
1237 for (
const XMLNode& node : x.m_childNodes )
1238 m_childNodes << node.
Clone();
1246 (void)XMLNode::operator =( x );
1248 m_attributes = x.m_attributes;
1249 m_childTypes = x.m_childTypes;
1250 m_childNodes.Destroy();
1251 for (
const XMLNode& node : x.m_childNodes )
1252 m_childNodes << node.
Clone();
1262 DestroyChildNodes();
1277 return ParentElement() ==
nullptr;
1293 return m_attributes;
1301 return !m_attributes.IsEmpty();
1310 return m_attributes.HasAttribute( name );
1320 return m_attributes.AttributeValue( name );
1342 m_attributes.SetAttribute( a );
1354 m_attributes.SetAttribute( a );
1364 SetAttribute( attribute );
1385 for (
auto a : list )
1396 SetAttributes( list );
1407 m_attributes.RemoveAttribute( name );
1415 m_attributes.Clear();
1424 m_attributes.Sort();
1436 m_attributes.Sort( p );
1454 SetAttributes( list );
1469 m_attributes.Serialize( text );
1477 return int( m_childNodes.Length() );
1486 return m_childNodes.IsEmpty();
1496 return m_childNodes[i];
1506 return m_childNodes.First();
1516 return m_childNodes.Last();
1525 return m_childNodes.Begin();
1534 return m_childNodes.End();
1537 #ifndef __PCL_NO_STL_COMPATIBLE_ITERATORS
1555 #ifndef __PCL_NO_MUTABLE_XML_ELEMENT_ITERATORS
1563 return m_childNodes.Begin();
1572 return m_childNodes.End();
1581 return m_childNodes.ConstBegin();
1590 return m_childNodes.ConstEnd();
1593 # ifndef __PCL_NO_STL_COMPATIBLE_ITERATORS
1618 return m_childTypes.IsFlagSet( XMLNodeType::Element );
1626 return m_childTypes.IsFlagSet( XMLNodeType::Text );
1634 return m_childTypes.IsFlagSet( XMLNodeType::CDATA );
1643 return m_childTypes.IsFlagSet( XMLNodeType::ProcessingInstructions );
1652 return m_childTypes.IsFlagSet( XMLNodeType::Comment );
1669 for (
const XMLNode& node : m_childNodes )
1670 if ( node.IsElement() )
1675 element.GetChildElements( list, recursive );
1689 GetChildElements( list, recursive );
1696 void GetChildElementsByName( child_element_list& list,
const String& name,
bool recursive )
const
1698 for (
const XMLNode& node : m_childNodes )
1699 if ( node.IsElement() )
1702 if ( element.
Name() == name )
1706 element.GetChildElementsByName( list, name, recursive );
1722 GetChildElementsByName( list, name, recursive );
1736 for (
const XMLNode& node : m_childNodes )
1737 if ( node.IsElement() )
1740 if ( element.
Name() == name )
1754 for (
const XMLNode& node : m_childNodes )
1755 if ( types.
IsFlagSet( node.NodeType() ) )
1759 if ( node.IsElement() )
1760 static_cast<const XMLElement&
>( node ).GetChildNodesByType( list, types, recursive );
1776 GetChildNodesByType( list, types, recursive );
1784 void GetChildNodesThat(
XMLNodeList& list, UP u,
bool recursive )
const
1786 for (
const XMLNode& node : m_childNodes )
1791 if ( node.IsElement() )
1792 static_cast<const XMLElement&
>( node ).GetChildNodesThat( list, u, recursive );
1811 GetChildNodesThat( list, u, recursive );
1826 for (
const XMLNode& node : m_childNodes )
1831 if ( node.IsElement() )
1832 if (
static_cast<const XMLElement&
>( node ).HasChildNodeThat( u, recursive ) )
1846 m_childNodes << node;
1847 node->m_parent =
this;
1848 node->m_type.
SetFlag( XMLNodeType::ChildNode );
1849 m_childTypes.SetFlag( node->
NodeType() );
1860 AddChildNode( node );
1874 AddChildNode( &node );
1885 AddChildNodes( nodes );
1898 node->m_location = location;
1899 AddChildNode( node );
1908 m_childNodes.Destroy();
1909 m_childTypes = XMLNodeType::Undefined;
1924 m_childNodes.
Clear();
1925 m_childTypes = XMLNodeType::Undefined;
1944 void Serialize(
IsoString& text,
bool autoFormat,
char indentChar,
unsigned indentSize,
unsigned level )
const override;
2019 XMLText(
const String& text,
bool preserveSpaces =
true,
bool verbatim =
false )
2020 :
XMLNode( XMLNodeType::Text )
2021 , m_text( preserveSpaces ? text :
XML::CollapsedSpaces(
XML::TrimmedSpaces( text ) ) )
2022 , m_preserveSpaces( preserveSpaces )
2023 , m_verbatim( verbatim )
2052 return m_preserveSpaces;
2098 void Serialize(
IsoString& text,
bool autoFormat,
char indentChar,
unsigned indentSize,
unsigned level )
const override;
2111 return !m_preserveSpaces;
2117 bool m_preserveSpaces =
true;
2118 bool m_verbatim =
false;
2145 :
XMLNode( XMLNodeType::CDATA )
2182 void Serialize(
IsoString& text,
bool autoFormat,
char indentChar,
unsigned indentSize,
unsigned level )
const override;
2215 :
XMLNode( XMLNodeType::ProcessingInstructions )
2216 , m_target( target )
2217 , m_instructions( instructions )
2244 return m_instructions;
2260 void Serialize(
IsoString& text,
bool autoFormat,
char indentChar,
unsigned indentSize,
unsigned level )
const override;
2293 :
XMLNode( XMLNodeType::Comment )
2294 , m_comment( comment )
2329 void Serialize(
IsoString& text,
bool autoFormat,
char indentChar,
unsigned indentSize,
unsigned level )
const override;
2359 :
XMLNode( XMLNodeType::Unknown )
2361 , m_parameters( parameters )
2388 return m_parameters;
2404 void Serialize(
IsoString& text,
bool autoFormat,
char indentChar,
unsigned indentSize,
unsigned level )
const override;
2433 : m_version( version )
2434 , m_encoding( encoding )
2435 , m_standalone( standalone )
2465 return m_standalone;
2475 return !m_version.IsEmpty();
2493 bool m_standalone =
false;
2519 , m_definition( definition )
2541 return m_definition;
2551 return !m_name.IsEmpty();
2644 namespace XMLParserOption
2648 IgnoreComments = 0x00000001,
2649 IgnoreUnknownElements = 0x00000002,
2650 IgnoreStrayCharacters = 0x00000004,
2651 NormalizeTextSpaces = 0x00000008
2762 using parser_option = XMLParserOption::mask_type;
2784 RemoveElementFilter();
2840 m_docType = docType;
2871 m_nodes.RemovePointer( m_root );
2882 return int( m_nodes.Length() );
2891 return m_nodes.IsEmpty();
2910 return m_nodes.Begin();
2919 return m_nodes.End();
2922 #ifndef __PCL_NO_STL_COMPATIBLE_ITERATORS
3008 m_nodes.Destroy( dum,
3013 const XMLElement& element = static_cast<const XMLElement&>( node );
3014 if ( !filter( nullptr, element.Name() ) ||
3015 !filter( nullptr, element.Name(), element.Attributes() ) )
3030 m_nodes.Destroy( dum,
3035 const XMLElement& element = static_cast<const XMLElement&>( node );
3036 if ( (caseSensitive ? element.Name().Compare( name ) : element.Name().CompareIC( name )) == 0 )
3054 delete m_filter, m_filter = filter;
3064 SetElementFilter(
nullptr );
3074 m_parserOptions.SetFlag( option, on );
3084 m_parserOptions = options;
3093 m_parserOptions.Clear();
3131 return m_autoFormatting;
3140 m_autoFormatting = enable;
3149 EnableAutoFormatting( !disable );
3163 return m_indentSize;
3191 m_indentSize =
Range( indentSize, 0, 8 );
3204 return m_indentTabs;
3213 m_indentTabs = enable;
3222 EnableIndentTabs( !disable );
3238 return Serialize(
false );
3256 return Serialize(
true );
3291 bool m_autoFormatting =
false;
3292 bool m_indentTabs =
false;
3293 int m_indentSize = 3;
3295 IsoString Serialize(
bool isHTML )
const;
A simple exception with an associated error message.
constexpr bool IsFlagSet(enum_type e) const
void SetFlag(enum_type e, bool on=true)
typename FlagType< std::is_unsigned< enum_type >::value >::type flag_type
bool IsEmpty() const noexcept
Eight-bit string (ISO/IEC-8859-1 or UTF-8 string)
string_base::const_iterator const_iterator
Dynamic list of XML element attributes.
void SetAttribute(const String &name, const String &value)
const_iterator begin() const
XMLAttributeList(const String &text)
XMLAttributeList()=default
const_iterator end() const
const_iterator End() const
void SetAttribute(const XMLAttribute &attribute)
bool HasAttribute(const String &name) const
String AttributeValue(const String &name) const
void RemoveAttribute(const String &name)
void Parse(const String &text)
XMLAttributeList(const XMLAttributeList &)=default
void SetAttributes(const XMLAttributeList &list)
void Serialize(IsoString &text) const
const_iterator Begin() const
void SetValue(const String &text)
XMLAttribute(const XMLAttribute &)=default
const String & Value() const
XMLAttribute(const String &name, const String &value=String())
String EncodedValue() const
const String & Name() const
XMLNode * Clone() const override
void Serialize(IsoString &text, bool autoFormat, char indentChar, unsigned indentSize, unsigned level) const override
const String & CData() const
XMLCDATA(const XMLCDATA &)=default
Root base class of all XML document components.
XMLComponent(const XMLComponent &)=default
XMLElement * ParentElement() const
bool IsStandaloneDocument() const
XMLDeclaration(const String &version=String(), const String &encoding=String(), bool standalone=false)
void Serialize(IsoString &text) const
const String & DocumentEncoding() const
const String & Version() const
XMLDeclaration(const XMLDeclaration &)=default
XMLDocTypeDeclaration(const String &name=String(), const String &definition=String())
const String & Definition() const
const String & Name() const
XMLDocTypeDeclaration(const XMLDocTypeDeclaration &)=default
void Serialize(IsoString &text) const
XML document parsing and generation
void SerializeToFileAsHTML(const String &path) const
IsoString SerializeAsHTML() const
XMLNodeList::const_iterator const_iterator
const XMLDeclaration & XML() const
bool IsIndentTabs() const
XMLElement * ReleaseRootElement()
void SetParserOption(parser_option option, bool on=true)
const XMLDocTypeDeclaration & DocType() const
XMLDocument(const XMLDocument &)=delete
void SetRootElement(XMLElement *element)
XMLNodeList::iterator iterator
void RemoveElementsByName(const String &name, bool caseSensitive=true)
const_iterator Begin() const
const_iterator begin() const
void EnableIndentTabs(bool enable=true)
void DisableIndentTabs(bool disable=true)
void EnableAutoFormatting(bool enable=true)
void SerializeToFile(const String &path) const
const_iterator End() const
const XMLElement * RootElement() const
bool IsAutoFormatting() const
void SetXML(const XMLDeclaration &xml)
void RemoveElementsByFilter(const XMLElementFilter &filter)
void SetElementFilter(XMLElementFilter *filter)
void SetXML(const String &version="1.0", const String &encoding="UTF-8", bool standalone=false)
void DisableAutoFormatting(bool disable=true)
void SetDocType(const XMLDocTypeDeclaration &docType)
IsoString Serialize() const
void ClearParserOptions()
void SetParserOptions(XMLParserOptions options)
void Parse(const String &text)
const_iterator end() const
void RemoveElementFilter()
void AddNode(XMLNode *node)
void SetIndentSize(int indentSize)
bool HasChildNodeThat(UP u, bool recursive=false) const
void Serialize(IsoString &text, bool autoFormat, char indentChar, unsigned indentSize, unsigned level) const override
bool HasAttribute(const String &name) const
const_iterator ConstBegin() const
XMLElement(const String &name, const XMLAttributeList &attributes=XMLAttributeList())
void SerializeAsHTML(IsoString &text, bool autoFormat, char indentChar, unsigned indentSize, unsigned level) const override
const_iterator End() const
void SetAttribute(const XMLAttribute &attribute)
bool IsRootElement() const
String AttributeValue(const String &name) const
void SetAttribute(const String &name, const String &value)
bool HasAttributes() const
XMLNodeList::iterator iterator
bool HasChildElementWithName(const String &name, bool recursive=false) const
child_element_list ChildElementsByName(const String &name, bool recursive=false) const
XMLElement(const XMLElement &x)
void SetAttributes(const XMLAttributeList &list)
const XMLNode & Last() const
const_iterator end() const
XMLNode * Clone() const override
XMLNodeList::const_iterator const_iterator
void AddChildNode(XMLNode *node)
XMLNodeList ChildNodesThat(UP u, bool recursive=false) const
void RemoveAttribute(const String &name)
void ParseAttributes(const String &text)
XMLNodeList ChildNodesByType(XMLNodeTypes types, bool recursive=false) const
void SerializeAttributes(IsoString &text) const
XMLNodeList ReleaseChildNodes()
const_iterator begin() const
const_iterator Begin() const
XMLElement(XMLElement &parent, const String &name, const XMLAttributeList &attributes=XMLAttributeList())
void SortAttributes(BP p)
bool HasProcessingInstructions() const
const_iterator ConstEnd() const
const XMLNode & First() const
void AddChildNodes(XMLNodeList &nodes)
ReferenceArray< XMLElement > child_element_list
XMLAttributeList Attributes() const
child_element_list ChildElements(bool recursive=false) const
const String & Name() const
Abstract base class of all XML document node classes.
virtual XMLNode * Clone() const =0
XMLNode(const XMLNode &x)
virtual void SerializeAsHTML(IsoString &text, bool autoFormat, char indentChar, unsigned indentSize, unsigned level) const
virtual void Serialize(IsoString &text, bool autoFormat, char indentChar, unsigned indentSize, unsigned level) const =0
const XMLNodeLocation & Location() const
virtual bool NLAfter(const XMLNode &previous) const
node_type NodeType() const
XML parsing error with automatic text location information generation
XMLParseError(const XMLNode &node, const String &whileDoing, const String &whatHappened)
XMLParseError(const XMLNodeLocation &where, const String &whileDoing, const String &whatHappened)
XMLParseError(const XMLParseError &)=default
A collection of XML document parsing options.
XML processing instructions
XMLProcessingInstructions(const String &target, const String &instructions)
void Serialize(IsoString &text, bool autoFormat, char indentChar, unsigned indentSize, unsigned level) const override
const String & Target() const
XMLNode * Clone() const override
const String & Instructions() const
XMLProcessingInstructions & operator=(const XMLProcessingInstructions &)=default
XMLProcessingInstructions(const XMLProcessingInstructions &)=default
XMLText(const String &text, bool preserveSpaces=true, bool verbatim=false)
XMLText(const XMLText &)=default
bool IsPreserveSpaces() const
void Serialize(IsoString &text, bool autoFormat, char indentChar, unsigned indentSize, unsigned level) const override
XMLNode * Clone() const override
bool NLAfter(const XMLNode &previous) const override
const String & Text() const
String SpaceTransformedText(bool collapse, bool trim) const
String EncodedText() const
Unsupported or invalid XML element.
XMLUnknownElement(const XMLUnknownElement &)=default
XMLUnknownElement(const String &name, const String ¶meters=String())
const String & Parameters() const
void Serialize(IsoString &text, bool autoFormat, char indentChar, unsigned indentSize, unsigned level) const override
XMLNode * Clone() const override
const String & Name() const
Utility functions and data for XML document parsing and generation.
static String TrimmedSpaces(String::const_iterator i, String::const_iterator j)
static String TrimmedSpaces(const String &text)
static String DecodedText(String::const_iterator i, String::const_iterator j)
static String EncodedText(String::const_iterator i, String::const_iterator j, bool apos=true)
static String EncodedText(const String &text, bool apos=true)
static bool IsValidName(const String &name)
static String CollapsedSpaces(const String &text)
static bool IsWhiteSpaceChar(T c)
static String ReferenceValue(const String &reference)
static String DecodedText(const String &text)
static bool IsSpaceChar(T c)
static bool IsNameStartChar(T c)
static String CollapsedSpaces(String::const_iterator i, String::const_iterator j)
static bool IsRestrictedChar(T c)
static bool IsNameChar(T c)
static bool IsLineBreakChar(T c)
static String ReferenceValue(String::const_iterator i, String::const_iterator j)
Array< T, A > & operator<<(Array< T, A > &x, const V &v)
bool operator==(const Array< T, A > &x1, const Array< T, A > &x2) noexcept
bool operator<(const Array< T, A > &x1, const Array< T, A > &x2) noexcept
constexpr const T & Range(const T &x, const T &a, const T &b) noexcept
String AsString(mask_type type)
A functional class for filtering XML elements.
virtual ~XMLElementFilter()
virtual bool operator()(const XMLElement *parent, const String &name) const =0
virtual bool operator()(const XMLElement *parent, const String &name, const XMLAttributeList &attributes) const
Source code location of a parsed XML document node.
XMLNodeLocation(const XMLNodeLocation &)=default
XMLNodeLocation(int line_, int column_)
XMLNodeLocation()=default