Class ChildSupport
ChildSupport when it should take comments. If only
comments should be allowed, the instance can be instantiated with
ChildSupport( parent, false, false, false, null );.The flag
checkValid that applies to the constructors
ChildSupport(Element,boolean)
and
ChildSupport(Element,boolean,boolean,boolean,Function)
affects only child elements that are added through
addChild(Element).- Author:
- Thomas Thrien (thomas.thrien@tquadrat.org)
- Version:
- $Id: ChildSupport.java 1071 2023-09-30 01:49:32Z tquadrat $
- Since:
- 0.0.5
- UML Diagram
-
UML Diagram for "org.tquadrat.foundation.xml.builder.spi.ChildSupport"
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final booleanFlag that indicates whether child elements other than text are allowed.private final booleanFlag that indicates whether text is allowed.private final booleanFlag that indicates whether the validity of children should be checked.The list with the element's children.private final Function<CharSequence, String> The escape function that is used for text elements.private final ElementThe element that owns thisChildSupportinstance.private final Collection<String> The element names of valid children for a given element; the key for this map is the element name for the parent element.private static final StringThe message indicating that no children are allowed: "No children allowed for element \'%1$s\'". -
Constructor Summary
ConstructorsConstructorDescriptionChildSupport(Element owner) Creates a newChildSupportinstance for comments only.ChildSupport(Element owner, boolean checkValid) Creates a newChildSupportinstance that allows child elements, but no text.ChildSupport(Element owner, boolean checkValid, boolean allowChildren, boolean allowText, Function<CharSequence, String> escapeFunction) Creates a newChildSupportinstance.ChildSupport(Element owner, Function<CharSequence, String> escapeFunction) Creates a newChildSupportinstance that allows text, but no child elements. -
Method Summary
Modifier and TypeMethodDescriptionfinal voidaddCDATA(CharSequence text) Adds aCDATAelement.final <E extends Element>
voidaddChild(E child) Adds a child.private final <E extends Element>
voidaddChildElement(String operationName, E child) Adds a child element.final voidaddComment(CharSequence comment) Adds a comment.final voidaddPredefinedMarkup(CharSequence markup) Adds predefined markup.final voidaddText(CharSequence text) Adds text.private final voidaddText(CharSequence text, Function<? super CharSequence, String> escapeFunction, boolean addEmpty) Adds text.final booleanReturns the flag that indicates whether this instance ofChildSupportallows other elements to be added as children.final booleanReturns the flag that indicates whether this instance ofChildSupportallows that text andCDATAelements can be added.final booleanReturns a flag that indicates whether an extended validity check is performed on child elements before adding them.private final voidcheckValid(Element child, String operationName) Checks whether a child is valid for the element that owns thisChildSupportinstance.final Collection<? extends Element> Provides access to the children for this element; the returned collection is not modifiable.final booleanReturnstrueif the element has children,falseotherwise.final voidregisterChildren(String... children) Registers the element names of valid child elements for the owning element.final Collection<String> Returns the list of the registered children.private static final StringtoCDATA(CharSequence text) "Escapes" the given String to aCDATAsequence.final StringtoString(int indentationLevel, boolean prettyPrint) Returns the children as a single formatted string.
-
Field Details
-
MSG_NoChildrenAllowed
The message indicating that no children are allowed: "No children allowed for element \'%1$s\'".- See Also:
-
m_AllowChildren
Flag that indicates whether child elements other than text are allowed. -
m_AllowText
Flag that indicates whether text is allowed. -
m_CheckValid
Flag that indicates whether the validity of children should be checked. -
m_Children
The list with the element's children. -
m_EscapeFunction
The escape function that is used for text elements. -
m_Owner
The element that owns thisChildSupportinstance. -
m_ValidChildren
The element names of valid children for a given element; the key for this map is the element name for the parent element.
-
-
Constructor Details
-
ChildSupport
Creates a newChildSupportinstance for comments only.- Parameters:
owner- The element that owns thisChildSupportinstance.
-
ChildSupport
Creates a newChildSupportinstance that allows text, but no child elements.- Parameters:
owner- The element that owns thisChildSupportinstance.escapeFunction- The escape function that is used to convert special characters in texts; only required whenallowTextistrue.
-
ChildSupport
Creates a newChildSupportinstance that allows child elements, but no text.- Parameters:
owner- The element that owns thisChildSupportinstance.checkValid-truewhether children are checked to be allowed before they are added.
-
ChildSupport
public ChildSupport(Element owner, boolean checkValid, boolean allowChildren, boolean allowText, Function<CharSequence, String> escapeFunction) Creates a newChildSupportinstance.- Parameters:
owner- The element that owns thisChildSupportinstance.checkValid-truewhether children are checked to be allowed before they are added.allowChildren-trueif other elements could be added as children,falseotherwise.allowText-trueit text could be added to the element,falseif not.escapeFunction- The escape function that is used to convert special characters in texts; only required whenallowTextistrue.
-
-
Method Details
-
addCDATA
Adds aCDATAelement. AsCDATAis basically text, this is controlled by the same flag as text, too.- Parameters:
text- The text for theCDATAsequence.- Throws:
IllegalOperationException- No text is allowed for the owner.
-
addChild
public final <E extends Element> void addChild(E child) throws IllegalArgumentException, IllegalStateException, IllegalOperationException Adds a child.- Type Parameters:
E- The implementation type for thechild.- Parameters:
child- The child to add.- Throws:
IllegalArgumentException- The child is not allowed for the owner of this instance ofChildSupport.IllegalStateException- The child has already a parent that is not the owner of this instance ofChildSupport.IllegalOperationException- No children allowed for this element.
-
addChildElement
private final <E extends Element> void addChildElement(String operationName, E child) throws IllegalArgumentException, IllegalStateException, IllegalOperationException Adds a child element.- Type Parameters:
E- The implementation type for thechild.- Parameters:
operationName- The name of the operation that was originally called.child- The child to add.- Throws:
IllegalArgumentException- The child is not allowed for the owner of this instance ofChildSupport.IllegalStateException- The child has already a parent that is not the owner of this instance ofChildSupport.IllegalOperationException- No children allowed for this element.
-
addComment
Adds a comment.- Parameters:
comment- The comment text.
-
addPredefinedMarkup
public final void addPredefinedMarkup(CharSequence markup) throws IllegalArgumentException, IllegalOperationException 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.
- Parameters:
markup- The predefined markup.- Throws:
IllegalArgumentException- The child is not allowed for the owner of this instance ofChildSupport.IllegalOperationException- No children allowed for this element.
-
addText
Adds text. Special characters will be escaped by the escape function given with the constructor.- Parameters:
text- The text.- Throws:
IllegalArgumentException- No text is allowed for the owner.
-
addText
private final void addText(CharSequence text, Function<? super CharSequence, String> escapeFunction, boolean addEmpty) throws IllegalOperationExceptionAdds text.- Parameters:
text- The text.escapeFunction- The function the escapes the text in compliance with the type.addEmpty- Iftruea newTextinstance will be added even when the giventextis empty,falsemeans that emptytextwill be omitted.- Throws:
IllegalOperationException- No text is allowed for the owner.
-
allowsChildren
Returns the flag that indicates whether this instance ofChildSupportallows other elements to be added as children.- Returns:
truewhen child elements are allowed,falseif not.
-
allowsText
Returns the flag that indicates whether this instance ofChildSupportallows that text andCDATAelements can be added.- Returns:
trueif it is allowed to add text andCDATA,falseotherwise.
-
checkValid
private final void checkValid(Element child, String operationName) throws IllegalArgumentException, IllegalOperationException Checks whether a child is valid for the element that owns this
ChildSupportinstance.The child is valid either when
checkValidisfalse, the child is aCommentorText,m_ValidChildrendoes not contain an entry for the owner's element name, or the child's element name is explicitly configured. Obviously, it is not valid, when no children (other then text or comments) are allowed at all.- Parameters:
child- The child to check for.operationName- The name of the attempted operation.- Throws:
IllegalArgumentException- The child is not allowed for the owner.IllegalOperationException- No children allowed for the owner.
-
checksIfValid
Returns a flag that indicates whether an extended validity check is performed on child elements before adding them.- Returns:
trueif extended validation are performed,falseif any instance ofElementcan be added. Alsofalseif no children are allowed at all.- See Also:
-
getChildren
Provides access to the children for this element; the returned collection is not modifiable.- Returns:
- A reference the children of this element; if the element does not have children, an empty collection will be returned.
-
hasChildren
Returnstrueif the element has children,falseotherwise.- Returns:
trueif the element has children.
-
registerChildren
Registers the element names of valid child elements for the owning element.- Note:
-
- The given children will be added to the already existing ones!
- Parameters:
children- The element names of the valid children.
-
retrieveValidChildren
Returns the list of the registered children.- Returns:
- The registered children.
-
toCDATA
"Escapes" the given String to aCDATAsequence.- Parameters:
text- The text.- Returns:
- The
CDATAsequence.
-
toString
Returns the children as a single formatted string.- Parameters:
indentationLevel- The indentation level.prettyPrint- The pretty print flag.- Returns:
- The children string.
-
