Code:
/ FX-1434 / FX-1434 / 1.0 / untmp / whidbey / REDBITS / ndp / fx / src / Designer / System / data / design / SimpleNameService.cs / 1 / SimpleNameService.cs
//------------------------------------------------------------------------------ //// Copyright (c) Microsoft Corporation. All Rights Reserved. // Information Contained Herein is Proprietary and Confidential. // //----------------------------------------------------------------------------- namespace System.Data.Design { using System; using System.Diagnostics; using System.Collections; using System.Globalization; using System.Text.RegularExpressions; using System.Design; internal class SimpleNamedObject:INamedObject{ object _obj; public SimpleNamedObject(object obj){ _obj = obj; } public string Name { get{ if (_obj is INamedObject){ return (_obj as INamedObject).Name; } else if (_obj is string){ return _obj as string; } else { return _obj.ToString(); } } set { if (_obj is INamedObject){ (_obj as INamedObject).Name = value; } else if (_obj is string){ _obj = value; } else { // do nothing } } } } internal class SimpleNamedObjectCollection:ArrayList, INamedObjectCollection{ private static SimpleNameService myNameService; protected virtual INameService NameService { get { if( SimpleNamedObjectCollection.myNameService == null ) { SimpleNamedObjectCollection.myNameService = new SimpleNameService(); } return SimpleNamedObjectCollection.myNameService; } } public INameService GetNameService() { return NameService; } } ////// SimpleNameService is intended to have some basic naming rule as following /// 1. Use the identifier regular expression /// 2. Name is case insensitive /// 3. Limit length to 1024 /// 4. Undone: Use CodeGen's NameHandler Bug 45100 /// internal class SimpleNameService:INameService { internal const int DEFAULT_MAX_TRIALS = 100000; private const int MAX_LENGTH = 1024; private int maxNumberOfTrials = DEFAULT_MAX_TRIALS; private static readonly string regexAlphaCharacter = @"[\p{L}\p{Nl}]"; private static readonly string regexUnderscoreCharacter = @"\p{Pc}"; private static readonly string regexIdentifierCharacter = @"[\p{L}\p{Nl}\p{Nd}\p{Mn}\p{Mc}\p{Cf}]"; private static readonly string regexIdentifierStart = "(" + regexAlphaCharacter + "|(" + regexUnderscoreCharacter + regexIdentifierCharacter + "))"; private static readonly string regexIdentifier = regexIdentifierStart + regexIdentifierCharacter + "*"; private static SimpleNameService defaultInstance; private bool caseSensitive = true; internal SimpleNameService() { } internal static SimpleNameService DefaultInstance { get { if (defaultInstance == null) { defaultInstance = new SimpleNameService(); } return defaultInstance; } } ////// return proposed if it is not exist /// /// /// ///public string CreateUniqueName(INamedObjectCollection container, string proposed){ if( !NameExist(container, proposed) ) { this.ValidateName(proposed); return proposed; } return this.CreateUniqueName(container, proposed, 1); } /// /// Return typeName1 or typeName2, etc. /// /// /// ///public string CreateUniqueName(INamedObjectCollection container, Type type) { return this.CreateUniqueName(container, type.Name, 1); } /// /// try proposedNameRoot + startSuffix first then ... /// /// /// ///public string CreateUniqueName(INamedObjectCollection container, string proposedNameRoot, int startSuffix){ return CreateUniqueNameOnCollection(container, proposedNameRoot, startSuffix); } public string CreateUniqueNameOnCollection(ICollection container, string proposedNameRoot, int startSuffix) { int suffix = startSuffix; if (suffix < 0){ Debug.Fail("startSuffix should >= 0"); suffix = 0; } // It will throw if proposedNameRoot is bad! ValidateName(proposedNameRoot); string current = proposedNameRoot + suffix.ToString(System.Globalization.CultureInfo.CurrentCulture); while (NameExist(container, current)){ suffix++; if (suffix >= maxNumberOfTrials){ throw new InternalException(VSDExceptions.COMMON.SIMPLENAMESERVICE_NAMEOVERFLOW_MSG, VSDExceptions.COMMON.SIMPLENAMESERVICE_NAMEOVERFLOW_CODE,true); } current = proposedNameRoot + suffix.ToString(System.Globalization.CultureInfo.CurrentCulture); } ValidateName(current); return current; } /// /// Check to see if the name is exist in the container /// /// /// ///private bool NameExist(ICollection container, string nameTobeChecked){ return NameExist(container, null, nameTobeChecked); } /// /// Check to see if the name is exist in the container /// /// /// ///private bool NameExist(ICollection container, INamedObject objTobeChecked, string nameTobeChecked) { if (StringUtil.Empty(nameTobeChecked) && objTobeChecked != null) { nameTobeChecked = objTobeChecked.Name; } foreach (INamedObject obj in container) { if (obj != objTobeChecked && StringUtil.EqualValue(obj.Name, nameTobeChecked, !caseSensitive)){ return true; } } return false; } /// /// Should throw NameValidationException when invalid name passed. /// valid name syntx only /// /// public virtual void ValidateName(string name) { if (StringUtil.EmptyOrSpace(name)){ throw new NameValidationException(SR.GetString(SR.CM_NameNotEmptyExcption)); } if (name.Length > MAX_LENGTH){ throw new NameValidationException(SR.GetString(SR.CM_NameTooLongExcption)); } Match m = Regex.Match(name, regexIdentifier); if(!m.Success) { throw new NameValidationException(SR.GetString(SR.CM_NameInvalid, name)); } } ////// Should throw NameValidationException when invalid name passed. /// Valid syntax + dup /// This function is used when adding a new object to the collection /// /// public void ValidateUniqueName(INamedObjectCollection container, string proposedName){ ValidateUniqueName(container, null, proposedName); } // Check if the name is unique and valid // This function is useful when renaming an existing object. public void ValidateUniqueName(INamedObjectCollection container, INamedObject namedObject, string proposedName) { ValidateName(proposedName); if (NameExist(container, namedObject, proposedName)) { throw new NameValidationException(SR.GetString(SR.CM_NameExist, proposedName)); } } } ////// Used to validate the DataSet objects: tables, columns, etc. /// internal class DataSetNameService : SimpleNameService { private static DataSetNameService defaultInstance; internal static new DataSetNameService DefaultInstance { get { if (defaultInstance == null) { defaultInstance = new DataSetNameService(); } return defaultInstance; } } ////// Do nothing. Leave the name validation to DataSet /// /// public override void ValidateName(string name) { } } ////// Used to validate the sources /// internal class SourceNameService : SimpleNameService { private static SourceNameService defaultInstance; internal static new SourceNameService DefaultInstance { get { if (defaultInstance == null) { defaultInstance = new SourceNameService(); } return defaultInstance; } } } } // 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
- CultureInfo.cs
- ImageList.cs
- XmlILConstructAnalyzer.cs
- TemplateInstanceAttribute.cs
- XLinq.cs
- PauseStoryboard.cs
- Int16AnimationBase.cs
- CustomCredentialPolicy.cs
- DataGridViewSelectedCellsAccessibleObject.cs
- SoapObjectInfo.cs
- HttpAsyncResult.cs
- AutomationEventArgs.cs
- HtmlWindowCollection.cs
- SiteMap.cs
- StrokeCollection2.cs
- Attributes.cs
- SqlClientWrapperSmiStreamChars.cs
- ErrorStyle.cs
- QuestionEventArgs.cs
- HttpProfileBase.cs
- InheritanceRules.cs
- TextOptionsInternal.cs
- StreamReader.cs
- DispatcherEventArgs.cs
- DataException.cs
- TextSelectionProcessor.cs
- XmlUnspecifiedAttribute.cs
- StorageAssociationTypeMapping.cs
- Task.cs
- WebPartCatalogAddVerb.cs
- TemplateColumn.cs
- AdapterDictionary.cs
- DocobjHost.cs
- FixUp.cs
- XmlProcessingInstruction.cs
- Int16AnimationUsingKeyFrames.cs
- ToolboxItem.cs
- ActivityExecutionContextCollection.cs
- AuthenticationSchemesHelper.cs
- ISFClipboardData.cs
- ColorContextHelper.cs
- MessageEventSubscriptionService.cs
- ValueType.cs
- AssemblyBuilder.cs
- HttpWebResponse.cs
- AuthenticodeSignatureInformation.cs
- TailPinnedEventArgs.cs
- Int64Converter.cs
- filewebrequest.cs
- DataGridViewColumnHeaderCell.cs
- BinaryObjectWriter.cs
- CheckBoxStandardAdapter.cs
- ActivityScheduledQuery.cs
- HeaderedContentControl.cs
- XmlUtf8RawTextWriter.cs
- ByeOperation11AsyncResult.cs
- Bitmap.cs
- Viewport3DAutomationPeer.cs
- ServicePointManagerElement.cs
- FakeModelPropertyImpl.cs
- Path.cs
- SetIndexBinder.cs
- SafeEventLogReadHandle.cs
- BamlTreeUpdater.cs
- ChtmlCommandAdapter.cs
- JoinCqlBlock.cs
- AuthenticodeSignatureInformation.cs
- DescendentsWalker.cs
- XPathSelfQuery.cs
- StateManagedCollection.cs
- DesignerHost.cs
- TextLineBreak.cs
- WebPartExportVerb.cs
- Main.cs
- FormatConvertedBitmap.cs
- ObjectDisposedException.cs
- ISFTagAndGuidCache.cs
- SequentialOutput.cs
- AgileSafeNativeMemoryHandle.cs
- HttpCookiesSection.cs
- XmlSchemaAnyAttribute.cs
- PropertyValue.cs
- Stack.cs
- MappingMetadataHelper.cs
- HelloOperationCD1AsyncResult.cs
- ReflectionPermission.cs
- InputElement.cs
- PeerPresenceInfo.cs
- ServiceModelDictionary.cs
- Attributes.cs
- EnumerableRowCollectionExtensions.cs
- TimeSpanStorage.cs
- InvalidComObjectException.cs
- Base64Decoder.cs
- COAUTHIDENTITY.cs
- TargetPerspective.cs
- MachineKeySection.cs
- SecurityContextCookieSerializer.cs
- InternalMappingException.cs
- SessionParameter.cs