Class Element
- java.lang.Object
-
- org.symphonyoss.symphony.messageml.elements.Element
-
- Direct Known Subclasses:
Bold,BulletList,Button,Card,CardBody,CardHeader,Chime,Code,Dialog,DialogChild,Div,Entity,ExpandableCard,ExpandableCardBody,ExpandableCardHeader,Form,FormElement,Header,HorizontalRule,Image,Italic,LineBreak,Link,ListItem,MessageML,OrderedList,Paragraph,Preformatted,Span,Table,TableBody,TableCell,TableFooter,TableHeader,TableHeaderCell,TableRow,TextNode,UIAction
public abstract class Element extends Object
Base class for MessageML elements. Contains methods for constructing MessageML document trees and their PresentationML and Markdown representation, overridden in subclasses if special treatment is required.By default all elements support the "class" and "style" attributes and translate to PresentationML as container elements including their children. To override this behaviour (e.g. to make the element empty), overload the respective methods in subclasses of this class.
The intended use of this class is primarily internal. The main entry point are parse() methods in
MessageMLContext.- Since:
- 3/27/17
- Author:
- lukasz
-
-
Field Summary
Fields Modifier and Type Field Description static StringCLASS_ATTRprotected FormatEnumformatstatic StringID_ATTRstatic intID_MAX_LENGTHstatic com.fasterxml.jackson.databind.ObjectMapperMAPPERstatic StringSTYLE_ATTR
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddChild(Element child)Append a child to the element.booleanareNestedElementsAllowed()Return true if nested elements are allowed By default true, override to false for elements that dont support nested elementsStringasText()Return a text representation of the element, descending into its children.voidbuildAll(MessageMLParser parser, Element element)Process a DOM element, descending into its children, and construct the output MessageML tree.protected voidbuildNode(MessageMLParser context, Node node)Build a text node or a MessageML element based on the provided DOM node.protected intcheckIntegerAttribute(String attributeName, int minValue, String errorMessage)IntegercountChildrenOfType(Class<? extends Element> type)This method applies a breadth-first traversal of a tree of elements counting the number of elements found which belong to the class type passed as inputIntegercountNonTextNodesInNodeList(NodeList nodeList)List<Element>findElements(Class<?> type)Search the MessageML tree (depth-first) for elements of a given type.List<Element>findElements(String tag)Search the MessageML tree (depth-first) for elements with a given MessageML tag.List<Element>findElements(String attribute, String value)Search the MessageML tree (depth-first) for elements with a given attribute-value pair.StringgetAttribute(String attr)Return the value of the element's attribute "attr".Map<String,String>getAttributes()Return a map of the element's attributes.ElementgetChild(int n)Return the nth child of the element.List<Element>getChildren()Return a list of the element's children.FormatEnumgetFormat()Return the format (MessageML or PresentationML) of the element.StringgetMessageMLTag()Return the element's MessageML tag.ElementgetParent()Return the parent of the element.StringgetPresentationMLTag()Return the element's PresentationML tag.StringgetStringAttribute(Node attribute)Get a DOM attribute as a String value.booleanhasExactNumberOfChildren(int childrenNumber)BooleanhasIdAttribute()Informs if the element has an "id" attribute.protected voidputIntegerIfPresent(Map<String,Object> attributesMap, String propertyKey, String attributeKey)Gets the key's value from element's attributes and put it in the given map as an Integerprotected voidputOneIfPresent(Map<String,Object> attributesMap, String propertyKey, String attributeKey)Puts 1 if the key is found within element's attributesprotected voidputStringIfPresent(Map<String,Object> attributesMap, String propertyKey, String attributeKey)Gets the key's value from element's attributes and put it in the given map as a Stringintsize()Return the number of children of the element.protected voidthrowInvalidInputException(Node item)
-
-
-
Field Detail
-
CLASS_ATTR
public static final String CLASS_ATTR
- See Also:
- Constant Field Values
-
STYLE_ATTR
public static final String STYLE_ATTR
- See Also:
- Constant Field Values
-
ID_ATTR
public static final String ID_ATTR
- See Also:
- Constant Field Values
-
ID_MAX_LENGTH
public static final int ID_MAX_LENGTH
- See Also:
- Constant Field Values
-
format
protected FormatEnum format
-
MAPPER
public static final com.fasterxml.jackson.databind.ObjectMapper MAPPER
-
-
Method Detail
-
hasIdAttribute
public Boolean hasIdAttribute()
Informs if the element has an "id" attribute. The parser that builds all elements keeps track of all ids from the elements that have it, in order to ensure unique values.
-
buildAll
public void buildAll(MessageMLParser parser, Element element) throws InvalidInputException, ProcessingException
Process a DOM element, descending into its children, and construct the output MessageML tree.
-
putOneIfPresent
protected void putOneIfPresent(Map<String,Object> attributesMap, String propertyKey, String attributeKey)
Puts 1 if the key is found within element's attributes- Parameters:
attributesMap- map of BI properties to update BI contextpropertyKey- BI's property key to put in the given mapBiFieldsattributeKey- the attribute key of the element
-
putStringIfPresent
protected void putStringIfPresent(Map<String,Object> attributesMap, String propertyKey, String attributeKey)
Gets the key's value from element's attributes and put it in the given map as a String- Parameters:
attributesMap- map of BI properties to update BI contextpropertyKey- BI's property key to put in the given mapBiFieldsattributeKey- the attribute key of the element
-
putIntegerIfPresent
protected void putIntegerIfPresent(Map<String,Object> attributesMap, String propertyKey, String attributeKey)
Gets the key's value from element's attributes and put it in the given map as an Integer- Parameters:
attributesMap- map of BI properties to update BI contextpropertyKey- BI's property key to put in the given mapBiFieldsattributeKey- the attribute key of the element
-
buildNode
protected void buildNode(MessageMLParser context, Node node) throws InvalidInputException, ProcessingException
Build a text node or a MessageML element based on the provided DOM node.
-
asText
public String asText()
Return a text representation of the element, descending into its children.
-
countChildrenOfType
public Integer countChildrenOfType(Class<? extends Element> type)
This method applies a breadth-first traversal of a tree of elements counting the number of elements found which belong to the class type passed as input
-
getStringAttribute
public String getStringAttribute(Node attribute)
Get a DOM attribute as a String value.
-
hasExactNumberOfChildren
public boolean hasExactNumberOfChildren(int childrenNumber)
-
getMessageMLTag
public final String getMessageMLTag()
Return the element's MessageML tag.
-
getPresentationMLTag
public String getPresentationMLTag()
Return the element's PresentationML tag. By default is equals to MessageML tag, override when needed
-
areNestedElementsAllowed
public boolean areNestedElementsAllowed()
Return true if nested elements are allowed By default true, override to false for elements that dont support nested elements
-
getAttribute
public String getAttribute(String attr)
Return the value of the element's attribute "attr".
-
getChild
public Element getChild(int n)
Return the nth child of the element.
-
addChild
public void addChild(Element child)
Append a child to the element.
-
size
public int size()
Return the number of children of the element.
-
getParent
public Element getParent()
Return the parent of the element.
-
getFormat
public FormatEnum getFormat()
Return the format (MessageML or PresentationML) of the element.
-
findElements
public List<Element> findElements(Class<?> type)
Search the MessageML tree (depth-first) for elements of a given type.- Parameters:
type- the class of elements to find- Returns:
- found elements
-
findElements
public List<Element> findElements(String tag)
Search the MessageML tree (depth-first) for elements with a given MessageML tag.- Parameters:
tag- the MessageML tag of elements to find- Returns:
- found elements
-
findElements
public List<Element> findElements(String attribute, String value)
Search the MessageML tree (depth-first) for elements with a given attribute-value pair.- Parameters:
attribute- the attribute name matchvalue- the attribute value to match- Returns:
- found elements
-
throwInvalidInputException
protected void throwInvalidInputException(Node item) throws InvalidInputException
- Throws:
InvalidInputException
-
checkIntegerAttribute
protected int checkIntegerAttribute(String attributeName, int minValue, String errorMessage) throws InvalidInputException
- Throws:
InvalidInputException
-
-