1
0
mirror of https://github.com/twiglet/cs2j.git synced 2025-01-18 13:15:17 +01:00

Add support for alternate translations. So when looking for A.B.C.D.E, we will first look in A.B.C.D.<alt>.E we need this to provide alternate translations for Java method renaming

This commit is contained in:
Kevin Glynn 2011-09-03 10:10:30 +02:00
parent eb6e71c6e8
commit 542cc43895
9 changed files with 261 additions and 24 deletions

View File

@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
This file is
Copyright 2007,2008,2009,2010 Rustici Software, LLC
Copyright 2010,2011 Kevin Glynn (kevin.glynn@twigletsoftware.com)
-->
<Interface xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="urn:www.twigletsoftware.com:schemas:txtemplate:1:0">
<Imports>
<Import>CS2JNet.System.LCC.IDisposable</Import>
</Imports>
<Java>IDisposable</Java>
<Name>System.IDisposable</Name>
<Uses />
<Inherits> <Type>System.Object</Type> </Inherits>
<Methods>
<Method>
<Imports />
<Java>${this:16}.close()</Java>
<Params />
<Name>Dispose</Name>
<Return>System.Void</Return>
</Method>
</Methods>
<Properties />
<Events />
<Indexers />
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#"><SignedInfo><CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" /><SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" /><Reference URI=""><Transforms><Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" /></Transforms><DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" /><DigestValue>+vVUUFftRdH5Pr1U9UgceoocL/c=</DigestValue></Reference></SignedInfo><SignatureValue>iSpArQXUl74iZlhpxXJA0uxKNQP58ZhDGoiQafnENk6Sr2WkJrv1+FbrcL/VoqEvgT6EZ0lzvv1jbKJY0h6grUsn4CcN2J4MCttnAOCnemuxaVb00aASlLUwmN7InqdhebdjH4uFR7UUKm7qYq5EvAR095fu0A/zGWSJ7L/tXO4=</SignatureValue></Signature></Interface>

View File

@ -0,0 +1,31 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
This file is
Copyright 2007,2008,2009,2010 Rustici Software, LLC
Copyright 2010,2011 Kevin Glynn (kevin.glynn@twigletsoftware.com)
-->
<Interface xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="urn:www.twigletsoftware.com:schemas:txtemplate:1:0">
<Imports>
<Import>CS2JNet.System.Xml.Serialization.LCC.IXmlSerializable</Import>
</Imports>
<Java>IXmlSerializable</Java>
<Name>System.Xml.Serialization.IXmlSerializable</Name>
<Uses />
<Inherits> <Type>System.Object</Type> </Inherits>
<Methods />
<Properties>
<Property>
<Imports />
<Java>${this:16}.getValue()</Java>
<Type>System.String</Type>
<Name>Value</Name>
<Get>${this:16}.getValue()</Get>
<Set>${this:16}.setValue(${value})</Set>
</Property>
</Properties>
<Events />
<Indexers />
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#"><SignedInfo><CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" /><SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" /><Reference URI=""><Transforms><Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" /></Transforms><DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" /><DigestValue>SiUhORBUjQ/2aYjJrS7nmB9D7c8=</DigestValue></Reference></SignedInfo><SignatureValue>B5pXm+f7e2+z7PlAmqp1DoJ3dIZnqxZGHs8b0anRWpYtPjIWm0kc4+KTC/n9uPU9+OINcskAJfSU6X4HDXhOq1mrDUaPTKC/dRBGdqCReA7T5geYxFv+xOM5Rge5hzkkZajUftben1I7BQVbsXhKfbujN18rNza2BrMAvMyZw1s=</SignatureValue></Signature></Interface>

View File

@ -0,0 +1,55 @@
/*
Copyright 2010,2011 Kevin Glynn (kevin.glynn@twigletsoftware.com)
Licensed 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.
Author(s):
Kevin Glynn (kevin.glynn@twigletsoftware.com)
*/
package CS2JNet.System.LCC;
import java.io.Closeable;
import CS2JNet.System.LCC.IDisposable;
/**
* @author keving
*
*/
public class Disposable implements IDisposable {
private Closeable closerVal = null;
private IDisposable dispVal = null;
public static Disposable mkDisposable(Closeable obj) {
Disposable ret = new Disposable();
ret.closerVal = obj;
return ret;
}
public static Disposable mkDisposable(IDisposable obj) {
Disposable ret = new Disposable();
ret.dispVal = obj;
return ret;
}
/* (non-Javadoc)
* @see CS2JNet.System.IDisposable#Dispose()
*/
@Override
public void dispose() throws Exception {
if (dispVal != null)
dispVal.dispose();
else if (closerVal != null)
closerVal.close();
}
}

View File

@ -0,0 +1,27 @@
/*
Copyright 2007,2008,2009,2010 Rustici Software, LLC
Copyright 2010,2011 Kevin Glynn (kevin.glynn@twigletsoftware.com)
Licensed 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.
Author(s):
Kevin Glynn (kevin.glynn@twigletsoftware.com)
*/
package CS2JNet.System.LCC;
public interface IDisposable {
void dispose() throws Exception;
//void close() throws Exception;
}

View File

@ -0,0 +1,35 @@
/*
Copyright 2007,2008,2009,2010 Rustici Software, LLC
Copyright 2010,2011 Kevin Glynn (kevin.glynn@twigletsoftware.com)
Licensed 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.
Author(s):
Kevin Glynn (kevin.glynn@twigletsoftware.com)
*/
package CS2JNet.System.Xml.Serialization.LCC;
import CS2JNet.System.Xml.XmlReader;
import CS2JNet.System.Xml.XmlWriter;
public interface IXmlSerializable {
public Object getSchema() throws Throwable;
public void readXml(XmlReader reader) throws Throwable;
public void writeXml(XmlWriter writer) throws Throwable;
}

View File

@ -91,8 +91,7 @@ namespace Twiglet.CS2J.Translator
long startTime = DateTime.Now.Ticks;
IList<string> csDir = new List<string>();
XmlTextWriter enumXmlWriter = null;
AppEnv = new DirectoryHT<TypeRepTemplate>(null);
// Use a try/catch block for parser exceptions
try
{
@ -128,6 +127,7 @@ namespace Twiglet.CS2J.Translator
.Add ("exappdir=", dirs => addDirectories(cfg.ExAppRoot, dirs))
.Add ("csdir=", dirs => addDirectories(csDir, dirs))
.Add ("excsdir=", dirs => addDirectories(cfg.Exclude, dirs))
.Add ("alttranslations=", asub => cfg.AltTranslations.Add(asub))
.Add ("translator-keep-parens=", v => cfg.TranslatorKeepParens = Boolean.Parse(v))
.Add ("translator-timestamp-files=", v => cfg.TranslatorAddTimeStamp = Boolean.Parse(v))
.Add ("translator-blanket-throw=", v => cfg.TranslatorBlanketThrow = Boolean.Parse(v))
@ -152,6 +152,17 @@ namespace Twiglet.CS2J.Translator
Environment.Exit(0);
AppEnv = new DirectoryHT<TypeRepTemplate>();
foreach (string alt in cfg.AltTranslations)
{
AppEnv.Alts.Add(alt);
}
if (cfg.TranslatorMakeJavaNamingConventions && !AppEnv.Alts.Contains("LCC"))
{
// Search lowerCamelCase
AppEnv.Alts.Add("LCC");
}
// Initialise RSA signing key so that we can verify signatures
RsaKey = new RSACryptoServiceProvider();
string rsaPubXml = RSAPubKey.PubKey;

View File

@ -30,6 +30,9 @@ namespace Twiglet.CS2J.Translator
public IList<string> ExAppRoot { get; set; }
public IList<string> Exclude { get; set; }
public IList<string> MacroDefines { get; set; }
public IList<string> AltTranslations { get; set; }
public string XmlDir { get; set; }
public string EnumDir { get; set; }
public int Verbosity { get; set; }
@ -113,6 +116,7 @@ namespace Twiglet.CS2J.Translator
ExAppRoot = new List<string>();
Exclude = new List<string>();
MacroDefines = new List<string>();
AltTranslations = new List<string>();
XmlDir = Path.Combine(Directory.GetCurrentDirectory(), "tmpXMLs");
EnumDir = Path.Combine(Directory.GetCurrentDirectory(), "enums");
KeyFile = null;

View File

@ -380,7 +380,7 @@ scope TypeContext {
// if (conn3 != null)
// Disposable.mkDisposable(conn3).Dispose();
// used in the finally block of the using translation
protected CommonTree addDisposeVars(IToken tok, List<string> vars) {
protected CommonTree addDisposeVars(IToken tok, List<string> vars, string disposeMethod) {
CommonTree root = (CommonTree)adaptor.Nil;
@ -428,7 +428,7 @@ scope TypeContext {
adaptor.AddChild(root_3, root_4);
adaptor.AddChild(root_3, (CommonTree)adaptor.Create(IDENTIFIER, tok, "Dispose"));
adaptor.AddChild(root_3, (CommonTree)adaptor.Create(IDENTIFIER, tok, disposeMethod));
adaptor.AddChild(root_2, root_3);
@ -1998,8 +1998,10 @@ using_statement[bool isStatementListCtxt]
}:
// see http://msdn.microsoft.com/en-us/library/yh598w02.aspx for translation
u='using' '(' resource_acquisition c=')' embedded_statement[/* isStatementListCtxt */ false]
{ disposers = addDisposeVars($c.token, $resource_acquisition.resourceNames);
AddToImports("CS2JNet.System.Disposable"); }
{
disposers = addDisposeVars($c.token, $resource_acquisition.resourceNames, Cfg.TranslatorMakeJavaNamingConventions ? "dispose" : "Dispose");
AddToImports(String.Format("CS2JNet.System{0}Disposable", Cfg.TranslatorMakeJavaNamingConventions ? ".LCC." : "."));
}
f=magicFinally[$c.token, disposers]
magicTry[$u.token, state.backtracking == 0 ? embeddedStatementToBlock($u.token, $embedded_statement.tree) : null, null, $f.tree]
-> {!isStatementListCtxt}? OPEN_BRACE[$u.token, "{"] resource_acquisition SEMI[$c.token, ";"] magicTry CLOSE_BRACE[$u.token, "}"]
@ -2240,9 +2242,9 @@ magicTry[IToken tok, CommonTree body, CommonTree catches, CommonTree fin]:
^(TRY[tok, "try"] { dupTree(body) } { dupTree(catches) } { dupTree(fin) })
;
magicDispose[IToken tok, string var]:
magicDispose[IToken tok, string var, string disposeMethod]:
-> ^(IF[tok, "if"] ^(NOT_EQUAL[tok, "!="] IDENTIFIER[tok, var] NULL[tok, "null"]) SEP
^(APPLY[tok, "APPLY"] ^(DOT[tok,"."] ^(APPLY[tok, "APPLY"] ^(DOT[tok, "."] IDENTIFIER[tok, "Disposable"] IDENTIFIER[tok, "mkDisposable"]) ^(ARGS[tok,"ARGS"] IDENTIFIER[tok, var])) IDENTIFIER[tok, "Dispose"])) SEMI[tok, ";"])
^(APPLY[tok, "APPLY"] ^(DOT[tok,"."] ^(APPLY[tok, "APPLY"] ^(DOT[tok, "."] IDENTIFIER[tok, "Disposable"] IDENTIFIER[tok, "mkDisposable"]) ^(ARGS[tok,"ARGS"] IDENTIFIER[tok, var])) IDENTIFIER[tok, disposeMethod])) SEMI[tok, ";"])
;
magicFinally[IToken tok, CommonTree statement_list]:

View File

@ -19,9 +19,12 @@ namespace Twiglet.CS2J.Translator.Utils
private Dictionary<string, DirectoryHT<TValue>> children = new Dictionary<string, DirectoryHT<TValue>>();
private IList<string> alts = new List<string>();
public DirectoryHT(DirectoryHT<TValue> p)
{
_parent = p;
alts.Add("");
}
public DirectoryHT()
@ -40,6 +43,13 @@ namespace Twiglet.CS2J.Translator.Utils
get { return _parent; }
}
// When searching for A.B.C.D.E we will first search for each A.B.C.D.<alt>.E where <alt> comes from Alts
// This allows to override the default translations where necessary
public IList<string> Alts
{
get { return alts; }
}
// p is key to a sub directory
public DirectoryHT<TValue> subDir(string p)
{
@ -183,28 +193,60 @@ namespace Twiglet.CS2J.Translator.Utils
// search for name, given searchPath
// searchPath is searched in reverse order
// When searching for A.B.C.D.E we will first search for each A.B.C.D.<alt>.E where <alt> comes from Alts
// This allows to override the default translations where necessary
public TValue Search(IList<string> searchPath, string name, TValue def) {
TValue ret = def;
bool found = false;
// First check against each element of the search path
if (searchPath != null)
{
for (int i = searchPath.Count-1; i >= 0; i--) {
String ns = searchPath[i];
String fullName = (ns ?? "") + (String.IsNullOrEmpty(ns) ? "" : ".") + name;
if (this.ContainsKey(fullName)) {
ret = this[fullName];
found = true;
break;
}
}
// check against each alt override in turn
foreach (string altIterator in Alts) {
string alt = altIterator.EndsWith(".") ? altIterator : altIterator + ".";
for (int i = searchPath.Count-1; i >= 0; i--) {
String ns = searchPath[i];
String fullName = (ns ?? "") + (String.IsNullOrEmpty(ns) ? "" : ".") + name;
// insert alt after last period
int lastPeriod = fullName.LastIndexOf('.')+1;
fullName = fullName.Substring(0,lastPeriod) + alt + fullName.Substring(lastPeriod);
// Console.WriteLine(fullName);
if (this.ContainsKey(fullName)) {
return this[fullName];
}
}
}
// Not in alts, check kosher
for (int i = searchPath.Count-1; i >= 0; i--) {
String ns = searchPath[i];
String fullName = (ns ?? "") + (String.IsNullOrEmpty(ns) ? "" : ".") + name;
// Console.WriteLine(fullName);
if (this.ContainsKey(fullName)) {
return this[fullName];
}
}
}
// Check if name is fully qualified
if (!found && this.ContainsKey(name)) {
ret = this[name];
foreach (string altIterator in Alts) {
string alt = altIterator.EndsWith(".") ? altIterator : altIterator + ".";
// insert alt after last period
int lastPeriod = name.LastIndexOf('.')+1;
string fullName = name.Substring(0,lastPeriod) + alt + name.Substring(lastPeriod);
// Console.WriteLine(fullName);
if (this.ContainsKey(fullName)) {
return this[fullName];
}
}
// if (ret != null)
// Console.Out.WriteLine("findType: found {0}", ret.TypeName);
return ret;
// Not in alt, check kosher
// Console.WriteLine(name);
if (this.ContainsKey(name)) {
return this[name];
}
return def;
}
// search for name, given searchPath