KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > wizards > ListenerGenerator


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.web.wizards;
20
21 import com.sun.source.tree.ClassTree;
22 import com.sun.source.tree.CompilationUnitTree;
23 import com.sun.source.tree.Tree;
24 import java.io.IOException JavaDoc;
25 import org.netbeans.api.java.source.JavaSource;
26 import org.netbeans.api.java.source.JavaSource.Phase;
27 import org.netbeans.api.java.source.ModificationResult;
28 import org.netbeans.api.java.source.TreeMaker;
29 import org.netbeans.api.java.source.WorkingCopy;
30 import org.netbeans.modules.j2ee.common.source.AbstractTask;
31 import org.netbeans.modules.j2ee.common.source.GenerationUtils;
32
33 /**
34  * Generator for servlet listener class
35  *
36  * @author milan.kuchtiak@sun.com
37  * Created on March, 2004
38  */

39 public class ListenerGenerator {
40     boolean isContext,isContextAttr,isSession,isSessionAttr,isRequest,isRequestAttr;
41     
42     private JavaSource clazz;
43     private GenerationUtils gu;
44     
45     /** Creates a new instance of ListenerGenerator */
46     public ListenerGenerator(boolean isContext, boolean isContextAttr, boolean isSession, boolean isSessionAttr,
47                              boolean isRequest, boolean isRequestAttr) {
48         this.isContext=isContext;
49         this.isContextAttr=isContextAttr;
50         this.isSession=isSession;
51         this.isSessionAttr=isSessionAttr;
52         this.isRequest=isRequest;
53         this.isRequestAttr=isRequestAttr;
54     }
55     
56     public void generate(JavaSource clazz) throws IOException JavaDoc {
57         this.clazz=clazz;
58         
59         AbstractTask task = new AbstractTask<WorkingCopy>() {
60             public void run(WorkingCopy workingCopy) throws Exception JavaDoc {
61                 workingCopy.toPhase(Phase.RESOLVED);
62                 CompilationUnitTree cut = workingCopy.getCompilationUnit();
63                 TreeMaker make = workingCopy.getTreeMaker();
64                 
65                 
66                 for (Tree typeDecl : cut.getTypeDecls()) {
67                     if (Tree.Kind.CLASS == typeDecl.getKind()) {
68                         gu = GenerationUtils.newInstance(workingCopy);
69                         workingCopy.rewrite(gu.getClassTree(), generateInterfaces(gu));
70
71                     }
72                 }
73                 
74             }
75         };
76         ModificationResult result = clazz.runModificationTask(task);
77         result.commit();
78
79         
80 // if (isContext) addContextListenerMethods();
81
// if (isContextAttr) addContextAttrListenerMethods();
82
// if (isSession) addSessionListenerMethods();
83
// if (isSessionAttr) addSessionAttrListenerMethods();
84
// if (isRequest) addRequestListenerMethods();
85
// if (isRequestAttr) addRequestAttrListenerMethods();
86
}
87     
88     private ClassTree generateInterfaces(GenerationUtils gu) {
89         ClassTree newClassTree = gu.getClassTree();
90         
91         if (isContext)
92             newClassTree = gu.addImplementsClause(newClassTree, "javax.servlet.ServletContextListener");
93         if (isContextAttr)
94             newClassTree = gu.addImplementsClause(newClassTree, "javax.servlet.ServletContextAttributeListener");
95         if (isSession)
96             newClassTree = gu.addImplementsClause(newClassTree, "javax.servlet.http.HttpSessionListener");
97         if (isSessionAttr)
98             newClassTree = gu.addImplementsClause(newClassTree, "javax.servlet.http.HttpSessionAttributeListener");
99         if (isRequest)
100             newClassTree = gu.addImplementsClause(newClassTree, "javax.servlet.ServletRequestListener");
101         if (isRequestAttr)
102             newClassTree = gu.addImplementsClause(newClassTree, "javax.servlet.ServletRequestAttributeListener");
103         
104         return newClassTree;
105     }
106     
107 // private void addContextListenerMethods() throws SourceException {
108
// StringBuffer docBuf = new StringBuffer();
109
// docBuf.append(NbBundle.getMessage(ListenerGenerator.class,"TXT_DOC_contextListener_m1"));
110
// StringBuffer bodyBuf = new StringBuffer();
111
// bodyBuf.append("// "+NbBundle.getMessage(ListenerGenerator.class,"TXT_todo_eg")); //NOI18N
112
// bodyBuf.append("\n/*\n"); //NOI18N
113
// bodyBuf.append(" Connection con = // create connection\n"); //NOI18N
114
// bodyBuf.append(" evt.getServletContext().setAttribute(\"con\", con);\n"); //NOI18N
115
// bodyBuf.append("*/\n"); //NOI18N
116
// addMethod("contextInitialized","ServletContextEvent",docBuf,bodyBuf); //NOI18N
117
//
118
// docBuf = new StringBuffer();
119
// docBuf.append(NbBundle.getMessage(ListenerGenerator.class,"TXT_DOC_contextListener_m2"));
120
// bodyBuf = new StringBuffer();
121
// bodyBuf.append("// "+NbBundle.getMessage(ListenerGenerator.class,"TXT_todo_eg")); //NOI18N
122
// bodyBuf.append("\n/*\n"); //NOI18N
123
// bodyBuf.append(" Connection con = (Connection) e.getServletContext().getAttribute(\"con\");\n"); //NOI18N
124
// bodyBuf.append(" try { con.close(); } catch (SQLException ignored) { } // close connection\n"); //NOI18N
125
// bodyBuf.append("*/\n"); //NOI18N
126
// addMethod("contextDestroyed","ServletContextEvent",docBuf,bodyBuf); //NOI18N
127
// }
128
// private void addContextAttrListenerMethods() throws SourceException {
129
// StringBuffer docBuf = new StringBuffer();
130
// docBuf.append(NbBundle.getMessage(ListenerGenerator.class,"TXT_DOC_contextAttrListener_m1"));
131
// StringBuffer bodyBuf = new StringBuffer();
132
// bodyBuf.append("// "+NbBundle.getMessage(ListenerGenerator.class,"TXT_todo")+"\n"); //NOI18N
133
// addMethod("attributeAdded","ServletContextAttributeEvent",docBuf,bodyBuf); //NOI18N
134
//
135
// docBuf = new StringBuffer();
136
// docBuf.append(NbBundle.getMessage(ListenerGenerator.class,"TXT_DOC_contextAttrListener_m2"));
137
// bodyBuf = new StringBuffer();
138
// bodyBuf.append("// "+NbBundle.getMessage(ListenerGenerator.class,"TXT_todo")+"\n"); //NOI18N
139
// addMethod("attributeRemoved","ServletContextAttributeEvent",docBuf,bodyBuf); //NOI18N
140
//
141
// docBuf = new StringBuffer();
142
// docBuf.append(NbBundle.getMessage(ListenerGenerator.class,"TXT_DOC_contextAttrListener_m3"));
143
// bodyBuf = new StringBuffer();
144
// bodyBuf.append("// "+NbBundle.getMessage(ListenerGenerator.class,"TXT_todo")+"\n"); //NOI18N
145
// addMethod("attributeReplaced","ServletContextAttributeEvent",docBuf,bodyBuf); //NOI18N
146
// }
147
// private void addSessionListenerMethods() throws SourceException {
148
// StringBuffer docBuf = new StringBuffer();
149
// docBuf.append(NbBundle.getMessage(ListenerGenerator.class,"TXT_DOC_sessionListener_m1"));
150
// StringBuffer bodyBuf = new StringBuffer();
151
// bodyBuf.append("// "+NbBundle.getMessage(ListenerGenerator.class,"TXT_todo")+"\n"); //NOI18N
152
// addMethod("sessionCreated","HttpSessionEvent",docBuf,bodyBuf); //NOI18N
153
//
154
// docBuf = new StringBuffer();
155
// docBuf.append(NbBundle.getMessage(ListenerGenerator.class,"TXT_DOC_sessionListener_m2"));
156
// bodyBuf = new StringBuffer();
157
// bodyBuf.append("// "+NbBundle.getMessage(ListenerGenerator.class,"TXT_todo")+"\n"); //NOI18N
158
// addMethod("sessionDestroyed","HttpSessionEvent",docBuf,bodyBuf); //NOI18N
159
// }
160
// private void addSessionAttrListenerMethods() throws SourceException {
161
// StringBuffer docBuf = new StringBuffer();
162
// docBuf.append(NbBundle.getMessage(ListenerGenerator.class,"TXT_DOC_sessionAttrListener_m1"));
163
// StringBuffer bodyBuf = new StringBuffer();
164
// bodyBuf.append("// "+NbBundle.getMessage(ListenerGenerator.class,"TXT_todo")+"\n"); //NOI18N
165
// addMethod("attributeAdded","HttpSessionBindingEvent",docBuf,bodyBuf); //NOI18N
166
//
167
// docBuf = new StringBuffer();
168
// docBuf.append(NbBundle.getMessage(ListenerGenerator.class,"TXT_DOC_sessionAttrListener_m2"));
169
// bodyBuf = new StringBuffer();
170
// bodyBuf.append("// "+NbBundle.getMessage(ListenerGenerator.class,"TXT_todo")+"\n"); //NOI18N
171
// addMethod("attributeRemoved","HttpSessionBindingEvent",docBuf,bodyBuf); //NOI18N
172
//
173
// docBuf = new StringBuffer();
174
// docBuf.append(NbBundle.getMessage(ListenerGenerator.class,"TXT_DOC_sessionAttrListener_m3"));
175
// bodyBuf = new StringBuffer();
176
// bodyBuf.append("// "+NbBundle.getMessage(ListenerGenerator.class,"TXT_todo")+"\n"); //NOI18N
177
// addMethod("attributeReplaced","HttpSessionBindingEvent",docBuf,bodyBuf); //NOI18N
178
// }
179
// private void addRequestListenerMethods() throws SourceException {
180
// StringBuffer docBuf = new StringBuffer();
181
// docBuf.append(NbBundle.getMessage(ListenerGenerator.class,"TXT_DOC_requestListener_m1"));
182
// StringBuffer bodyBuf = new StringBuffer();
183
// bodyBuf.append("// "+NbBundle.getMessage(ListenerGenerator.class,"TXT_todo")+"\n"); //NOI18N
184
// addMethod("requestInitialized","ServletRequestEvent",docBuf,bodyBuf); //NOI18N
185
//
186
// docBuf = new StringBuffer();
187
// docBuf.append(NbBundle.getMessage(ListenerGenerator.class,"TXT_DOC_requestListener_m2"));
188
// bodyBuf = new StringBuffer();
189
// bodyBuf.append("// "+NbBundle.getMessage(ListenerGenerator.class,"TXT_todo")+"\n"); //NOI18N
190
// addMethod("requestDestroyed","ServletRequestEvent",docBuf,bodyBuf); //NOI18N
191
// }
192
// private void addRequestAttrListenerMethods() throws SourceException {
193
// StringBuffer docBuf = new StringBuffer();
194
// docBuf.append(NbBundle.getMessage(ListenerGenerator.class,"TXT_DOC_requestAttrListener_m1"));
195
// StringBuffer bodyBuf = new StringBuffer();
196
// bodyBuf.append("// "+NbBundle.getMessage(ListenerGenerator.class,"TXT_todo")+"\n"); //NOI18N
197
// addMethod("attributeAdded","ServletRequestAttributeEvent",docBuf,bodyBuf); //NOI18N
198
//
199
// docBuf = new StringBuffer();
200
// docBuf.append(NbBundle.getMessage(ListenerGenerator.class,"TXT_DOC_requestAttrListener_m2"));
201
// bodyBuf = new StringBuffer();
202
// bodyBuf.append("// "+NbBundle.getMessage(ListenerGenerator.class,"TXT_todo")+"\n"); //NOI18N
203
// addMethod("attributeRemoved","ServletRequestAttributeEvent",docBuf,bodyBuf); //NOI18N
204
//
205
// docBuf = new StringBuffer();
206
// docBuf.append(NbBundle.getMessage(ListenerGenerator.class,"TXT_DOC_requestAttrListener_m3"));
207
// bodyBuf = new StringBuffer();
208
// bodyBuf.append("// "+NbBundle.getMessage(ListenerGenerator.class,"TXT_todo")+"\n"); //NOI18N
209
// addMethod("attributeReplaced","ServletRequestAttributeEvent",docBuf,bodyBuf); //NOI18N
210
// }
211
// private void addMethod(String methodName,
212
// String eventName,
213
// StringBuffer javadoc,
214
// StringBuffer body) throws SourceException {
215
// MethodElement method = new MethodElement();
216
// method.setName(Identifier.create(methodName));
217
// method.setReturn(Type.VOID);
218
// method.setModifiers(java.lang.reflect.Modifier.PUBLIC);
219
// MethodParameter mp = MethodParameter.parse(eventName+" evt"); //NOI18N
220
// method.setParameters(new MethodParameter[]{mp});
221
// JavaDoc doc = method.getJavaDoc();
222
// doc.setRawText(javadoc.toString());
223
// method.setBody(body.toString());
224
// clazz.addMethod(method);
225
// }
226
}
Popular Tags