Class TextUtilities


  • public class TextUtilities
    extends java.lang.Object
    A collection of text functions.

    This class is neither intended to be instantiated nor subclassed.

    Restriction:
    This class is not intended to be subclassed by clients.
    Restriction:
    This class is not intended to be instantiated by clients.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String[] DELIMITERS
      Default line delimiters used by the text functions of this class.
      static java.lang.String[] fgDelimiters
      Deprecated.
      use DELIMITERS instead
    • Constructor Summary

      Constructors 
      Constructor Description
      TextUtilities()  
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static void addDocumentPartitioners​(IDocument document, java.util.Map<java.lang.String,​? extends IDocumentPartitioner> partitioners)
      Connects the given document with all document partitioners stored in the given map under their partitioning name.
      static ITypedRegion[] computePartitioning​(IDocument document, java.lang.String partitioning, int offset, int length, boolean includeZeroLengthPartitions)
      Computes and returns the partitioning for the given region of the given document for the given partitioning name.
      static java.lang.String[] computePartitionManagingCategories​(IDocument document)
      Computes and returns the partition managing position categories for the given document or null if this was impossible.
      static int[] copy​(int[] array)
      Returns a copy of the given integer array.
      static java.lang.String[] copy​(java.lang.String[] array)
      Returns a copy of the given string array.
      static java.lang.String determineLineDelimiter​(java.lang.String text, java.lang.String hint)
      Determines which one of default line delimiters appears first in the list.
      static int endsWith​(java.lang.String[] searchStrings, java.lang.String text)
      Returns the index of the longest search string with which the given text ends or -1 if none matches.
      static int equals​(java.lang.String[] compareStrings, java.lang.String text)
      Returns the index of the first compare string that equals the given text or -1 if none is equal.
      static java.lang.String getContentType​(IDocument document, java.lang.String partitioning, int offset, boolean preferOpenPartitions)
      Returns the content type at the given offset of the given document.
      static java.lang.String getDefaultLineDelimiter​(IDocument document)
      Returns the default line delimiter for the given document.
      static ITypedRegion getPartition​(IDocument document, java.lang.String partitioning, int offset, boolean preferOpenPartitions)
      Returns the partition of the given offset of the given document.
      static int[] indexOf​(java.lang.String[] searchStrings, java.lang.String text, int offset)
      static DocumentEvent mergeProcessedDocumentEvents​(java.util.List<? extends DocumentEvent> documentEvents)
      Returns a document event which is an accumulation of a list of document events, null if the list of document events is empty.
      static DocumentEvent mergeUnprocessedDocumentEvents​(IDocument unprocessedDocument, java.util.List<? extends DocumentEvent> documentEvents)
      Returns a document event which is an accumulation of a list of document events, null if the list of documentEvents is empty.
      static AbstractLineTracker.DelimiterInfo nextDelimiter​(java.lang.CharSequence text, int offset)
      Search for the first standard line delimiter in text starting at given offset.
      static boolean overlaps​(IRegion left, IRegion right)
      Returns true if the two regions overlap.
      static java.util.Map<java.lang.String,​IDocumentPartitioner> removeDocumentPartitioners​(IDocument document)
      Removes all connected document partitioners from the given document and stores them under their partitioning name in a map.
      static int startsWith​(java.lang.String[] searchStrings, java.lang.String text)
      Returns the index of the longest search string with which the given text starts or -1 if none matches.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • DELIMITERS

        public static final java.lang.String[] DELIMITERS
        Default line delimiters used by the text functions of this class.
      • fgDelimiters

        @Deprecated
        public static final java.lang.String[] fgDelimiters
        Deprecated.
        use DELIMITERS instead
        Default line delimiters used by these text functions.
    • Constructor Detail

      • TextUtilities

        public TextUtilities()
    • Method Detail

      • determineLineDelimiter

        public static java.lang.String determineLineDelimiter​(java.lang.String text,
                                                              java.lang.String hint)
        Determines which one of default line delimiters appears first in the list. If none of them the hint is returned.
        Parameters:
        text - the text to be checked
        hint - the line delimiter hint
        Returns:
        the line delimiter
      • indexOf

        @Deprecated
        public static int[] indexOf​(java.lang.String[] searchStrings,
                                    java.lang.String text,
                                    int offset)
        Deprecated.
        use MultiStringMatcher.indexOf(CharSequence, int, String...) instead. Notable differences:
        • new matcher indexOf does not allow negative offsets (old matcher treated them as 0)
        • new matcher indexOf will tolerate null and empty search strings (old accepted empty but throw on null)
        • new matcher indexOf will not match empty string (old matched empty if nothing else matched)
        For the common case of searching the next default delimiter use the optimized nextDelimiter(CharSequence, int) method instead.
        Returns the starting position and the index of the first matching search string in the given text that is greater than the given offset. If more than one search string matches with the same starting position then the longest one is returned.
        Parameters:
        searchStrings - the strings to search for
        text - the text to be searched
        offset - the offset at which to start the search
        Returns:
        an int[] with two elements where the first is the starting offset, the second the index of the found search string in the given searchStrings array, returns [-1, -1] if no match exists
      • endsWith

        public static int endsWith​(java.lang.String[] searchStrings,
                                   java.lang.String text)
        Returns the index of the longest search string with which the given text ends or -1 if none matches.
        Parameters:
        searchStrings - the strings to search for
        text - the text to search
        Returns:
        the index in searchStrings of the longest string with which text ends or -1
      • startsWith

        public static int startsWith​(java.lang.String[] searchStrings,
                                     java.lang.String text)
        Returns the index of the longest search string with which the given text starts or -1 if none matches.
        Parameters:
        searchStrings - the strings to search for
        text - the text to search
        Returns:
        the index in searchStrings of the longest string with which text starts or -1
      • equals

        public static int equals​(java.lang.String[] compareStrings,
                                 java.lang.String text)
        Returns the index of the first compare string that equals the given text or -1 if none is equal.
        Parameters:
        compareStrings - the strings to compare with
        text - the text to check
        Returns:
        the index of the first equal compare string or -1
      • mergeUnprocessedDocumentEvents

        public static DocumentEvent mergeUnprocessedDocumentEvents​(IDocument unprocessedDocument,
                                                                   java.util.List<? extends DocumentEvent> documentEvents)
                                                            throws BadLocationException
        Returns a document event which is an accumulation of a list of document events, null if the list of documentEvents is empty. The document of the document events are ignored.
        Parameters:
        unprocessedDocument - the document to which the document events would be applied
        documentEvents - the list of document events to merge
        Returns:
        returns the merged document event
        Throws:
        BadLocationException - might be thrown if document is not in the correct state with respect to document events
      • mergeProcessedDocumentEvents

        public static DocumentEvent mergeProcessedDocumentEvents​(java.util.List<? extends DocumentEvent> documentEvents)
                                                          throws BadLocationException
        Returns a document event which is an accumulation of a list of document events, null if the list of document events is empty. The document events being merged must all refer to the same document, to which the document changes have been already applied.
        Parameters:
        documentEvents - the list of document events to merge
        Returns:
        returns the merged document event
        Throws:
        BadLocationException - might be thrown if document is not in the correct state with respect to document events
      • removeDocumentPartitioners

        public static java.util.Map<java.lang.String,​IDocumentPartitioner> removeDocumentPartitioners​(IDocument document)
        Removes all connected document partitioners from the given document and stores them under their partitioning name in a map. This map is returned. After this method has been called the given document is no longer connected to any document partitioner.
        Parameters:
        document - the document
        Returns:
        the map containing the removed partitioners
      • addDocumentPartitioners

        public static void addDocumentPartitioners​(IDocument document,
                                                   java.util.Map<java.lang.String,​? extends IDocumentPartitioner> partitioners)
        Connects the given document with all document partitioners stored in the given map under their partitioning name. This method cleans the given map.
        Parameters:
        document - the document
        partitioners - the map containing the partitioners to be connected
        Since:
        3.0
      • getContentType

        public static java.lang.String getContentType​(IDocument document,
                                                      java.lang.String partitioning,
                                                      int offset,
                                                      boolean preferOpenPartitions)
                                               throws BadLocationException
        Returns the content type at the given offset of the given document.
        Parameters:
        document - the document
        partitioning - the partitioning to be used
        offset - the offset
        preferOpenPartitions - true if precedence should be given to a open partition ending at offset over a closed partition starting at offset
        Returns:
        the content type at the given offset of the document
        Throws:
        BadLocationException - if offset is invalid in the document
        Since:
        3.0
      • getPartition

        public static ITypedRegion getPartition​(IDocument document,
                                                java.lang.String partitioning,
                                                int offset,
                                                boolean preferOpenPartitions)
                                         throws BadLocationException
        Returns the partition of the given offset of the given document.
        Parameters:
        document - the document
        partitioning - the partitioning to be used
        offset - the offset
        preferOpenPartitions - true if precedence should be given to a open partition ending at offset over a closed partition starting at offset
        Returns:
        the content type at the given offset of this viewer's input document
        Throws:
        BadLocationException - if offset is invalid in the given document
        Since:
        3.0
      • computePartitioning

        public static ITypedRegion[] computePartitioning​(IDocument document,
                                                         java.lang.String partitioning,
                                                         int offset,
                                                         int length,
                                                         boolean includeZeroLengthPartitions)
                                                  throws BadLocationException
        Computes and returns the partitioning for the given region of the given document for the given partitioning name.
        Parameters:
        document - the document
        partitioning - the partitioning name
        offset - the region offset
        length - the region length
        includeZeroLengthPartitions - whether to include zero-length partitions
        Returns:
        the partitioning for the given region of the given document for the given partitioning name
        Throws:
        BadLocationException - if the given region is invalid for the given document
        Since:
        3.0
      • computePartitionManagingCategories

        public static java.lang.String[] computePartitionManagingCategories​(IDocument document)
        Computes and returns the partition managing position categories for the given document or null if this was impossible.
        Parameters:
        document - the document
        Returns:
        the partition managing position categories or null
        Since:
        3.0
      • getDefaultLineDelimiter

        public static java.lang.String getDefaultLineDelimiter​(IDocument document)
        Returns the default line delimiter for the given document. This is IDocumentExtension4.getDefaultLineDelimiter() if available. Otherwise, this is either the delimiter of the first line, or the platform line delimiter if it is a legal line delimiter, or the first one of the legal line delimiters. The default line delimiter should be used when performing document manipulations that span multiple lines.
        Parameters:
        document - the document
        Returns:
        the document's default line delimiter
        Since:
        3.0
      • overlaps

        public static boolean overlaps​(IRegion left,
                                       IRegion right)
        Returns true if the two regions overlap. Returns false if one of the arguments is null.
        Parameters:
        left - the left region
        right - the right region
        Returns:
        true if the two regions overlap, false otherwise
        Since:
        3.0
      • copy

        public static java.lang.String[] copy​(java.lang.String[] array)
        Returns a copy of the given string array.
        Parameters:
        array - the string array to be copied
        Returns:
        a copy of the given string array or null when array is null
        Since:
        3.1
      • copy

        public static int[] copy​(int[] array)
        Returns a copy of the given integer array.
        Parameters:
        array - the integer array to be copied
        Returns:
        a copy of the given integer array or null when array is null
        Since:
        3.1