/// <include file='doc\ResXResourceWriter.uex' path='docs/doc[@for="ResXResourceWriter.AddResource3"]/*' /> /// <devdoc> /// Adds a string resource to the resources. /// </devdoc> public void AddResource(ResXDataNode node) { // we're modifying the node as we're adding it to the resxwriter // this is BAD, so we clone it. adding it to a writer doesnt change it // we're messing with a copy ResXDataNode nodeClone = node.DeepClone(); ResXFileRef fileRef = nodeClone.FileRef; string modifiedBasePath = BasePath; if (!String.IsNullOrEmpty(modifiedBasePath)) { if (!(modifiedBasePath.EndsWith("\\"))) { modifiedBasePath += "\\"; } if (fileRef != null) { fileRef.MakeFilePathRelative(modifiedBasePath); } } DataNodeInfo info = nodeClone.GetDataNodeInfo(); AddDataRow(DataStr, info.Name, info.ValueData, info.TypeName, info.MimeType, info.Comment); }
/// <devdoc> /// Adds a resource to the resources. If the resource is a string, /// it will be saved that way, otherwise it will be serialized /// and stored as in binary. /// </devdoc> private void AddDataRow(string elementName, string name, object value) { Debug.WriteLineIf(ResValueProviderSwitch.TraceVerbose, " resx: adding resource " + name); if (value is string) { AddDataRow(elementName, name, (string)value); } else if (value is byte[]) { AddDataRow(elementName, name, (byte[])value); } else if (value is ResXFileRef) { ResXFileRef fileRef = (ResXFileRef)value; ResXDataNode node = new ResXDataNode(name, fileRef, this.typeNameConverter); if (fileRef != null) { fileRef.MakeFilePathRelative(BasePath); } DataNodeInfo info = node.GetDataNodeInfo(); AddDataRow(elementName, info.Name, info.ValueData, info.TypeName, info.MimeType, info.Comment); } else { ResXDataNode node = new ResXDataNode(name, value, this.typeNameConverter); DataNodeInfo info = node.GetDataNodeInfo(); AddDataRow(elementName, info.Name, info.ValueData, info.TypeName, info.MimeType, info.Comment); } }
public void FileRef() { ResXFileRef fileRef = new ResXFileRef("fileName", "Type.Name"); ResXDataNode node = new ResXDataNode("name", fileRef); Assert.AreEqual(fileRef, node.FileRef, "#A1"); }
public ResXNode(string name, ResXFileRef fileRef, string comment, string typeName) { Name = name; ObjectValue = fileRef; Comment = comment; TypeName = typeName; }
public ResXNode CreateNode(FilePath absPath, Type type) { FilePath relPath = absPath.ToRelative(((FilePath)Path).ParentDirectory); var fileRef = new ResXFileRef(relPath, type.AssemblyQualifiedName); return(new ResXDataNode(absPath.FileNameWithoutExtension, fileRef)); }
public ResXDataNode GetNodeFileRefToIcon() { ResXFileRef fileRef = new ResXFileRef(TestResourceHelper.GetFullPathOfResource("Test/resources/32x32.ico"), typeof(Icon).AssemblyQualifiedName); ResXDataNode node = new ResXDataNode("test", fileRef); return(node); }
public void Constructor1() { ResXFileRef r = new ResXFileRef("mono.bmp", "Bitmap"); MonoTests.System.Windows.Forms.TestHelper.RemoveWarning(r); Assert.AreEqual("mono.bmp", r.FileName, "#1"); Assert.AreEqual("Bitmap", r.TypeName, "#2"); }
public void IfTypeResolutionFailsReturnsOrigString() { ResXFileRef fileRef = new ResXFileRef("afile.name", "a.type.name"); ResXDataNode node = new ResXDataNode("aname", fileRef); string returnedType = node.GetValueTypeName((AssemblyName [])null); Assert.AreEqual("a.type.name", returnedType); }
public void AttemptsTypeResolution() { ResXFileRef fileRef = new ResXFileRef("afile.name", "System.String"); ResXDataNode node = new ResXDataNode("aname", fileRef); string returnedType = node.GetValueTypeName((AssemblyName [])null); Assert.AreEqual(typeof(string).AssemblyQualifiedName, returnedType); }
public void ResxDataNode_ResXFileRefConstructor() { var nodeName = "Node"; var fileRef = new ResXFileRef(string.Empty, string.Empty); var dataNode = new ResXDataNode(nodeName, fileRef); Assert.Equal(nodeName, dataNode.Name); Assert.Same(fileRef, dataNode.FileRef); }
public void LoadingFileFails() { string corruptFile = Path.GetTempFileName(); ResXFileRef fileRef = new ResXFileRef(corruptFile, typeof(serializable).AssemblyQualifiedName); File.AppendAllText(corruptFile, "corrupt"); ResXDataNode node = new ResXDataNode("aname", fileRef); node.GetValue((AssemblyName [])null); }
public void ResXFileRef_Constructor() { var fileName = "SomeFile"; var typeName = "SomeType"; var fileRef = new ResXFileRef(fileName, typeName); Assert.Equal(fileName, fileRef.FileName); Assert.Equal(typeName, fileRef.TypeName); Assert.Null(fileRef.TextFileEncoding); }
public ResXDataNode GetNodeFileRefToIcon() { tempFileWithIcon = Path.GetTempFileName(); // remember to delete file in teardown Path.ChangeExtension(tempFileWithIcon, "ico"); WriteEmbeddedResource("32x32.ico", tempFileWithIcon); ResXFileRef fileRef = new ResXFileRef(tempFileWithIcon, typeof(Icon).AssemblyQualifiedName); ResXDataNode node = new ResXDataNode("test", fileRef); return(node); }
public object GetValue(ResXNode node) { if (node.ObjectValue is ResXFileRef fileRef) { var absolutePath = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(Path), fileRef.FileName); var absoluteRef = new ResXFileRef(absolutePath, fileRef.TypeName, fileRef.TextFileEncoding); var newNode = new ResXDataNode(node.Name, absoluteRef).GetValue(Constants.DefaultResolutionService); return(newNode); } return(node.ObjectValue); }
public void ToStringTest() { ResXFileRef r = new ResXFileRef("mono.bmp", "Bitmap"); Assert.AreEqual("mono.bmp;Bitmap", r.ToString(), "#1"); r = new ResXFileRef("mono.bmp", "Bitmap", Encoding.UTF8); Assert.AreEqual("mono.bmp;Bitmap;utf-8", r.ToString(), "#2"); r = new ResXFileRef("mono.bmp", "Bitmap", (Encoding)null); Assert.AreEqual("mono.bmp;Bitmap", r.ToString(), "#3"); }
public void ResXFileRef_EncodingConstructor() { var fileName = "SomeFile"; var typeName = "SomeType"; Encoding encoding = Encoding.Default; var fileRef = new ResXFileRef(fileName, typeName, encoding); Assert.Equal(fileName, fileRef.FileName); Assert.Equal(typeName, fileRef.TypeName); Assert.Equal(encoding, fileRef.TextFileEncoding); }
public void ConvertTo() { ResXFileRef r = new ResXFileRef("mono.bmp", "Bitmap"); Assert.AreEqual("mono.bmp;Bitmap", (string)_converter.ConvertTo( r, typeof(string)), "#1"); r = new ResXFileRef("mono.bmp", "Bitmap", Encoding.UTF8); Assert.AreEqual("mono.bmp;Bitmap;utf-8", (string)_converter.ConvertTo( r, typeof(string)), "#2"); r = new ResXFileRef("mono.bmp", "Bitmap", (Encoding)null); Assert.AreEqual("mono.bmp;Bitmap", (string)_converter.ConvertTo( r, typeof(string)), "#3"); }
public void Constructor2() { Encoding utf8 = Encoding.UTF8; ResXFileRef r = new ResXFileRef("mono.bmp", "Bitmap", utf8); Assert.AreEqual("mono.bmp", r.FileName, "#A1"); Assert.AreSame(utf8, r.TextFileEncoding, "#A2"); Assert.AreEqual("Bitmap", r.TypeName, "#A3"); r = new ResXFileRef("mono.bmp", "Bitmap", (Encoding)null); Assert.AreEqual("mono.bmp", r.FileName, "#B1"); Assert.IsNull(r.TextFileEncoding, "#B2"); Assert.AreEqual("Bitmap", r.TypeName, "#B3"); }
/// <include file='doc\ResXDataNode.uex' path='docs/doc[@for="ResXDataNode.GetValue"]/*' /> /// <devdoc> /// Get the value contained in this datanode /// </devdoc> public object GetValue(ITypeResolutionService typeResolver) { if (value != null) { return(value); } object result = null; if (FileRefFullPath != null) { Type objectType = ResolveType(FileRefType, typeResolver); if (objectType != null) { // we have the FQN for this type if (FileRefTextEncoding != null) { fileRef = new ResXFileRef(FileRefFullPath, FileRefType, Encoding.GetEncoding(FileRefTextEncoding)); } else { fileRef = new ResXFileRef(FileRefFullPath, FileRefType); } TypeConverter tc = TypeDescriptor.GetConverter(typeof(ResXFileRef)); result = tc.ConvertFrom(fileRef.ToString()); } else { string newMessage = SR.GetString(SR.TypeLoadExceptionShort, FileRefType); TypeLoadException newTle = new TypeLoadException(newMessage); throw (newTle); } } else if (result == null && nodeInfo.ValueData != null) { // it's embedded, we deserialize it result = GenerateObjectFromDataNodeInfo(nodeInfo, typeResolver); } else { // schema is wrong and say minOccur for Value is 0, // but it's too late to change it... // we need to return null here vswhidbey 180605 return(null); } return(result); }
public ResXDataNode(string name, ResXFileRef fileRef, Func <Type, string> typeNameConverter) { if (name == null) { throw (new ArgumentNullException("name")); } if (fileRef == null) { throw (new ArgumentNullException("fileRef")); } if (name.Length == 0) { throw (new ArgumentException("name")); } this.name = name; this.fileRef = fileRef; this.typeNameConverter = typeNameConverter; }
public void Constructor1_TypeName_Null() { #if NET_2_0 try { new ResXFileRef("mono.bmp", (string)null); Assert.Fail("#1"); } catch (ArgumentNullException ex) { Assert.AreEqual(typeof(ArgumentNullException), ex.GetType(), "#2"); Assert.IsNotNull(ex.Message, "#3"); Assert.IsNotNull(ex.ParamName, "#4"); Assert.AreEqual("typeName", ex.ParamName, "#5"); Assert.IsNull(ex.InnerException, "#6"); } #else ResXFileRef r = new ResXFileRef("mono.bmp", (string)null); Assert.AreEqual("mono.bmp;", r.ToString()); #endif }
public void BasePathSetOnResXResourceReaderDoesAffectResXDataNode() { ResXFileRef fileRef = new ResXFileRef("file.name", "type.name"); ResXDataNode node = new ResXDataNode("anode", fileRef); string resXFile = GetResXFileWithNode(node, "afilename.xxx"); using (ResXResourceReader rr = new ResXResourceReader(resXFile)) { rr.BasePath = "basePath"; rr.UseResXDataNodes = true; IDictionaryEnumerator en = rr.GetEnumerator(); en.MoveNext(); ResXDataNode returnedNode = ((DictionaryEntry)en.Current).Value as ResXDataNode; Assert.IsNotNull(node, "#A1"); Assert.AreEqual(Path.Combine("basePath", "file.name"), returnedNode.FileRef.FileName, "#A2"); } }
/// <summary> /// Given a .RESX file, returns all the linked files that are referenced within that .RESX. /// </summary> /// <param name="filename"></param> /// <param name="baseLinkedFileDirectory"></param> /// <returns></returns> /// <exception cref="ArgumentException">May be thrown if Resx is invalid. May contain XmlException.</exception> /// <exception cref="XmlException">May be thrown if Resx is invalid</exception> internal static string[] GetLinkedFiles(string filename, string baseLinkedFileDirectory) { // This method finds all linked .resx files for the .resx file that is passed in. // filename is the filename of the .resx file that is to be examined. // Construct the return array ArrayList retVal = new ArrayList(); #if !EVERETT_BUILD using (ResXResourceReader resxReader = new ResXResourceReader(filename)) { // Tell the reader to return ResXDataNode's instead of the object type // the resource becomes at runtime so we can figure out which files // the .resx references resxReader.UseResXDataNodes = true; // First we need to figure out where the linked file resides in order // to see if it exists & compare its timestamp, and we need to do that // comparison in the same way ResGen does it. ResGen has two different // ways for resolving relative file-paths in linked files. The way // that ResGen resolved relative paths before Whidbey was always to // resolve from the current working directory. In Whidbey a new command-line // switch "/useSourcePath" instructs ResGen to use the folder that // contains the .resx file as the path from which it should resolve // relative paths. So we should base our timestamp/existence checking // on the same switch & resolve in the same manner as ResGen. resxReader.BasePath = (baseLinkedFileDirectory == null) ? Path.GetDirectoryName(filename) : baseLinkedFileDirectory; foreach (DictionaryEntry dictEntry in resxReader) { if ((dictEntry.Value != null) && (dictEntry.Value is ResXDataNode)) { ResXFileRef resxFileRef = ((ResXDataNode)dictEntry.Value).FileRef; if (resxFileRef != null) { retVal.Add(resxFileRef.FileName); } } } } #endif return((string[])retVal.ToArray(typeof(string))); }
public void FileRefWithEncoding_WriteBack() { ResXFileRef fileRef = new ResXFileRef("afilename", "A.Type.Name", Encoding.UTF7); ResXDataNode node = new ResXDataNode("aname", fileRef); node.Comment = "acomment"; ResXDataNode returnedNode = GetNodeFromResXReader(node); Assert.IsNotNull(returnedNode, "#A1"); Assert.AreEqual("afilename", returnedNode.FileRef.FileName, "#A2"); Assert.AreEqual("A.Type.Name", returnedNode.FileRef.TypeName, "#A3"); Assert.AreEqual("acomment", returnedNode.Comment, "#A4"); Assert.IsInstanceOfType(Encoding.UTF7.GetType(), returnedNode.FileRef.TextFileEncoding, "#A5"); ResXDataNode finalNode = GetNodeFromResXReader(returnedNode); Assert.IsNotNull(finalNode, "#A6"); Assert.AreEqual("afilename", finalNode.FileRef.FileName, "#A7"); Assert.AreEqual("A.Type.Name", finalNode.FileRef.TypeName, "#A8"); Assert.AreEqual("acomment", finalNode.Comment, "#A9"); Assert.IsInstanceOfType(Encoding.UTF7.GetType(), finalNode.FileRef.TextFileEncoding, "#A10"); }
public void BasePath_ChangesAbsoluteFileRef_Object() { var fileref = new ResXFileRef(@"/dir/dir/filename.ext", "System.String"); string basePath = @"/dir"; StringWriter sw = new StringWriter(); using (ResXResourceWriter writer = new ResXResourceWriter(sw)) { writer.BasePath = basePath; writer.AddResource("name", fileref); } StringReader sr = new StringReader(sw.ToString()); using (ResXResourceReader reader = new ResXResourceReader(sr)) { reader.UseResXDataNodes = true; IDictionaryEnumerator enumerator = reader.GetEnumerator(); enumerator.MoveNext(); var returnedNode = ((DictionaryEntry)enumerator.Current).Value as ResXDataNode; Assert.AreEqual(@"dir/filename.ext", returnedNode.FileRef.FileName); } }
public ResXDataNode GetNodeFileRefToSerializable(string filename, bool assemblyQualifiedName) { tempFileWithSerializable = Path.GetTempFileName(); // remember to delete file in teardown serializable ser = new serializable("name", "value"); SerializeToFile(tempFileWithSerializable, ser); string typeName; if (assemblyQualifiedName) { typeName = typeof(serializable).AssemblyQualifiedName; } else { typeName = typeof(serializable).FullName; } ResXFileRef fileRef = new ResXFileRef(tempFileWithSerializable, typeName); ResXDataNode node = new ResXDataNode("test", fileRef); return(node); }
private byte[] GenerateResource() { using (var writer = new StringWriter()) using (var resx = new ResXResourceWriter(writer)) { resx.AddAlias("System.Windows.Forms", new AssemblyName("System.Windows.Forms")); resx.AddResource(new ResXDataNode("110", _manifest.Name)); resx.AddResource(new ResXDataNode("112", _manifest.Description)); if (!string.IsNullOrEmpty(_manifest.Icon)) { string iconFileName = Path.GetFileNameWithoutExtension(InputFilePath) + ".ico"; var fileRef = new ResXFileRef(iconFileName, "System.Drawing.Icon, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"); resx.AddResource(new ResXDataNode("400", fileRef)); } resx.Generate(); writer.Flush(); var sb = writer.GetStringBuilder(); return(Encoding.UTF8.GetBytes(sb.ToString())); } }
public ResXDataNode(string name, ResXFileRef fileRef) { }
/// <include file='doc\ResXDataNode.uex' path='docs/doc[@for="ResXDataNode.ResXDataNode2"]/*' /> /// <devdoc> /// /// </devdoc> public ResXDataNode(string name, ResXFileRef fileRef) : this(name, fileRef, null) { }
public ResXDataNode(string name, ResXFileRef fileRef) { }
public static bool Export(IServiceProvider serviceProvider, string project, string module, string fName, string language, string exportPath, string key = null) { var filter = new ResCurrent { Project = new ResProject { Name = project }, Module = new ResModule { Name = module }, Language = new ResCulture { Title = language }, Word = new ResWord() { ResFile = new ResFile() { FileName = fName } } }; using var scope = serviceProvider.CreateScope(); var resourceData = scope.ServiceProvider.GetService <ResourceData>(); var words = resourceData.GetListResWords(filter, string.Empty).GroupBy(x => x.ResFile.FileID).ToList(); if (!words.Any()) { Console.WriteLine($"db empty file:{fName}, lang:{language}"); return(false); } foreach (var fileWords in words) { var wordsDictionary = new Dictionary <string, object>(); var firstWord = fileWords.FirstOrDefault(); var fileName = firstWord == null ? module : Path.GetFileNameWithoutExtension(firstWord.ResFile.FileName); var zipFileName = Path.Combine(exportPath, $"{fileName}{(language == "Neutral" ? string.Empty : "." + language)}.resx"); var dirName = Path.GetDirectoryName(zipFileName); if (!Directory.Exists(dirName)) { Directory.CreateDirectory(dirName); } var toAdd = new List <ResWord>(); var toAddFiles = new Dictionary <string, ResXFileRef>(); if (!string.IsNullOrEmpty(key)) { var keys = key.Split(",").Distinct(); if (File.Exists(zipFileName)) { using var resXResourceReader = new ResXResourceReader(zipFileName); resXResourceReader.BasePath = Path.GetDirectoryName(zipFileName); resXResourceReader.UseResXDataNodes = true; foreach (var v in resXResourceReader.OfType <DictionaryEntry>()) { var k = v.Key.ToString(); var val = v.Value as ResXDataNode; if (keys.Any()) { if (val.FileRef != null) { var fileRef = new ResXFileRef(Path.GetFileName(val.FileRef.FileName), val.FileRef.TypeName); toAddFiles.Add(k, fileRef); } else { if (!keys.Any(r => r.EndsWith("*") && k.StartsWith(r.Replace("*", ""))) && (!k.Contains("_") || k.StartsWith("subject_") || k.StartsWith("pattern_"))) { k = keys.FirstOrDefault(r => r == k); } if (k != null) { var word = fileWords.FirstOrDefault(r => r.Title == k); if (word != null) { toAdd.Add(word); } else { toAdd.Add(new ResWord() { Title = k, ValueTo = val.GetValue((ITypeResolutionService)null)?.ToString() }); } } } } else { if (val.FileRef != null) { var fileRef = new ResXFileRef(Path.GetFileName(val.FileRef.FileName), val.FileRef.TypeName); toAddFiles.Add(k, fileRef); } else { toAdd.Add(new ResWord { Title = k, ValueTo = val.GetValue((ITypeResolutionService)null)?.ToString() }); } } } } foreach (var k in keys) { if (!toAdd.Any(r => r.Title == k)) { var exists = fileWords.FirstOrDefault(r => r.Title == k); if (exists != null) { toAdd.Add(exists); } } } } else { if (File.Exists(zipFileName)) { using var resXResourceReader = new ResXResourceReader(zipFileName); resXResourceReader.BasePath = Path.GetDirectoryName(zipFileName); resXResourceReader.UseResXDataNodes = true; foreach (var v in resXResourceReader.OfType <DictionaryEntry>()) { var k = v.Key.ToString(); var val = v.Value as ResXDataNode; if (val.FileRef != null) { var fileRef = new ResXFileRef(Path.GetFileName(val.FileRef.FileName), val.FileRef.TypeName); toAddFiles.Add(k, fileRef); } } } toAdd.AddRange(fileWords.Where(word => !wordsDictionary.ContainsKey(word.Title))); } using var resXResourceWriter = new ResXResourceWriter(zipFileName); foreach (var word in toAdd.Where(r => r != null && (!string.IsNullOrEmpty(r.ValueTo) || language == "Neutral")).OrderBy(x => x.Title)) { resXResourceWriter.AddResource(word.Title, word.ValueTo); } foreach (var f in toAddFiles) { resXResourceWriter.AddResource(new ResXDataNode(f.Key, f.Value)); } resXResourceWriter.Generate(); resXResourceWriter.Close(); } return(true); }