added SSCLI 1.0
[windows-sources.git] / sdk / samples / WPFSamples / LocBaml / csharp / inputbamlstreamlist.cs
blob77fc04ecee31a9c07e40f0b65e56da3d58b4146e
1 //---------------------------------------------------------------------------
2 //
3 // Copyright (c) Microsoft Corporation. All rights reserved.
4 //
5 // Description: InputBamlStreamList class
6 // It enumerates all the baml streams in the input file for parsing
7 //
8 //---------------------------------------------------------------------------
9 using System;
10 using System.IO;
11 using System.Globalization;
12 using System.Runtime.InteropServices;
13 using System.Collections;
14 using System.Reflection;
15 using System.Diagnostics;
16 using System.Resources;
18 namespace BamlLocalization
20 /// <summary>
21 /// Class that enumerates all the baml streams in the input file
22 /// </summary>
23 internal class InputBamlStreamList
25 /// <summary>
26 /// constructor
27 /// </summary>
28 internal InputBamlStreamList(LocBamlOptions options)
30 _bamlStreams = new ArrayList();
31 switch(options.InputType)
33 case FileType.BAML:
35 _bamlStreams.Add(
36 new BamlStream(
37 Path.GetFileName(options.Input),
38 File.OpenRead(options.Input)
41 break;
43 case FileType.RESOURCES:
45 using (ResourceReader resourceReader = new ResourceReader(options.Input))
47 // enumerate all bamls in a resources
48 EnumerateBamlInResources(resourceReader, options.Input);
50 break;
52 case FileType.EXE:
53 case FileType.DLL:
55 // for a dll, it is the same idea
56 Assembly assembly = Assembly.LoadFrom(options.Input);
57 foreach (string resourceName in assembly.GetManifestResourceNames())
59 ResourceLocation resourceLocation = assembly.GetManifestResourceInfo(resourceName).ResourceLocation;
61 // if this resource is in another assemlby, we will skip it
62 if ((resourceLocation & ResourceLocation.ContainedInAnotherAssembly) != 0)
64 continue; // in resource assembly, we don't have resource that is contained in another assembly
67 Stream resourceStream = assembly.GetManifestResourceStream(resourceName);
68 using (ResourceReader reader = new ResourceReader(resourceStream))
70 EnumerateBamlInResources(reader, resourceName);
73 break;
75 default:
78 Debug.Assert(false, "Not supported type");
79 break;
84 /// <summary>
85 /// return the number of baml streams found
86 /// </summary>
87 internal int Count
89 get{return _bamlStreams.Count;}
92 /// <summary>
93 /// Gets the baml stream in the input file through indexer
94 /// </summary>
95 internal BamlStream this[int i]
97 get { return (BamlStream) _bamlStreams[i];}
100 /// <summary>
101 /// Close the baml streams enumerated
102 /// </summary>
103 internal void Close()
105 for (int i = 0; i < _bamlStreams.Count; i++)
107 ((BamlStream) _bamlStreams[i]).Close();
111 //--------------------------------
112 // private function
113 //--------------------------------
114 /// <summary>
115 /// Enumerate baml streams in a resources file
116 /// </summary>
117 private void EnumerateBamlInResources(ResourceReader reader, string resourceName)
119 foreach (DictionaryEntry entry in reader)
121 string name = entry.Key as string;
122 if (BamlStream.IsResourceEntryBamlStream(name, entry.Value))
124 _bamlStreams.Add(
125 new BamlStream(
126 BamlStream.CombineBamlStreamName(resourceName, name),
127 (Stream) entry.Value
134 ArrayList _bamlStreams;
137 /// <summary>
138 /// BamlStream class which represents a baml stream
139 /// </summary>
140 internal class BamlStream
142 private string _name;
143 private Stream _stream;
145 /// <summary>
146 /// constructor
147 /// </summary>
148 internal BamlStream(string name, Stream stream)
150 _name = name;
151 _stream = stream;
154 /// <summary>
155 /// name of the baml
156 /// </summary>
157 internal string Name
159 get { return _name;}
162 /// <summary>
163 /// The actual Baml stream
164 /// </summary>
165 internal Stream Stream
167 get { return _stream;}
170 /// <summary>
171 /// close the stream
172 /// </summary>
173 internal void Close()
175 if (_stream != null)
177 _stream.Close();
181 /// <summary>
182 /// Helper method which determines whether a stream name and value pair indicates a baml stream
183 /// </summary>
184 internal static bool IsResourceEntryBamlStream(string name, object value)
186 string extension = Path.GetExtension(name);
187 if (string.Compare(
188 extension,
189 "." + FileType.BAML.ToString(),
190 true,
191 CultureInfo.InvariantCulture
192 ) == 0
195 //it has .Baml at the end
196 Type type = value.GetType();
198 if (typeof(Stream).IsAssignableFrom(type))
199 return true;
201 return false;
204 /// <summary>
205 /// Combine baml stream name and resource name to uniquely identify a baml within a
206 /// localization project
207 /// </summary>
208 internal static string CombineBamlStreamName(string resource, string bamlName)
210 Debug.Assert(resource != null && bamlName != null, "Resource name and baml name can't be null");
212 string suffix = Path.GetFileName(bamlName);
213 string prefix = Path.GetFileName(resource);
215 return prefix + LocBamlConst.BamlAndResourceSeperator + suffix;