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 */
017package org.apache.activemq.openwire.tool;
018
019import java.io.File;
020import java.io.PrintWriter;
021import java.io.StringWriter;
022import java.util.Iterator;
023import java.util.List;
024
025import org.codehaus.jam.JClass;
026import org.codehaus.jam.JProperty;
027
028/**
029 * 
030 */
031public class CSharpClassesGenerator extends MultiSourceGenerator {
032
033    protected String targetDir = "./src/main/csharp";
034
035    public Object run() {
036        filePostFix = ".cs";
037        if (destDir == null) {
038            destDir = new File(targetDir + "/ActiveMQ/Commands");
039        }
040        return super.run();
041    }
042
043    public String makeHashCodeBody() throws Exception {
044        if (simpleName.endsWith("Id")) {
045            StringWriter buffer = new StringWriter();
046            PrintWriter out = new PrintWriter(buffer);
047            out.println("            int answer = 0;");
048            Iterator iter = getProperties().iterator();
049            while (iter.hasNext()) {
050                JProperty property = (JProperty)iter.next();
051                out.println("            answer = (answer * 37) + HashCode(" + property.getSimpleName() + ");");
052            }
053            out.println("            return answer;");
054            return buffer.toString();
055        }
056        return null;
057    }
058
059    public String makeEqualsBody() throws Exception {
060        if (simpleName.endsWith("Id")) {
061            StringWriter buffer = new StringWriter();
062            PrintWriter out = new PrintWriter(buffer);
063
064            Iterator iter = getProperties().iterator();
065            while (iter.hasNext()) {
066                JProperty property = (JProperty)iter.next();
067                String name = property.getSimpleName();
068                out.println("            if (! Equals(this." + name + ", that." + name + ")) return false;");
069            }
070            out.println("            return true;");
071            return buffer.toString();
072        }
073        return null;
074    }
075
076    public String makeToStringBody() throws Exception {
077        StringWriter buffer = new StringWriter();
078        PrintWriter out = new PrintWriter(buffer);
079        out.println("            return GetType().Name + \"[\"");
080        Iterator iter = getProperties().iterator();
081        while (iter.hasNext()) {
082            JProperty property = (JProperty)iter.next();
083            String name = property.getSimpleName();
084            out.println("                + \" " + name + "=\" + " + name);
085        }
086        out.println("                + \" ]\";");
087        return buffer.toString();
088    }
089
090    private void generateLicence(PrintWriter out) {
091        out.println("/*");
092        out.println(" * Licensed to the Apache Software Foundation (ASF) under one or more");
093        out.println(" * contributor license agreements.  See the NOTICE file distributed with");
094        out.println(" * this work for additional information regarding copyright ownership.");
095        out.println(" * The ASF licenses this file to You under the Apache License, Version 2.0");
096        out.println(" * (the \"License\"); you may not use this file except in compliance with");
097        out.println(" * the License.  You may obtain a copy of the License at");
098        out.println(" *");
099        out.println(" * http://www.apache.org/licenses/LICENSE-2.0");
100        out.println(" *");
101        out.println(" * Unless required by applicable law or agreed to in writing, software");
102        out.println(" * distributed under the License is distributed on an \"AS IS\" BASIS,");
103        out.println(" * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.");
104        out.println(" * See the License for the specific language governing permissions and");
105        out.println(" * limitations under the License.");
106        out.println(" */");
107    }
108
109    protected void generateFile(PrintWriter out) throws Exception {
110        generateLicence(out);
111
112        out.println("//");
113        out.println("//  NOTE!: This file is autogenerated - do not modify!");
114        out.println("//         if you need to make a change, please see the Groovy scripts in the");
115        out.println("//         activemq-core module");
116        out.println("//");
117        out.println("");
118        out.println("using System;");
119        out.println("using System.Collections;");
120        out.println("");
121        out.println("using ActiveMQ.OpenWire;");
122        out.println("using ActiveMQ.Commands;");
123        out.println("");
124        out.println("namespace ActiveMQ.Commands");
125        out.println("{");
126        out.println("    /// <summary>");
127        out.println("    ///  The ActiveMQ " + jclass.getSimpleName() + " Command");
128        out.println("    /// </summary>");
129        out.print("    public class " + jclass.getSimpleName() + " : " + baseClass);
130
131        for (int i = 0; i < jclass.getInterfaces().length; i++) {
132            JClass intf = jclass.getInterfaces()[i];
133            out.print(", " + intf.getSimpleName());
134        }
135
136        out.println("");
137        out.println("    {");
138        out.println("        public const byte ID_" + jclass.getSimpleName() + " = " + getOpenWireOpCode(jclass) + ";");
139        out.println("                ");
140
141        List properties = getProperties();
142        String type;
143        Object name;
144        for (Iterator iter = properties.iterator(); iter.hasNext();) {
145            JProperty property = (JProperty)iter.next();
146            type = toCSharpType(property.getType());
147            name = decapitalize(property.getSimpleName());
148            out.println("        " + type + " " + name + ";");
149        }
150
151        String text = makeHashCodeBody();
152        if (text != null) {
153            out.println("");
154            out.println("        public override int GetHashCode() {");
155            out.println("" + text + "");
156            out.println("        }");
157        }
158
159        text = makeEqualsBody();
160        if (text != null) {
161            out.println("");
162            out.println("        public override bool Equals(object that) {");
163            out.println("            if (that is " + className + ") {");
164            out.println("                return Equals((" + className + ") that);");
165            out.println("            }");
166            out.println("            return false;");
167            out.println("        }");
168            out.println("");
169            out.println("        public virtual bool Equals(" + className + " that) {");
170            out.println("" + text + "");
171            out.println("        }");
172        }
173
174        text = makeToStringBody();
175        if (text != null) {
176            out.println("");
177            out.println("        public override string ToString() {");
178            out.println("" + text + "");
179            out.println("        }");
180        }
181
182        out.println("");
183        out.println("        public override byte GetDataStructureType() {");
184        out.println("            return ID_" + jclass.getSimpleName() + ";");
185        out.println("        }");
186        out.println("");
187        out.println("");
188        out.println("        // Properties");
189
190        for (Iterator iter = properties.iterator(); iter.hasNext();) {
191            JProperty property = (JProperty)iter.next();
192            type = toCSharpType(property.getType());
193            name = decapitalize(property.getSimpleName());
194            String propertyName = property.getSimpleName();
195
196            out.println("");
197            out.println("        public " + type + " " + propertyName + "");
198            out.println("        {");
199            out.println("            get { return " + name + "; }");
200            out.println("            set { this." + name + " = value; }            ");
201            out.println("        }");
202        }
203
204        out.println("");
205        out.println("    }");
206        out.println("}");
207    }
208
209    public String getTargetDir() {
210        return targetDir;
211    }
212
213    public void setTargetDir(String targetDir) {
214        this.targetDir = targetDir;
215    }
216}