1 //---------------------------------------------------------------------------
3 // Copyright (c) Microsoft Corporation. All rights reserved.
5 // Description: InputBamlStreamList class
6 // It enumerates all the baml streams in the input file for parsing
8 //---------------------------------------------------------------------------
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
21 /// Class that enumerates all the baml streams in the input file
23 internal class InputBamlStreamList
28 internal InputBamlStreamList(LocBamlOptions options
)
30 _bamlStreams
= new ArrayList();
31 switch(options
.InputType
)
37 Path
.GetFileName(options
.Input
),
38 File
.OpenRead(options
.Input
)
43 case FileType
.RESOURCES
:
45 using (ResourceReader resourceReader
= new ResourceReader(options
.Input
))
47 // enumerate all bamls in a resources
48 EnumerateBamlInResources(resourceReader
, options
.Input
);
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
);
78 Debug
.Assert(false, "Not supported type");
85 /// return the number of baml streams found
89 get{return _bamlStreams.Count;}
93 /// Gets the baml stream in the input file through indexer
95 internal BamlStream
this[int i
]
97 get { return (BamlStream) _bamlStreams[i];}
101 /// Close the baml streams enumerated
103 internal void Close()
105 for (int i
= 0; i
< _bamlStreams
.Count
; i
++)
107 ((BamlStream
) _bamlStreams
[i
]).Close();
111 //--------------------------------
113 //--------------------------------
115 /// Enumerate baml streams in a resources file
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
))
126 BamlStream
.CombineBamlStreamName(resourceName
, name
),
134 ArrayList _bamlStreams
;
138 /// BamlStream class which represents a baml stream
140 internal class BamlStream
142 private string _name
;
143 private Stream _stream
;
148 internal BamlStream(string name
, Stream stream
)
163 /// The actual Baml stream
165 internal Stream Stream
167 get { return _stream;}
173 internal void Close()
182 /// Helper method which determines whether a stream name and value pair indicates a baml stream
184 internal static bool IsResourceEntryBamlStream(string name
, object value)
186 string extension
= Path
.GetExtension(name
);
189 "." + FileType
.BAML
.ToString(),
191 CultureInfo
.InvariantCulture
195 //it has .Baml at the end
196 Type type
= value.GetType();
198 if (typeof(Stream
).IsAssignableFrom(type
))
205 /// Combine baml stream name and resource name to uniquely identify a baml within a
206 /// localization project
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
;