1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
|
//------------------------------------------------------------------------------
// <copyright file="BaseResourcesBuildProvider.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
namespace System.Web.Compilation {
using System;
using System.Resources;
using System.Resources.Tools;
using System.Reflection;
using System.Globalization;
using System.Collections;
using System.IO;
using System.Xml;
using System.Xml.Schema;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Web.Configuration;
using System.Web.Hosting;
using System.Web.Compilation;
using System.Web.UI;
using System.Web.Util;
using Util=System.Web.UI.Util;
/// Base class for BuildProviders that generate resources
[BuildProviderAppliesTo(BuildProviderAppliesTo.Resources)]
internal abstract class BaseResourcesBuildProvider : BuildProvider {
internal const string DefaultResourcesNamespace = "Resources";
// The generated namespace and type name
private string _ns;
private string _typeName;
private string _cultureName;
private bool _dontGenerateStronglyTypedClass;
internal void DontGenerateStronglyTypedClass() {
_dontGenerateStronglyTypedClass = true;
}
public override void GenerateCode(AssemblyBuilder assemblyBuilder) {
_cultureName = GetCultureName();
if (!_dontGenerateStronglyTypedClass) {
// Get the namespace and type name that we will use
_ns = Util.GetNamespaceAndTypeNameFromVirtualPath(VirtualPathObject,
(_cultureName == null) ? 1 : 2 /*chunksToIgnore*/, out _typeName);
// Always prepend the namespace with Resources.
if (_ns.Length == 0)
_ns = DefaultResourcesNamespace;
else
_ns = DefaultResourcesNamespace + "." + _ns;
}
// Get an input stream for our virtual path, and get a resource reader from it
using (Stream inputStream = OpenStream()) {
IResourceReader reader = GetResourceReader(inputStream);
try {
GenerateResourceFile(assemblyBuilder, reader);
}
catch (ArgumentException e) {
// If the inner exception is Xml, throw that instead, as it contains more
// useful info
if (e.InnerException != null &&
(e.InnerException is XmlException || e.InnerException is XmlSchemaException)) {
throw e.InnerException;
}
// Otherwise, so just rethrow
throw;
}
// Skip the code part for satellite assemblies, or if dontGenerate is set
if (_cultureName == null && !_dontGenerateStronglyTypedClass)
GenerateStronglyTypedClass(assemblyBuilder, reader);
}
}
protected abstract IResourceReader GetResourceReader(Stream inputStream);
private void GenerateResourceFile(AssemblyBuilder assemblyBuilder, IResourceReader reader) {
// Get the name of the generated .resource file
string resourceFileName;
if (_ns == null) {
// In the case where we don't generate code, just name the resource file
// after the virtual file
resourceFileName = UrlPath.GetFileNameWithoutExtension(VirtualPath) + ".resources";
}
else if (_cultureName == null) {
// Name the resource file after the generated class, since that's what the
// generated class expects
resourceFileName = _ns + "." + _typeName + ".resources";
}
else {
// If it's a non-default resource, include the culture in the name
resourceFileName = _ns + "." + _typeName + "." + _cultureName + ".resources";
}
// Make it lower case, since GetManifestResourceStream (which we use later on) is
// case sensitive
resourceFileName = resourceFileName.ToLower(CultureInfo.InvariantCulture);
Stream outputStream = null;
try {
try {
try {
}
finally {
// Put the assignment in a finally block to avoid a ThreadAbortException from
// causing the created stream to not get assigned and become leaked (Dev10 bug 844463)
outputStream = assemblyBuilder.CreateEmbeddedResource(this, resourceFileName);
}
}
catch (ArgumentException) {
// This throws an ArgumentException if the resource file name was already added.
// Catch the situation, and give a better error message (VSWhidbey 87110)
throw new HttpException(SR.GetString(SR.Duplicate_Resource_File, VirtualPath));
}
// Create an output stream from the .resource file
using (outputStream) {
using (ResourceWriter writer = new ResourceWriter(outputStream)) {
// Enable resource writer to be target-aware
writer.TypeNameConverter = System.Web.UI.TargetFrameworkUtil.TypeNameConverter;
// Copy the resources
foreach (DictionaryEntry de in reader) {
writer.AddResource((string)de.Key, de.Value);
}
}
}
}
finally {
// Always close the stream to avoid a ThreadAbortException from causing the stream
// to be leaked (Dev10 bug 844463)
if (outputStream != null) {
outputStream.Close();
}
}
}
private void GenerateStronglyTypedClass(AssemblyBuilder assemblyBuilder, IResourceReader reader) {
// Copy the resources into an IDictionary
IDictionary resourceList;
using (reader) {
resourceList = GetResourceList(reader);
}
// Generate a strongly typed class from the resources
CodeDomProvider provider = assemblyBuilder.CodeDomProvider;
string[] unmatchable;
CodeCompileUnit ccu = StronglyTypedResourceBuilder.Create(
resourceList, _typeName, _ns,
provider, false /*internalClass*/, out unmatchable);
// Ignore the unmatchable items. We just won't generate code for them,
// but they'll still be usable via the ResourceManager (VSWhidbey 248226)
// We decided to cut support for My.Resources (VSWhidbey 358088)
#if OLD
// generate a My.Resources.* override (VSWhidbey 251554)
CodeNamespace ns = new CodeNamespace();
ns.Name = "My." + _ns;
CodeTypeDeclaration type = new CodeTypeDeclaration();
type.Name = _typeName;
CodeTypeReference baseType = new CodeTypeReference(_ns + "." + _typeName);
// Need to use a global reference to avoid a conflict, since the classes have the same name
baseType.Options = CodeTypeReferenceOptions.GlobalReference;
type.BaseTypes.Add(baseType);
ns.Types.Add(type);
ccu.Namespaces.Add(ns);
#endif
// Add the code compile unit to the compilation
assemblyBuilder.AddCodeCompileUnit(this, ccu);
}
private IDictionary GetResourceList(IResourceReader reader) {
// Read the resources into a dictionary.
IDictionary resourceList = new Hashtable(StringComparer.OrdinalIgnoreCase);
foreach(DictionaryEntry de in reader)
resourceList.Add(de.Key, de.Value);
return resourceList;
}
}
}
|