001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.activemq.openwire.tool;
018    
019    import java.io.File;
020    import java.io.FileWriter;
021    import java.io.PrintWriter;
022    import java.util.ArrayList;
023    import java.util.HashSet;
024    import java.util.List;
025    import java.util.Set;
026    
027    import org.apache.tools.ant.Project;
028    import org.apache.tools.ant.taskdefs.FixCRLF;
029    import org.codehaus.jam.JClass;
030    import org.codehaus.jam.JProperty;
031    import org.codehaus.jam.JamClassIterator;
032    
033    /**
034     * @version $Revision: 386442 $
035     */
036    public abstract class MultiSourceGenerator extends OpenWireGenerator {
037        protected Set<String> manuallyMaintainedClasses = new HashSet<String>();
038        protected File destDir;
039        protected File destFile;
040    
041        protected JClass jclass;
042        protected JClass superclass;
043        protected String simpleName;
044        protected String className;
045        protected String baseClass;
046        protected StringBuffer buffer;
047    
048        public MultiSourceGenerator() {
049            initialiseManuallyMaintainedClasses();
050        }
051    
052        public Object run() {
053            if (destDir == null) {
054                throw new IllegalArgumentException("No destDir defined!");
055            }
056            System.out.println(getClass().getName() + " generating files in: " + destDir);
057            destDir.mkdirs();
058            buffer = new StringBuffer();
059    
060            JamClassIterator iter = getClasses();
061            while (iter.hasNext()) {
062                jclass = iter.nextClass();
063                if (isValidClass(jclass)) {
064                    processClass(jclass);
065                }
066            }
067            return null;
068        }
069    
070        /**
071         * Returns all the valid properties available on the current class
072         */
073        public List<JProperty> getProperties() {
074            List<JProperty> answer = new ArrayList<JProperty>();
075            JProperty[] properties = jclass.getDeclaredProperties();
076            for (int i = 0; i < properties.length; i++) {
077                JProperty property = properties[i];
078                if (isValidProperty(property)) {
079                    answer.add(property);
080                }
081            }
082            return answer;
083        }
084    
085        protected boolean isValidClass(JClass jclass) {
086            if (jclass.getAnnotation("openwire:marshaller") == null) {
087                return false;
088            }
089            return !manuallyMaintainedClasses.contains(jclass.getSimpleName());
090        }
091    
092        protected void processClass(JClass jclass) {
093            simpleName = jclass.getSimpleName();
094            superclass = jclass.getSuperclass();
095    
096            System.out.println(getClass().getName() + " processing class: " + simpleName);
097    
098            className = getClassName(jclass);
099    
100            destFile = new File(destDir, className + filePostFix);
101    
102            baseClass = getBaseClassName(jclass);
103    
104            PrintWriter out = null;
105            try {
106                out = new PrintWriter(new FileWriter(destFile));
107                generateFile(out);
108            } catch (Exception e) {
109                throw new RuntimeException(e);
110            } finally {
111                if (out != null) {
112                    out.close();
113                }
114            }
115    
116            // Use the FixCRLF Ant Task to make sure the file has consistent
117            // newlines
118            // so that SVN does not complain on checkin.
119            Project project = new Project();
120            project.init();
121            FixCRLF fixCRLF = new FixCRLF();
122            fixCRLF.setProject(project);
123            fixCRLF.setSrcdir(destFile.getParentFile());
124            fixCRLF.setIncludes(destFile.getName());
125            fixCRLF.execute();
126        }
127    
128        protected abstract void generateFile(PrintWriter out) throws Exception;
129    
130        protected String getBaseClassName(JClass jclass) {
131            String answer = "BaseDataStructure";
132            if (superclass != null) {
133                String name = superclass.getSimpleName();
134                if (name != null && !name.equals("Object")) {
135                    answer = name;
136                }
137            }
138            return answer;
139        }
140    
141        protected String getClassName(JClass jclass) {
142            return jclass.getSimpleName();
143        }
144    
145        public boolean isAbstractClass() {
146            return jclass != null && jclass.isAbstract();
147        }
148    
149        public String getAbstractClassText() {
150            return isAbstractClass() ? "abstract " : "";
151        }
152    
153        public boolean isMarshallerAware() {
154            return isMarshallAware(jclass);
155        }
156    
157        protected void initialiseManuallyMaintainedClasses() {
158            String[] names = {
159                "ActiveMQDestination", "ActiveMQTempDestination", "ActiveMQQueue", "ActiveMQTopic", "ActiveMQTempQueue", "ActiveMQTempTopic", "BaseCommand", "ActiveMQMessage", "ActiveMQTextMessage",
160                "ActiveMQMapMessage", "ActiveMQBytesMessage", "ActiveMQStreamMessage", "ActiveMQBlobMessage", "DataStructureSupport", "WireFormatInfo", "ActiveMQObjectMessage"
161            };
162    
163            for (int i = 0; i < names.length; i++) {
164                manuallyMaintainedClasses.add(names[i]);
165            }
166        }
167    
168        public String getBaseClass() {
169            return baseClass;
170        }
171    
172        public void setBaseClass(String baseClass) {
173            this.baseClass = baseClass;
174        }
175    
176        public String getClassName() {
177            return className;
178        }
179    
180        public void setClassName(String className) {
181            this.className = className;
182        }
183    
184        public File getDestDir() {
185            return destDir;
186        }
187    
188        public void setDestDir(File destDir) {
189            this.destDir = destDir;
190        }
191    
192        public File getDestFile() {
193            return destFile;
194        }
195    
196        public void setDestFile(File destFile) {
197            this.destFile = destFile;
198        }
199    
200        public JClass getJclass() {
201            return jclass;
202        }
203    
204        public void setJclass(JClass jclass) {
205            this.jclass = jclass;
206        }
207    
208        public Set<String> getManuallyMaintainedClasses() {
209            return manuallyMaintainedClasses;
210        }
211    
212        public void setManuallyMaintainedClasses(Set<String> manuallyMaintainedClasses) {
213            this.manuallyMaintainedClasses = manuallyMaintainedClasses;
214        }
215    
216        public String getSimpleName() {
217            return simpleName;
218        }
219    
220        public void setSimpleName(String simpleName) {
221            this.simpleName = simpleName;
222        }
223    
224        public JClass getSuperclass() {
225            return superclass;
226        }
227    
228        public void setSuperclass(JClass superclass) {
229            this.superclass = superclass;
230        }
231    
232    }