KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.File JavaDoc;
21 import java.io.FileWriter JavaDoc;
22 import java.io.PrintWriter JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.HashSet JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Set JavaDoc;
28
29 import org.apache.tools.ant.Project;
30 import org.apache.tools.ant.taskdefs.FixCRLF;
31 import org.codehaus.jam.JClass;
32 import org.codehaus.jam.JProperty;
33 import org.codehaus.jam.JamClassIterator;
34
35 /**
36  *
37  * @version $Revision: 386442 $
38  */

39 public abstract class SingleSourceGenerator extends OpenWireGenerator {
40     
41     protected Set JavaDoc manuallyMaintainedClasses = new HashSet JavaDoc();
42     protected File JavaDoc destFile;
43
44     protected JClass jclass;
45     protected JClass superclass;
46     protected String JavaDoc simpleName;
47     protected String JavaDoc className;
48     protected String JavaDoc baseClass;
49     protected List JavaDoc sortedClasses;
50
51     public SingleSourceGenerator() {
52         initialiseManuallyMaintainedClasses();
53     }
54
55     public Object JavaDoc run() {
56         
57         if (destFile == null) {
58             throw new IllegalArgumentException JavaDoc("No destFile defined!");
59         }
60         destFile.getParentFile().mkdirs();
61
62         PrintWriter JavaDoc out = null;
63         try {
64             out = new PrintWriter JavaDoc(new FileWriter JavaDoc(destFile));
65             
66             ArrayList JavaDoc classes = new ArrayList JavaDoc();
67             JamClassIterator iter = getClasses();
68             while (iter.hasNext()) {
69                 jclass = iter.nextClass();
70                 if (isValidClass(jclass)) {
71                     classes.add(jclass);
72                 }
73             }
74             sortedClasses = sort(classes);
75             
76             generateSetup(out);
77             for (Iterator JavaDoc iterator = sortedClasses.iterator(); iterator.hasNext();) {
78                 jclass = (JClass) iterator.next();
79                 simpleName = jclass.getSimpleName();
80                 superclass = jclass.getSuperclass();
81                 className = getClassName(jclass);
82                 baseClass = getBaseClassName(jclass);
83
84                 System.out.println(getClass().getName() + " processing class: " + simpleName);
85                 generateFile(out);
86             }
87             generateTearDown(out);
88             
89         } catch (Exception JavaDoc e) {
90             throw new RuntimeException JavaDoc(e);
91         } finally {
92             if (out != null) {
93                 out.close();
94             }
95         }
96         
97         // Use the FixCRLF Ant Task to make sure the file has consistent newlines
98
// so that SVN does not complain on checkin.
99
Project project = new Project();
100         project.init();
101         FixCRLF fixCRLF = new FixCRLF();
102         fixCRLF.setProject(project);
103         fixCRLF.setSrcdir(destFile.getParentFile());
104         fixCRLF.setIncludes(destFile.getName());
105         fixCRLF.execute();
106         return null;
107     }
108     
109     protected List JavaDoc sort(List JavaDoc classes) {
110         return classes;
111     }
112
113     protected void generateTearDown(PrintWriter JavaDoc out) {
114     }
115
116     protected void generateSetup(PrintWriter JavaDoc out) {
117     }
118
119     /**
120      * Returns all the valid properties available on the current class
121      */

122     public List JavaDoc getProperties() {
123         List JavaDoc answer = new ArrayList JavaDoc();
124         JProperty[] properties = jclass.getDeclaredProperties();
125         for (int i = 0; i < properties.length; i++) {
126             JProperty property = properties[i];
127             if (isValidProperty(property)) {
128                 answer.add(property);
129             }
130         }
131         return answer;
132     }
133
134     protected boolean isValidClass(JClass jclass) {
135         if (jclass==null || jclass.getAnnotation("openwire:marshaller") == null) {
136             return false;
137         }
138         return true;
139         //return !manuallyMaintainedClasses.contains(jclass.getSimpleName());
140
}
141
142
143     protected abstract void generateFile(PrintWriter JavaDoc out) throws Exception JavaDoc;
144
145     protected String JavaDoc getBaseClassName(JClass jclass) {
146         String JavaDoc answer = "BaseDataStructure";
147         if (superclass != null) {
148             String JavaDoc name = superclass.getSimpleName();
149             if (name != null && !name.equals("Object")) {
150                 answer = name;
151             }
152         }
153         return answer;
154     }
155
156     protected String JavaDoc getClassName(JClass jclass) {
157         return jclass.getSimpleName();
158     }
159     
160     public boolean isAbstractClass() {
161         return jclass != null & jclass.isAbstract();
162     }
163
164     public String JavaDoc getAbstractClassText() {
165         return isAbstractClass() ? "abstract " : "";
166     }
167     
168     public boolean isMarshallerAware() {
169         return isMarshallAware(jclass);
170     }
171
172     protected void initialiseManuallyMaintainedClasses() {
173         String JavaDoc[] names = { "ActiveMQDestination", "ActiveMQTempDestination", "ActiveMQQueue", "ActiveMQTopic", "ActiveMQTempQueue", "ActiveMQTempTopic",
174                 "BaseCommand", "ActiveMQMessage", "ActiveMQTextMessage", "ActiveMQMapMessage", "ActiveMQBytesMessage", "ActiveMQStreamMessage",
175                 "ActiveMQStreamMessage", "DataStructureSupport", "WireFormatInfo", "ActiveMQObjectMessage" };
176
177         for (int i = 0; i < names.length; i++) {
178             manuallyMaintainedClasses.add(names[i]);
179         }
180     }
181
182     public String JavaDoc getBaseClass() {
183         return baseClass;
184     }
185
186     public void setBaseClass(String JavaDoc baseClass) {
187         this.baseClass = baseClass;
188     }
189
190     public String JavaDoc getClassName() {
191         return className;
192     }
193
194     public void setClassName(String JavaDoc className) {
195         this.className = className;
196     }
197
198     public File JavaDoc getDestFile() {
199         return destFile;
200     }
201
202     public void setDestFile(File JavaDoc destFile) {
203         this.destFile = destFile;
204     }
205
206     public JClass getJclass() {
207         return jclass;
208     }
209
210     public void setJclass(JClass jclass) {
211         this.jclass = jclass;
212     }
213
214     public Set JavaDoc getManuallyMaintainedClasses() {
215         return manuallyMaintainedClasses;
216     }
217
218     public void setManuallyMaintainedClasses(Set JavaDoc manuallyMaintainedClasses) {
219         this.manuallyMaintainedClasses = manuallyMaintainedClasses;
220     }
221
222     public String JavaDoc getSimpleName() {
223         return simpleName;
224     }
225
226     public void setSimpleName(String JavaDoc simpleName) {
227         this.simpleName = simpleName;
228     }
229
230     public JClass getSuperclass() {
231         return superclass;
232     }
233
234     public void setSuperclass(JClass superclass) {
235         this.superclass = superclass;
236     }
237
238 }
239
Popular Tags