diff --git a/CS2JLibrary/NetFramework/System/LCC/IDisposable.xml b/CS2JLibrary/NetFramework/System/LCC/IDisposable.xml new file mode 100644 index 0000000..3ca1c49 --- /dev/null +++ b/CS2JLibrary/NetFramework/System/LCC/IDisposable.xml @@ -0,0 +1,30 @@ + + + + + CS2JNet.System.LCC.IDisposable + + IDisposable + System.IDisposable + + System.Object + + + + ${this:16}.close() + + Dispose + System.Void + + + + + ++vVUUFftRdH5Pr1U9UgceoocL/c=iSpArQXUl74iZlhpxXJA0uxKNQP58ZhDGoiQafnENk6Sr2WkJrv1+FbrcL/VoqEvgT6EZ0lzvv1jbKJY0h6grUsn4CcN2J4MCttnAOCnemuxaVb00aASlLUwmN7InqdhebdjH4uFR7UUKm7qYq5EvAR095fu0A/zGWSJ7L/tXO4= diff --git a/CS2JLibrary/NetFramework/System/Xml/Serialization/LCC/IXmlSerializable.xml b/CS2JLibrary/NetFramework/System/Xml/Serialization/LCC/IXmlSerializable.xml new file mode 100644 index 0000000..e1eb192 --- /dev/null +++ b/CS2JLibrary/NetFramework/System/Xml/Serialization/LCC/IXmlSerializable.xml @@ -0,0 +1,31 @@ + + + + + CS2JNet.System.Xml.Serialization.LCC.IXmlSerializable + + IXmlSerializable + System.Xml.Serialization.IXmlSerializable + + System.Object + + + + + ${this:16}.getValue() + System.String + Value + ${this:16}.getValue() + ${this:16}.setValue(${value}) + + + + +SiUhORBUjQ/2aYjJrS7nmB9D7c8=B5pXm+f7e2+z7PlAmqp1DoJ3dIZnqxZGHs8b0anRWpYtPjIWm0kc4+KTC/n9uPU9+OINcskAJfSU6X4HDXhOq1mrDUaPTKC/dRBGdqCReA7T5geYxFv+xOM5Rge5hzkkZajUftben1I7BQVbsXhKfbujN18rNza2BrMAvMyZw1s= diff --git a/CS2JLibrary/src/CS2JNet/System/LCC/Disposable.java b/CS2JLibrary/src/CS2JNet/System/LCC/Disposable.java new file mode 100644 index 0000000..d70a6e9 --- /dev/null +++ b/CS2JLibrary/src/CS2JNet/System/LCC/Disposable.java @@ -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(); + } + +} diff --git a/CS2JLibrary/src/CS2JNet/System/LCC/IDisposable.java b/CS2JLibrary/src/CS2JNet/System/LCC/IDisposable.java new file mode 100644 index 0000000..c6d5786 --- /dev/null +++ b/CS2JLibrary/src/CS2JNet/System/LCC/IDisposable.java @@ -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; +} diff --git a/CS2JLibrary/src/CS2JNet/System/Xml/Serialization/LCC/IXmlSerializable.java b/CS2JLibrary/src/CS2JNet/System/Xml/Serialization/LCC/IXmlSerializable.java new file mode 100755 index 0000000..b39c66e --- /dev/null +++ b/CS2JLibrary/src/CS2JNet/System/Xml/Serialization/LCC/IXmlSerializable.java @@ -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; + +} diff --git a/CSharpTranslator/antlr3/src/CS2JTranslator/CS2JMain/CS2JMain.cs b/CSharpTranslator/antlr3/src/CS2JTranslator/CS2JMain/CS2JMain.cs index 92f87ff..afb38ce 100644 --- a/CSharpTranslator/antlr3/src/CS2JTranslator/CS2JMain/CS2JMain.cs +++ b/CSharpTranslator/antlr3/src/CS2JTranslator/CS2JMain/CS2JMain.cs @@ -91,8 +91,7 @@ namespace Twiglet.CS2J.Translator long startTime = DateTime.Now.Ticks; IList csDir = new List(); XmlTextWriter enumXmlWriter = null; - AppEnv = new DirectoryHT(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(); + 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; diff --git a/CSharpTranslator/antlr3/src/CS2JTranslator/CS2JMain/CS2JSettings.cs b/CSharpTranslator/antlr3/src/CS2JTranslator/CS2JMain/CS2JSettings.cs index c850205..965fcdd 100644 --- a/CSharpTranslator/antlr3/src/CS2JTranslator/CS2JMain/CS2JSettings.cs +++ b/CSharpTranslator/antlr3/src/CS2JTranslator/CS2JMain/CS2JSettings.cs @@ -30,6 +30,9 @@ namespace Twiglet.CS2J.Translator public IList ExAppRoot { get; set; } public IList Exclude { get; set; } public IList MacroDefines { get; set; } + + public IList 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(); Exclude = new List(); MacroDefines = new List(); + AltTranslations = new List(); XmlDir = Path.Combine(Directory.GetCurrentDirectory(), "tmpXMLs"); EnumDir = Path.Combine(Directory.GetCurrentDirectory(), "enums"); KeyFile = null; diff --git a/CSharpTranslator/antlr3/src/CS2JTranslator/CS2JTransform/JavaMaker.g b/CSharpTranslator/antlr3/src/CS2JTranslator/CS2JTransform/JavaMaker.g index 8c8f6ea..d01b753 100644 --- a/CSharpTranslator/antlr3/src/CS2JTranslator/CS2JTransform/JavaMaker.g +++ b/CSharpTranslator/antlr3/src/CS2JTranslator/CS2JTransform/JavaMaker.g @@ -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 vars) { + protected CommonTree addDisposeVars(IToken tok, List 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]: diff --git a/CSharpTranslator/antlr3/src/CS2JTranslator/CS2JUtil/DirectoryHT.cs b/CSharpTranslator/antlr3/src/CS2JTranslator/CS2JUtil/DirectoryHT.cs index d2ad0fc..2516119 100644 --- a/CSharpTranslator/antlr3/src/CS2JTranslator/CS2JUtil/DirectoryHT.cs +++ b/CSharpTranslator/antlr3/src/CS2JTranslator/CS2JUtil/DirectoryHT.cs @@ -19,9 +19,12 @@ namespace Twiglet.CS2J.Translator.Utils private Dictionary> children = new Dictionary>(); + private IList alts = new List(); + public DirectoryHT(DirectoryHT 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..E where comes from Alts + // This allows to override the default translations where necessary + public IList Alts + { + get { return alts; } + } + // p is key to a sub directory public DirectoryHT 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..E where comes from Alts + // This allows to override the default translations where necessary public TValue Search(IList 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