Convert POJO Java Classes To POCO C# Classes


public class Converter
 {
 public static void BatchConvert(string sourcePath, string destinationPath)
 {
 var files = Directory.GetFiles(sourcePath);
 var classNames = new Dictionary<string, List<string>>();

foreach (var file in files)
 {
 var className = file.Replace(sourcePath, string.Empty).Replace(".java", string.Empty);
 var types = Convert(sourcePath, destinationPath, className);
 classNames.Add(className, types);
 }

var ok = true;

while (ok)
 {
 ok = false;

foreach (var key in classNames.Keys)
 {
 string type = null;

foreach (var currentType in classNames[key])
 {
 if (classNames.ContainsKey(currentType))
 {
 type = currentType;
 break;
 }
 }

if (type != null)
 {
 classNames[key].Remove(type);
 ok = true;
 }
 }
 }

foreach (var key in classNames.Keys)
 {
 if (classNames[key].Count > 0)
 {
 Convert(sourcePath, destinationPath, key, classNames[key]);
 }
 }
 }

public static List<string> Convert(string sourcePath, string destinationPath, string className, List<string> excludedTypes = null)
 {
 var types = new List<string>();

#region Initializing the Java/C# types counterparts

var map = new Dictionary<string, string>();
 map.Add("boolean", "bool");
 map.Add("Boolean", "bool?");
 map.Add("byte", "byte");
 map.Add("Byte", "sbyte?");
 map.Add("short", "short?");
 map.Add("Short", "short?");
 map.Add("int", "int");
 map.Add("Integer", "int?");
 map.Add("long", "long");
 map.Add("Long", "long?");
 map.Add("char", "char");
 map.Add("Character", "char?");
 map.Add("float", "float");
 map.Add("Foat", "float?");
 map.Add("double", "double");
 map.Add("Double", "double?");
 map.Add("String", "string");
 map.Add("Date", "DateTime");
 map.Add("HashMap", "Hashtable");
 //map.Add("ArrayList", "ArrayList");

#endregion

using (var reader = new StreamReader(string.Concat(sourcePath, className, ".java")))
 {
 using (var writer = new StreamWriter(string.Concat(destinationPath, className, ".cs")))
 {
 writer.WriteLine("using System;");
 writer.WriteLine("using System.Collections;");
 writer.WriteLine("using System.Collections.Generic;");
 writer.WriteLine();
 writer.WriteLine("namespace Models");
 writer.WriteLine("{");
 writer.WriteLine(string.Concat("\tpublic class ", className));
 writer.WriteLine("\t{");
 string line;

while ((line = reader.ReadLine()) != null)
 {
 line = line.Trim();

if (line.StartsWith("private") &&
 !line.ToLower().Contains(" static ") &&
 !line.Contains("("))
 {
 var sb = new StringBuilder("\t\tpublic ");
 var tokens = line.Split(new char[] { ' ' });
 var propertyType = tokens[1].Trim().Replace("java.lang.", string.Empty).Replace("java.util.", string.Empty);
 var property = tokens[2].Trim().Replace(";", string.Empty);

if (property.ToLower() == className.ToLower())
 {
 property = string.Concat(property, " { get; set; }");
 }
 else
 {
 property = string.Concat(property, " { get; set; }");
 //property = string.Concat(CapitalizeFirstLetter(property), " { get; set; }"); We're not doing this to accomodate Java
 }

var found = false;

foreach (string javaType in map.Keys)
 {
 if (javaType == propertyType)
 {
 found = true;
 sb.Append(string.Format("{0} {1}", map[javaType], property));
 break;
 }
 }

if (!found)
 {
 if (propertyType.StartsWith("List<"))
 {
 propertyType = propertyType.Substring(5, propertyType.Length - 6);

if (map.ContainsKey(propertyType))
 {
 sb.AppendFormat("List<{0}> {1}", map[propertyType], property);
 }
 else
 {
 types.Add(propertyType);
 sb.AppendFormat("List<{0}> {1}", propertyType, property);
 }
 }
 else
 {
 types.Add(propertyType);
 sb.AppendFormat("{0} {1}", propertyType, property);
 }
 }

if ((sb.Length > 0) && (sb.ToString() != "\t\tpublic "))
 {
 var newLine = sb.ToString();

if (excludedTypes != null)
 {
 foreach (var excludedType in excludedTypes)
 {
 if (newLine.Contains(string.Format(" {0} ", excludedType)))
 {
 newLine = newLine.Replace("\t\t", "\t\t//");
 }
 }
 }

writer.WriteLine(newLine);
 }
 }
 }

writer.WriteLine("\t}");
 writer.WriteLine("}");
 }
 }

return types;
 }

public static string CapitalizeFirstLetter(string text)
 {
 return string.Concat(text.Substring(0, 1).ToUpper(), text.Substring(1));
 }
 }