KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > methodhead > tag > InvalidTag


1 /*
2  * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
3  *
4  * This file is part of TransferCM.
5  *
6  * TransferCM is free software; you can redistribute it and/or modify it under the
7  * terms of the GNU General Public License as published by the Free Software
8  * Foundation; either version 2 of the License, or (at your option) any later
9  * version.
10  *
11  * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
18  * Fifth Floor, Boston, MA 02110-1301 USA
19  */

20
21 package com.methodhead.tag;
22
23 import java.io.IOException JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
26
27 import org.apache.struts.Globals;
28 import org.apache.struts.action.ActionErrors;
29 import org.apache.struts.action.ActionError;
30 import org.apache.struts.util.MessageResources;
31 import org.apache.commons.lang.exception.ExceptionUtils;
32
33 public class InvalidTag
34 extends
35   TagSupport JavaDoc {
36
37   // constructors /////////////////////////////////////////////////////////////
38

39   // constants ////////////////////////////////////////////////////////////////
40

41   // classes //////////////////////////////////////////////////////////////////
42

43   // methods //////////////////////////////////////////////////////////////////
44

45   public int doStartTag() {
46     try {
47       invalid_ = false;
48
49       ActionErrors errors =
50         ( ActionErrors )pageContext.getRequest().getAttribute(
51           Globals.ERROR_KEY );
52
53       MessageResources messages =
54         ( MessageResources )pageContext.getRequest().getAttribute(
55           Globals.MESSAGES_KEY );
56
57       if ( property_ == null )
58         property_ = ActionErrors.GLOBAL_ERROR;
59
60       String JavaDoc style = " class=\"formcell\"";
61       String JavaDoc message = "";
62
63       if ( ( errors != null ) &&
64            ( messages != null ) &&
65            ( property_ != null ) ) {
66
67         Iterator JavaDoc iter = errors.get( property_ );
68
69         if ( iter.hasNext() ) {
70
71           invalid_ = true;
72           pageContext.getOut().println( "<div class=\"invalid\"><div class=\"invalid-message\">" );
73
74           while ( iter.hasNext() ) {
75             ActionError error = ( ActionError )iter.next();
76
77             pageContext.getOut().println(
78               messages.getMessage( error.getKey(), error.getValues() ) );
79           }
80
81           pageContext.getOut().println( "</div>" );
82         }
83       }
84     }
85     catch ( IOException JavaDoc e ) {
86       throw new RuntimeException JavaDoc(
87         "Unexpected IOException: " + ExceptionUtils.getStackTrace( e ) );
88     }
89
90     return EVAL_BODY_INCLUDE;
91   }
92
93   public int doEndTag() {
94     try {
95       if ( invalid_ )
96         pageContext.getOut().println( "</div>" );
97     }
98     catch ( IOException JavaDoc e ) {
99     }
100
101     return EVAL_PAGE;
102   }
103
104   // properties ///////////////////////////////////////////////////////////////
105

106   public void setProperty(
107     String JavaDoc property ) {
108     property_ = property;
109   }
110
111   // attributes ///////////////////////////////////////////////////////////////
112

113   protected String JavaDoc property_ = null;
114
115   private boolean invalid_ = false;
116 }
117
Popular Tags