001: /*
002: *
003: * $Id: MsgFmtTag.java,v 1.5 2005/09/21 10:53:32 dg154973 Exp $
004: * Copyright 2005 Sun Microsystems, Inc. All
005: * rights reserved. Use of this product is subject
006: * to license terms. Federal Acquisitions:
007: * Commercial Software -- Government Users
008: * Subject to Standard License Terms and
009: * Conditions.
010: *
011: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
012: * are trademarks or registered trademarks of Sun Microsystems,
013: * Inc. in the United States and other countries.
014: * MsgFragmentTag.java
015: *
016: * MsgFmtTag.java
017: *
018: * Created on February 25, 2005, 10:00 AM
019: */
020:
021: package com.sun.portal.wireless.taglibs.util;
022:
023: import java.io.IOException;
024:
025: import java.util.Map;
026: import java.util.HashMap;
027: import java.util.ResourceBundle;
028: import java.util.logging.Level;
029: import java.util.logging.Logger;
030:
031: import javax.servlet.jsp.JspException;
032: import javax.servlet.jsp.JspWriter;
033: import javax.servlet.jsp.tagext.BodyContent;
034: import javax.servlet.jsp.tagext.BodyTagSupport;
035:
036: import com.sun.portal.log.common.PortalLogger;
037:
038: /**
039: * MsgFmtTag performs formatting of message which is
040: * equivalent to MessageFormat class in java
041: * This class allows the arguments to be the JSP tag and
042: * they can be evaluated independently. The body of the
043: * tag should be embedded in the MessageFragment Tag. each
044: * fragment tag has unique id. The sequence of the format
045: * is defined in a resource bundle referred in catalog
046: * and key attribute. It can be used to reorder firstname, lastname
047: * in different locales
048: * Any messages/tags that are not embedded in Fragment tag will
049: * be ignored.
050: * @author senthils
051: */
052: public class MsgFmtTag extends BodyTagSupport {
053:
054: private static Logger logger = PortalLogger
055: .getLogger(MsgFmtTag.class);
056:
057: /**
058: * Initialization of catalog property.
059: */
060: private java.lang.String catalog;
061:
062: /**
063: * Initialization of key property.
064: */
065: private java.lang.String key;
066:
067: /** Creates a new instance of MsgFmtTag */
068: public MsgFmtTag() {
069: super ();
070: }
071:
072: /**
073: * This method is called when the JSP engine encounters the start tag,
074: * after the attributes are processed.
075: * Scripting variables (if any) have their values set here.
076: * @return EVAL_BODY_BUFFERED if the JSP engine should evaluate the tag body, otherwise return SKIP_BODY.
077: * This method is automatically generated. Do not modify this method.
078: * Instead, modify the methods that this method calls.
079: */
080:
081: public int doStartTag() throws JspException {
082: fragments = new HashMap();
083: return EVAL_BODY_BUFFERED;
084: }
085:
086: /**
087: * This method is called after the JSP engine finished processing the tag.
088: * @return EVAL_PAGE if the JSP engine should continue evaluating the JSP page, otherwise return SKIP_PAGE.
089: * This method is automatically generated. Do not modify this method.
090: * Instead, modify the methods that this method calls.
091: */
092: public int doEndTag() throws JspException, JspException {
093: return EVAL_PAGE;
094: }
095:
096: /**
097: * This method is called after the JSP engine processes the body content of the tag.
098: * @return EVAL_BODY_AGAIN if the JSP engine should evaluate the tag body again, otherwise return SKIP_BODY.
099: * This method is automatically generated. Do not modify this method.
100: * Instead, modify the methods that this method calls.
101: */
102: public int doAfterBody() throws JspException {
103: try {
104: BodyContent bodyContent = getBodyContent();
105: JspWriter out = bodyContent.getEnclosingWriter();
106: writeTagBodyContent(out, bodyContent);
107: } catch (Exception ex) {
108: handleBodyContentException(ex);
109: }
110: return SKIP_BODY;
111: }
112:
113: /**
114: * Setter for the catalog attribute.
115: */
116: public void setCatalog(java.lang.String value) {
117: this .catalog = value;
118: }
119:
120: /**
121: * Setter for the key attribute.
122: */
123: public void setKey(java.lang.String value) {
124: this .key = value;
125: }
126:
127: public void addFragment(String id, String message) {
128: fragments.put(id, message);
129: }
130:
131: private void writeTagBodyContent(JspWriter out,
132: BodyContent bodyContent) throws IOException {
133: rb = (Catalog) pageContext.getAttribute(catalog);
134: String token = rb.getMsg(key);
135: String[] tokens = token.split(",");
136: for (int i = 0; i < tokens.length; i++) {
137: String result = null;
138: result = (String) fragments.get(tokens[i]);
139: if (result != null)
140: out.println(result);
141: }
142: bodyContent.clearBody();
143: }
144:
145: private void handleBodyContentException(Exception ex)
146: throws JspException {
147: // Since the doAfterBody method is guarded, place exception handing code here.
148: throw new JspException("error in MsgFmtTag: " + ex);
149: }
150:
151: public void release() {
152: super .release();
153: rb = null;
154: fragments = null;
155: }
156:
157: Map fragments = null;
158: Catalog rb = null;
159: }
|