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

Formatter.java

// Generated by Together
//---------------------------------------------------------
// Copyright (c) 1998  Object International Software GmbH
// Author: Nick Entin
// Version: 1.01  03 aug 98
//---------------------------------------------------------
package oisoft.togetherx.scripts.tools;

import java.util.StringTokenizer;
import java.util.Properties;
import java.util.Enumeration;

/**String Formatter*/
public class Formatter {
    /**Creates string which contains len blankspaces*/
    static public String makeBlankString (int len) {
        String blankString = new String ();
        for (int i = 0; i < len; i++) {
            blankString += (" ");
        }
        return blankString;
    }
    /**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*/
    static public String fillToLen(String source, int len, boolean right){
        String blankString = makeBlankString (len-source.length ());

        if (source.length () > len) {
            return source.substring (0, len);
        }
        if (right)
            return source+blankString;
        else
            return blankString+source;
    }

    /**Converts source string to result by adding spaces to the end,
    until len characters be accepted*/
    static public String fillToLen(String source, int len) {
        return fillToLen (source, len, true);
    }

    /**Converts source integer value to result string and adds spaces to the end,
    until len characters be accepted*/
    static public String fillToLen(int value, int len) {
        return fillToLen (Integer.toString (value), len);
    }

    /**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*/
    static public String fillToLen(int value, int len, boolean right) {
        return fillToLen (Integer.toString (value), len, right);
    }

    /**Converts source double value to result string and adds spaces to the end,
    until len characters be accepted*/
    static public String fillToLen(double value, int len) {
        return fillToLen (Double.toString (value), len);
    }

    /**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*/
    static public String fillToLen(double value, int len, boolean right) {
        return fillToLen (Double.toString (value), len, right);
    }

    /**Generates string from the source one with indentation of every new line*/
    static public String indent (String documentation, int indent) throws FormatterIndentOverRMarginException {
        return indentWithWrap (documentation, indent, 0);
    }

    /**Generates string from the source one with indentation of every new line and move a word to next line if right
    margin is accepted*/
    static public String indentWithWrap (String documentation, int indent, int rightMargin) throws FormatterIndentOverRMarginException {
        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;
    }

    /**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
    */
    static public String replace (String source, String searchFor, String replaceWith) {
        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;
    }

    /**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*/
    static public String replaceTags (String source, String tagName, String tagValue) {
        if (tagName == null || tagName.length () == 0)
            throw new IllegalArgumentException ("macroName is null");
        String macroName = mySpecDelimiter + tagName + mySpecDelimiter;
        return replace (source, macroName, tagValue);
    }

    /**Replaces all keys of the property by their values accordinately*/
    static public String substituteMacros (String source, Properties props) {
        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;
    }

    /**Sets new delimiter, which rounds tag names for using with substituteMacros method*/
    static public void setTagDelimiter (String delimiter) {
        mySpecDelimiter = delimiter;
    }

    /**The delimiter, which rounds replacing tags
    May be changed by Formatter.setTagDelimiter*/
    static private String mySpecDelimiter = "%%";
}