Class XMLElementImpl
- All Implemented Interfaces:
Element,XMLElement
- Direct Known Subclasses:
XMLElementAdapter
XMLElement
that supports attributes, namespaces, children, text, CDATA and
comments.- Author:
- Thomas Thrien (thomas.thrien@tquadrat.org)
- Version:
- $Id: XMLElementImpl.java 1086 2024-01-05 23:18:33Z tquadrat $
- Since:
- 0.0.5
- UML Diagram
-
UML Diagram for "org.tquadrat.foundation.xml.builder.internal.XMLElementImpl"
-
Nested Class Summary
Nested classes/interfaces inherited from interface org.tquadrat.foundation.xml.builder.XMLElement
XMLElement.Flags -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final AttributeSupportThe attribute support.private final ChildSupportThe child support.private final StringThe element name.private ElementThe parent element.Fields inherited from interface org.tquadrat.foundation.xml.builder.XMLElement
EMPTY_XMLElement_ARRAY, NO_APPEND -
Constructor Summary
ConstructorsModifierConstructorDescriptionXMLElementImpl(String elementName) Creates a newXMLElementImplinstance.protectedXMLElementImpl(String elementName, Set<XMLElement.Flags> flags) Creates a newXMLElementImplinstance. -
Method Summary
Modifier and TypeMethodDescriptionfinal XMLElementaddCDATA(CharSequence text) Adds aCDATAelement to this XML element.final <E extends XMLElement>
XMLElementaddChild(E child) Adds a child to this element.final XMLElementaddComment(CharSequence comment) Adds a comment.final XMLElementaddPredefinedMarkup(CharSequence markup) Adds predefined markup.final XMLElementaddText(CharSequence text) Adds text to this element.getAttribute(String name) Returns the value for the attribute with the given name.Provides read access to the attributes.Collection<? extends Element> Provides access to the children for this element; the returned collection is not modifiable.final StringReturns the name of the element.final Set<XMLElement.Flags> getFlags()Returns the flags for this element.final Collection<Namespace> Provides access to the namespaces for this element; the returned collection is not modifiable.Returns the parent of this element.final Comparator<String> Returns the attribute sort order.booleanReturnstrueif the element has children,falseotherwise.final voidregisterAttributeSequence(String... attributes) Registers an attribute sequence for this elementfinal voidregisterAttributeSequence(Comparator<String> sortOrder) Registers an attribute sequence for this element; this modifies any sort order that was previously set.final voidregisterValidAttributes(String... attributes) Registers the valid attributes for this element.
Nothing happens ifAttributeSupport.checksIfValid()returnsfalse, although a call to this method is obsolete then.final voidregisterValidChildren(String... children) Registers the element names of valid child elements for this element.final Collection<String> Returns the list of the registered attributes.final Collection<String> Returns the list of the registered children.final XMLElementsetAttribute(String name, CharSequence value, Optional<? extends CharSequence> append) Sets the attribute with the given name.final XMLElementsetNamespace(String identifier) Sets the given namespace.final XMLElementsetNamespace(String prefix, String identifier) Sets the given namespace.final XMLElementsetNamespace(String prefix, URI identifier) Sets the given namespace.final XMLElementsetNamespace(URI identifier) Sets the given namespace.final XMLElementsetNamespace(Namespace namespace) Sets the given namespace.final <E extends Element>
voidsetParent(E parent) Sets the parent for this element.toString()Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface org.tquadrat.foundation.xml.builder.XMLElement
addCDATA, addCDATA, addCDATA, addCDATA, addCDATA, addCDATA, addCDATA, addCDATA, addCDATA, addCDATA, addCDATA, addCDATA, addCDATA, addText, addText, addText, addText, addText, addText, addText, addText, addText, addText, addText, addText, addText, setAttribute, setAttribute, setAttribute, setAttribute, setAttribute, setAttribute, setAttribute, setAttribute, setAttribute, setAttribute, setAttribute, setAttribute, setAttributeIfNotEmpty, setAttributeIfNotEmpty, setId
-
Field Details
-
m_Attributes
The attribute support. -
m_Children
The child support. -
m_ElementName
The element name. -
m_Parent
The parent element.
-
-
Constructor Details
-
XMLElementImpl
Creates a new
XMLElementImplinstance.The given element name is validated using the method that is provided by
XMLBuilderUtils.getElementNameValidator().The new element allows attributes and children, but will not validate them. It also allows text.
- Note:
-
- This constructor is mainly used by the factory methods in for on-the-fly XML generation.
- Parameters:
elementName- The element name.
-
XMLElementImpl
Creates a new
XMLElementImplinstance.The given element name is validated using the method that is provided by
XMLBuilderUtils.getElementNameValidator().- Note:
-
- This constructor is used for the implementation of XML specialisations, like SVG or HTML (although this not really XML). It is made accessible through
- Parameters:
elementName- The element name.flags- The configuration flags for the new element.- See Also:
-
-
Method Details
-
addCDATA
Adds aCDATAelement to this XML element.- Specified by:
addCDATAin interfaceXMLElement- Parameters:
text- The text.- Returns:
- This instance.
- Throws:
IllegalArgumentException
-
addChild
public final <E extends XMLElement> XMLElement addChild(E child) throws IllegalArgumentException, IllegalStateException Adds a child to this element.- Specified by:
addChildin interfaceXMLElement- Type Parameters:
E- The implementation type for thechildren.- Parameters:
child- The child to add.- Returns:
- This instance.
- Throws:
IllegalArgumentException- The given child is not valid for this element.IllegalStateException- The child has already a parent that is not this element.
-
addComment
Adds a comment.- Specified by:
addCommentin interfaceXMLElement- Parameters:
comment- The comment text.- Returns:
- This instance.
- Throws:
IllegalArgumentException
-
addPredefinedMarkup
Adds predefined markup.
The given markup will not be validated, it just may not be
null. So the caller is responsible that it will be proper markup.As the markup may be formatted differently (or not formatted at all), the pretty printed output may be distorted when this is used.
- Specified by:
addPredefinedMarkupin interfaceXMLElement- Parameters:
markup- The predefined markup.- Returns:
- This instance.
- Throws:
IllegalArgumentException
-
addText
Adds text to this element. Special characters will be escaped according to the rules for the respective SGML flavour.- Specified by:
addTextin interfaceXMLElement- Parameters:
text- The text.- Returns:
- This instance.
- Throws:
IllegalArgumentException
-
getAttribute
Returns the value for the attribute with the given name.- Specified by:
getAttributein interfaceElement- Parameters:
name- The attribute name.- Returns:
- An instance of
Optionalthat holds the value for that attribute.
-
getAttributes
Provides read access to the attributes.- Specified by:
getAttributesin interfaceElement- Returns:
- A reference to the attributes.
-
getChildren
Provides access to the children for this element; the returned collection is not modifiable.- Specified by:
getChildrenin interfaceElement- Returns:
- A reference to the children of this element; if the element does not have children, an empty collection will be returned.
-
getElementName
Returns the name of the element.- Specified by:
getElementNamein interfaceElement- Returns:
- The name of the element.
-
getFlags
Returns the flags for this element.- Specified by:
getFlagsin interfaceXMLElement- Returns:
- The flags.
- See Also:
-
getNamespaces
Provides access to the namespaces for this element; the returned collection is not modifiable.- Specified by:
getNamespacesin interfaceElement- Returns:
- A reference to the namespaces of this element; if the element does not have namespaces assigned, an empty collection will be returned.
-
getParent
Returns the parent of this element. -
getSortOrder
Returns the attribute sort order.- Returns:
- The comparator that determines the attribute's sequence.
-
hasChildren
Returnstrueif the element has children,falseotherwise.- Specified by:
hasChildrenin interfaceElement- Returns:
trueif the element has children.
-
registerAttributeSequence
Registers an attribute sequence for this element; this modifies any sort order that was previously set.
The names for the attributes are not validated; in particular, it is not checked whether an attribute is listed as valid.
- Parameters:
attributes- The names of the attributes in the desired sequence.
-
registerAttributeSequence
Registers an attribute sequence for this element; this modifies any sort order that was previously set.- Parameters:
sortOrder- The sort order for the attributes.
-
registerValidAttributes
Registers the valid attributes for this element.
Nothing happens ifAttributeSupport.checksIfValid()returnsfalse, although a call to this method is obsolete then.- Note:
-
- The given attributes will be added to the already existing ones!
- Parameters:
attributes- The names of the valid attributes.- Throws:
InvalidXMLNameException- One of the attribute names is invalid.
-
registerValidChildren
Registers the element names of valid child elements for this element.- Note:
-
- The given children will be added to the already existing ones!
- Parameters:
children- The element names of the valid children.
-
retrieveValidAttributes
Returns the list of the registered attributes.- Returns:
- The registered attributes.
-
retrieveValidChildren
Returns the list of the registered children.- Returns:
- The registered children.
-
setAttribute
public final XMLElement setAttribute(String name, CharSequence value, Optional<? extends CharSequence> append) throws IllegalArgumentException Sets the attribute with the given name.The given attribute name is validated using the method that is provided by
XMLBuilderUtils.getAttributeNameValidator().- Specified by:
setAttributein interfaceXMLElement- Parameters:
name- The name of the attribute; the name is case-sensitive.value- The attribute's value; ifnullthe attribute will be removed.append- If not empty (XMLElement.NO_APPEND), the new value will be appended to an already existing one, and the provided char sequence is used as the separator.- Returns:
- This instance.
- Throws:
IllegalArgumentException- An attribute with the given name is not valid for the element
-
setNamespace
public final XMLElement setNamespace(String identifier) throws IllegalArgumentException, URISyntaxException Sets the given namespace.- Specified by:
setNamespacein interfaceXMLElement- Parameters:
identifier- The namespace identifier.- Returns:
- This instance.
- Throws:
URISyntaxException- The provided URI String is invalid.IllegalArgumentException
-
setNamespace
Sets the given namespace.- Specified by:
setNamespacein interfaceXMLElement- Parameters:
identifier- The namespace identifier.- Returns:
- This instance.
- Throws:
IllegalArgumentException
-
setNamespace
public final XMLElement setNamespace(String prefix, String identifier) throws IllegalArgumentException, URISyntaxException Sets the given namespace.
The given prefix is validated using the method that is provided by
XMLBuilderUtils.getPrefixValidator().The given prefix is validated using the method that is provided by
XMLBuilderUtils.getPrefixValidator().- Specified by:
setNamespacein interfaceXMLElement- Parameters:
prefix- The namespace prefix.identifier- The namespace identifier.- Returns:
- This instance.
- Throws:
URISyntaxException- The provided URI String is invalid.IllegalArgumentException
-
setNamespace
Sets the given namespace.
The given prefix is validated using the method that is provided by
XMLBuilderUtils.getPrefixValidator().The given prefix is validated using the method that is provided by
XMLBuilderUtils.getPrefixValidator().- Specified by:
setNamespacein interfaceXMLElement- Parameters:
prefix- The namespace prefix.identifier- The namespace identifier.- Returns:
- This instance.
- Throws:
IllegalArgumentException
-
setNamespace
Sets the given namespace.- Specified by:
setNamespacein interfaceXMLElement- Parameters:
namespace- The namespace.- Returns:
- This instance.
- Throws:
IllegalArgumentException
-
setParent
Sets the parent for this element.- Specified by:
setParentin interfaceElement- Specified by:
setParentin interfaceXMLElement- Type Parameters:
E- The implementation ofElement.- Parameters:
parent- The parent.
-
toString
-
