001: /*
002: * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
003: *
004: * This file is part of TransferCM.
005: *
006: * TransferCM is free software; you can redistribute it and/or modify it under the
007: * terms of the GNU General Public License as published by the Free Software
008: * Foundation; either version 2 of the License, or (at your option) any later
009: * version.
010: *
011: * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
012: * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
013: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
014: * details.
015: *
016: * You should have received a copy of the GNU General Public License along with
017: * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
018: * Fifth Floor, Boston, MA 02110-1301 USA
019: */
020:
021: package com.methodhead.tag;
022:
023: import java.io.IOException;
024: import java.util.Iterator;
025: import javax.servlet.jsp.tagext.TagSupport;
026:
027: import org.apache.struts.Globals;
028: import org.apache.struts.action.ActionErrors;
029: import org.apache.struts.action.ActionError;
030: import org.apache.struts.util.MessageResources;
031: import org.apache.commons.lang.exception.ExceptionUtils;
032:
033: public class InvalidTag extends TagSupport {
034:
035: // constructors /////////////////////////////////////////////////////////////
036:
037: // constants ////////////////////////////////////////////////////////////////
038:
039: // classes //////////////////////////////////////////////////////////////////
040:
041: // methods //////////////////////////////////////////////////////////////////
042:
043: public int doStartTag() {
044: try {
045: invalid_ = false;
046:
047: ActionErrors errors = (ActionErrors) pageContext
048: .getRequest().getAttribute(Globals.ERROR_KEY);
049:
050: MessageResources messages = (MessageResources) pageContext
051: .getRequest().getAttribute(Globals.MESSAGES_KEY);
052:
053: if (property_ == null)
054: property_ = ActionErrors.GLOBAL_ERROR;
055:
056: String style = " class=\"formcell\"";
057: String message = "";
058:
059: if ((errors != null) && (messages != null)
060: && (property_ != null)) {
061:
062: Iterator iter = errors.get(property_);
063:
064: if (iter.hasNext()) {
065:
066: invalid_ = true;
067: pageContext
068: .getOut()
069: .println(
070: "<div class=\"invalid\"><div class=\"invalid-message\">");
071:
072: while (iter.hasNext()) {
073: ActionError error = (ActionError) iter.next();
074:
075: pageContext.getOut().println(
076: messages.getMessage(error.getKey(),
077: error.getValues()));
078: }
079:
080: pageContext.getOut().println("</div>");
081: }
082: }
083: } catch (IOException e) {
084: throw new RuntimeException("Unexpected IOException: "
085: + ExceptionUtils.getStackTrace(e));
086: }
087:
088: return EVAL_BODY_INCLUDE;
089: }
090:
091: public int doEndTag() {
092: try {
093: if (invalid_)
094: pageContext.getOut().println("</div>");
095: } catch (IOException e) {
096: }
097:
098: return EVAL_PAGE;
099: }
100:
101: // properties ///////////////////////////////////////////////////////////////
102:
103: public void setProperty(String property) {
104: property_ = property;
105: }
106:
107: // attributes ///////////////////////////////////////////////////////////////
108:
109: protected String property_ = null;
110:
111: private boolean invalid_ = false;
112: }
|