public static int ConvertResSchema(XmlDocument doc, CompatibleCLRVersion version, bool verbose) { int changes = 0; foreach (XmlNode sn in doc.SelectNodes("/root/xsd:schema", GetNamespaceManager(doc))) { if (version == CompatibleCLRVersion.Version_1_0 || version == CompatibleCLRVersion.Version_1_1) { sn.InnerXml = res_schema_1_x; changes++; } else if (version == CompatibleCLRVersion.Version_2_0) { XmlNode newNode = doc.CreateElement("xsd", "schema", "http://www.w3.org/2001/XMLSchema"); XmlAttribute a = doc.CreateAttribute("id", null); a.Value = "root"; newNode.Attributes.Append(a); sn.ParentNode.InsertBefore(newNode, sn); newNode.InnerXml = res_schema_2_0; // fails with unknown namespace? sn.ParentNode.RemoveChild(sn); changes++; } } return(changes); }
public bool Run(bool testOnly, CompatibleCLRVersion version) { if (!File.Exists(resource)) { return(ExitWith(new ArgumentException(String.Format("File does not exist: '{0}'", resource)))); } ArrayList resources = new ArrayList(); resources.Add(resource); string baseName = Path.GetFileNameWithoutExtension(resource); string[] languageResources = Directory.GetFiles(Path.GetDirectoryName(resource), baseName + ".*.resx"); if (languageResources != null && languageResources.Length > 0) { resources.AddRange(languageResources); } bool isWindowsFormsResource = false; XmlDocument docSource = new XmlDocument(); docSource.PreserveWhitespace = true; docSource.XmlResolver = null; try { docSource.Load(resource); isWindowsFormsResource = (null != docSource.SelectSingleNode("/root/data[@name='>>$this.Type']")); } catch (Exception ex) { return(ExitWith(ex)); } foreach (string langResFile in resources) { XmlDocument docToConvert = new XmlDocument(); docToConvert.PreserveWhitespace = true; docToConvert.XmlResolver = null; try { Console.WriteLine(" + {0} ", Path.GetFileName(langResFile)); docToConvert.Load(langResFile); int changes = ConvertResSchema(docToConvert, version, testOnly); if (isWindowsFormsResource) { try { changes += ConvertResHeader(docToConvert, version, testOnly); changes += ConvertResTypes(docToConvert, version, testOnly); } catch (Exception taskEx) { return(ExitWith(taskEx)); } } else { try { changes += ConvertResHeader(docToConvert, version, testOnly); changes += ConvertResTypes(docToConvert, version, testOnly); } catch (Exception taskEx) { return(ExitWith(taskEx)); } } if (!testOnly && (changes != 0)) { File.Copy(langResFile, langResFile + ".bak", true); docToConvert.Save(langResFile); } Console.WriteLine("\tconversions:{0}", changes); } catch (Exception ex) { return(ExitWith(ex)); } } return(true); // success }
public static int ConvertResHeader(XmlDocument doc, CompatibleCLRVersion version, bool verbose) { int changes = 0; foreach (XmlNode sn in doc.SelectNodes("/root/resheader")) { XmlNode name = sn.Attributes.GetNamedItem("name"); string idName = name.InnerText; XmlNode srcValue = sn.SelectSingleNode("value"); if (0 == String.Compare(idName, "version", StringComparison.InvariantCultureIgnoreCase)) { if (version == CompatibleCLRVersion.Version_1_0) { if ("1.0.0.0" != srcValue.InnerText) { srcValue.InnerText = "1.0.0.0"; changes++; } } else if (version == CompatibleCLRVersion.Version_1_1) { if ("1.0.0.0" != srcValue.InnerText) { srcValue.InnerText = "1.0.0.0"; changes++; } } else if (version == CompatibleCLRVersion.Version_2_0) { if ("2.0" != srcValue.InnerText) { srcValue.InnerText = "2.0"; changes++; } } } if (0 == String.Compare(idName, "reader", StringComparison.InvariantCultureIgnoreCase)) { string v = GetVersion(srcValue.InnerText); if (version == CompatibleCLRVersion.Version_1_0) { if (v != null && v != "1.0.3102.0") { srcValue.InnerText = srcValue.InnerText.Replace(v, "1.0.3102.0"); changes++; } } else if (version == CompatibleCLRVersion.Version_1_1) { if (v != null && v != "1.0.5000.0") { srcValue.InnerText = srcValue.InnerText.Replace(v, "1.0.5000.0"); changes++; } } else if (version == CompatibleCLRVersion.Version_2_0) { if (v != null && v != "2.0.0.0") { srcValue.InnerText = srcValue.InnerText.Replace(v, "2.0.0.0"); changes++; } } } if (0 == String.Compare(idName, "writer", StringComparison.InvariantCultureIgnoreCase)) { string v = GetVersion(srcValue.InnerText); if (version == CompatibleCLRVersion.Version_1_0) { if (v != null && v != "1.0.3102.0") { srcValue.InnerText = srcValue.InnerText.Replace(v, "1.0.3102.0"); changes++; } } else if (version == CompatibleCLRVersion.Version_1_1) { if (v != null && v != "1.0.5000.0") { srcValue.InnerText = srcValue.InnerText.Replace(v, "1.0.5000.0"); changes++; } } else if (version == CompatibleCLRVersion.Version_2_0) { if (v != null && v != "2.0.0.0") { srcValue.InnerText = srcValue.InnerText.Replace(v, "2.0.0.0"); changes++; } } } } return(changes); }
public static int ConvertResTypes(XmlDocument doc, CompatibleCLRVersion version, bool verbose) { int changes = 0; foreach (XmlNode sn in doc.SelectNodes("/root/data", GetNamespaceManager(doc))) { XmlAttribute name = sn.Attributes["name"]; XmlAttribute type = sn.Attributes["type"]; XmlAttribute space = sn.Attributes["xml:space"]; XmlNode srcValue = sn.SelectSingleNode("value"); if (name != null && name.InnerText.StartsWith(">>") && name.InnerText.EndsWith(".Type")) { string v = GetVersion(srcValue.InnerText); string t = GetPublicKeyToken(srcValue.InnerText); // convert only CLR framework types (Microsoft): if (t == "b03f5f7f11d50a3a" || t == "b77a5c561934e089") { if (RepairTypePublicKeyToken(srcValue, t)) { changes++; } if (version == CompatibleCLRVersion.Version_1_0) { if (v != null && v != "1.0.3300.0") { srcValue.InnerText = srcValue.InnerText.Replace(v, "1.0.3300.0"); changes++; } } else if (version == CompatibleCLRVersion.Version_1_1) { if (v != null && v != "1.0.5000.0") { srcValue.InnerText = srcValue.InnerText.Replace(v, "1.0.5000.0"); changes++; } } else if (version == CompatibleCLRVersion.Version_2_0) { //TODO: remove the ", Version=..., Culture=... " part //TODO: add assembly aliases } } } if (type != null) { string v = GetVersion(type.InnerText); string t = GetPublicKeyToken(type.InnerText); // convert only CLR framework types (Microsoft): if (t == "b03f5f7f11d50a3a" || t == "b77a5c561934e089") { if (RepairTypePublicKeyToken(type, t)) { changes++; } if (version == CompatibleCLRVersion.Version_1_0) { if (v != null && v != "1.0.3300.0") { type.InnerText = type.InnerText.Replace(v, "1.0.3300.0"); changes++; } } else if (version == CompatibleCLRVersion.Version_1_1) { if (v != null && v != "1.0.5000.0") { type.InnerText = type.InnerText.Replace(v, "1.0.5000.0"); changes++; } } else if (version == CompatibleCLRVersion.Version_2_0) { //TODO: remove the ", Version=..., Culture=... " part //TODO: add assembly aliases } } } if (space != null) { if (version == CompatibleCLRVersion.Version_1_0 || version == CompatibleCLRVersion.Version_1_1) { sn.Attributes.Remove(space); changes++; } } if (srcValue != null && srcValue.Attributes["xml:space"] != null) { if (version == CompatibleCLRVersion.Version_1_0 || version == CompatibleCLRVersion.Version_1_1) { XmlAttribute srcSp = srcValue.Attributes["xml:space"]; srcValue.Attributes.Remove(srcSp); changes++; } } } if (version == CompatibleCLRVersion.Version_1_0 || version == CompatibleCLRVersion.Version_1_1) { foreach (XmlNode sn in doc.SelectNodes("/root/assembly")) { doc.DocumentElement.RemoveChild(sn); changes++; } } return(changes); }
static void Main(string[] args) { if (args == null || args.Length == 0) { Console.WriteLine(@"missing parameter: path to main resource file (e.g. 'C:\Projects\My\myproject.resx')"); return; } bool convert = false; CompatibleCLRVersion version = CompatibleCLRVersion.Version_1_0; bool testOnly = false; bool cleanup = false; bool makeStrings = false; foreach (string path in args) { if (path == "-?" || path == "/?") { WriteUsage(); break; } if (path == "-t" || path == "/t") { testOnly = true; continue; } if (path == "-c" || path == "/c") { convert = true; continue; } if (path == "-d" || path == "/d") { cleanup = true; continue; } if (path == "-s" || path == "/s") { makeStrings = true; continue; } if (convert) { if (path == "1.0") { version = CompatibleCLRVersion.Version_1_0; continue; } else if (path == "1.1") { version = CompatibleCLRVersion.Version_1_1; continue; } else if (path == "2.0") { version = CompatibleCLRVersion.Version_2_0; continue; } } string fp = Path.GetFullPath(path); if (!File.Exists(path)) { if (File.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, path))) { fp = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, path); } } if (makeStrings) { StringsFromResources sfr = new StringsFromResources(fp); Console.WriteLine("Create .strings from {0} ... ", fp); if (!sfr.Run(testOnly)) { Console.WriteLine("failed: {0}", sfr.ExecutionException.Message); } else { Console.WriteLine("succeeds."); } break; } DiffPatchTasks tasks = new DiffPatchTasks(fp); Console.WriteLine("Diff/Patch process {0} ... ", fp); if (!tasks.Run(testOnly)) { Console.WriteLine("failed: {0}", tasks.ExecutionException.Message); } else { Console.WriteLine("succeeds."); } if (convert) { ConvertResources cr = new ConvertResources(fp); Console.WriteLine("Conversion process to {0} of {1} ... ", version, fp); if (!cr.Run(testOnly, version)) { Console.WriteLine("failed: {0}", cr.ExecutionException.Message); } else { Console.WriteLine("succeeds."); } } if (cleanup) { CleanupResources cr = new CleanupResources(fp); Console.WriteLine("Cleanup processing of {0}... ", fp); if (!cr.Run(testOnly)) { Console.WriteLine("failed: {0}", cr.ExecutionException.Message); } else { Console.WriteLine("succeeds."); } } } Console.WriteLine("Press a key..."); Console.ReadLine(); }