KickJava   Java API By Example, From Geeks To Geeks.

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


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.PrintWriter JavaDoc;
22 import java.io.StringWriter JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25
26 import org.codehaus.jam.JClass;
27 import org.codehaus.jam.JProperty;
28
29 /**
30  *
31  * @version $Revision: 383749 $
32  */

33 public class CSharpClassesGenerator extends MultiSourceGenerator {
34     
35     protected String JavaDoc targetDir="./src/main/csharp";
36
37     public Object JavaDoc run() {
38         filePostFix = ".cs";
39         if (destDir == null) {
40             destDir = new File JavaDoc(targetDir+"/ActiveMQ/Commands");
41         }
42         return super.run();
43     }
44     
45     public String JavaDoc makeHashCodeBody() throws Exception JavaDoc {
46         if (simpleName.endsWith("Id")) {
47             StringWriter JavaDoc buffer = new StringWriter JavaDoc();
48             PrintWriter JavaDoc out = new PrintWriter JavaDoc(buffer);
49             out.println(" int answer = 0;");
50             Iterator JavaDoc iter = getProperties().iterator();
51             while (iter.hasNext()) {
52                 JProperty property = (JProperty) iter.next();
53                 out.println(" answer = (answer * 37) + HashCode(" + property.getSimpleName() + ");");
54             }
55             out.println(" return answer;");
56             return buffer.toString();
57         }
58         return null;
59     }
60
61     public String JavaDoc makeEqualsBody() throws Exception JavaDoc {
62         if (simpleName.endsWith("Id")) {
63             StringWriter JavaDoc buffer = new StringWriter JavaDoc();
64             PrintWriter JavaDoc out = new PrintWriter JavaDoc(buffer);
65             
66             Iterator JavaDoc iter = getProperties().iterator();
67             while (iter.hasNext()) {
68                 JProperty property = (JProperty) iter.next();
69                 String JavaDoc name = property.getSimpleName();
70                 out.println(" if (! Equals(this." + name + ", that." + name + ")) return false;");
71             }
72             out.println(" return true;");
73             return buffer.toString();
74         }
75         return null;
76     }
77     
78     public String JavaDoc makeToStringBody() throws Exception JavaDoc {
79             StringWriter JavaDoc buffer = new StringWriter JavaDoc();
80             PrintWriter JavaDoc out = new PrintWriter JavaDoc(buffer);
81             out.println(" return GetType().Name + \"[\"");
82             Iterator JavaDoc iter = getProperties().iterator();
83             while (iter.hasNext()) {
84                 JProperty property = (JProperty) iter.next();
85                 String JavaDoc name = property.getSimpleName();
86                 out.println(" + \" " + name + "=\" + " + name);
87             }
88             out.println(" + \" ]\";");
89             return buffer.toString();
90     }
91
92     private void generateLicence(PrintWriter JavaDoc out) {
93 out.println("/*");
94 out.println(" * Licensed to the Apache Software Foundation (ASF) under one or more");
95 out.println(" * contributor license agreements. See the NOTICE file distributed with");
96 out.println(" * this work for additional information regarding copyright ownership.");
97 out.println(" * The ASF licenses this file to You under the Apache License, Version 2.0");
98 out.println(" * (the \"License\"); you may not use this file except in compliance with");
99 out.println(" * the License. You may obtain a copy of the License at");
100 out.println(" *");
101 out.println(" * http://www.apache.org/licenses/LICENSE-2.0");
102 out.println(" *");
103 out.println(" * Unless required by applicable law or agreed to in writing, software");
104 out.println(" * distributed under the License is distributed on an \"AS IS\" BASIS,");
105 out.println(" * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.");
106 out.println(" * See the License for the specific language governing permissions and");
107 out.println(" * limitations under the License.");
108 out.println(" */");
109     }
110
111
112     protected void generateFile(PrintWriter JavaDoc out) throws Exception JavaDoc {
113         generateLicence(out);
114
115 out.println("//");
116 out.println("// NOTE!: This file is autogenerated - do not modify!");
117 out.println("// if you need to make a change, please see the Groovy scripts in the");
118 out.println("// activemq-core module");
119 out.println("//");
120 out.println("");
121 out.println("using System;");
122 out.println("using System.Collections;");
123 out.println("");
124 out.println("using ActiveMQ.OpenWire;");
125 out.println("using ActiveMQ.Commands;");
126 out.println("");
127 out.println("namespace ActiveMQ.Commands");
128 out.println("{");
129 out.println(" /// <summary>");
130 out.println(" /// The ActiveMQ "+jclass.getSimpleName()+" Command");
131 out.println(" /// </summary>");
132   out.print(" public class "+jclass.getSimpleName()+" : "+baseClass);
133
134
135     for (int i = 0; i < jclass.getInterfaces().length; i++) {
136         JClass intf = jclass.getInterfaces()[i];
137         out.print(", "+intf.getSimpleName());
138     }
139     
140 out.println("");
141 out.println(" {");
142 out.println(" public const byte ID_"+jclass.getSimpleName()+" = "+getOpenWireOpCode(jclass)+";");
143 out.println(" ");
144
145         List JavaDoc properties = getProperties();
146         String JavaDoc type;
147         Object JavaDoc name;
148         for (Iterator JavaDoc iter = properties.iterator(); iter.hasNext();) {
149             JProperty property = (JProperty) iter.next();
150             type = toCSharpType(property.getType());
151             name = decapitalize(property.getSimpleName());
152 out.println(" "+type+" "+name+";");
153     }
154
155     String JavaDoc text = makeHashCodeBody();
156     if (text != null) {
157 out.println("");
158 out.println(" public override int GetHashCode() {");
159 out.println(""+text+"");
160 out.println(" }");
161     }
162
163     text = makeEqualsBody();
164     if (text != null) {
165 out.println("");
166 out.println(" public override bool Equals(object that) {");
167 out.println(" if (that is "+className+") {");
168 out.println(" return Equals(("+className+") that);");
169 out.println(" }");
170 out.println(" return false;");
171 out.println(" }");
172 out.println("");
173 out.println(" public virtual bool Equals("+className+" that) {");
174 out.println(""+text+"");
175 out.println(" }");
176     }
177         
178     text = makeToStringBody();
179     if (text != null) {
180 out.println("");
181 out.println(" public override string ToString() {");
182 out.println(""+text+"");
183 out.println(" }");
184     }
185         
186 out.println("");
187 out.println(" public override byte GetDataStructureType() {");
188 out.println(" return ID_"+jclass.getSimpleName()+";");
189 out.println(" }");
190 out.println("");
191 out.println("");
192 out.println(" // Properties");
193
194                 for (Iterator JavaDoc iter = properties.iterator(); iter.hasNext();) {
195                     JProperty property = (JProperty) iter.next();
196                     type = toCSharpType(property.getType());
197                     name = decapitalize(property.getSimpleName());
198                     String JavaDoc propertyName = property.getSimpleName();
199                     String JavaDoc getter = capitalize(property.getGetter().getSimpleName());
200                     String JavaDoc setter = capitalize(property.getSetter().getSimpleName());
201
202 out.println("");
203 out.println(" public "+type+" "+propertyName+"");
204 out.println(" {");
205 out.println(" get { return "+name+"; }");
206 out.println(" set { this."+name+" = value; } ");
207 out.println(" }");
208                 }
209
210 out.println("");
211 out.println(" }");
212 out.println("}");
213     }
214
215     public String JavaDoc getTargetDir() {
216         return targetDir;
217     }
218
219     public void setTargetDir(String JavaDoc targetDir) {
220         this.targetDir = targetDir;
221     }
222 }
223
Popular Tags