Author: phunt
Date: Mon May  3 21:49:49 2010
New Revision: 940640

URL: http://svn.apache.org/viewvc?rev=940640&view=rev
Log:
ZOOKEEPER-747. Add C# generation to Jute

Added:
    
hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/CSharpGenerator.java
Modified:
    hadoop/zookeeper/trunk/CHANGES.txt
    hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/JBoolean.java
    hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/JBuffer.java
    hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/JByte.java
    hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/JCompType.java
    hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/JDouble.java
    hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/JField.java
    hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/JFile.java
    hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/JFloat.java
    hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/JInt.java
    hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/JLong.java
    hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/JMap.java
    hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/JRecord.java
    hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/JString.java
    hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/JType.java
    hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/JVector.java

Modified: hadoop/zookeeper/trunk/CHANGES.txt
URL: 
http://svn.apache.org/viewvc/hadoop/zookeeper/trunk/CHANGES.txt?rev=940640&r1=940639&r2=940640&view=diff
==============================================================================
--- hadoop/zookeeper/trunk/CHANGES.txt (original)
+++ hadoop/zookeeper/trunk/CHANGES.txt Mon May  3 21:49:49 2010
@@ -52,6 +52,8 @@ NEW FEATURES:
   ZOOKEEPER-729. Java client API to recursively delete a subtree. (Kay
   Kay via henry)
 
+  ZOOKEEPER-747. Add C# generation to Jute (Eric Hauser via phunt)
+
 Release 3.3.0 - 2010-03-24
 
 Non-backward compatible changes:

Added: 
hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/CSharpGenerator.java
URL: 
http://svn.apache.org/viewvc/hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/CSharpGenerator.java?rev=940640&view=auto
==============================================================================
--- 
hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/CSharpGenerator.java
 (added)
+++ 
hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/CSharpGenerator.java
 Mon May  3 21:49:49 2010
@@ -0,0 +1,53 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.jute.compiler;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+
+public class CSharpGenerator {
+    private ArrayList<JRecord> mRecList;
+    private final File outputDirectory;
+
+    /** Creates a new instance of CSharpGenerator
+     *
+     * @param name possibly full pathname to the file
+     * @param ilist included files (as JFile)
+     * @param rlist List of records defined within this file
+     * @param outputDirectory
+     */
+    CSharpGenerator(String name, ArrayList<JFile> ilist, ArrayList<JRecord> 
rlist,
+            File outputDirectory)
+     {
+        this.outputDirectory = outputDirectory;
+        mRecList = rlist;
+    }
+
+    /**
+     * Generate C# code. This method only creates the requested file(s)
+     * and spits-out file-level elements (such as include statements etc.)
+     * record-level code is generated by JRecord.
+     */
+    void genCode() throws IOException {
+        for (JRecord rec : mRecList) {
+            rec.genCsharpCode(outputDirectory);
+        }
+    }
+}

Modified: 
hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/JBoolean.java
URL: 
http://svn.apache.org/viewvc/hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/JBoolean.java?rev=940640&r1=940639&r2=940640&view=diff
==============================================================================
--- hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/JBoolean.java 
(original)
+++ hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/JBoolean.java 
Mon May  3 21:49:49 2010
@@ -25,7 +25,7 @@ public class JBoolean extends JType {
     
     /** Creates a new instance of JBoolean */
     public JBoolean() {
-        super("int32_t", "bool", "boolean", "Bool", "Boolean", "toBoolean");
+        super("int32_t", "bool", "bool", "boolean", "Bool", "Boolean", "bool", 
"toBoolean");
     }
     
     public String getSignature() {
@@ -39,4 +39,12 @@ public class JBoolean extends JType {
     public String genJavaHashCode(String fname) {
         return "     ret = ("+fname+")?0:1;\n";
     }
+
+    String genCsharpHashCode(String fname) {
+        return "     ret = ("+capitalize(fname)+")?0:1;\n";
+    }
+
+    String genCsharpCompareTo(String name) {
+        return "    ret = ("+capitalize(name)+" == peer."+capitalize(name)+")? 
0 : ("+capitalize(name)+"?1:-1);\n";
+    }
 }

Modified: 
hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/JBuffer.java
URL: 
http://svn.apache.org/viewvc/hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/JBuffer.java?rev=940640&r1=940639&r2=940640&view=diff
==============================================================================
--- hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/JBuffer.java 
(original)
+++ hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/JBuffer.java 
Mon May  3 21:49:49 2010
@@ -25,7 +25,7 @@ public class JBuffer extends JCompType {
     
     /** Creates a new instance of JBuffer */
     public JBuffer() {
-        super("struct buffer", " ::std::string", "byte[]", "Buffer", "byte[]");
+        super("struct buffer", " ::std::string", "byte[]", "byte[]", "Buffer", 
"byte[]", "byte[]");
     }
     
     public String genCppGetSet(String fname, int fIdx) {

Modified: 
hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/JByte.java
URL: 
http://svn.apache.org/viewvc/hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/JByte.java?rev=940640&r1=940639&r2=940640&view=diff
==============================================================================
--- hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/JByte.java 
(original)
+++ hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/JByte.java 
Mon May  3 21:49:49 2010
@@ -25,7 +25,7 @@ public class JByte extends JType {
     
     /** Creates a new instance of JByte */
     public JByte() {
-        super("char", "int8_t", "byte", "Byte", "Byte", "toByte");
+        super("char", "int8_t", "byte", "byte", "Byte", "Byte", "byte", 
"toByte");
     }
     
     public String getSignature() {

Modified: 
hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/JCompType.java
URL: 
http://svn.apache.org/viewvc/hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/JCompType.java?rev=940640&r1=940639&r2=940640&view=diff
==============================================================================
--- 
hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/JCompType.java 
(original)
+++ 
hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/JCompType.java 
Mon May  3 21:49:49 2010
@@ -25,8 +25,8 @@ package org.apache.jute.compiler;
 abstract class JCompType extends JType {
     
     /** Creates a new instance of JCompType */
-    JCompType(String cType, String cppType, String javaType, String suffix, 
String wrapper) {
-        super(cType, cppType, javaType, suffix, wrapper, null);
+    JCompType(String cType, String cppType, String csharpType, String 
javaType, String suffix, String wrapper, String csharpWrapper) {
+        super(cType, cppType, csharpType, javaType, suffix, wrapper, 
csharpWrapper, null);
     }
     
     String genCppGetSet(String fname, int fIdx) {
@@ -50,4 +50,17 @@ abstract class JCompType extends JType {
     String genJavaHashCode(String fname) {
         return "    ret = "+fname+".hashCode();\n";
     }
+
+    String genCsharpHashCode(String fname) {
+        return "    ret = "+capitalize(fname)+".GetHashCode();\n";
+    }
+
+    String genCsharpEquals(String name, String peer) {
+        String[] peerSplit = peer.split("\\.");
+        return "    ret = "+capitalize(name)+".Equals("+peerSplit[0] + "." + 
capitalize(peerSplit[1]) + ");\n";
+    }
+
+    String genCsharpCompareTo(String name) {
+        return "    ret = 
"+capitalize(name)+".CompareTo(peer."+capitalize(name)+");\n";
+    }
 }

Modified: 
hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/JDouble.java
URL: 
http://svn.apache.org/viewvc/hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/JDouble.java?rev=940640&r1=940639&r2=940640&view=diff
==============================================================================
--- hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/JDouble.java 
(original)
+++ hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/JDouble.java 
Mon May  3 21:49:49 2010
@@ -25,7 +25,7 @@ public class JDouble extends JType {
     
     /** Creates a new instance of JDouble */
     public JDouble() {
-        super("double", "double", "double", "Double", "Double", "toDouble");
+        super("double", "double", "double", "double", "Double", "Double", 
"double", "toDouble");
     }
     
     public String getSignature() {

Modified: 
hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/JField.java
URL: 
http://svn.apache.org/viewvc/hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/JField.java?rev=940640&r1=940639&r2=940640&view=diff
==============================================================================
--- hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/JField.java 
(original)
+++ hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/JField.java 
Mon May  3 21:49:49 2010
@@ -43,6 +43,14 @@ public class JField {
        public String genCDecl() {
                return mType.genCDecl(mName);
        }
+
+    public String genCsharpDecl() {
+        return mType.genCsharpDecl(mName);
+    }
+
+    public String genCsharpConstructorParam(String fname) {
+        return mType.genCsharpConstructorParam(fname);
+    }
        
     public String genJavaDecl() {
         return mType.genJavaDecl(mName);
@@ -55,6 +63,10 @@ public class JField {
     public String getName() {
         return mName;
     }
+
+    public String getCsharpName() {
+        return "Id".equals(mName) ? "ZKId" : mName;
+    }
     
     public String getTag() {
         return mName;
@@ -67,6 +79,35 @@ public class JField {
     public String genCppGetSet(int fIdx) {
         return mType.genCppGetSet(mName, fIdx);
     }
+
+    public String genCsharpConstructorSet(String fname) {
+        return mType.genCsharpConstructorSet(mName, fname);
+    }
+
+    public String genCsharpGetSet(int fIdx) {
+        return mType.genCsharpGetSet(getCsharpName(), fIdx);
+    }
+
+    public String genCsharpWriteMethodName() {
+        return mType.genCsharpWriteMethod(getCsharpName(), getTag());
+    }
+
+    public String genCsharpReadMethodName() {
+        return mType.genCsharpReadMethod(getCsharpName(), getTag());
+    }
+
+    public String genCsharpCompareTo() {
+        return mType.genCsharpCompareTo(getCsharpName());
+    }
+
+    public String genCsharpEquals() {
+        return mType.genCsharpEquals(getCsharpName(), "peer."+getCsharpName());
+    }
+
+    public String genCsharpHashCode() {
+        return mType.genCsharpHashCode(getCsharpName());
+    }
+
     
     public String genJavaGetSet(int fIdx) {
         return mType.genJavaGetSet(mName, fIdx);
@@ -95,4 +136,5 @@ public class JField {
     public String genJavaConstructorSet(String fname) {
         return mType.genJavaConstructorSet(mName, fname);
     }
+
 }

Modified: 
hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/JFile.java
URL: 
http://svn.apache.org/viewvc/hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/JFile.java?rev=940640&r1=940639&r2=940640&view=diff
==============================================================================
--- hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/JFile.java 
(original)
+++ hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/JFile.java 
Mon May  3 21:49:49 2010
@@ -73,6 +73,10 @@ public class JFile {
                CGenerator gen = new CGenerator(mName, mInclFiles, mRecords,
                        outputDirectory);
                gen.genCode();
+        } else if ("csharp".equals(language)) {
+               CSharpGenerator gen = new CSharpGenerator(mName, mInclFiles, 
mRecords,
+                       outputDirectory);
+               gen.genCode();
         } else {
             throw new IOException("Cannnot recognize language:" + language);
         }

Modified: 
hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/JFloat.java
URL: 
http://svn.apache.org/viewvc/hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/JFloat.java?rev=940640&r1=940639&r2=940640&view=diff
==============================================================================
--- hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/JFloat.java 
(original)
+++ hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/JFloat.java 
Mon May  3 21:49:49 2010
@@ -25,7 +25,7 @@ public class JFloat extends JType {
     
     /** Creates a new instance of JFloat */
     public JFloat() {
-        super("float", "float", "float", "Float", "Float", "toFloat");
+        super("float", "float", "float", "float", "Float", "Float", "float", 
"toFloat");
     }
     
     public String getSignature() {

Modified: 
hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/JInt.java
URL: 
http://svn.apache.org/viewvc/hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/JInt.java?rev=940640&r1=940639&r2=940640&view=diff
==============================================================================
--- hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/JInt.java 
(original)
+++ hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/JInt.java Mon 
May  3 21:49:49 2010
@@ -25,7 +25,7 @@ public class JInt extends JType {
     
     /** Creates a new instance of JInt */
     public JInt() {
-        super("int32_t", "int32_t", "int", "Int", "Integer", "toInt");
+        super("int32_t", "int32_t", "int", "int", "Int", "Integer", "int", 
"toInt");
     }
     
     public String getSignature() {

Modified: 
hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/JLong.java
URL: 
http://svn.apache.org/viewvc/hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/JLong.java?rev=940640&r1=940639&r2=940640&view=diff
==============================================================================
--- hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/JLong.java 
(original)
+++ hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/JLong.java 
Mon May  3 21:49:49 2010
@@ -25,7 +25,7 @@ public class JLong extends JType {
     
     /** Creates a new instance of JLong */
     public JLong() {
-        super("int64_t", "int64_t", "long", "Long", "Long", "toLong");
+        super("int64_t", "int64_t", "long", "long", "Long", "Long", "long", 
"toLong");
     }
     
     public String getSignature() {

Modified: 
hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/JMap.java
URL: 
http://svn.apache.org/viewvc/hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/JMap.java?rev=940640&r1=940639&r2=940640&view=diff
==============================================================================
--- hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/JMap.java 
(original)
+++ hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/JMap.java Mon 
May  3 21:49:49 2010
@@ -18,8 +18,6 @@
 
 package org.apache.jute.compiler;
 
-import org.apache.zookeeper.KeeperException.UnimplementedException;
-
 /**
  *
  */
@@ -41,7 +39,7 @@ public class JMap extends JCompType {
     /** Creates a new instance of JMap */
     public JMap(JType t1, JType t2) {
         super("#error", " ::std::map<"+t1.getCppType()+","+t2.getCppType()+">",
-                "java.util.TreeMap", "Map", "java.util.TreeMap");
+                "System.Collections.Generic.SortedDictionary<string, string>", 
"java.util.TreeMap", "Map", 
"System.Collections.Generic.SortedDictionary<string, string>", 
"java.util.TreeMap");
         mKey = t1;
         mValue = t2;
     }
@@ -100,4 +98,52 @@ public class JMap extends JCompType {
     public String genJavaWriteMethod(String fname, String tag) {
         return genJavaWriteWrapper(fname, tag);
     }
+
+    public String genCsharpWriteWrapper(String fname, int tag) {
+        StringBuilder ret = new StringBuilder("    {\n");
+        incrLevel();
+        ret.append("      a_.StartMap("+fname+",\""+tag+"\");\n");
+        ret.append("      java.util.Set "+getId("es")+" = 
"+fname+".entrySet();\n");
+        ret.append("      for(java.util.Iterator "+getId("midx")+" = 
"+getId("es")+".iterator(); "+getId("midx")+".hasNext(); ) {\n");
+        ret.append("        java.util.Map.Entry "+getId("me")+" = 
(java.util.Map.Entry) "+getId("midx")+".next();\n");
+        ret.append("        "+mKey.getCsharpWrapperType()+" "+getId("k")+" = 
("+mKey.getCsharpWrapperType()+") "+getId("me")+".getKey();\n");
+        ret.append("        "+mValue.getCsharpWrapperType()+" "+getId("v")+" = 
("+mValue.getCsharpWrapperType()+") "+getId("me")+".getValue();\n");
+        ret.append(mKey.genCsharpWriteWrapper(getId("k"),getId("k")));
+        ret.append(mValue.genCsharpWriteWrapper(getId("v"),getId("v")));
+        ret.append("      }\n");
+        ret.append("      a_.EndMap("+fname+",\""+tag+"\");\n");
+        ret.append("    }\n");
+        decrLevel();
+        return ret.toString();
+    }
+
+    String genCsharpWriteMethod(String fname, int tag) {
+        return genCsharpWriteWrapper(fname, tag);
+    }
+
+    public String genCsharpReadWrapper(String fname, int tag, boolean decl) {
+        StringBuilder ret = new StringBuilder("");
+        if (decl) {
+            ret.append("    System.Collections.SortedDictionary<string,string> 
"+capitalize(fname)+";\n");
+        }
+        ret.append("    {\n");
+        incrLevel();
+        ret.append("      Org.Apache.Jute.IIndex "+getId("midx")+" = 
a_.StartMap(\""+tag+"\");\n");
+        ret.append("      "+fname+"= new 
System.Collections.SortedDictionary<string,string>();\n");
+        ret.append("      for (; !"+getId("midx")+".done(); 
"+getId("midx")+".incr()) {\n");
+        ret.append(mKey.genCsharpReadWrapper(getId("k"),getId("k"),true));
+        ret.append(mValue.genCsharpReadWrapper(getId("v"),getId("v"),true));
+        ret.append("        "+fname+".Add("+getId("k")+","+getId("v")+");\n");
+        ret.append("      }\n");
+        ret.append("    a_.EndMap(\""+tag+"\");\n");
+        decrLevel();
+        ret.append("    }\n");
+        return ret.toString();
+    }
+
+
+
+    String genCsharpReadMethod(String fname, int tag) {
+        return genCsharpReadWrapper(fname, tag, false);
+    }
 }

Modified: 
hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/JRecord.java
URL: 
http://svn.apache.org/viewvc/hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/JRecord.java?rev=940640&r1=940639&r2=940640&view=diff
==============================================================================
--- hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/JRecord.java 
(original)
+++ hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/JRecord.java 
Mon May  3 21:49:49 2010
@@ -40,7 +40,7 @@ public class JRecord extends JCompType {
      */
     public JRecord(String name, ArrayList<JField> flist) {
         super("struct " + name.substring(name.lastIndexOf('.')+1),
-                name.replaceAll("\\.","::"), name, "Record", name);
+                name.replaceAll("\\.","::"), getCsharpFQName(name), name, 
"Record", name, getCsharpFQName("IRecord"));
         mFQName = name;
         int idx = name.lastIndexOf('.');
         mName = name.substring(idx+1);
@@ -52,6 +52,10 @@ public class JRecord extends JCompType {
         return mName;
     }
 
+    public String getCsharpName() {
+        return "Id".equals(mName) ? "ZKId" : mName;
+    }
+
     public String getJavaFQName() {
         return mFQName;
     }
@@ -68,6 +72,17 @@ public class JRecord extends JCompType {
         return mModule.replaceAll("\\.", "::");
     }
 
+    public String getCsharpNameSpace() {
+        String[] parts = mModule.split("\\.");
+        StringBuffer namespace = new StringBuffer();
+        for (int i = 0; i < parts.length; i++) {
+            String capitalized = parts[i].substring(0, 1).toUpperCase() + 
parts[i].substring(1).toLowerCase();
+            namespace.append(capitalized);
+            if (i != parts.length - 1) namespace.append(".");
+        }
+        return namespace.toString();
+    }
+
     public ArrayList<JField> getFields() {
         return mFields;
     }
@@ -105,6 +120,25 @@ public class JRecord extends JCompType {
         return "    a_.writeRecord("+fname+",\""+tag+"\");\n";
     }
 
+    String genCsharpReadMethod(String fname, String tag) {
+        //return "    "+capitalize(fname)+"=a_.Read"+mMethodSuffix+"(" + 
capitalize(fname) + ",\""+tag+"\");\n";
+        return genCsharpReadWrapper(capitalize(fname), tag, false);
+    }
+
+    public String genCsharpReadWrapper(String fname, String tag, boolean decl) 
{
+        StringBuilder ret = new StringBuilder("");
+        if (decl) {
+            ret.append("    "+getCsharpFQName(mFQName)+" "+fname+";\n");
+        }
+        ret.append("    "+fname+"= new "+getCsharpFQName(mFQName)+"();\n");
+        ret.append("    a_.ReadRecord("+fname+",\""+tag+"\");\n");
+        return ret.toString();
+    }
+
+    public String genCsharpWriteWrapper(String fname, String tag) {
+        return "    a_.WriteRecord("+fname+",\""+tag+"\");\n";
+    }
+
     static HashMap<String, String> vectorStructs = new HashMap<String, 
String>();
     public void genCCode(FileWriter h, FileWriter c) throws IOException {
         for (JField f : mFields) {
@@ -533,4 +567,196 @@ public class JRecord extends JCompType {
 
         jj.close();
     }
+
+    public void genCsharpCode(File outputDirectory) throws IOException {
+        if (!outputDirectory.exists()) {
+            // create the pkg directory
+            if (!outputDirectory.mkdirs()) {
+                throw new IOException("Cannnot create directory: " + 
outputDirectory);
+            }
+        } else if (!outputDirectory.isDirectory()) {
+            throw new IOException(outputDirectory + " is not a directory.");
+        }
+        File csharpFile = new File(outputDirectory, getName()+".cs");
+        FileWriter cs = new FileWriter(csharpFile);
+        cs.write("// File generated by hadoop record compiler. Do not 
edit.\n");
+        cs.write("/**\n");
+        cs.write("* Licensed to the Apache Software Foundation (ASF) under 
one\n");
+        cs.write("* or more contributor license agreements.  See the NOTICE 
file\n");
+        cs.write("* distributed with this work for additional information\n");
+        cs.write("* regarding copyright ownership.  The ASF licenses this 
file\n");
+        cs.write("* to you under the Apache License, Version 2.0 (the\n");
+        cs.write("* \"License\"); you may not use this file except in 
compliance\n");
+        cs.write("* with the License.  You may obtain a copy of the License 
at\n");
+        cs.write("*\n");
+        cs.write("*     http://www.apache.org/licenses/LICENSE-2.0\n";);
+        cs.write("*\n");
+        cs.write("* Unless required by applicable law or agreed to in writing, 
software\n");
+        cs.write("* distributed under the License is distributed on an \"AS 
IS\" BASIS,\n");
+        cs.write("* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 
express or implied.\n");
+        cs.write("* See the License for the specific language governing 
permissions and\n");
+        cs.write("* limitations under the License.\n");
+        cs.write("*/\n");
+        cs.write("\n");
+        cs.write("using System;\n");
+        cs.write("using Org.Apache.Jute;\n");
+        cs.write("\n");        
+        cs.write("namespace "+getCsharpNameSpace()+"\n");
+        cs.write("{\n");
+
+        String className = getCsharpName();
+        cs.write("public class "+className+" : IRecord, IComparable \n");
+        cs.write("{\n");
+        cs.write("  public "+ className +"() {\n");
+        cs.write("  }\n");
+
+        cs.write("  public "+className+"(\n");
+        int fIdx = 0;
+        int fLen = mFields.size();
+        for (Iterator<JField> i = mFields.iterator(); i.hasNext(); fIdx++) {
+            JField jf = i.next();
+            cs.write(jf.genCsharpConstructorParam(jf.getCsharpName()));
+            cs.write((fLen-1 == fIdx)?"":",\n");
+        }
+        cs.write(") {\n");
+        fIdx = 0;
+        for (Iterator<JField> i = mFields.iterator(); i.hasNext(); fIdx++) {
+            JField jf = i.next();
+            cs.write(jf.genCsharpConstructorSet(jf.getCsharpName()));
+        }
+        cs.write("  }\n");
+        fIdx = 0;
+        for (Iterator<JField> i = mFields.iterator(); i.hasNext(); fIdx++) {
+            JField jf = i.next();
+            cs.write(jf.genCsharpGetSet(fIdx));
+            cs.write("\n");
+        }
+        cs.write("  public void Serialize(IOutputArchive a_, String tag) {\n");
+        cs.write("    a_.StartRecord(this,tag);\n");
+        fIdx = 0;
+        for (Iterator<JField> i = mFields.iterator(); i.hasNext(); fIdx++) {
+            JField jf = i.next();
+            cs.write(jf.genCsharpWriteMethodName());
+        }
+        cs.write("    a_.EndRecord(this,tag);\n");
+        cs.write("  }\n");
+
+        cs.write("  public void Deserialize(IInputArchive a_, String tag) 
{\n");
+        cs.write("    a_.StartRecord(tag);\n");
+        fIdx = 0;
+        for (Iterator<JField> i = mFields.iterator(); i.hasNext(); fIdx++) {
+            JField jf = i.next();
+            cs.write(jf.genCsharpReadMethodName());
+        }
+        cs.write("    a_.EndRecord(tag);\n");
+        cs.write("}\n");
+
+        cs.write("  public override String ToString() {\n");
+        cs.write("    try {\n");
+        cs.write("      System.IO.MemoryStream ms = new 
System.IO.MemoryStream();\n");
+        cs.write("      MiscUtil.IO.EndianBinaryWriter writer =\n");
+        cs.write("        new 
MiscUtil.IO.EndianBinaryWriter(MiscUtil.Conversion.EndianBitConverter.Big, ms, 
System.Text.Encoding.UTF8);\n");
+        cs.write("      BinaryOutputArchive a_ = \n");
+        cs.write("        new BinaryOutputArchive(writer);\n");
+        cs.write("      a_.StartRecord(this,\"\");\n");
+        fIdx = 0;
+        for (Iterator<JField> i = mFields.iterator(); i.hasNext(); fIdx++) {
+            JField jf = i.next();
+            cs.write(jf.genCsharpWriteMethodName());
+        }
+        cs.write("      a_.EndRecord(this,\"\");\n");
+        cs.write("      ms.Position = 0;\n");
+        cs.write("      return 
System.Text.Encoding.UTF8.GetString(ms.ToArray());\n");
+        cs.write("    } catch (Exception ex) {\n");
+        cs.write("      Console.WriteLine(ex.StackTrace);\n");
+        cs.write("    }\n");
+        cs.write("    return \"ERROR\";\n");
+        cs.write("  }\n");
+
+        cs.write("  public void Write(MiscUtil.IO.EndianBinaryWriter writer) 
{\n");
+        cs.write("    BinaryOutputArchive archive = new 
BinaryOutputArchive(writer);\n");
+        cs.write("    Serialize(archive, \"\");\n");
+        cs.write("  }\n");
+
+        cs.write("  public void ReadFields(MiscUtil.IO.EndianBinaryReader 
reader) {\n");
+        cs.write("    BinaryInputArchive archive = new 
BinaryInputArchive(reader);\n");
+        cs.write("    Deserialize(archive, \"\");\n");
+        cs.write("  }\n");
+
+        cs.write("  public int CompareTo (object peer_) {\n");
+        boolean unimplemented = false;
+        for (JField f : mFields) {
+            if ((f.getType() instanceof JMap)
+                    || (f.getType() instanceof JVector))
+            {
+                unimplemented = true;
+            }
+        }
+        if (unimplemented) {
+            cs.write("    throw new InvalidOperationException(\"comparing "
+                    + getCsharpName() + " is unimplemented\");\n");
+        } else {
+            cs.write("    if (!(peer_ is "+getCsharpName()+")) {\n");
+            cs.write("      throw new InvalidOperationException(\"Comparing 
different types of records.\");\n");
+            cs.write("    }\n");
+            cs.write("    "+getCsharpName()+" peer = ("+getCsharpName()+") 
peer_;\n");
+            cs.write("    int ret = 0;\n");
+            for (Iterator<JField> i = mFields.iterator(); i.hasNext(); fIdx++) 
{
+                JField jf = i.next();
+                cs.write(jf.genCsharpCompareTo());
+                cs.write("    if (ret != 0) return ret;\n");
+            }
+            cs.write("     return ret;\n");
+        }
+        cs.write("  }\n");
+
+        cs.write("  public override bool Equals(object peer_) {\n");
+        cs.write("    if (!(peer_ is "+getCsharpName()+")) {\n");
+        cs.write("      return false;\n");
+        cs.write("    }\n");
+        cs.write("    if (peer_ == this) {\n");
+        cs.write("      return true;\n");
+        cs.write("    }\n");
+        cs.write("    bool ret = false;\n");
+        cs.write("    " + getCsharpName() + " peer = (" + getCsharpName() + 
")peer_;\n");
+        for (Iterator<JField> i = mFields.iterator(); i.hasNext(); fIdx++) {
+            JField jf = i.next();
+            cs.write(jf.genCsharpEquals());
+            cs.write("    if (!ret) return ret;\n");
+        }
+        cs.write("     return ret;\n");
+        cs.write("  }\n");
+
+        cs.write("  public override int GetHashCode() {\n");
+        cs.write("    int result = 17;\n");
+        cs.write("    int ret;\n");
+        for (Iterator<JField> i = mFields.iterator(); i.hasNext(); fIdx++) {
+            JField jf = i.next();
+            cs.write(jf.genCsharpHashCode());
+            cs.write("    result = 37*result + ret;\n");
+        }
+        cs.write("    return result;\n");
+        cs.write("  }\n");
+        cs.write("  public static string Signature() {\n");
+        cs.write("    return \""+getSignature()+"\";\n");
+        cs.write("  }\n");
+
+        cs.write("}\n");
+        cs.write("}\n");
+
+        cs.close();
+    }
+
+    public static String getCsharpFQName(String name) {
+        String[] packages = name.split("\\.");
+        StringBuffer fQName = new StringBuffer();
+        for (int i = 0; i < packages.length; i++) {
+            String pack = packages[i];
+            pack = capitalize(pack);
+            pack = "Id".equals(pack) ? "ZKId" : pack;
+            fQName.append(capitalize(pack));
+            if (i != packages.length - 1) fQName.append(".");
+        }
+        return fQName.toString();
+    }    
 }

Modified: 
hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/JString.java
URL: 
http://svn.apache.org/viewvc/hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/JString.java?rev=940640&r1=940639&r2=940640&view=diff
==============================================================================
--- hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/JString.java 
(original)
+++ hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/JString.java 
Mon May  3 21:49:49 2010
@@ -25,7 +25,7 @@ public class JString extends JCompType {
     
     /** Creates a new instance of JString */
     public JString() {
-        super("char *", " ::std::string", "String", "String", "String");
+        super("char *", " ::std::string", "string", "String", "String", 
"String", "string");
     }
     
     public String getSignature() {

Modified: 
hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/JType.java
URL: 
http://svn.apache.org/viewvc/hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/JType.java?rev=940640&r1=940639&r2=940640&view=diff
==============================================================================
--- hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/JType.java 
(original)
+++ hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/JType.java 
Mon May  3 21:49:49 2010
@@ -26,20 +26,24 @@ abstract public class JType {
     
        private String mCName;
     private String mCppName;
+    private String mCsharpName;
     private String mJavaName;
-    private String mMethodSuffix;
+    protected String mMethodSuffix;
     private String mWrapper;
+    private String mSharpWrapper;
     private String mUnwrapMethod;
-    
+
     /**
      * Creates a new instance of JType
      */
-    JType(String cname, String cppname, String javaname, String suffix, String 
wrapper, String unwrap) {
+    JType(String cname, String cppname, String csharpName, String javaname, 
String suffix, String wrapper, String csharpWrapper, String unwrap) {
        mCName = cname;
         mCppName = cppname;
+        mCsharpName = "Id".equals(csharpName) ? "ZKId" : csharpName;
         mJavaName = javaname;
         mMethodSuffix = suffix;
         mWrapper = wrapper;
+        mSharpWrapper = csharpWrapper;
         mUnwrapMethod = unwrap;
     }
     
@@ -52,6 +56,11 @@ abstract public class JType {
        String genCDecl(String name) {
                return "    " + mCName + " "+name+";\n"; 
        }
+
+    public String genCsharpDecl(String name) {
+        return "  private "+mCsharpName+" " +name+";\n";
+    }
+
     String genJavaDecl (String fname) {
         return "  private "+mJavaName+" " +fname+";\n";
     }
@@ -69,6 +78,11 @@ abstract public class JType {
         setFunc += "  }\n";
         return getFunc+setFunc;
     }
+
+    String genCsharpGetSet(String fname, int fIdx) {
+        String getFunc = "  public " + getCsharpType() + " " + 
capitalize(fname) + " { get; set; } ";
+        return getFunc;
+    }
     
     static String capitalize(String s) {
         return s.substring(0,1).toUpperCase()+s.substring(1);
@@ -90,6 +104,10 @@ abstract public class JType {
         return mCppName;
     }
     
+    String getCsharpType() {
+        return mCsharpName;
+    }
+
     String getJavaType() {
         return mJavaName;
     }
@@ -97,6 +115,10 @@ abstract public class JType {
     String getJavaWrapperType() {
         return mWrapper;
     }
+
+    String getCsharpWrapperType() {
+        return mSharpWrapper;
+    }
     
     String getMethodSuffix() {
         return mMethodSuffix;
@@ -138,4 +160,45 @@ abstract public class JType {
         return "    this."+fname+"="+name+";\n";
     }
 
+    String genCsharpWriteMethod(String fname, String tag) {
+        return "    
a_.Write"+mMethodSuffix+"("+capitalize(fname)+",\""+tag+"\");\n";
+    }
+
+    String genCsharpReadMethod(String fname, String tag) {
+        return "    
"+capitalize(fname)+"=a_.Read"+mMethodSuffix+"(\""+tag+"\");\n";
+    }
+
+    String genCsharpReadWrapper(String fname, String tag, boolean decl) {
+        String ret = "";
+        if (decl) {
+            ret = "    "+mWrapper+" "+fname+";\n";
+        }
+        return ret + "    "+fname+"=a_.Read"+mMethodSuffix+"(\""+tag+"\");\n";
+    }
+
+    String genCsharpWriteWrapper(String fname, String tag) {
+        if (mUnwrapMethod == null) return "        
a_.Write"+mMethodSuffix+"("+fname+","+tag+");\n";
+        return "        
a_.Write"+mMethodSuffix+"("+fname+"."+mUnwrapMethod+"(),\""+tag+"\");\n";
+    }
+
+    String genCsharpCompareTo(String name) {
+        return "    ret = ("+capitalize(name)+" == peer."+capitalize(name)+")? 
0 :(("+capitalize(name)+"<peer."+capitalize(name)+")?-1:1);\n";
+    }
+
+    String genCsharpEquals(String name, String peer) {
+        String[] peerSplit = peer.split("\\.");
+        return "    ret = ("+capitalize(name)+"=="+peerSplit[0] + "." + 
capitalize(peerSplit[1]) + ");\n";
+    }
+
+    String genCsharpHashCode(String fname) {
+        return "    ret = (int)"+capitalize(fname)+";\n";
+    }
+
+    String genCsharpConstructorSet(String mName, String fname) {
+        return capitalize(fname)+"="+mName+";\n";
+    }
+
+    public String genCsharpConstructorParam(String fname) {
+        return "  "+mCsharpName+" " +fname+"\n";
+    }
 }

Modified: 
hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/JVector.java
URL: 
http://svn.apache.org/viewvc/hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/JVector.java?rev=940640&r1=940639&r2=940640&view=diff
==============================================================================
--- hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/JVector.java 
(original)
+++ hadoop/zookeeper/trunk/src/java/main/org/apache/jute/compiler/JVector.java 
Mon May  3 21:49:49 2010
@@ -37,7 +37,8 @@ public class JVector extends JCompType {
     
     /** Creates a new instance of JVector */
     public JVector(JType t) {
-        super("struct " + extractVectorName(t), " 
::std::vector<"+t.getCppType()+">", "java.util.List<" + t.getJavaType() + ">", 
"Vector", "java.util.ArrayList<" + t.getJavaType() + ">");
+        super("struct " + extractVectorName(t), " 
::std::vector<"+t.getCppType()+">", "System.Collections.Generic.List<" + 
t.getCsharpType() + ">", "java.util.List<" + t.getJavaType() + ">", "Vector",
+                "System.Collections.Generic.List<" + t.getCsharpType() + ">", 
"java.util.ArrayList<" + t.getJavaType() + ">");
         mElement = t;
     }
     
@@ -100,7 +101,53 @@ public class JVector extends JCompType {
        return mElement;
     }
 
-       static public String extractVectorName(JType jvType) {
+    public String genCsharpWriteWrapper(String fname, String tag) {
+        StringBuilder ret = new StringBuilder("    {\n");
+        incrLevel();
+        ret.append("      
a_.StartVector("+capitalize(fname)+",\""+tag+"\");\n");
+        ret.append("      if ("+capitalize(fname)+"!= null) {");
+        ret.append("          int "+getId("len")+" = 
"+capitalize(fname)+".Count;\n");
+        ret.append("          for(int "+getId("vidx")+" = 0; 
"+getId("vidx")+"<"+getId("len")+"; "+getId("vidx")+"++) {\n");
+        ret.append("            "+mElement.getCsharpWrapperType()+" 
"+getId("e")+" = ("+mElement.getCsharpWrapperType()+") 
"+capitalize(fname)+"["+getId("vidx")+"];\n");
+        ret.append(mElement.genCsharpWriteWrapper(getId("e"), getId("e")));
+        ret.append("          }\n");
+        ret.append("      }\n");
+        ret.append("      a_.EndVector("+capitalize(fname)+",\""+tag+"\");\n");
+        ret.append("    }\n");
+        decrLevel();
+        return ret.toString();
+    }
+
+    String genCsharpWriteMethod(String fname, String tag) {
+        return genCsharpWriteWrapper(fname, tag);
+    }
+
+    public String genCsharpReadWrapper(String fname, String tag, boolean decl) 
{
+        StringBuilder ret = new StringBuilder("");
+        if (decl) {
+            ret.append("      System.Collections.Generic.List<" + 
mElement.getCsharpType()+ "> "+capitalize(fname)+";\n");
+        }
+        ret.append("    {\n");
+        incrLevel();
+        ret.append("      IIndex "+getId("vidx")+" = 
a_.StartVector(\""+tag+"\");\n");
+        ret.append("      if ("+getId("vidx")+"!= null) {");
+        ret.append("          "+capitalize(fname)+"=new 
System.Collections.Generic.List<"+ mElement.getCsharpType() + ">();\n");
+        ret.append("          for (; !"+getId("vidx")+".Done(); 
"+getId("vidx")+".Incr()) {\n");
+        ret.append(mElement.genCsharpReadWrapper(getId("e"), getId("e"), 
true));
+        ret.append("            "+capitalize(fname)+".Add("+getId("e")+");\n");
+        ret.append("          }\n");
+        ret.append("      }\n");
+        ret.append("    a_.EndVector(\""+tag+"\");\n");
+        decrLevel();
+        ret.append("    }\n");
+        return ret.toString();
+    }
+    
+    String genCsharpReadMethod(String fname, String tag) {
+        return genCsharpReadWrapper(fname, tag, false);
+    }
+
+    static public String extractVectorName(JType jvType) {
                return JRecord.extractMethodSuffix(jvType)+"_vector";
        }
 }


Reply via email to