Code:
/ DotNET / DotNET / 8.0 / untmp / Orcas / RTM / ndp / fx / src / xsp / System / Web / Extensions / ui / ScriptResourceAttribute.cs / 1 / ScriptResourceAttribute.cs
//------------------------------------------------------------------------------ //// Copyright (c) Microsoft Corporation. All rights reserved. // //----------------------------------------------------------------------------- namespace System.Web.UI { using System; using System.Collections; using System.Globalization; using System.IO; using System.Reflection; using System.Resources; using System.Security.Permissions; using System.Text; using System.Text.RegularExpressions; using System.Threading; using System.Web; using System.Web.Handlers; using System.Web.Resources; using System.Web.Script.Serialization; using System.Web.Util; [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] [AspNetHostingPermission(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)] public sealed class ScriptResourceAttribute : Attribute { private string _scriptName; private string _scriptResourceName; private string _typeName; private static readonly Regex _webResourceRegEx = new Regex( @"<%\s*=\s*(?WebResource|ScriptResource)\(""(? [^""]*)""\)\s*%>", RegexOptions.Singleline | RegexOptions.Multiline); public ScriptResourceAttribute(string scriptName, string scriptResourceName, string typeName) { if (String.IsNullOrEmpty(scriptName)) { throw new ArgumentException(AtlasWeb.Common_NullOrEmpty, "scriptName"); } if (String.IsNullOrEmpty(scriptResourceName)) { throw new ArgumentException(AtlasWeb.Common_NullOrEmpty, "scriptResourceName"); } if (String.IsNullOrEmpty(typeName)) { throw new ArgumentException(AtlasWeb.Common_NullOrEmpty, "typeName"); } _scriptName = scriptName; _scriptResourceName = scriptResourceName; _typeName = typeName; } public string ScriptName { get { return _scriptName; } } public string ScriptResourceName { get { return _scriptResourceName; } } public string TypeName { get { return _typeName; } } private static void CopyScriptToStringBuilderWithSubstitution( string content, Assembly assembly, bool zip, bool notifyScriptLoaded, StringBuilder output) { // Looking for something of the form: WebResource("resourcename") MatchCollection matches = _webResourceRegEx.Matches(content); int startIndex = 0; foreach (Match match in matches) { output.Append(content.Substring(startIndex, match.Index - startIndex)); Group group = match.Groups["resourceName"]; string embeddedResourceName = group.Value; bool isScriptResource = String.Equals( match.Groups["resourceType"].Value, "ScriptResource", StringComparison.Ordinal); try { if (isScriptResource) { output.Append(ScriptResourceHandler.GetScriptResourceUrl( assembly, embeddedResourceName, CultureInfo.CurrentUICulture, zip, notifyScriptLoaded)); } else { output.Append(ScriptResourceHandler.GetWebResourceUrl(assembly, embeddedResourceName)); } } catch (HttpException e) { throw new HttpException(String.Format(CultureInfo.CurrentCulture, AtlasWeb.ScriptResourceHandler_UnknownResource, embeddedResourceName), e); } startIndex = match.Index + match.Length; } output.Append(content.Substring(startIndex, content.Length - startIndex)); } internal static ResourceManager GetResourceManager(string resourceName, Assembly assembly) { if (String.IsNullOrEmpty(resourceName)) { return null; } return new ResourceManager(GetResourceName(resourceName), assembly); } private static string GetResourceName(string rawResourceName) { if (rawResourceName.EndsWith(".resources", StringComparison.OrdinalIgnoreCase)) { return rawResourceName.Substring(0, rawResourceName.Length - 10); } return rawResourceName; } internal static string GetScriptFromWebResourceInternal( Assembly assembly, string resourceName, CultureInfo culture, bool zip, bool notifyScriptLoaded, out Encoding encoding, out string contentType) { ScriptResourceInfo resourceInfo = ScriptResourceInfo.GetInstance(assembly, resourceName); ScriptResourceInfo releaseResourceInfo = null; if (resourceName.EndsWith(".debug.js", StringComparison.OrdinalIgnoreCase)) { // This is a debug script, we'll need to merge the debug resource // with the release one. string releaseResourceName = resourceName.Substring(0, resourceName.Length - 9) + ".js"; releaseResourceInfo = ScriptResourceInfo.GetInstance(assembly, releaseResourceName); } if ((resourceInfo == ScriptResourceInfo.Empty) && ((releaseResourceInfo == null) || (releaseResourceInfo == ScriptResourceInfo.Empty))) { throw new HttpException(AtlasWeb.ScriptResourceHandler_InvalidRequest); } ResourceManager resourceManager = null; ResourceSet neutralSet = null; ResourceManager releaseResourceManager = null; ResourceSet releaseNeutralSet = null; CultureInfo previousCulture = Thread.CurrentThread.CurrentUICulture; try { Thread.CurrentThread.CurrentUICulture = culture; if (!String.IsNullOrEmpty(resourceInfo.ScriptResourceName)) { resourceManager = GetResourceManager(resourceInfo.ScriptResourceName, assembly); // The following may throw MissingManifestResourceException neutralSet = resourceManager.GetResourceSet(CultureInfo.InvariantCulture, true, true); } if ((releaseResourceInfo != null) && !String.IsNullOrEmpty(releaseResourceInfo.ScriptResourceName)) { releaseResourceManager = GetResourceManager(releaseResourceInfo.ScriptResourceName, assembly); releaseNeutralSet = releaseResourceManager.GetResourceSet(CultureInfo.InvariantCulture, true, true); } if ((releaseResourceInfo != null) && !String.IsNullOrEmpty(releaseResourceInfo.ScriptResourceName) && !String.IsNullOrEmpty(resourceInfo.ScriptResourceName) && (releaseResourceInfo.TypeName != resourceInfo.TypeName)) { throw new HttpException(String.Format( CultureInfo.CurrentCulture, AtlasWeb.ScriptResourceHandler_TypeNameMismatch, releaseResourceInfo.ScriptResourceName)); } StringBuilder builder = new StringBuilder(); encoding = WriteScript(assembly, resourceInfo, releaseResourceInfo, resourceManager, neutralSet, releaseResourceManager, releaseNeutralSet, zip, notifyScriptLoaded, builder); contentType = resourceInfo.ContentType; return builder.ToString(); } finally { Thread.CurrentThread.CurrentUICulture = previousCulture; if (releaseNeutralSet != null) { releaseNeutralSet.Dispose(); } if (neutralSet != null) { neutralSet.Dispose(); } } } private static void RegisterNamespace(StringBuilder builder, string typeName, bool isDebug) { int lastDot = typeName.LastIndexOf('.'); if (lastDot != -1) { builder.Append("Type.registerNamespace('"); builder.Append(typeName.Substring(0, lastDot)); builder.Append("');"); if (isDebug) builder.AppendLine(); } } private static void WriteNotificationToStringBuilder(bool notifyScriptLoaded, StringBuilder builder, bool isDebug) { if (notifyScriptLoaded) { // DevDiv Bugs 131109: Resources and notification should go on a new line even in release mode // because main script may not end in a semi-colon or may end in a javascript comment. // Note: If resources exist, this newline isn't necessary because one was already output. // But it is not worth keeping track of whether a newline has been output. // A newline is required for resources and notification, better to just output it for both. builder.AppendLine(); builder.Append("if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();"); } } private static bool WriteResource( StringBuilder builder, ResourceManager resourceManager, ResourceSet neutralSet, bool first, bool isDebug) { foreach (DictionaryEntry res in neutralSet) { string key = (string)res.Key; string value = resourceManager.GetObject(key) as string; if (value != null) { if (first) { first = false; } else { builder.Append(','); } if (isDebug) builder.AppendLine(); builder.Append('"'); builder.Append(JavaScriptString.QuoteString(key)); builder.Append("\":\""); builder.Append(JavaScriptString.QuoteString(value)); builder.Append('"'); } } return first; } private static void WriteResourceToStringBuilder( ScriptResourceInfo resourceInfo, ScriptResourceInfo releaseResourceInfo, ResourceManager resourceManager, ResourceSet neutralSet, ResourceManager releaseResourceManager, ResourceSet releaseNeutralSet, StringBuilder builder) { if ((resourceManager != null) || (releaseResourceManager != null)) { string typeName = resourceInfo.TypeName; if (String.IsNullOrEmpty(typeName)) { typeName = releaseResourceInfo.TypeName; } WriteResources(builder, typeName, resourceManager, neutralSet, releaseResourceManager, releaseNeutralSet, resourceInfo.IsDebug); } } private static void WriteResources(StringBuilder builder, string typeName, ResourceManager resourceManager, ResourceSet neutralSet, ResourceManager releaseResourceManager, ResourceSet releaseNeutralSet, bool isDebug) { // DevDiv Bugs 131109: Resources and notification should go on a new line even in release mode // because main script may not end in a semi-colon or may end in a javascript comment. builder.AppendLine(); RegisterNamespace(builder, typeName, isDebug); builder.Append(typeName); builder.Append("={"); bool first = true; if (resourceManager != null) { first = WriteResource(builder, resourceManager, neutralSet, first, isDebug); } if (releaseResourceManager != null) { WriteResource(builder, releaseResourceManager, releaseNeutralSet, first, isDebug); } if (isDebug) { builder.AppendLine(); builder.AppendLine("};"); } else{ builder.Append("};"); } } private static Encoding WriteScript(Assembly assembly, ScriptResourceInfo resourceInfo, ScriptResourceInfo releaseResourceInfo, ResourceManager resourceManager, ResourceSet neutralSet, ResourceManager releaseResourceManager, ResourceSet releaseNeutralSet, bool zip, bool notifyScriptLoaded, StringBuilder output) { Encoding encoding = null; using (StreamReader reader = new StreamReader( assembly.GetManifestResourceStream(resourceInfo.ScriptName), true)) { encoding = reader.CurrentEncoding; if (resourceInfo.IsDebug) { // Output version information AssemblyName assemblyName = assembly.GetName(); output.AppendLine("// Name: " + resourceInfo.ScriptName); output.AppendLine("// Assembly: " + assemblyName.Name); output.AppendLine("// Version: " + assemblyName.Version.ToString()); output.AppendLine("// FileVersion: " + AssemblyUtil.GetAssemblyFileVersion(assembly)); } if (resourceInfo.PerformSubstitution) { CopyScriptToStringBuilderWithSubstitution( reader.ReadToEnd(), assembly, zip, notifyScriptLoaded, output); } else { output.Append(reader.ReadToEnd()); } WriteResourceToStringBuilder(resourceInfo, releaseResourceInfo, resourceManager, neutralSet, releaseResourceManager, releaseNeutralSet, output); WriteNotificationToStringBuilder(notifyScriptLoaded, output, resourceInfo.IsDebug); } return encoding; } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- StrokeFIndices.cs
- EntityDataSourceDesigner.cs
- RemoveStoryboard.cs
- QilValidationVisitor.cs
- RegexCharClass.cs
- DetailsViewPagerRow.cs
- DictionaryEntry.cs
- DockAndAnchorLayout.cs
- SizeKeyFrameCollection.cs
- SafeNativeMethods.cs
- LazyLoadBehavior.cs
- brushes.cs
- ListViewTableRow.cs
- BitmapEffectGeneralTransform.cs
- XmlCodeExporter.cs
- LinqDataSourceEditData.cs
- ValidationErrorCollection.cs
- DataBindingHandlerAttribute.cs
- DataColumnCollection.cs
- RowUpdatingEventArgs.cs
- BinaryMessageEncodingElement.cs
- CodeGroup.cs
- IsolationInterop.cs
- VectorAnimationUsingKeyFrames.cs
- StylusButton.cs
- XmlSchemaSubstitutionGroup.cs
- NegatedConstant.cs
- AssemblyName.cs
- HMACSHA256.cs
- SoapFault.cs
- TextReader.cs
- Label.cs
- ObjectListItem.cs
- RegistrySecurity.cs
- ObjectNotFoundException.cs
- DoubleLinkList.cs
- DiffuseMaterial.cs
- BindingCompleteEventArgs.cs
- FontStretches.cs
- Stroke2.cs
- FacetValueContainer.cs
- DataControlField.cs
- XmlQualifiedName.cs
- ToolStripPanelSelectionBehavior.cs
- webbrowsersite.cs
- QilName.cs
- HostedElements.cs
- PropertyStore.cs
- sqlnorm.cs
- InvokeBinder.cs
- ConfigXmlCDataSection.cs
- NumberFormatInfo.cs
- FormViewDeleteEventArgs.cs
- ExpressionParser.cs
- webbrowsersite.cs
- OdbcRowUpdatingEvent.cs
- WebPartsSection.cs
- TypeToken.cs
- FormViewPageEventArgs.cs
- XsltException.cs
- FontWeights.cs
- WebColorConverter.cs
- FormViewAutoFormat.cs
- LinqDataSourceHelper.cs
- DataRowCollection.cs
- GeometryHitTestResult.cs
- DataBindEngine.cs
- SslStream.cs
- ConfigurationManagerInternalFactory.cs
- MimeBasePart.cs
- PointCollectionConverter.cs
- TextElementEnumerator.cs
- Types.cs
- OutOfMemoryException.cs
- RegexWorker.cs
- CqlQuery.cs
- CatalogZone.cs
- OdbcConnectionHandle.cs
- NonVisualControlAttribute.cs
- SendMessageRecord.cs
- TreeWalkHelper.cs
- InternalResources.cs
- UnicodeEncoding.cs
- GeometryCombineModeValidation.cs
- TreeNodeMouseHoverEvent.cs
- DemultiplexingClientMessageFormatter.cs
- GACMembershipCondition.cs
- UserControlParser.cs
- DriveNotFoundException.cs
- EventLog.cs
- MessageSecurityTokenVersion.cs
- WithStatement.cs
- XPathDocumentIterator.cs
- ServiceModelReg.cs
- Overlapped.cs
- ScalarOps.cs
- HttpListenerException.cs
- ReferenceEqualityComparer.cs
- OleDbPermission.cs
- KerberosTokenFactoryCredential.cs