Beispiel #1
0
 //--------------------------------
 // private function
 //--------------------------------
 /// <summary>
 /// Enumerate baml streams in a resources file
 /// </summary>
 private void EnumerateBamlInResources(ResourceReader reader, string resourceName, bool isMsBamlMode)
 {
     foreach (DictionaryEntry entry in reader)
     {
         var name = entry.Key as string;
         if (BamlStream.IsResourceEntryBamlStream(name, entry.Value))
         {
             bamlStreams.Add(
                 new BamlStream(
                     BamlStream.CombineBamlStreamName(resourceName, name, isMsBamlMode),
                     (Stream)entry.Value
                     )
                 );
         }
     }
 }
Beispiel #2
0
        private void GenerateResourceStream(
            string resourceName,                           // the name of the .resources file
            IResourceReader reader,                        // the reader for the .resources
            IResourceWriter writer                         // the writer for the output .resources
            )
        {
            options.WriteLine(StringLoader.Get("GenerateResource", resourceName));
            // enumerate through each resource and generate it
            foreach (DictionaryEntry entry in reader)
            {
                var    name          = entry.Key as string;
                object resourceValue = null;

                // See if it looks like a Baml resource
                if (BamlStream.IsResourceEntryBamlStream(name, entry.Value))
                {
                    Stream targetStream = null;
                    options.Write("    ");
                    options.Write(StringLoader.Get("GenerateBaml", name));

                    // grab the localizations available for this Baml
                    string       bamlName      = BamlStream.CombineBamlStreamName(resourceName, name, options.IsMsBamlMode);
                    Dictionaries localizations = GetDictionaries(bamlName);
                    if (!localizations.IsEmpty)
                    {
                        targetStream = new MemoryStream();

                        // generate into a new Baml stream
                        GenerateBamlStream((Stream)entry.Value, targetStream, localizations);
                    }
                    options.WriteLine(StringLoader.Get("Done"));

                    // sets the generated object to be the generated baml stream
                    resourceValue = targetStream;
                }

                if (resourceValue == null)
                {
                    //
                    // The stream is not localized as Baml yet, so we will make a copy of this item into
                    // the localized resources
                    //

                    // We will add the value as is if it is serializable. Otherwise, make a copy
                    resourceValue = entry.Value;

                    object[] serializableAttributes =
                        resourceValue.GetType().GetCustomAttributes(typeof(SerializableAttribute), true);
                    if (serializableAttributes.Length == 0)
                    {
                        // The item returned from resource reader is not serializable
                        // If it is Stream, we can wrap all the values in a MemoryStream and
                        // add to the resource. Otherwise, we had to skip this resource.
                        var resourceStream = resourceValue as Stream;
                        if (resourceStream != null)
                        {
                            var buffer = new byte[resourceStream.Length];
                            resourceStream.Read(buffer, 0, buffer.Length);
                            Stream targetStream = new MemoryStream(buffer);
                            resourceValue = targetStream;
                        }
                    }
                }

                if (resourceValue != null)
                {
                    writer.AddResource(name, resourceValue);
                }
            }
        }