[Mc4j-cvs] mc4j/src/org/mc4j/console/dashboard/context MessageMapFormat.java,NONE,1.1 ContextHelper.
Brought to you by:
ghinkl
From: Greg H. <gh...@us...> - 2004-04-08 20:41:53
|
Update of /cvsroot/mc4j/mc4j/src/org/mc4j/console/dashboard/context In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv5292/src/org/mc4j/console/dashboard/context Modified Files: ContextHelper.java ContextType.java Added Files: MessageMapFormat.java Log Message: New message map format for context lookups should help a lot in making usable dashboards. --- NEW FILE: MessageMapFormat.java --- /* * Author: Greg Hinkle * * The contents of this file are subject to the Sapient Public License Version 1.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://mc4j.sf.net/License-SPL.html. * * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, * either express or implied. See the License for the specific language governing rights and limitations * under the License. * * The Original Code is The MC4J Management Console * The Initial Developer of the Original Code is Greg Hinkle (gh...@us...) * Copyright (C) 2004 Greg Hinkle. All Rights Reserved. * * Redistributions of code or binary files using or based on this code must reproduce the * above copyright and disclaimer. For more information see <http://mc4j.sourceforge.net>. */ package org.mc4j.console.dashboard.context; import java.text.FieldPosition; import java.text.Format; import java.text.ParsePosition; import java.util.Locale; import java.util.Map; import java.util.StringTokenizer; /** * <p>Supports replacement of arbitrary codes in a string with values from * a map. Includes support for replacing data with formatted objects using * the FormatManager. Each replaced element can be formatted with any other * com.sapient.framework.presentation.format.Formatter. Recursive formatting * using other instances of this formatter within a larger message are <b>not</b> * supported.</p> * * <h3>Use</h3> * <p>A message element is defined as follows <code>{ mapKey [, formatName] }</code>. * This allows any count of replacable elements to be in a single message. Other * non-replaced can also be supported around the message elements.</p> * * @author Greg Hinkle, January 2001 * @version $Revision: 1.1 $($Author: ghinkl $) */ public class MessageMapFormat extends Format { private static final String START = new String("{"); private static final String COMMA = new String(","); private static final String END = new String("}"); /** Creates new MessageMapFormat */ public MessageMapFormat() { } public StringBuffer format(Object obj,StringBuffer stringBuffer, FieldPosition fieldPosition) { return null; //return new StringBuffer(MessageMapFormat. // format(stringBuffer.toString(), this.formatMap, null)); } /** overloaded format function with locale information */ public StringBuffer format(Object obj,StringBuffer stringBuffer, FieldPosition fieldPosition, Locale locale) { return null; //return new StringBuffer(MessageMapFormat. // format(stringBuffer.toString(), this.formatMap, locale)); } public static String format(String message, Map formatMap) { return format(message, formatMap, null); } public static String format(String message, Map formatMap, Locale locale) { if (formatMap == null) return message; else { StringBuffer output = new StringBuffer(message.length()+10); StringTokenizer msgTokenizer = new StringTokenizer(message,"{,}",true); while (msgTokenizer.hasMoreElements()) { String element = (String) msgTokenizer.nextToken(); if (element.equals(END)) { // bad throw new IllegalArgumentException("The format string was invalid. " + "\"}\" was found where \"{\" was expected."); } else if (element.equals(START)) { // just output it String nextToken = msgTokenizer.nextToken(); if (!nextToken.equals(COMMA) && !nextToken.equals(START) && !nextToken.equals(END)) { // Start of element block String newNextToken = msgTokenizer.nextToken(); if (newNextToken.equals(COMMA)) { /* // has formatter String formatName = msgTokenizer.nextToken().trim(); // Fail if the data we're looking up is missing. if (!formatMap.containsKey(nextToken.trim())) throw new IllegalArgumentException("MessageMapFormat.format - " + "The parameter [" + nextToken.trim() + "] was not found in the supplied map."); Object data = formatMap.get(nextToken.trim()); try { String formattedData = FormatManager.format(data,formatName, locale); output.append(formattedData); } catch (FormatterNotFoundException fnfe) { throw new IllegalArgumentException("MessageMapFormat.format - " + "The specified formatter was not defined.",fnfe); } if (!msgTokenizer.nextToken().equals(END)) { throw new InvalidParameterException("MessageMapFormat.format - " + "The format string was invalid.", new IllegalMaskException("A \"}\" was expected at the end of the message mask element.")); } */ } else if (newNextToken.equals(END)) { // Non-formatted piece of data // Fail if the data we're looking up is missing. if (!formatMap.containsKey(nextToken.trim())) throw new IllegalArgumentException("MessageMapFormat.format - " + "The parameter [" + nextToken.trim() + "] was not found in the supplied map."); Object data = formatMap.get(nextToken); output.append(data); } else { // bad throw new IllegalArgumentException("MessageMapFormat.format - " + "The format string was invalid. " + "A replacement element should end with \"}\"."); } } else { // No more tokens... } } else { // Just text output.append(element); } } return output.toString(); } } public Object parseObject(String str, ParsePosition parsePosition) { throw new UnsupportedOperationException("Map message parsing is not yet supported."); } } Index: ContextHelper.java =================================================================== RCS file: /cvsroot/mc4j/mc4j/src/org/mc4j/console/dashboard/context/ContextHelper.java,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** ContextHelper.java 7 Feb 2004 16:10:41 -0000 1.5 --- ContextHelper.java 8 Apr 2004 20:28:36 -0000 1.6 *************** *** 134,137 **** --- 134,141 ---- } + if (ContextHelper.typeFromString(identifier) == ContextType.MESSAGE_MAP) { + return (new MessageMapFormat()).format(ContextHelper.attributeNameFromString(identifier), displayObjects); + } + //Gather some information from the identifier String //to prepare for handling backwardCompatibility mode and *************** *** 252,256 **** throw new IllegalArgumentException(errorMsg); } ! else{ //This is the catch-all error case String errorMsg = --- 256,264 ---- throw new IllegalArgumentException(errorMsg); } ! else if (sourceType.equals(ContextType.MESSAGE_MAP)) { ! currentAttribute = ! (new MessageMapFormat()).format(currentAttributeName, (Map) source); ! ! } else { //This is the catch-all error case String errorMsg = Index: ContextType.java =================================================================== RCS file: /cvsroot/mc4j/mc4j/src/org/mc4j/console/dashboard/context/ContextType.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** ContextType.java 7 Feb 2004 16:10:41 -0000 1.3 --- ContextType.java 8 Apr 2004 20:28:37 -0000 1.4 *************** *** 36,39 **** --- 36,40 ---- public static final ContextType LIST = new ContextType (3, "List"); public static final ContextType LITERAL = new ContextType (4,"Literal"); + public static final ContextType MESSAGE_MAP = new ContextType (5, "Message"); /** *************** *** 68,72 **** return ContextType.LITERAL; } ! else { return ContextType.INVALID; } --- 69,75 ---- return ContextType.LITERAL; } ! else if (name.equals(ContextType.MESSAGE_MAP.name)) { ! return ContextType.MESSAGE_MAP; ! } else { return ContextType.INVALID; } |