Browse Source

Update code converter Web site to support conversion to Ruby and Python

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/3.0@5365 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
pull/1/head
Christoph Wille 17 years ago
parent
commit
de3fc84d5d
  1. 2
      samples/CodeConverter/Source/CCSite/About.aspx.cs
  2. 47
      samples/CodeConverter/Source/CCSite/ConvertService.cs
  3. 6
      samples/CodeConverter/Source/CCSite/SnippetConverter.aspx
  4. 37
      samples/CodeConverter/Source/CCSite/SnippetConverter.aspx.cs
  5. 2
      samples/CodeConverter/Source/CCSite/SnippetConverter.aspx.designer.cs
  6. 63
      samples/CodeConverter/Source/ICSharpCode.CodeConversion/HelperFunctions.cs
  7. 12
      samples/CodeConverter/Source/ICSharpCode.CodeConversion/ICSharpCode.CodeConversion.csproj
  8. 26
      samples/CodeConverter/Source/ICSharpCode.CodeConversion/PythonHelpers.cs
  9. 26
      samples/CodeConverter/Source/ICSharpCode.CodeConversion/RubyHelpers.cs
  10. 38
      samples/CodeConverter/Source/ICSharpCode.CodeConversion/SnippetConversion.cs

2
samples/CodeConverter/Source/CCSite/About.aspx.cs

@ -15,7 +15,7 @@ namespace CCSite
{ {
protected void Page_Load(object sender, EventArgs e) protected void Page_Load(object sender, EventArgs e)
{ {
VersionLabel.Text = ICSharpCode.CodeConversion.HelperFunctions.GetNRefactoryVersion(); VersionLabel.Text = ICSharpCode.CodeConversion.CodeConversionHelpers.GetNRefactoryVersion();
} }
} }
} }

47
samples/CodeConverter/Source/CCSite/ConvertService.cs

@ -14,7 +14,8 @@ using ICSharpCode.CodeConversion;
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class ConvertService : System.Web.Services.WebService { public class ConvertService : System.Web.Services.WebService {
public ConvertService () { public ConvertService ()
{
//Uncomment the following line if using designed components //Uncomment the following line if using designed components
//InitializeComponent(); //InitializeComponent();
@ -37,50 +38,10 @@ public class ConvertService : System.Web.Services.WebService {
public bool ConvertSnippet(string TypeOfConversion, string SourceCode, out string ConvertedCode, out string ErrorMessage) public bool ConvertSnippet(string TypeOfConversion, string SourceCode, out string ConvertedCode, out string ErrorMessage)
{ {
ErrorMessage = ConvertedCode = ""; ErrorMessage = ConvertedCode = "";
string convertedSource = "", errorMessage = "";
bool bSuccessfulConversion = false;
IConvertCode currentConverter = null;
switch (TypeOfConversion)
{
case "cs2boo":
currentConverter = new ConvertCSharpToBoo();
break;
case "vbnet2boo":
currentConverter = new ConvertVbNetToBoo();
break;
case "cs2vbnet":
currentConverter = new ConvertCSharpSnippetToVbNet();
break;
case "vbnet2cs":
currentConverter = new ConvertVbNetSnippetToCSharp();
break;
default:
return false;
}
try bool result = CodeConversionHelpers.ConvertSnippet(TypeOfConversion, SourceCode, out ConvertedCode, out ErrorMessage);
{
bSuccessfulConversion = currentConverter.Convert(SourceCode,
out convertedSource,
out errorMessage);
}
catch (Exception ex)
{
bSuccessfulConversion = false;
errorMessage = "Exception occured: " + ex.ToString() + "\r\n\r\nError Message:" + errorMessage;
}
if (bSuccessfulConversion)
{
ConvertedCode = convertedSource;
}
else
{
ErrorMessage = errorMessage;
}
return bSuccessfulConversion; return result;
} }
} }

6
samples/CodeConverter/Source/CCSite/SnippetConverter.aspx

@ -7,11 +7,15 @@
<p class="page_title">Step 1: Choose source language and destination language for conversion</p> <p class="page_title">Step 1: Choose source language and destination language for conversion</p>
<p> <p>
<asp:RadioButtonList ID="languageChoice" runat="server" RepeatColumns="2"> <asp:RadioButtonList ID="languageChoice" runat="server" RepeatColumns="4">
<asp:ListItem Selected="True" Value="cs2vbnet">C# to VB.NET</asp:ListItem> <asp:ListItem Selected="True" Value="cs2vbnet">C# to VB.NET</asp:ListItem>
<asp:ListItem Value="vbnet2cs">VB.NET to C#</asp:ListItem> <asp:ListItem Value="vbnet2cs">VB.NET to C#</asp:ListItem>
<asp:ListItem Value="cs2boo">C# to Boo</asp:ListItem> <asp:ListItem Value="cs2boo">C# to Boo</asp:ListItem>
<asp:ListItem Value="vbnet2boo">VB.NET to Boo</asp:ListItem> <asp:ListItem Value="vbnet2boo">VB.NET to Boo</asp:ListItem>
<asp:ListItem Value="cs2python">C# to Python</asp:ListItem>
<asp:ListItem Value="vbnet2python">VB.NET to Python</asp:ListItem>
<asp:ListItem Value="cs2ruby">C# to Ruby</asp:ListItem>
<asp:ListItem Value="vbnet2ruby">VB.NET to Ruby</asp:ListItem>
</asp:RadioButtonList> </asp:RadioButtonList>
<br /> <br />
</p> </p>

37
samples/CodeConverter/Source/CCSite/SnippetConverter.aspx.cs

@ -2,6 +2,7 @@ using System;
using System.Data; using System.Data;
using System.Configuration; using System.Configuration;
using System.Collections; using System.Collections;
using System.Collections.Generic;
using System.Web; using System.Web;
using System.Web.Security; using System.Web.Security;
using System.Web.UI; using System.Web.UI;
@ -15,44 +16,18 @@ public partial class SnippetConverterPage : System.Web.UI.Page
{ {
protected void Page_Load(object sender, EventArgs e) protected void Page_Load(object sender, EventArgs e)
{ {
} }
protected void convertCode_Click(object sender, EventArgs e) protected void convertCode_Click(object sender, EventArgs e)
{ {
string convertedSource = "", errorMessage = ""; string convertedSource = "", errorMessage = "";
bool bSuccessfulConversion = false;
IConvertCode currentConverter = null;
try string typeOfConversion = languageChoice.SelectedValue;
{ string sourceCode = inputTextBox.Text.Trim();
switch (languageChoice.SelectedValue)
{
case "cs2boo":
currentConverter = new ConvertCSharpToBoo();
break;
case "vbnet2boo":
currentConverter = new ConvertVbNetToBoo();
break;
case "cs2vbnet":
currentConverter = new ConvertCSharpSnippetToVbNet();
break;
case "vbnet2cs":
currentConverter = new ConvertVbNetSnippetToCSharp();
break;
}
bSuccessfulConversion = currentConverter.Convert(inputTextBox.Text, bool result = CodeConversionHelpers.ConvertSnippet(typeOfConversion, sourceCode, out convertedSource, out errorMessage);
out convertedSource,
out errorMessage);
}
catch (Exception ex)
{
OutputLabel.Text = "Exception occured, please report in the bug reporting forum";
outputTextBox.Text = ex.ToString();
return;
}
if (bSuccessfulConversion) if (result)
{ {
OutputLabel.Text = "Converted Sourcecode"; OutputLabel.Text = "Converted Sourcecode";
outputTextBox.Text = convertedSource; outputTextBox.Text = convertedSource;

2
samples/CodeConverter/Source/CCSite/SnippetConverter.aspx.designer.cs generated

@ -1,7 +1,7 @@
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// <auto-generated> // <auto-generated>
// This code was generated by a tool. // This code was generated by a tool.
// Runtime Version:2.0.50727.1433 // Runtime Version:2.0.50727.4927
// //
// Changes to this file may cause incorrect behavior and will be lost if // Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated. // the code is regenerated.

63
samples/CodeConverter/Source/ICSharpCode.CodeConversion/HelperFunctions.cs

@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Generic;
// Conversion general // Conversion general
using ICSharpCode.NRefactory.PrettyPrinter; using ICSharpCode.NRefactory.PrettyPrinter;
@ -6,9 +7,9 @@ using ICSharpCode.NRefactory.PrettyPrinter;
namespace ICSharpCode.CodeConversion namespace ICSharpCode.CodeConversion
{ {
public class HelperFunctions public class CodeConversionHelpers
{ {
private HelperFunctions() { } private CodeConversionHelpers() { }
public static string GetNRefactoryVersion() public static string GetNRefactoryVersion()
{ {
@ -20,5 +21,63 @@ namespace ICSharpCode.CodeConversion
return s; return s;
} }
public static IConvertCode InferConverter(string typeOfConversion)
{
Dictionary<string, Type> converters = new Dictionary<string, Type>()
{
{ "cs2boo", typeof(ConvertCSharpToBoo) },
{ "vbnet2boo", typeof(ConvertVbNetToBoo) },
{ "cs2vbnet", typeof(ConvertCSharpSnippetToVbNet) },
{ "vbnet2cs", typeof(ConvertVbNetSnippetToCSharp) },
{ "vbnet2ruby", typeof(ConvertVbNetToRuby) },
{ "vbnet2python", typeof(ConvertVbNetToPython) },
{ "cs2ruby", typeof(ConvertCSharpToRuby) },
{ "cs2python", typeof(ConvertCSharpToPython) }
};
if (converters.ContainsKey(typeOfConversion))
{
Type t = converters[typeOfConversion];
object o = Activator.CreateInstance(t, null);
return (o as IConvertCode);
}
return null;
}
public static bool ConvertSnippet(string TypeOfConversion, string SourceCode, out string ConvertedCode, out string ErrorMessage)
{
ErrorMessage = ConvertedCode = "";
string convertedSource = "", errorMessage = "";
bool bSuccessfulConversion = false;
IConvertCode currentConverter = InferConverter(TypeOfConversion);
if (null == currentConverter) return false;
try
{
bSuccessfulConversion = currentConverter.Convert(SourceCode,
out convertedSource,
out errorMessage);
}
catch (Exception ex)
{
bSuccessfulConversion = false;
errorMessage = "Exception occured: " + ex.ToString() + "\r\n\r\nError Message:" + errorMessage;
}
if (bSuccessfulConversion)
{
ConvertedCode = convertedSource;
}
else
{
ErrorMessage = errorMessage;
}
return bSuccessfulConversion;
}
} }
} }

12
samples/CodeConverter/Source/ICSharpCode.CodeConversion/ICSharpCode.CodeConversion.csproj

@ -3,7 +3,7 @@
<PropertyGroup> <PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>9.0.21022</ProductVersion> <ProductVersion>9.0.30729</ProductVersion>
<SchemaVersion>2.0</SchemaVersion> <SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{BF122CB4-E896-40CA-BF0A-AB44906AB87D}</ProjectGuid> <ProjectGuid>{BF122CB4-E896-40CA-BF0A-AB44906AB87D}</ProjectGuid>
<OutputType>Library</OutputType> <OutputType>Library</OutputType>
@ -51,6 +51,14 @@
<SpecificVersion>False</SpecificVersion> <SpecificVersion>False</SpecificVersion>
<HintPath>..\..\Dependencies\NRefactoryToBooConverter.dll</HintPath> <HintPath>..\..\Dependencies\NRefactoryToBooConverter.dll</HintPath>
</Reference> </Reference>
<Reference Include="PythonBinding, Version=3.2.0.5362, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\Dependencies\PythonBinding.dll</HintPath>
</Reference>
<Reference Include="RubyBinding, Version=3.2.0.5362, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\Dependencies\RubyBinding.dll</HintPath>
</Reference>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Core"> <Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework> <RequiredTargetFramework>3.5</RequiredTargetFramework>
@ -69,7 +77,9 @@
<Compile Include="HelperFunctions.cs" /> <Compile Include="HelperFunctions.cs" />
<Compile Include="IConvertCode.cs" /> <Compile Include="IConvertCode.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="PythonHelpers.cs" />
<Compile Include="ReferencedContentsSingleton.cs" /> <Compile Include="ReferencedContentsSingleton.cs" />
<Compile Include="RubyHelpers.cs" />
<Compile Include="SnippetConversion.cs" /> <Compile Include="SnippetConversion.cs" />
</ItemGroup> </ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

26
samples/CodeConverter/Source/ICSharpCode.CodeConversion/PythonHelpers.cs

@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ICSharpCode.NRefactory;
using ICSharpCode.PythonBinding;
namespace ICSharpCode.CodeConversion
{
public class PythonHelpers
{
public static bool Convert(SupportedLanguage inputLanguage, string ProvidedSource, out string ConvertedSource, out string ErrorMessage)
{
NRefactoryToPythonConverter converter = new
NRefactoryToPythonConverter(inputLanguage);
string convertedCode = converter.Convert(ProvidedSource);
ConvertedSource = convertedCode;
ErrorMessage = "";
return true;
}
}
}

26
samples/CodeConverter/Source/ICSharpCode.CodeConversion/RubyHelpers.cs

@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ICSharpCode.NRefactory;
using ICSharpCode.RubyBinding;
namespace ICSharpCode.CodeConversion
{
public class RubyHelpers
{
public static bool Convert(SupportedLanguage inputLanguage, string ProvidedSource, out string ConvertedSource, out string ErrorMessage)
{
NRefactoryToRubyConverter converter = new
NRefactoryToRubyConverter(inputLanguage);
string convertedCode = converter.Convert(ProvidedSource);
ConvertedSource = convertedCode;
ErrorMessage = "";
return true;
}
}
}

38
samples/CodeConverter/Source/ICSharpCode.CodeConversion/SnippetConversion.cs

@ -1,10 +1,12 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using ICSharpCode.SharpDevelop.Dom;
using ICSharpCode.SharpDevelop.Dom.NRefactoryResolver;
using System.IO; using System.IO;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using ICSharpCode.NRefactory;
using ICSharpCode.SharpDevelop.Dom;
using ICSharpCode.SharpDevelop.Dom.NRefactoryResolver;
namespace ICSharpCode.CodeConversion namespace ICSharpCode.CodeConversion
{ {
public class ConvertCSharpSnippetToVbNet : IConvertCode public class ConvertCSharpSnippetToVbNet : IConvertCode
@ -52,4 +54,36 @@ namespace ICSharpCode.CodeConversion
return bSuccessfulConversion; return bSuccessfulConversion;
} }
} }
public class ConvertCSharpToPython : IConvertCode
{
public bool Convert(string ProvidedSource, out string ConvertedSource, out string ErrorMessage)
{
return PythonHelpers.Convert(SupportedLanguage.CSharp, ProvidedSource, out ConvertedSource, out ErrorMessage);
}
}
public class ConvertCSharpToRuby : IConvertCode
{
public bool Convert(string ProvidedSource, out string ConvertedSource, out string ErrorMessage)
{
return RubyHelpers.Convert(SupportedLanguage.CSharp, ProvidedSource, out ConvertedSource, out ErrorMessage);
}
}
public class ConvertVbNetToPython : IConvertCode
{
public bool Convert(string ProvidedSource, out string ConvertedSource, out string ErrorMessage)
{
return PythonHelpers.Convert(SupportedLanguage.VBNet, ProvidedSource, out ConvertedSource, out ErrorMessage);
}
}
public class ConvertVbNetToRuby : IConvertCode
{
public bool Convert(string ProvidedSource, out string ConvertedSource, out string ErrorMessage)
{
return RubyHelpers.Convert(SupportedLanguage.VBNet, ProvidedSource, out ConvertedSource, out ErrorMessage);
}
}
} }
Loading…
Cancel
Save