Class StandardParagraph

java.lang.Object
pro.verron.officestamper.core.StandardParagraph
All Implemented Interfaces:
Paragraph

public class StandardParagraph extends Object implements Paragraph
Represents a wrapper for managing and manipulating DOCX paragraph elements. This class provides methods to manipulate the underlying paragraph content, process placeholders, and interact with runs within the paragraph.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    apply(Consumer<org.docx4j.wml.ContentAccessor> pConsumer)
    Applies the given consumer to the paragraph represented by the current instance.
    Returns the aggregated text over all runs.
    from(DocxPart part, Object parent)
    Creates a new instance of StandardParagraph from the provided DocxPart and parent object.
    from(DocxPart source, org.docx4j.wml.CTSdtContentRun paragraph)
    Creates a new instance of StandardParagraph from the provided DocxPart and CTSdtContentRun objects.
    from(DocxPart source, org.docx4j.wml.P paragraph)
    Creates a new instance of StandardParagraph using the provided DocxPart and P objects.
    Collection<org.docx4j.wml.Comments.Comment>
    Retrieves the collection of comments associated with the current paragraph.
    <T> Optional<T>
    parent(Class<T> aClass)
    Retrieves the nearest parent of the specified type for the current paragraph.
    Retrieves the parent table of the current paragraph, if it exists.
    Retrieves the parent table row of the current paragraph, if it exists.
    void
    Removes the paragraph represented by the current instance.
    void
    replace(Object start, Object end, Insert insert)
    Replaces a section of elements within the document, defined by the start and end objects, with the elements provided by the given insert.
    void
    replace(String expression, Insert insert)
    Replaces a specified placeholder within the paragraph with the provided insert.
    void
    replace(List<org.docx4j.wml.P> toRemove, List<org.docx4j.wml.P> toAdd)
    Replaces a set of paragraph elements with new ones within the current paragraph's siblings.
    Returns the string representation of the paragraph.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Method Details

    • from

      public static StandardParagraph from(DocxPart part, Object parent)
      Creates a new instance of StandardParagraph from the provided DocxPart and parent object.
      Parameters:
      part - the source DocxPart.
      parent - the parent object.
      Returns:
      a new StandardParagraph instance.
    • from

      public static StandardParagraph from(DocxPart source, org.docx4j.wml.P paragraph)
      Creates a new instance of StandardParagraph using the provided DocxPart and P objects.
      Parameters:
      source - the source DocxPart containing the paragraph.
      paragraph - the P object representing the structure and content of the paragraph.
      Returns:
      a new instance of StandardParagraph constructed based on the provided source and paragraph.
    • from

      public static StandardParagraph from(DocxPart source, org.docx4j.wml.CTSdtContentRun paragraph)
      Creates a new instance of StandardParagraph from the provided DocxPart and CTSdtContentRun objects.
      Parameters:
      source - the source DocxPart containing the paragraph content.
      paragraph - the CTSdtContentRun object representing the content of the paragraph.
      Returns:
      a new instance of StandardParagraph constructed based on the provided DocxPart and paragraph.
    • replace

      public void replace(List<org.docx4j.wml.P> toRemove, List<org.docx4j.wml.P> toAdd)
      Replaces a set of paragraph elements with new ones within the current paragraph's siblings. Ensures that the elements to be removed are replaced in the appropriate position.
      Specified by:
      replace in interface Paragraph
      Parameters:
      toRemove - the list of paragraph elements to be removed.
      toAdd - the list of paragraph elements to be added.
      Throws:
      OfficeStamperException - if the current paragraph object is not found in its siblings.
    • remove

      public void remove()
      Removes the paragraph represented by the current instance. Delegates the removal process to a utility method that handles the underlying P object.
      Specified by:
      remove in interface Paragraph
    • replace

      public void replace(String expression, Insert insert)
      Description copied from interface: Paragraph
      Replaces a specified placeholder within the paragraph with the provided insert.
      Specified by:
      replace in interface Paragraph
      Parameters:
      expression - the expression to replace.
      insert - the insert containing elements that will replace the placeholder.
    • replace

      public void replace(Object start, Object end, Insert insert)
      Description copied from interface: Paragraph
      Replaces a section of elements within the document, defined by the start and end objects, with the elements provided by the given insert.
      Specified by:
      replace in interface Paragraph
      Parameters:
      start - the starting object marking the beginning of the section to replace.
      end - the ending object marking the end of the section to replace.
      insert - the insert containing the elements that will replace the specified section.
    • asString

      public String asString()
      Returns the aggregated text over all runs.
      Specified by:
      asString in interface Paragraph
      Returns:
      the text of all runs.
    • apply

      public void apply(Consumer<org.docx4j.wml.ContentAccessor> pConsumer)
      Applies the given consumer to the paragraph represented by the current instance. This method facilitates custom processing by allowing the client to define specific operations to be performed on the paragraph's internal structure.
      Specified by:
      apply in interface Paragraph
      Parameters:
      pConsumer - the consumer function to apply to the paragraph's structure.
    • parent

      public <T> Optional<T> parent(Class<T> aClass)
      Retrieves the nearest parent of the specified type for the current paragraph. The search is performed starting from the current paragraph and traversing up to the root, with a default maximum depth of Integer.MAX_VALUE.
      Specified by:
      parent in interface Paragraph
      Type Parameters:
      T - the generic type of the parent
      Parameters:
      aClass - the class type of the parent to search for
      Returns:
      an Optional containing the parent of the specified type if found, or an empty Optional if no parent of the given type exists
    • getComment

      public Collection<org.docx4j.wml.Comments.Comment> getComment()
      Retrieves the collection of comments associated with the current paragraph.
      Specified by:
      getComment in interface Paragraph
      Returns:
      a collection of Comments.Comment objects related to the paragraph.
    • parentTableRow

      public Optional<Table.Row> parentTableRow()
      Description copied from interface: Paragraph
      Retrieves the parent table row of the current paragraph, if it exists.
      Specified by:
      parentTableRow in interface Paragraph
      Returns:
      an Optional containing the parent Table.Row if the paragraph is within a table row, otherwise an empty Optional
    • parentTable

      public Optional<Table> parentTable()
      Description copied from interface: Paragraph
      Retrieves the parent table of the current paragraph, if it exists.
      Specified by:
      parentTable in interface Paragraph
      Returns:
      an Optional containing the parent Table if the paragraph is within a table, otherwise an empty Optional
    • toString

      public String toString()
      Returns the string representation of the paragraph. This method delegates to the asString method to aggregate the text content of all runs.
      Overrides:
      toString in class Object
      Returns:
      a string containing the combined text content of the paragraph's runs.