KickJava   Java API By Example, From Geeks To Geeks.

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


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
20 package org.netbeans.modules.web.wizards;
21
22 import java.io.IOException JavaDoc;
23 import java.util.Collections JavaDoc;
24
25 import javax.lang.model.element.Modifier;
26 import javax.lang.model.type.TypeKind;
27
28 import com.sun.source.tree.ClassTree;
29 import com.sun.source.tree.CompilationUnitTree;
30 import com.sun.source.tree.ExpressionTree;
31 import com.sun.source.tree.MethodTree;
32 import com.sun.source.tree.Tree;
33 import com.sun.source.tree.TypeParameterTree;
34 import com.sun.source.tree.VariableTree;
35
36 import org.netbeans.api.java.source.JavaSource;
37 import org.netbeans.api.java.source.JavaSource.Phase;
38 import org.netbeans.api.java.source.ModificationResult;
39 import org.netbeans.api.java.source.TreeMaker;
40 import org.netbeans.api.java.source.WorkingCopy;
41
42 import org.netbeans.modules.j2ee.common.source.AbstractTask;
43 import org.netbeans.modules.j2ee.common.source.GenerationUtils;
44
45 /**
46  * Generator of attributes for tag handler class
47  *
48  * @author milan.kuchtiak@sun.com
49  * Created on May, 2004
50  */

51 public class TagHandlerGenerator {
52     private Object JavaDoc[][] attributes;
53     private JavaSource clazz;
54     private boolean isBodyTag;
55     private boolean evaluateBody;
56
57     /** Creates a new instance of ListenerGenerator */
58     public TagHandlerGenerator(JavaSource clazz, Object JavaDoc[][] attributes, boolean isBodyTag, boolean evaluateBody) {
59         this.clazz=clazz;
60         this.attributes=attributes;
61         this.isBodyTag=isBodyTag;
62         this.evaluateBody=evaluateBody;
63     }
64     
65     public void generate() throws IOException JavaDoc {
66         AbstractTask task = new AbstractTask<WorkingCopy>() {
67             public void run(WorkingCopy workingCopy) throws Exception JavaDoc {
68                 workingCopy.toPhase(Phase.RESOLVED);
69                 TreeMaker make = workingCopy.getTreeMaker();
70                 GenerationUtils gu = GenerationUtils.newInstance(workingCopy);
71                 ClassTree oldClassTree = gu.getClassTree();
72                 ClassTree classTree = addFields(gu, make, oldClassTree);
73                 if (isBodyTag) {
74                     classTree = make.addClassMember(classTree, addBodyEvaluatorCheck(evaluateBody, make));
75                 }
76                 classTree = addSetters(gu, make, classTree);
77                 workingCopy.rewrite(oldClassTree, classTree);
78             }
79         };
80         ModificationResult result = clazz.runModificationTask(task);
81         result.commit();
82     }
83     
84     private ClassTree addFields(GenerationUtils gu, TreeMaker make, ClassTree classTree) throws IOException JavaDoc {
85         for (int i = 0; i < attributes.length; i++) {
86             VariableTree field = gu.createField(gu.createModifiers(Modifier.PRIVATE), (String JavaDoc) attributes[i][0], (String JavaDoc) attributes[i][1]);
87             classTree = make.insertClassMember(classTree, i, field);
88             
89             //TODO: generate Javadoc
90
}
91         
92         return classTree;
93     }
94
95     private ClassTree addSetters(GenerationUtils gu, TreeMaker make, ClassTree newClassTree) throws IOException JavaDoc {
96         for (int i = 0; i < attributes.length; i++) {
97             MethodTree setter = gu.createPropertySetterMethod(gu.createModifiers(Modifier.PUBLIC), (String JavaDoc) attributes[i][0], (String JavaDoc) attributes[i][1]);
98             newClassTree = make.addClassMember(newClassTree, setter);
99             
100             //TODO: generate Javadoc
101
}
102         
103         return newClassTree;
104     }
105
106     private MethodTree addBodyEvaluatorCheck(boolean evaluateBody, TreeMaker make) throws IOException JavaDoc {
107         StringBuffer JavaDoc methodBody = new StringBuffer JavaDoc();
108         methodBody.append("{\n//"); //NOI18N
109
methodBody.append("\n// TODO: code that determines whether the body should be"); //NOI18N
110
methodBody.append("\n// evaluated should be placed here."); //NOI18N
111
methodBody.append("\n// Called from the doStartTag() method."); //NOI18N
112
methodBody.append("\n//"); //NOI18N
113
methodBody.append("\nreturn " + (evaluateBody ? "true;" : "false;")); //NOI18N
114
methodBody.append("\n}"); //NOI18N
115

116         MethodTree method = make.Method(
117                 make.Modifiers(Collections.<Modifier>singleton(Modifier.PRIVATE)),
118                 "theBodyShouldBeEvaluated", //NOI18N
119
make.PrimitiveType(TypeKind.BOOLEAN),
120                 Collections.<TypeParameterTree>emptyList(),
121                 Collections.<VariableTree>emptyList(),
122                 Collections.<ExpressionTree>emptyList(),
123                 methodBody.toString(),
124                 null);
125         
126         //TODO: generate Javadoc
127

128         return method;
129     }
130
131 }
Popular Tags