Class Doc
- java.lang.Object
-
- com.atlassian.adf.model.node.AbstractNode<C>
-
- com.atlassian.adf.model.node.AbstractContentNode<Doc,DocContent>
-
- com.atlassian.adf.model.node.Doc
-
- All Implemented Interfaces:
Element,Node,ContentNode<Doc,DocContent>
@Documentation(state=REVIEWED, date="2023-07-26") public class Doc extends AbstractContentNode<Doc,DocContent>
The root container node of every Atlassian Document Format (ADF) document.About ADF
ADF stands for Atlassian Document Format. It is a structured JSON document, conforming to a documented JSON schema, that represents a piece of rich content to be displayed in an Atlassian Cloud application. It might represent a comment on a Confluence page or the description of an issue in Jira.About ADF's Structure
An ADF document is composed of a hierarchy of nodes. There are two categories of nodes: block andinline. Block nodes define the structural elements of the document such asheadings,paragraphs,tables,horizontal ruleseparators,lists, and the like. Inline nodes specify the document content such as text and images. Several node types can also acceptmarksthat decorate that node type with formatting information or text markup such asbold,italics, andlinks.A document is ordered; that is, there's a single sequential path through it: traversing a document in sequence and concatenating the nodes yields content in the correct order. Every independent ADF value must consist of a single well-formed
docnode. The various top-level block nodes (which in this library are those marked with theDocContentinterface) may be added to thisdocto indicate the rich content that should be displayed for it.About This Library
This library is a manually constructed representation of the JSON schema that has been published for ADF. It does not consult that schema when validating the content, so there is always some risk that it will disagree with some aspect of the schema. However, if any such discrepancies are found, please report this as a bug.Additionally, any new features that are added to the schema have very limited support in this library and may not be preserved across round trips if the library version is too far behind. See
Schema.version()for information about which JSON schema version was consulted at the time this library was last checked against it.Most of the elements that are available in this library share the following features:
- The various elements use static factories with appropriate names so that they can conveniently be used as static imports and combined intuitively like in the Java example below.
- Most elements are mutable. The only cases where they aren't are those
that have little or no state, such that making them mutable does not really make
any sense. For example,
strongmarks don't have anything to mutate. - The only erroneous state that the library usually tolerates is failing to add
content to a node that requires it. Required parameters are demanded and validated
by the static factory methods used to construct the node and that construction is
not complete until all of them have been set. These have names like
Status.Partial.NeedsColorthat try to help indicate why the element is not fully constructed.
Example
Java
doc(p(text("Hello "),text("world").strong() ) );ADF
{ "version": 1, "type": "doc", "content": [ { "type": "paragraph", "content": [ { "type": "text", "text": "Hello " }, { "type": "text", "text": "world", "marks": [ { "type": "strong" } ] } ] } ] }Result
Hello world
- See Also:
toMap(),parse(Map), Node - doc
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classDoc.Version-
Nested classes/interfaces inherited from class com.atlassian.adf.model.node.AbstractNode
AbstractNode.ToStringHelper
-
-
Field Summary
-
Fields inherited from class com.atlassian.adf.model.node.AbstractContentNode
content
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidappendContentNodeFields(AbstractNode.ToStringHelper buf)Allows content nodes that have their own fields to augment thetoString()implementation with their own field values.voidappendPlainText(StringBuilder sb)Renders this node as plain-text suitable for viewing by end users.Class<DocContent>contentClass()ReturnsClass<N>, the class representing the type of items held by this node.protected booleancontentNodeEquals(Doc other)Allows content nodes that have their own fields to augment theequalsimplementation with tests for their own field values.protected intcontentNodeHashCode()Allows content nodes that have their own fields to augment thehashCodeimplementation with a hash of their own field values.Doccopy()Returns a deep copy of this element, including copies of any nodes or marks that it contains.static Docdoc()static Docdoc(Doc.Version version)static Docdoc(Doc.Version version, DocContent content)static Docdoc(Doc.Version version, DocContent... content)static Docdoc(Doc.Version version, Iterable<? extends DocContent> content)static Docdoc(Doc.Version version, Stream<? extends DocContent> content)static Docdoc(DocContent content)static Docdoc(DocContent... content)static Docdoc(Iterable<? extends DocContent> content)static Docdoc(Stream<? extends DocContent> content)StringelementType()Thetypevalue that identifies this element, such as"paragraph"or"strong".static Docparse(Map<String,?> map)Parses ADF from a single map of string keys to their values, suitable for direct interpretation from JSON.Map<String,?>toMap()Renders this document as a single map of string keys to their values, suitable for direct translation into JSON.protected voidvalidateContentNodeForAppend(DocContent node)Called on each content node before it is added to validate that the node can be accepted.Doc.Versionversion()Returns the version of the ADF standard used for this document.Docversion(Doc.Version version)Sets a new version for this document.-
Methods inherited from class com.atlassian.adf.model.node.AbstractContentNode
addContent, addContentIfPresent, allNodes, appendNodeFields, appendPlainTextContentJoinedWith, appendPlainTextInlineContent, clear, content, content, content, content, content, contentFieldMaps, contentNodeValidate, isEmpty, nodeEquals, nodeHashCode, parseContentItem, parseContentItems, parseOptionalContent, parseRequiredContent, parseRequiredContentAllowEmpty, removeIf, replaceContent, requireNotEmpty, transformContent, transformDescendants, trim, validate, validateContentItems
-
Methods inherited from class com.atlassian.adf.model.node.AbstractNode
doubleEq, doubleHash, equals, hashCode, isSupported, mapWithType, numberEq, numberHash, self, toPlainText, toString
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface com.atlassian.adf.model.node.type.ContentNode
allNodesOfType, allNodesOfTypeAsList
-
Methods inherited from interface com.atlassian.adf.model.Element
isSupported
-
Methods inherited from interface com.atlassian.adf.model.node.Node
toPlainText
-
-
-
-
Method Detail
-
doc
public static Doc doc()
- Returns:
- a new, empty document
-
doc
public static Doc doc(DocContent content)
- Returns:
- a new document with the given content
-
doc
public static Doc doc(DocContent... content)
- Returns:
- a new document with the given content
-
doc
public static Doc doc(Iterable<? extends DocContent> content)
- Returns:
- a new document with the given content
-
doc
public static Doc doc(Stream<? extends DocContent> content)
- Returns:
- a new document with the given content
-
doc
public static Doc doc(Doc.Version version)
- Returns:
- a new document with the given version
-
doc
public static Doc doc(Doc.Version version, DocContent content)
- Returns:
- a new document with the given version and content
-
doc
public static Doc doc(Doc.Version version, DocContent... content)
- Returns:
- a new document with the given version and content
-
doc
public static Doc doc(Doc.Version version, Iterable<? extends DocContent> content)
- Returns:
- a new document with the given version and content
-
doc
public static Doc doc(Doc.Version version, Stream<? extends DocContent> content)
- Returns:
- a new document with the given version and content
-
contentClass
public Class<DocContent> contentClass()
Description copied from interface:ContentNodeReturnsClass<N>, the class representing the type of items held by this node.
-
copy
public Doc copy()
Description copied from interface:ElementReturns a deep copy of this element, including copies of any nodes or marks that it contains. The copy will not necessarily be in exactly the same state as the original in some cases. For example, atextnode that is used inside acodeBlockwill have the ability to use marks on it disabled, but a copy made of the text node using this method will not similarly disallow marks unless it is also added to a content node with those same restrictions.Implementations notes:
- Implementations should narrow the return type.
- Implementations should
return thisif the element is immutable. The@Immutableannotation should be used on the class to offer additional confirmation of this intent. - Implementations should
return parse(toMap())if they have state. - While there may be cases where it is worthwhile to do something more efficient than
the conversion to a map and back, this is discouraged because it would add yet another
fragile piece of code that breaks when new data is added to the node. The
parseandtoMapmethods already have to be updated in these circumstances, so it makes sense to take advantage of that.
- Returns:
- a copy of this element, or
thisif the element is immutable anyway
-
version
public Doc version(Doc.Version version)
Sets a new version for this document.- Parameters:
version- the new document version- Returns:
this
-
version
public Doc.Version version()
Returns the version of the ADF standard used for this document.- Returns:
- the version of the ADF standard used for this document.
-
contentNodeEquals
protected boolean contentNodeEquals(Doc other)
Description copied from class:AbstractContentNodeAllows content nodes that have their own fields to augment theequalsimplementation with tests for their own field values.Implementations need not check for identity,
null, or a different node class; those are already covered byAbstractNode.equals(Object). Implementations need not check theAbstractContentNode.content(), either; that is already covered byAbstractContentNode.nodeEquals(AbstractContentNode), which is expected to be this method's only consumer.Just as with the relationship between
hashCode,equals, andtoStringfor ordinary Java classes, subclasses ofAbstractContentNodeshould maintain consistent implementations ofcontentNodeHashCode,contentNodeEquals, andappendContentNodeFields.- Overrides:
contentNodeEqualsin classAbstractContentNode<Doc,DocContent>- Returns:
trueif all additional field values that belong to a particular type of content node test as equal;falseif differences are found- See Also:
AbstractContentNode.contentNodeHashCode(),AbstractContentNode.appendContentNodeFields(ToStringHelper)
-
contentNodeHashCode
protected int contentNodeHashCode()
Description copied from class:AbstractContentNodeAllows content nodes that have their own fields to augment thehashCodeimplementation with a hash of their own field values.Implementations need not include the node's class; that is already covered by
AbstractNode.hashCode(). Implementations need not include thecontent, either; that is already covered byAbstractNode.nodeHashCode(), which is expected to be this method's only consumer.Just as with the relationship between
hashCode,equals, andtoStringfor ordinary Java classes, subclasses ofAbstractContentNodeshould maintain consistent implementations ofcontentNodeHashCode,contentNodeEquals, andappendContentNodeFields.- Overrides:
contentNodeHashCodein classAbstractContentNode<Doc,DocContent>- Returns:
- the hash code of any additional field values that belong to a particular type of content node.
- See Also:
AbstractContentNode.contentNodeEquals(AbstractContentNode),AbstractContentNode.appendContentNodeFields(ToStringHelper)
-
appendContentNodeFields
protected void appendContentNodeFields(AbstractNode.ToStringHelper buf)
Description copied from class:AbstractContentNodeAllows content nodes that have their own fields to augment thetoString()implementation with their own field values.Each field's value should be provided by calling
AbstractNode.ToStringHelper.appendField(String, Object). Thevaluemay benull, in which case the field is omitted, for brevity. It will handle array values gracefully, including arrays of primitive types.Just as with the relationship between
hashCode,equals, andtoStringfor ordinary Java classes, subclasses ofAbstractContentNodeshould maintain consistent implementations ofcontentNodeHashCode,contentNodeEquals, andappendContentNodeFields.- Overrides:
appendContentNodeFieldsin classAbstractContentNode<Doc,DocContent>- Parameters:
buf- where the field values should be written- See Also:
AbstractContentNode.contentNodeHashCode(),AbstractContentNode.contentNodeEquals(AbstractContentNode)
-
elementType
public String elementType()
Description copied from interface:ElementThetypevalue that identifies this element, such as"paragraph"or"strong".
-
validateContentNodeForAppend
protected void validateContentNodeForAppend(DocContent node)
Description copied from class:AbstractContentNodeCalled on each content node before it is added to validate that the node can be accepted.- Overrides:
validateContentNodeForAppendin classAbstractContentNode<Doc,DocContent>- Parameters:
node- the node that is about to be added
-
toMap
public Map<String,?> toMap()
Renders this document as a single map of string keys to their values, suitable for direct translation into JSON.- Returns:
- this document, as a map
-
parse
public static Doc parse(Map<String,?> map)
Parses ADF from a single map of string keys to their values, suitable for direct interpretation from JSON.- Parameters:
map- the map of keys to values representing the ADF- Returns:
- the parsed and validated document
- Throws:
AdfException- if there is a problem parsing the document
-
appendPlainText
public void appendPlainText(StringBuilder sb)
Description copied from interface:NodeRenders this node as plain-text suitable for viewing by end users. This is equivalent to callingNode.toPlainText()and appending the result to the given buffer, except that it may be slightly more efficient, since it will write directly to the existing buffer instead of using a temporary buffer and having to make a copy of the result.- Specified by:
appendPlainTextin interfaceNode- Overrides:
appendPlainTextin classAbstractContentNode<Doc,DocContent>- Parameters:
sb- where to write the result
-
-