krysalis-update/src/java/org/krysalis/depot/update/util/text MessageConstants.java,NONE,1.1 Messages.java,NONE,1.1

Nick Chalko <[email protected]> Wed, 08 Dec 2004 09:07:23 +0000
Newsgroups gmane.comp.krysalis.cvs
Message-ID <[email protected]>
Update of /cvsroot/krysalis/krysalis-update/src/java/org/krysalis/depot/update/util/text
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv24726/src/java/org/krysalis/depot/update/util/text

Added Files:
	MessageConstants.java Messages.java 
Log Message:
Copied from the apache incubator.

--- NEW FILE: MessageConstants.java ---
/*
 * Copyright  2004 The Apache Software Foundation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.krysalis.depot.update.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_ARTIFACTS = "LIST_ARTIFACTS";
	public static final String GET_GROUP_MANIFEST = "GET_GROUP_MANIFEST";
	public static final String DELETE_RESOURCE = "DELETE_RESOURCE";
	public static final String PUBLISH_RESOURCE = "PUBLISH_RESOURCE";

	public static final String INITIALIZE_FAILED = "INITIALIZE_FAILED";
	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_ARTIFACTS_FAILED =
		"LIST_ARTIFACTS_FAILED";
	public static final String DELETE_RESOURCE_FAILED =
		"DELETE_RESOURCE_FAILED";
	public static final String PUBLISH_RESOURCE_FAILED =
		"PUBLISH_RESOURCE_FAILED";

	public static final String SEARCH_REPOSITORY = "SEARCH_REPOSITORY";
	public static final String NO_TARGET_REPOSITORY = "NO_TARGET_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 RESOURCES_FOUND_IN = "RESOURCES_FOUND_IN";
	public static final String RESOURCES_FOUND = "RESOURCES_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 NULL_RESOURCE = "NULL_RESOURCE";
	public static final String NULL_SPECIFIER = "NULL_SPECIFIER";
	public static final String NULL_GROUP = "NULL_GROUP";
	
	public static final String PROCESS = "PROCESS";
	public static final String EXTRACTED = "EXTRACTED";
	public static final String PARSED = "PARSED";
	public static final String IGNORED= "IGNORED";
	public static final String FAILED = "FAILED";
	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";
	public static final String CHILD_VRL_SELECTOR = "CHILD_VRL_SELECTOR";
	public static final String MATCHING_VRL_SELECTOR = "MATCHING_VRL_SELECTOR";
	public static final String PROTOCOL_VRL_SELECTOR = "PROTOCOL_VRL_SELECTOR";
		
	public static final String XML_BEAN_ERROR = "XML_BEAN_ERROR";

	// 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;
	}
}

--- NEW FILE: Messages.java ---
/*
 * Copyright  2004 The Apache Software Foundation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.krysalis.depot.update.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.depot.common.util.debug.DebugUtils;

/**
 * Formats messages.
 *
 * @version $Revision: 1.1 $ $Date: 2004/12/08 09:07:20 $
 */
public class Messages {
	//Name of the resource Bundle
	private static final String BUNDLE_NAME = "depot-update";

	//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 Update 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) {
			//:TODO: Do we need to print to System.err?
		}

		// get the message format
		if (msgText != null) {
			try {
				format = new MessageFormat(msgText);
			}
			catch (Exception e) {
				//:TODO: Do we need to print to System.err?
			}
		}

		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();
	}

}



-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users.
Discover which products truly live up to the hype. Start reading now. 
http://productguide.itmanagersjournal.com/