FileDocCategorySizeDatePackage
Formatter.javaAPI DocExample7799Tue Dec 08 01:21:00 GMT 1998oisoft.togetherx.scripts.tools

Formatter

public class Formatter extends Object
String Formatter

Fields Summary
private static String
mySpecDelimiter
The delimiter, which rounds replacing tags May be changed by Formatter.setTagDelimiter
Constructors Summary
Methods Summary
public static java.lang.StringfillToLen(java.lang.String source, int len, boolean right)
Converts source string to result by adding spaces to the end, until len characters be accepted

param
source a source string
param
len a field length
param
right if true - spaces are adding to the right (default), if false - right alingment

        String blankString = makeBlankString (len-source.length ());

        if (source.length () > len) {
            return source.substring (0, len);
        }
        if (right)
            return source+blankString;
        else
            return blankString+source;
    
public static java.lang.StringfillToLen(java.lang.String source, int len)
Converts source string to result by adding spaces to the end, until len characters be accepted

        return fillToLen (source, len, true);
    
public static java.lang.StringfillToLen(int value, int len)
Converts source integer value to result string and adds spaces to the end, until len characters be accepted

        return fillToLen (Integer.toString (value), len);
    
public static java.lang.StringfillToLen(int value, int len, boolean right)
Converts source integer value to result string and adds spaces to the end (if alignment is left), or to the beginning (if alignment is right) until len characters be accepted

        return fillToLen (Integer.toString (value), len, right);
    
public static java.lang.StringfillToLen(double value, int len)
Converts source double value to result string and adds spaces to the end, until len characters be accepted

        return fillToLen (Double.toString (value), len);
    
public static java.lang.StringfillToLen(double value, int len, boolean right)
Converts source double value to result string and adds spaces to the end (if alignment is left), or to the beginning (if alignment is right) until len characters be accepted

        return fillToLen (Double.toString (value), len, right);
    
public static java.lang.Stringindent(java.lang.String documentation, int indent)
Generates string from the source one with indentation of every new line

        return indentWithWrap (documentation, indent, 0);
    
public static java.lang.StringindentWithWrap(java.lang.String documentation, int indent, int rightMargin)
Generates string from the source one with indentation of every new line and move a word to next line if right margin is accepted

        if (rightMargin != 0 && indent >= rightMargin)
            throw new FormatterIndentOverRMarginException (indent, rightMargin);

        int i = 0;
        String result = new String ();

        StringTokenizer sp = new StringTokenizer (documentation, System.getProperty("line.separator"));
        while (sp.hasMoreTokens ()) {
            String curLine = sp.nextToken ();
            // Process line wrapping
            curLine = curLine.trim ();
            while (rightMargin > 0 && curLine.length () + indent > rightMargin) {
                String theLine = curLine.substring (0, rightMargin - indent);
                // Try to find last word beginning to move the whole word to the next line
                int lastWordBeginning = theLine.lastIndexOf (' ");
                if (lastWordBeginning != -1)
                    // If no last word found, cut the rest of string independently of words set
                    theLine = theLine.substring (0, lastWordBeginning);
                // Write indentation before
                result += makeBlankString (indent);
                result += (theLine + System.getProperty("line.separator"));
                curLine = curLine.substring (theLine.length ());
                curLine = curLine.trim ();
            }
            // Write last part of the single line
            result += makeBlankString (indent);
            result += curLine;
            // If it was not last part of the source text, append carriage return
            if (sp.hasMoreTokens ())
                result += System.getProperty("line.separator");
        }
        return result;
    
public static java.lang.StringmakeBlankString(int len)
Creates string which contains len blankspaces

        String blankString = new String ();
        for (int i = 0; i < len; i++) {
            blankString += (" ");
        }
        return blankString;
    
public static java.lang.Stringreplace(java.lang.String source, java.lang.String searchFor, java.lang.String replaceWith)
Replaces a part(s) of the source string by new contents The method replaces all occurences of searchFor in original string. It means that after first replacing new text (replaceWith) will not be searched for searchFor, and it's possible to make replacement of "%%tag%%" by "%%tag%%%%tag%%" without getting of infinitive cycle.

param
source a source string
param
searchFor a string which should be replaced
param
replaceWith a string which replaces searchFor

        if (searchFor == null || searchFor.length () == 0)
            throw new IllegalArgumentException ("Pattern to find is null");

        String result = new String (source);
        int i = 0;
        int localIndex;                 // Index of last successful search
        while ((localIndex = result.substring (i).indexOf (searchFor)) != -1) {
            result = result.substring (0, i+localIndex) + replaceWith + result.substring (i + localIndex + searchFor.length ());
            // Don't search again in part, which already processed (for instance, it is possible to replace "%%dir%%" by "%%dir%%%%dir%%")
            i = i + localIndex + replaceWith.length ();
        }
        return result;
    
public static java.lang.StringreplaceTags(java.lang.String source, java.lang.String tagName, java.lang.String tagValue)
Calls replace to replace all occurence of source tagName rounded with mySpecDelimiter by tagValue if tagName is dir, then %%dir%% string occurences will be under replacement by default

        if (tagName == null || tagName.length () == 0)
            throw new IllegalArgumentException ("macroName is null");
        String macroName = mySpecDelimiter + tagName + mySpecDelimiter;
        return replace (source, macroName, tagValue);
    
public static voidsetTagDelimiter(java.lang.String delimiter)
Sets new delimiter, which rounds tag names for using with substituteMacros method

        mySpecDelimiter = delimiter;
    
public static java.lang.StringsubstituteMacros(java.lang.String source, java.util.Properties props)
Replaces all keys of the property by their values accordinately

        String result = new String (source);
        // Process every properties one by one
        Enumeration names = props.propertyNames ();
        while (names.hasMoreElements ()) {
            String key = (String)names.nextElement ();
            String value = props.getProperty (key);
            result = replaceTags (result, key, value);
        }
        return result;