001: package net.xoetrope.xui.helper;
002:
003: import java.util.Hashtable;
004: import net.xoetrope.xui.XProjectManager;
005:
006: /**
007: * Formats messages (for 1.1.x JDKs)
008: * <p>Copyright (c) Xoetrope Ltd., 1998-2003<br>
009: * License: see license.txt
010: * @version $Revision: 1.9 $
011: */
012: public class MessageHelper {
013: static Hashtable messageText;
014: static MessageHelper msgHelper;
015:
016: /**
017: * Setup this helper object. getInstance or the XProject getMessageHelper method
018: * should be used instead of calling this constructor.
019: */
020: public MessageHelper() {
021: messageText = new Hashtable(10);
022: }
023:
024: /**
025: * Get an instance of the message helper
026: * @return
027: */
028: public static MessageHelper getInstance() {
029: return XProjectManager.getCurrentProject().getMessageHelper();
030: }
031:
032: /**
033: * Replace parts of a message
034: * @param message the raw message
035: * @param replacement the replace for the tokens
036: * @return the processed message
037: */
038: public static String replaceTokens(String message,
039: String replacement) {
040: String sRet = null;
041:
042: sRet = message;
043: int startPos = message.indexOf("[");
044: while (startPos > -1) {
045: int endPos = message.indexOf("]", startPos);
046: String key = message.substring(startPos + 1, endPos);
047: sRet = message.substring(0, startPos);
048: if (replacement == null) {
049: replacement = (String) messageText.get(key);
050: if (replacement == null)
051: replacement = key;
052: }
053: sRet += replacement;
054: sRet += message.substring(endPos + 1, message.length());
055: message = sRet;
056: startPos = message.indexOf("[");
057: }
058: return sRet;
059: }
060:
061: /**
062: * Get the next token in the message
063: * @param message the raw message
064: * @return the first token
065: */
066: public static String getNextToken(String message) {
067: String sRet = null;
068:
069: int startPos = message.indexOf("[");
070: if (startPos > -1) {
071: int endPos = message.indexOf("]", startPos);
072: sRet = message.substring(startPos + 1, endPos);
073: }
074: return sRet;
075: }
076:
077: /**
078: * Replace/Strip the tokens in a message
079: * @param message the raw messsage
080: * @return the precessed message
081: */
082: public static String replaceTokens(String message) {
083: return replaceTokens(message, null);
084: }
085:
086: /**
087: * Replace the tokens in a message
088: * @param message the raw messsage
089: * @param key the raw messsage the key/token to lookup/replace
090: * @param replacement the value to replace the token
091: * @return the precessed message
092: */
093: public static String replaceToken(String message, String key,
094: String replacement) {
095: String sRet = null;
096:
097: sRet = message;
098: int startPos = message.indexOf(key);
099: while (startPos > -1) {
100: sRet = message.substring(0, startPos);
101: sRet += replacement;
102: sRet += message.substring(startPos + key.length(), message
103: .length());
104: message = sRet;
105: startPos = message.indexOf(key);
106: }
107:
108: return sRet;
109: }
110: }
|