ruper2/src/java/core/org/krysalis/ruper2/util/text Messages.java,NONE,1.1 MessageConstants.java,NONE,1.1

[email protected]
Newsgroups gmane.comp.krysalis.metamorphosis.cvs
Message-ID <[email protected]>
Update of /cvsroot/metamorphosis/ruper2/src/java/core/org/krysalis/ruper2/util/text
In directory sc8-pr-cvs1:/tmp/cvs-serv30481/src/java/core/org/krysalis/ruper2/util/text

Added Files:
	Messages.java MessageConstants.java 
Log Message:
Avoid clash with existing Ruper...

--- NEW FILE: Messages.java ---
/*
 * Created on Aug 26, 2003
 */
package org.krysalis.ruper2.util.text;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import org.krysalis.version.util.debug.DebugUtils;

/**
 * Formats messages.
 *
 * @version $Revision: 1.1 $ $Date: 2003/09/29 18:09:21 $
 */
public class Messages {
	//Name of the resource Bundle
	private static final String BUNDLE_NAME = "krysalis-ruper";

	//Intialized the ResourceBundle.
	static ResourceBundle l_resourceBundle = null;

	static {
		try {
			l_resourceBundle =
				ResourceBundle.getBundle(BUNDLE_NAME, Locale.getDefault());
		}
		catch (Exception e) {
			System.err.println(
				"Unable to find Ruper resource bundle ["
					+ BUNDLE_NAME
					+ "] with locale ["
					+ Locale.getDefault()
					+ "]");
		}
	}

	private Messages() {
	}

	/**
	 * Formats a message.
	 *
	 * @param code The message code.
	 * @return The formatted message.
	 */
	public static String getString(final String code) {
		return getString(code, new Object[0]);
	}

	/**
	 * Formats a message.
	 *
	 * @param code The message code.
	 * @param param The message parameter.
	 * @return The formatted message.
	 */
	public static String getString(final String code, final Object param) {
		return getString(code, new Object[] { param });
	}

	/**
	 * Formats a message.
	 *
	 * @param code The message code.
	 * @param param1 The first message parameter.
	 * @param param2 The second message parameter.
	 * @return The formatted message.
	 */
	public static String getString(
		final String code,
		final Object param1,
		Object param2) {
		return getString(code, new Object[] { param1, param2});
	}

	/**
	 * Formats a message.
	 *
	 * @param code The message code.
	 * @param param1 The first message parameter.
	 * @param param2 The second message parameter.
	 * @param param3 The third message parameter.
	 * @return The formatted message.
	 */
	public static String getString(
		final String code,
		final Object param1,
		Object param2,
		Object param3) {
		return getString(
			code,
			new Object[] {
				param1,
				param2,
				param3});
	}

	/**
	 * Formats a message.
	 *
	 * @param code The message code.
	 * @param params The message parameters.
	 * @return The formatted message.
	 */
	public static String getString(final String code, final Object[] params) {
		String formatted = null;

		try {
			final MessageFormat msg = loadMessageFormat(code);

			if (null != msg)
				formatted = msg.format(params);
			else
				formatted = "Code [" + code + "] " + getParameterString(params);
		}
		catch (final MissingResourceException mre) {
			formatted = "Code [" + code + "] " + getParameterString(params);
		}

		return formatted;
	}

	public static String getParameterString(final Object[] params) {
		StringBuffer paramStr = new StringBuffer();

		for (int i = 0; i < params.length; ++i) {
			paramStr.append(
				(null != params[i]) ? params[i].toString() : "null");
			if (i < (params.length - 1))
				paramStr.append(' ');
		}

		return paramStr.toString();
	}

	/**
	 * Locates a message by its code.
	 * Note: package protected so testable.
	 */
	static MessageFormat loadMessageFormat(final String code)
		throws MissingResourceException {

		MessageFormat format = null;

		String msgText = null;

		try {
			msgText =
				(null != l_resourceBundle)
					? l_resourceBundle.getString(code)
					: null;
		}
		catch (MissingResourceException mre) {
		}

		// get the message format
		if (msgText != null)
			format = new MessageFormat(msgText);

		return format;
	}

	static List getCodes() {
		List codes = new ArrayList();

		for (Enumeration e = l_resourceBundle.getKeys();
			e.hasMoreElements();
			) {
			String code = (String) e.nextElement();
			codes.add(code);
		}

		return codes;
	}

	//	public static Object safeParam(Object param) {
	//		return (null == param) ? "*Unset*" : param;
	//	}

	public static void dump() {
		DebugUtils.dump(getCodes());
	}

	public static void main(String args[]) {
		Messages.dump();
	}

}

--- NEW FILE: MessageConstants.java ---
/*
 * Created on Sep 10, 2003
  */
package org.krysalis.ruper2.util.text;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * @author anou_mana
 */
public class MessageConstants {

	//:TODO: We have to (1) organzied (2) document these ASAP
	// before they get out of control...

	public static final String GENERAL_EXCEPTION =
		"GENERAL_EXCEPTION";
	public static final String GENERAL_EXCEPTION2 =
		"GENERAL_EXCEPTION2";
	public static final String GENERAL_EXCEPTION3 =
		"GENERAL_EXCEPTION3";

	//ERRORS
	public static final String WRONG_TYPE = "WRONG_TYPE";
	public static final String NO_SUCH_REFERENCE =
		"NO_SUCH_REFERENCE";
	public static final String CREATE_OBJECT_ERROR = "CREATE_OBJECT_ERROR";

	// Configuration

	public static final String CONFIG = "CONFIG";
	public static final String CONFIG_PARSER_ERROR =
		"CONFIG_PARSER_ERROR";
	public static final String UNABLE_TO_LOAD_CONFIG =
		"UNABLE_TO_LOAD_CONFIG";

	// Repository Operations..
	public static final String LIST_GROUPS = "LIST_GROUPS";
	public static final String LIST_RESOURCES = "LIST_RESOURCES";
	public static final String LIST_SPECIFIERS = "LIST_SPECIFIERS";
	public static final String DELETE_RESOURCE = "DELETE_RESOURCES";
	public static final String PUBLISH_RESOURCE = "PUBLISH_RESOURCE";

	public static final String LIST_GROUPS_FAILED = "LIST_GROUPS_FAILED";
	public static final String LIST_RESOURCES_FAILED = "LIST_RESOURCES_FAILED";
	public static final String LIST_SPECIFIERS_FAILED =
		"LIST_SPECIFIERS_FAILED";
	public static final String DELETE_RESOURCE_FAILED =
		"DELETE_RESOURCES_FAILED";
	public static final String PUBLISH_RESOURCE_FAILED =
		"PUBLISH_RESOURCE_FAILED";

	public static final String SEARCH_REPOSITORY = "SEARCH_REPOSITORY";
	public static final String RESOURCE_GROUP = "RESOURCE_GROUP";
	public static final String RESOURCE_REQUEST = "RESOURCE_REQUEST";

	public static final String UPDATE_FOUND = "UPDATE_FOUND";

	public static final String NO_RESOURCES_FOUND = "NO_RESOURCES_FOUND";
	public static final String NO_RESOURCES_FOUND_IN = "NO_RESOURCES_FOUND_IN";
	public static final String NO_UPDATE_FOUND = "NO_UPDATE_FOUND";
	public static final String LIST_FAILED = "LIST_FAILED";

	public static final String PROCESS = "PROCESS";
	public static final String EXTRACTED = "EXTRACTED";
	public static final String MERGE = "MERGE";

	public static final String FILENAME_COMPARATOR = "FILENAME_COMPARATOR";
	public static final String NAME_COMPARATOR = "NAME_COMPARATOR";
	public static final String TYPE_COMPARATOR = "TYPE_COMPARATOR";
	public static final String VERSION_COMPARATOR = "VERSION_COMPARATOR";

	public static final String FILENAME_SELECTOR = "FILENAME_SELECTOR";
	public static final String NAME_SELECTOR = "NAME_SELECTOR";
	public static final String TYPE_SELECTOR = "TYPE_SELECTOR";
	public static final String VERSION_SELECTOR = "VERSION_SELECTOR";

	// For introspection, see below...
	private MessageConstants() {
	}

	public static List getCodes() {
		List codes = new ArrayList();

		Object me = new MessageConstants();
		Class myself = MessageConstants.class;

		Field[] fields = myself.getFields();

		for (int i = 0; i < fields.length; ++i) {
			Field field = fields[i];

			try {
				codes.add((String) field.get(me));
			}
			catch (Exception e) {
				//Ignore...
			}
		}

		return codes;
	}
}




-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.