KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > openwire > tool > MultiSourceGenerator


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

18 package org.apache.activemq.openwire.tool;
19
20 import org.apache.tools.ant.Project;
21 import org.apache.tools.ant.taskdefs.FixCRLF;
22 import org.codehaus.jam.JClass;
23 import org.codehaus.jam.JProperty;
24 import org.codehaus.jam.JamClassIterator;
25
26 import java.io.File JavaDoc;
27 import java.io.FileWriter JavaDoc;
28 import java.io.PrintWriter JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.HashSet JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Set JavaDoc;
33
34 /**
35  *
36  * @version $Revision: 386442 $
37  */

38 public abstract class MultiSourceGenerator extends OpenWireGenerator {
39     protected Set JavaDoc manuallyMaintainedClasses = new HashSet JavaDoc();
40     protected File JavaDoc destDir;
41     protected File JavaDoc destFile;
42
43     protected JClass jclass;
44     protected JClass superclass;
45     protected String JavaDoc simpleName;
46     protected String JavaDoc className;
47     protected String JavaDoc baseClass;
48     protected StringBuffer JavaDoc buffer;
49
50     public MultiSourceGenerator() {
51         initialiseManuallyMaintainedClasses();
52     }
53
54     public Object JavaDoc run() {
55         if (destDir == null) {
56             throw new IllegalArgumentException JavaDoc("No destDir defined!");
57         }
58         System.out.println(getClass().getName() + " generating files in: " + destDir);
59         destDir.mkdirs();
60         buffer = new StringBuffer JavaDoc();
61
62         JamClassIterator iter = getClasses();
63         while (iter.hasNext()) {
64             jclass = iter.nextClass();
65             if (isValidClass(jclass)) {
66                 processClass(jclass);
67             }
68         }
69         return null;
70     }
71
72     /**
73      * Returns all the valid properties available on the current class
74      */

75     public List JavaDoc getProperties() {
76         List JavaDoc answer = new ArrayList JavaDoc();
77         JProperty[] properties = jclass.getDeclaredProperties();
78         for (int i = 0; i < properties.length; i++) {
79             JProperty property = properties[i];
80             if (isValidProperty(property)) {
81                 answer.add(property);
82             }
83         }
84         return answer;
85     }
86
87     protected boolean isValidClass(JClass jclass) {
88         if (jclass.getAnnotation("openwire:marshaller") == null) {
89             return false;
90         }
91         return !manuallyMaintainedClasses.contains(jclass.getSimpleName());
92     }
93
94     protected void processClass(JClass jclass) {
95         simpleName = jclass.getSimpleName();
96         superclass = jclass.getSuperclass();
97
98         System.out.println(getClass().getName() + " processing class: " + simpleName);
99
100         className = getClassName(jclass);
101
102         destFile = new File JavaDoc(destDir, className + filePostFix);
103
104         baseClass = getBaseClassName(jclass);
105
106         PrintWriter JavaDoc out = null;
107         try {
108             out = new PrintWriter JavaDoc(new FileWriter JavaDoc(destFile));
109             generateFile(out);
110         }
111         catch (Exception JavaDoc e) {
112             throw new RuntimeException JavaDoc(e);
113         }
114         finally {
115             if (out != null) {
116                 out.close();
117             }
118         }
119         
120         // Use the FixCRLF Ant Task to make sure the file has consistent newlines
121
// so that SVN does not complain on checkin.
122
Project project = new Project();
123         project.init();
124         FixCRLF fixCRLF = new FixCRLF();
125         fixCRLF.setProject(project);
126         fixCRLF.setSrcdir(destFile.getParentFile());
127         fixCRLF.setIncludes(destFile.getName());
128         fixCRLF.execute();
129     }
130
131     protected abstract void generateFile(PrintWriter JavaDoc out) throws Exception JavaDoc;
132
133     protected String JavaDoc getBaseClassName(JClass jclass) {
134         String JavaDoc answer = "BaseDataStructure";
135         if (superclass != null) {
136             String JavaDoc name = superclass.getSimpleName();
137             if (name != null && !name.equals("Object")) {
138                 answer = name;
139             }
140         }
141         return answer;
142     }
143
144     protected String JavaDoc getClassName(JClass jclass) {
145         return jclass.getSimpleName();
146     }
147     
148     public boolean isAbstractClass() {
149         return jclass != null & jclass.isAbstract();
150     }
151
152     public String JavaDoc getAbstractClassText() {
153         return isAbstractClass() ? "abstract " : "";
154     }
155     
156     public boolean isMarshallerAware() {
157         return isMarshallAware(jclass);
158     }
159
160     protected void initialiseManuallyMaintainedClasses() {
161         String JavaDoc[] names = { "ActiveMQDestination", "ActiveMQTempDestination", "ActiveMQQueue", "ActiveMQTopic", "ActiveMQTempQueue", "ActiveMQTempTopic",
162                 "BaseCommand",
163                 "ActiveMQMessage", "ActiveMQTextMessage", "ActiveMQMapMessage", "ActiveMQBytesMessage", "ActiveMQStreamMessage",
164                 "ActiveMQBlobMessage", "DataStructureSupport", "WireFormatInfo", "ActiveMQObjectMessage" };
165
166         for (int i = 0; i < names.length; i++) {
167             manuallyMaintainedClasses.add(names[i]);
168         }
169     }
170
171     public String JavaDoc getBaseClass() {
172         return baseClass;
173     }
174
175     public void setBaseClass(String JavaDoc baseClass) {
176         this.baseClass = baseClass;
177     }
178
179     public String JavaDoc getClassName() {
180         return className;
181     }
182
183     public void setClassName(String JavaDoc className) {
184         this.className = className;
185     }
186
187     public File JavaDoc getDestDir() {
188         return destDir;
189     }
190
191     public void setDestDir(File JavaDoc destDir) {
192         this.destDir = destDir;
193     }
194
195     public File JavaDoc getDestFile() {
196         return destFile;
197     }
198
199     public void setDestFile(File JavaDoc destFile) {
200         this.destFile = destFile;
201     }
202
203     public JClass getJclass() {
204         return jclass;
205     }
206
207     public void setJclass(JClass jclass) {
208         this.jclass = jclass;
209     }
210
211     public Set JavaDoc getManuallyMaintainedClasses() {
212         return manuallyMaintainedClasses;
213     }
214
215     public void setManuallyMaintainedClasses(Set JavaDoc manuallyMaintainedClasses) {
216         this.manuallyMaintainedClasses = manuallyMaintainedClasses;
217     }
218
219     public String JavaDoc getSimpleName() {
220         return simpleName;
221     }
222
223     public void setSimpleName(String JavaDoc simpleName) {
224         this.simpleName = simpleName;
225     }
226
227     public JClass getSuperclass() {
228         return superclass;
229     }
230
231     public void setSuperclass(JClass superclass) {
232         this.superclass = superclass;
233     }
234
235 }
236
Popular Tags