FileDocCategorySizeDatePackage
FormattingStyles.javaAPI DocExample15684Tue Dec 08 01:21:00 GMT 1998oisoft.togetherx.scripts.doc

FormattingStyles.java

package oisoft.togetherx.scripts.doc;

import oisoft.togetherx.scripts.tools.Formatter;
import java.util.ResourceBundle;
import java.util.PropertyResourceBundle;
import java.util.MissingResourceException;
import java.util.Properties;
import java.util.Hashtable;
import java.util.Enumeration;

import java.io.InputStream;
import java.io.IOException;

public class FormattingStyles implements Styles {
    public FormattingStyles (String docType) throws MissingResourceException {
        // Try to open resources as class/property
        myBundle = ResourceBundleAccessor.getResourceBundle ("oisoft.togetherx.scripts.doc.Res" + docType, "Res" + docType);
    }

    /**
     * General method to access style beginning
     */
    public String style (String styleName) {
        String stBegin = styleName + mySTARTMARK;
        String value;
        try {
            value = myBundle.getString (propertyKey (stBegin));
        }
        catch (MissingResourceException e) {
            value = new String (mySTYLE_NOT_DEFINED_MSG + styleName);
        }
        return value;
    }
    /**
     * General method to access style end
     */
    public String styleEnd (String styleName) {
        String stEnd = styleName + myENDMARK;
        String value;
        try {
            value = myBundle.getString (propertyKey (stEnd));
        }
        catch (MissingResourceException e) {
            value = new String (mySTYLE_NOT_DEFINED_MSG + stEnd);
        }
        return value;
    }

    /**
     * Private method to access internal properties (unavailable from the script)
     * Also it may be used to check resource availability
     * @return  String if resource found, or null if not
     */
    public String resource (String resourceName) {
        String value;
        try {
            value = myBundle.getString (propertyKey (resourceName));
        }
        catch (MissingResourceException e) {
            return null;
        }
        return value;
    }

    /**
     * Returns string, which should starts a new document (<HTML>, for instance, for HTML document)
     */
    public String document () {
        String documentHeader = new String (style ("Document"));

        // Collect a font declarations, which should be written into header
        String fontDeclarationTable = new String ();

        String fontReference = resource ("Font.reference");

        if (fontReference != null) {
            int i = 1;
            String fontName;
            do {
                String resourceName = new String ("Font."+Integer.toString (i));
                fontName = resource (resourceName);
                if (fontName != null) {
                    String fontDeclaration = resource (resourceName + ".declaration");
                    if (fontDeclaration != null) {
                        Properties props = new Properties ();
                        props.put ("number", Integer.toString (i));
                        props.put ("name", fontName);
                        fontDeclaration = Formatter.substituteMacros (fontDeclaration, props);
                        fontDeclarationTable += fontDeclaration;
                        // Put the font with reference into internal vector
                        String theFontReference = new String (fontReference);
                        theFontReference = Formatter.substituteMacros (theFontReference, props);
                        myFonts.put (fontName, theFontReference);
                    }
                }
                ++i;
            } while (fontName != null);
        }
        Properties fontsAndStyles = new Properties ();
        fontsAndStyles.put ("fontDeclarations", fontDeclarationTable);
        documentHeader = Formatter.substituteMacros (documentHeader, fontsAndStyles);
        return documentHeader;
    }
    /**
     * Returns string, which should finish a document
     */
    public String documentEnd () {
        return styleEnd ("Document");
    }

    /**
     * Title start
     */
    public String title () {
        return style ("Title");
    }
    /**
     * Title end
     */
    public String titleEnd () {
        return styleEnd ("Title");
    }

    /**
     * Body start
     */
    public String body () {
        return style ("Body");
    }
    /**
     * Body end
     */
    public String bodyEnd () {
        return styleEnd ("Body");
    }

    /**
     * Returns a paragraph separator
     */
    public String par () {
        return style ("par");
    }
    /**
     * Returns a paragraph separator end
     */
    public String parEnd () {
        return styleEnd ("par");
    }

    /**
     * Line separator
     */
    public String br () {
        return style ("new_line");
    }

    /**
     * Returns a section separator
     */
    public String hr () {
        return style ("new_section");
    }

    /**
     * Returns a page
     */
    public String page () {
        return style ("new_page");
    }

    /**
     * Starts of preformatted code
     */
    public String pre () {
        return style ("preformatted");
    }
    /**
     * Finishs of preformatted code
     */
    public String preEnd () {
        return styleEnd ("preformatted");
    }

    /**
     * Selects a font size
     */
    public String font (int size) {
        String fontDeclaration = style ("FontSize");
        fontDeclaration = Formatter.replaceTags (fontDeclaration, "size", Integer.toString (size));
        return fontDeclaration;
    }
    /**
     * Selects a font class
     */
    public String fontClass (String name) {
        String fontReference = (String)myFonts.get (name);
        if (fontReference == null) {
            String fontDeclaration = style ("FontClass");
            fontDeclaration = Formatter.replaceTags (fontDeclaration, "fontName", name);
            return fontDeclaration;
        }
        return fontReference;
    }
    /**
     * Selects a font class and size
     */
    public String fontSizeClass (int size, String name) {
        String fontDeclaration = style ("FontSizeClass");
        fontDeclaration = Formatter.replaceTags (fontDeclaration, "size", Integer.toString (size));
        fontDeclaration = Formatter.replaceTags (fontDeclaration, "fontName", name);
        return fontDeclaration;
    }
    /**
     * Selects a font color
     */
    public String fontColor (String color) {
        String fontDeclaration = style ("FontColor");
        fontDeclaration = Formatter.replaceTags (fontDeclaration, "color", color);
        return fontDeclaration;
    }
    /**
     * End font selection
     */
    public String fontEnd () {
        return styleEnd ("Font");
    }

    /**
     * Start anchor
     */
    public String anchor (String anchor, String displayText) {
        String anchorDeclaration = style ("Anchor");
        Properties macros = new Properties ();
        macros.put ("name", normalizeAnchor (anchor));
        macros.put ("text", displayText);
        anchorDeclaration = Formatter.substituteMacros (anchorDeclaration, macros);
        return anchorDeclaration;
    }

    /**
     * Start href
     */
    public String href (String name) {
        String hrefDeclaration = style ("Href");
        hrefDeclaration = Formatter.replaceTags (hrefDeclaration, "name", processBackSlashes (name));
        return hrefDeclaration;
    }
    /**
     * Start href with index
     */
    public String hrefWithIndex (String name, String index) {
        String hrefDeclaration = style ("HrefIndex");
        Properties macros = new Properties ();
        macros.put ("name", processBackSlashes (name));
        // For bookmark not all characters are acceptable, replace them by the same way as create anchor
        macros.put ("index", normalizeAnchor (index));
        hrefDeclaration = Formatter.substituteMacros (hrefDeclaration, macros);
        return hrefDeclaration;
    }
    /**
     * End href
     */
    public String hrefEnd () {
        return styleEnd ("Href");
    }

    /**
     * Picture
     */
    public String picture (String fileName) {
        String pictureDeclaration = style ("Picture");
        String goodFileName = processBackSlashes (fileName);
        pictureDeclaration = Formatter.replaceTags (pictureDeclaration, "fileName", goodFileName);
        return pictureDeclaration;
    }
    /**
     * Picture with alternative text
     */
    public String pictureWithAlternativeText (String fileName, String message) {
        String pictureDeclaration = style ("PictureAltText");
        Properties macros = new Properties ();
        String goodFileName = processBackSlashes (fileName);
        macros.put ("fileName", goodFileName);
        macros.put ("message", message);
        pictureDeclaration = Formatter.substituteMacros (pictureDeclaration, macros);
        return pictureDeclaration;
    }

    /**
     * Nested document
     */
    public String nestedDocument (String documentFileName) {
        String nestedDocumentDeclaration = style ("NestedDocument");
        String goodFileName = processBackSlashes (documentFileName);
        nestedDocumentDeclaration = Formatter.replaceTags (nestedDocumentDeclaration, "fileName", goodFileName);
        return nestedDocumentDeclaration;
    }

    /**
     * Start list
     */
    public String list () {
        myListIndent++;
        String listDeclaration;
        String rightResource = resource ("List"+"."+ Integer.toString (myDeflistIndent));
        if (rightResource != null)
            listDeclaration = rightResource;
        else
            listDeclaration = style ("List");
        return listDeclaration;
    }
    /**
     * Start list with bullet type
     */
    public String list (String style) {
        String listDeclaration = style ("ListType");
        listDeclaration = Formatter.replaceTags (listDeclaration, "style", style);
        return listDeclaration;
    }
    /**
     * List item
     */
    public String listItem (String itemText) {
        String indent = resource ("ListItem.indent");
        int numIndent;
        if (indent != null)
            numIndent = Integer.parseInt (indent, 10);
        else
            numIndent = 1;

        String text = style ("ListItem");

        Properties macros = new Properties ();
        macros.put ("indent", Integer.toString (numIndent * myDeflistIndent));
        macros.put ("text", itemText);

        text = Formatter.substituteMacros (text, macros);

        return text;
    }
    /**
     * End list
     */
    public String listEnd () {
        myListIndent--;
        return styleEnd ("List");
    }

    /**
     * Start definition list
     */
    public String definitionList () {
        myDeflistIndent++;
        return style ("DefinitionList");
    }
    /**
     * End definition list
     */
    public String definitionListEnd () {
        myDeflistIndent--;
        return styleEnd ("DefinitionList");
    }
    /**
     * Definition list term
     */
    public String definitionListTerm (String text) {
        String listItemDeclaration;
        String rightResource = resource ("DefinitionListTerm"+"."+ Integer.toString (myDeflistIndent));
        if (rightResource != null)
            listItemDeclaration = rightResource;
        else
            listItemDeclaration = style ("DefinitionListTerm");
        listItemDeclaration = Formatter.replaceTags (listItemDeclaration, "text", text);
        return listItemDeclaration;
    }
    /**
     * Start Definition list Definition
     */
    public String definitionListDefinition () {
        String indent = resource ("DefinitionListDefinition.indent");
        int numIndent;
        if (indent != null)
            numIndent = Integer.parseInt (indent, 10);
        else
            numIndent = 1;

        String text = style ("DefinitionListDefinition");

        Properties macros = new Properties ();
        macros.put ("indent", Integer.toString (numIndent * myDeflistIndent));

        text = Formatter.substituteMacros (text, macros);

        return text;
    }
    /**
     * End Definition list Definition
     */
    public String definitionListDefinitionEnd () {
        return styleEnd ("DefinitionListDefinition");
    }

    /**
     * Start Underline
     */
    public String underline () {
        return style ("Underline");
    }
    /**
     * End Underline
     */
    public String underlineEnd () {
        return styleEnd ("Underline");
    }

    /**
     * Start bold
     */
    public String bold () {
        return style ("Bold");
    }
    /**
     * End bold
     */
    public String boldEnd () {
        return styleEnd ("Bold");
    }

    /**
     * Start italic
     */
    public String italic () {
        return style ("Italic");
    }
    /**
     * End italic
     */
    public String italicEnd () {
        return styleEnd ("Italic");
    }

    /**
     * Start superscript
     */
    public String superscript () {
        return style ("Superscript");
    }
    /**
     * End superscript
     */
    public String superscriptEnd () {
        return styleEnd ("Superscript");
    }

    /**
     * Start subscript
     */
    public String subscript () {
        return style ("Subscript");
    }
    /**
     * End subscript
     */
    public String subscriptEnd () {
        return styleEnd ("Subscript");
    }

    /**
     * Start strikeout
     */
    public String strikeout () {
        return style ("Strikeout");
    }
    /**
     * End strikeout
     */
    public String strikeoutEnd () {
        return styleEnd ("Strikeout");
    }

    /**
     * Create unique anchor string for the source string
     * If the anchor already in table, return previous index,
     * otherwise create new one
     */
    private String normalizeAnchor (String source) {
        String anchor = (String)myAnchors.get (source);
        if (anchor == null) {
            myLastAnchorIndex++;
            anchor = myANCHOR_PREFIX + Integer.toString (myLastAnchorIndex);
            myAnchors.put (source, anchor);
        }
        return anchor;
    }

    /**
     * Reverses all backslashes to right one to make file names compatible with all documentation format
     */
    private String processBackSlashes (String source) {
        String result = new String (source);
        result = Formatter.replace (result, "\\", "/");
        return result;
    }

    /**
     * Convert all blanks of text to underscores, because property keys do not support spaces
     */
    private String propertyKey (String text) {
        return Formatter.replace (text, " ", "_");
    }

    private ResourceBundle myBundle;
    private Hashtable myAnchors = new Hashtable ();
    int myLastAnchorIndex = 0;
    private static final String mySTARTMARK = "_start";
    private static final String myENDMARK = "_end";
    private static final String myANCHOR_PREFIX = "a";
    private static final String mySTYLE_NOT_DEFINED_MSG = "Style not defined or defined incorrectly: ";
    private Hashtable myFonts = new Hashtable ();
    private Hashtable myStyles = new Hashtable ();
    private int myListIndent = 0;
    private int myDeflistIndent = 0;
}