KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > gnat > echoTag


1 /*
2  * Copyright 1999,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.taglibs.gnat;
18
19 import org.apache.taglibs.gnat.util.*;
20 import javax.servlet.jsp.*;
21 import javax.servlet.jsp.tagext.*;
22 import java.io.*;
23 import java.util.*;
24
25 public class echoTag extends TagSupport
26 {
27     private String JavaDoc message=null;
28     private File f = null; // real file
29
private String JavaDoc file = null; // "file" attribute value
30
private boolean append = false;
31     private ResourceBundle gnatRB = ListResourceBundle.getBundle("org.apache.taglibs.gnat.util.GnatTagStrings");
32     private ResourceBundle gnatERB = ListResourceBundle.getBundle("org.apache.taglibs.gnat.util.GnatExceptionStrings");
33         
34     public void setMessage(String JavaDoc msg)
35     {
36         this.message = msg;
37     }
38
39     public void setFile(String JavaDoc file)
40     {
41         this.file = file;
42     }
43
44     /**
45     * Shall we append to an existing file?
46     */

47     public void setAppend(boolean append)
48     {
49         this.append = append;
50     }
51
52     
53     public int doStartTag() throws JspException
54     {
55         if (message == null)
56         {
57             return EVAL_BODY_INCLUDE;
58         }
59         else
60         return SKIP_BODY;
61     }
62    
63     /**
64     * "Echoes" message to the JSP's output stream.
65     */

66     public int doEndTag() throws JspException
67     {
68         if (file != null)
69         {
70             f = new File(file);
71             
72             if (f.isDirectory())
73             {
74                 throw new JspTagException( gnatRB.getString("echo.tag") + ": " +
75                                            gnatERB.getString("echo.dir.fail") );
76             }
77             
78             FileWriter out = null;
79             try
80             {
81                 out = new FileWriter( f.getAbsolutePath(), append );
82                 out.write( message, 0, message.length() );
83             }
84             catch (IOException ioe)
85             {
86                 throw new JspTagException( gnatRB.getString("echo.tag") + ": " +
87                                            ioe.getMessage() );
88             }
89             finally
90             {
91                 if (out != null)
92                 {
93                     try
94                     {
95                         out.close();
96                     }
97                     catch (IOException ioex)
98                     {
99                         throw new JspTagException( gnatRB.getString("echo.tag") + ": " +
100                                                    ioex.getMessage() );
101                     }
102                 }
103             }
104         }
105         else if (message != null)
106         {
107             try
108             {
109                 pageContext.getOut().write(message);
110             }
111             catch(IOException ioe)
112             {
113                 throw new JspTagException( gnatRB.getString("echo.tag") + ": " +
114                                            ioe.getMessage() );
115             }
116         }
117         return EVAL_PAGE;
118     }
119
120     /* Put tag attribute accessors down here, out of the way, since they're more for
121        JavaBean completeness than programmer use.
122      */

123     public String JavaDoc getMessage() { return message; }
124
125     public String JavaDoc getFile() { return file; }
126
127     public boolean getAppend() { return append; }
128 }
129
Popular Tags