com.bbn.swede.editor.formatting
Class FormattingUtils

java.lang.Object
  extended bycom.bbn.swede.editor.formatting.FormattingUtils

final class FormattingUtils
extends Object

Static utility methods used for content formatting and auto-indenting.

Author:
jlerner, rblace

Field Summary
static String BEGIN_TAG_REGEX
          Regular expression for identifying begin tags.
static String COMMENT_TAG_REGEX
          Regular expression for identifying XML comments.
static String END_TAG_REGEX
          Regular expression for identifying end tags.
static String SINGLETON_TAG_REGEX
          Regular expression for identifying singleton tags.
static String XMLVERSION_TAG_REGEX
          Regular expression for identifying the XML version tag.
 
Method Summary
static int getAttributeIndentSize()
          Returns the number of spaces to indent attributes within an open tag.
static String getIndent(int length)
          Returns a string of whitespace of a specified length.
static int getIndentLength(IDocument d, ITypedRegion pr)
          Returns the amount of whitespace at the beginning of the line containing a region.
static int getIndentLength(String line)
          Returns the amount of whitespace is at the begginning of a String.
static String getIndentSpaces(int length)
          Returns a string of space characters of a specified length.
static String getIndentTabs(int length)
          Returns a string of tab characters equivalent to a specified length.
static ITypedRegion getNextPartition(IDocument d, ITypedRegion r)
          Gets the partition after to the one being examined.
static ITypedRegion getNextTagPartition(IDocument d, int offset)
          Gets the next partition of type BEING_TAG or END_TAG.
static String getPartitionText(IDocument document, IRegion partition)
          Retrieves the text for a partition in a document.
static ITypedRegion getPreviousPartition(IDocument d, ITypedRegion r)
          Gets the partition previous to the one being examined.
static ITypedRegion getPreviousTagPartition(IDocument d, int offset)
          Gets the previous partition of type BEING_TAG or END_TAG.
static int getTagIndentSize()
          Returns the number of spaces to indent between tags.
static boolean isBeginTag(IDocument document, ITypedRegion partition)
          Indicates whether a partition contains an XML begin tag.
static boolean isBeginTag(String tag)
          Indicates whether a string contains an XML begin tag.
static boolean isEndTag(IDocument document, ITypedRegion partition)
          Indicates whether a partition contains an XML end tag.
static boolean isEndTag(String tag)
          Indicates whether a string contains an XML end tag.
static boolean isSingletonTag(IDocument document, ITypedRegion partition)
          Indicates whether a partition contains an XML singleton tag.
static boolean isSingletonTag(String tag)
          Indicates whether a string contains an XML singleton tag.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

BEGIN_TAG_REGEX

public static final String BEGIN_TAG_REGEX
Regular expression for identifying begin tags.

See Also:
String.matches(java.lang.String), Constant Field Values

END_TAG_REGEX

public static final String END_TAG_REGEX
Regular expression for identifying end tags.

See Also:
String.matches(java.lang.String), Constant Field Values

SINGLETON_TAG_REGEX

public static final String SINGLETON_TAG_REGEX
Regular expression for identifying singleton tags.

See Also:
String.matches(java.lang.String), Constant Field Values

COMMENT_TAG_REGEX

public static final String COMMENT_TAG_REGEX
Regular expression for identifying XML comments.

See Also:
String.matches(java.lang.String), Constant Field Values

XMLVERSION_TAG_REGEX

public static final String XMLVERSION_TAG_REGEX
Regular expression for identifying the XML version tag.

See Also:
String.matches(java.lang.String), Constant Field Values
Method Detail

getPartitionText

public static String getPartitionText(IDocument document,
                                      IRegion partition)
                               throws BadLocationException
Retrieves the text for a partition in a document.

Parameters:
document - The document
partition - The partition
Returns:
The full text of the partition
Throws:
BadLocationException - if the partition text cannot be retrieved

isBeginTag

public static boolean isBeginTag(String tag)
Indicates whether a string contains an XML begin tag. Singletons are not considered begin tags for purposes of this check.

Parameters:
tag - The string
Returns:
true if tag is an XML begin tag, false if not.

isBeginTag

public static boolean isBeginTag(IDocument document,
                                 ITypedRegion partition)
                          throws BadLocationException

Indicates whether a partition contains an XML begin tag. Singletons are not considered begin tags for purposes of this check.

This is a convenience method, fully equivalent to:

isBeginTag(getPartitionText(document, partition))

Parameters:
document - The document containing the partition
partition - The partition
Returns:
true if partition contains an XML begin tag, false if not.
Throws:
BadLocationException - if the partition text cannot be retrieved

isSingletonTag

public static boolean isSingletonTag(String tag)
Indicates whether a string contains an XML singleton tag.

Parameters:
tag - The string
Returns:
true if tag is an XML singleton tag, false if not.

isSingletonTag

public static boolean isSingletonTag(IDocument document,
                                     ITypedRegion partition)
                              throws BadLocationException

Indicates whether a partition contains an XML singleton tag.

This is a convenience method, fully equivalent to:

isSingletonTag(getPartitionText(document, partition))

Parameters:
document - The document containing the partition
partition - The partition
Returns:
true if partition contains an XML singleton tag, false if not.
Throws:
BadLocationException - if the partition text cannot be retrieved

isEndTag

public static boolean isEndTag(String tag)
Indicates whether a string contains an XML end tag. Singletons are not considered begin tags for purposes of this check.

Parameters:
tag - The string
Returns:
true if tag is an XML end tag, false if not.

isEndTag

public static boolean isEndTag(IDocument document,
                               ITypedRegion partition)
                        throws BadLocationException

Indicates whether a partition contains an XML end tag. Singletons are not considered begin tags for purposes of this check.

This is a convenience method, fully equivalent to:

isEndTag(getPartitionText(document, partition))

Parameters:
document - The document containing the partition
partition - The partition
Returns:
true if partition contains an XML end tag, false if not.
Throws:
BadLocationException - if the partition text cannot be retrieved

getTagIndentSize

public static int getTagIndentSize()
Returns the number of spaces to indent between tags.

Returns:
number of spaces to indent

getAttributeIndentSize

public static int getAttributeIndentSize()
Returns the number of spaces to indent attributes within an open tag.

Returns:
number of spaces to indent

getIndent

public static String getIndent(int length)
Returns a string of whitespace of a specified length. The string will contain either spaces or tabs depending on editor preferences.

Parameters:
length - The length of whitespace to be generated, in spaces
Returns:
An indentation string equivalent to length spaces.

getIndentSpaces

public static String getIndentSpaces(int length)
Returns a string of space characters of a specified length.

Parameters:
length - The number of spaces
Returns:
A string consisting of length spaces.

getIndentTabs

public static String getIndentTabs(int length)
Returns a string of tab characters equivalent to a specified length. The number of tabs in the string is determined based on the tab width stored in editor preferences. If the specified length is not divisible by the tab width, the end of the string will be padded with space characters.

Parameters:
length - The desired whitespace length, measured in spaces
Returns:
A string of tabs equivalent to length spaces, possibly padded at the end with space characters.

getIndentLength

public static int getIndentLength(IDocument d,
                                  ITypedRegion pr)
Returns the amount of whitespace at the beginning of the line containing a region.

Parameters:
d - The document containing the region
pr - The region
Returns:
The amount of leading whitespace on pr's line in d, measured in spaces.

getIndentLength

public static int getIndentLength(String line)
Returns the amount of whitespace is at the begginning of a String.

Parameters:
line - The string
Returns:
The amount of leading whitespace in line, measured in spaces.

getNextPartition

public static ITypedRegion getNextPartition(IDocument d,
                                            ITypedRegion r)
Gets the partition after to the one being examined.

Parameters:
d - Document being edited
r - Typed region of current partition
Returns:
The partition after r, or null if there is no such partition.

getNextTagPartition

public static ITypedRegion getNextTagPartition(IDocument d,
                                               int offset)
                                        throws BadLocationException
Gets the next partition of type BEING_TAG or END_TAG.

Parameters:
d - The document containing the partitions
offset - The starting offset for the partition search. If this value falls within a tag partition, that partition will be returned.
Returns:
The partition representing the next tag in the document, or null if no such partition can be found.
Throws:
BadLocationException - if the partition text cannot be retrieved

getPreviousPartition

public static ITypedRegion getPreviousPartition(IDocument d,
                                                ITypedRegion r)
Gets the partition previous to the one being examined.

Parameters:
d - Document being edited
r - Typed region of current partition
Returns:
The partition previous to r, or null if there is no such partition.

getPreviousTagPartition

public static ITypedRegion getPreviousTagPartition(IDocument d,
                                                   int offset)
                                            throws BadLocationException
Gets the previous partition of type BEING_TAG or END_TAG.

Parameters:
d - The document containing the partitions
offset - The starting offset for the partition search. If this value falls within a tag partition, that partition will be returned.
Returns:
The partition representing the previous tag in the document, or null if no such partition can be found.
Throws:
BadLocationException - if the partition cannot be retrieved


Copyright © 2004-2005 BBN Technologies. All Rights Reserved.