Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / DataEntity / System / Data / EntityModel / SchemaObjectModel / CollectionTypeElement.cs / 1305376 / CollectionTypeElement.cs
//----------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// @owner [....]
// @backupOwner [....]
//---------------------------------------------------------------------
using Som = System.Data.EntityModel.SchemaObjectModel;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Globalization;
using System.Xml;
using System.Xml.Schema;
using System.Data;
using System.IO;
using System.Data.Metadata.Edm;
using System.Data.Entity;
using System.Text;
namespace System.Data.EntityModel.SchemaObjectModel
{
///
/// class representing the Schema element in the schema
///
internal class CollectionTypeElement : ModelFunctionTypeElement
{
private ModelFunctionTypeElement _typeSubElement = null;
#region constructor
///
///
///
///
internal CollectionTypeElement(SchemaElement parentElement)
: base(parentElement)
{
}
#endregion
protected override bool HandleAttribute(XmlReader reader)
{
if (base.HandleAttribute(reader))
{
return true;
}
else if (CanHandleAttribute(reader, XmlConstants.ElementType))
{
HandleElementTypeAttribute(reader);
return true;
}
return false;
}
protected void HandleElementTypeAttribute(XmlReader reader)
{
Debug.Assert(reader != null);
string type;
if (!Utils.GetString(Schema, reader, out type))
return;
_unresolvedType = type;
}
protected override bool HandleElement(XmlReader reader)
{
if (CanHandleElement(reader, XmlConstants.CollectionType))
{
HandleCollectionTypeElement(reader);
return true;
}
else if (CanHandleElement(reader, XmlConstants.ReferenceType))
{
HandleReferenceTypeElement(reader);
return true;
}
else if (CanHandleElement(reader, XmlConstants.TypeRef))
{
HandleTypeRefElement(reader);
return true;
}
else if (CanHandleElement(reader, XmlConstants.RowType))
{
HandleRowTypeElement(reader);
return true;
}
return false;
}
protected void HandleCollectionTypeElement(XmlReader reader)
{
Debug.Assert(reader != null);
var subElement = new CollectionTypeElement(this);
subElement.Parse(reader);
_typeSubElement = subElement;
}
protected void HandleReferenceTypeElement(XmlReader reader)
{
Debug.Assert(reader != null);
var subElement = new ReferenceTypeElement(this);
subElement.Parse(reader);
_typeSubElement = subElement;
}
protected void HandleTypeRefElement(XmlReader reader)
{
Debug.Assert(reader != null);
var subElement = new TypeRefElement(this);
subElement.Parse(reader);
_typeSubElement = subElement;
}
protected void HandleRowTypeElement(XmlReader reader)
{
Debug.Assert(reader != null);
var subElement = new RowTypeElement(this);
subElement.Parse(reader);
_typeSubElement = subElement;
}
internal override void ResolveTopLevelNames()
{
if (_typeSubElement != null)
{
_typeSubElement.ResolveTopLevelNames();
}
// Can't be "else if" because element could have attribute AND sub-element,
// in which case semantic validation won't work unless it has resolved both (so _type is not null)
if( _unresolvedType != null)
{
base.ResolveTopLevelNames();
}
}
internal override void WriteIdentity(StringBuilder builder)
{
if (UnresolvedType != null && !UnresolvedType.Trim().Equals(String.Empty))
{
builder.Append("Collection(" + UnresolvedType + ")");
}
else
{
builder.Append("Collection(");
_typeSubElement.WriteIdentity(builder);
builder.Append(")");
}
}
internal override TypeUsage GetTypeUsage()
{
if (_typeUsage != null)
{
return _typeUsage;
}
Debug.Assert(_typeSubElement != null, "For attributes typeusage should have been resolved");
if (_typeSubElement != null)
{
CollectionType collectionType = new CollectionType(_typeSubElement.GetTypeUsage());
collectionType.AddMetadataProperties(this.OtherContent);
_typeUsage = TypeUsage.Create(collectionType);
}
return _typeUsage;
}
internal override bool ResolveNameAndSetTypeUsage(Converter.ConversionCache convertedItemCache, Dictionary newGlobalItems)
{
if (_typeUsage == null)
{
if (_typeSubElement != null) //Has sub-elements
{
return _typeSubElement.ResolveNameAndSetTypeUsage(convertedItemCache, newGlobalItems);
}
else //Does not have sub-elements; try to resolve
{
if (_type is ScalarType) //Create and store type usage for scalar type
{
_typeUsageBuilder.ValidateAndSetTypeUsage(_type as ScalarType, false);
_typeUsage = TypeUsage.Create(new CollectionType(_typeUsageBuilder.TypeUsage));
return true;
}
else //Try to resolve edm type. If not now, it will resolve in the second pass
{
EdmType edmType = (EdmType)Converter.LoadSchemaElement(_type, _type.Schema.ProviderManifest, convertedItemCache, newGlobalItems);
if (edmType != null)
{
_typeUsageBuilder.ValidateAndSetTypeUsage(edmType, false); //use typeusagebuilder so dont lose facet information
_typeUsage = TypeUsage.Create(new CollectionType(_typeUsageBuilder.TypeUsage));
}
return _typeUsage != null;
}
}
}
return true;
}
internal override void Validate()
{
base.Validate();
if (_type != null && _type is ScalarType == false && _typeUsageBuilder.HasUserDefinedFacets)
{
//Non-scalar return type should not have Facets
AddError(ErrorCode.ModelFuncionFacetOnNonScalarType, EdmSchemaErrorSeverity.Error, Strings.FacetsOnNonScalarType(_type.FQName));
}
if (_type == null && _typeUsageBuilder.HasUserDefinedFacets)
{
//Type attribute not specified but facets exist
AddError(ErrorCode.ModelFunctionIncorrectlyPlacedFacet, EdmSchemaErrorSeverity.Error, Strings.FacetDeclarationRequiresTypeAttribute);
}
if (_type == null && _typeSubElement == null)
{
//Type not declared as either attribute or subelement
AddError(ErrorCode.ModelFunctionTypeNotDeclared, EdmSchemaErrorSeverity.Error, Strings.TypeMustBeDeclared);
}
if (_type != null && _typeSubElement != null)
{
//Both attribute and sub-element declarations exist
AddError(ErrorCode.TypeDeclaredAsAttributeAndElement, EdmSchemaErrorSeverity.Error, Strings.TypeDeclaredAsAttributeAndElement);
}
if (_typeSubElement != null)
{
_typeSubElement.Validate();
}
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//----------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// @owner [....]
// @backupOwner [....]
//---------------------------------------------------------------------
using Som = System.Data.EntityModel.SchemaObjectModel;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Globalization;
using System.Xml;
using System.Xml.Schema;
using System.Data;
using System.IO;
using System.Data.Metadata.Edm;
using System.Data.Entity;
using System.Text;
namespace System.Data.EntityModel.SchemaObjectModel
{
///
/// class representing the Schema element in the schema
///
internal class CollectionTypeElement : ModelFunctionTypeElement
{
private ModelFunctionTypeElement _typeSubElement = null;
#region constructor
///
///
///
///
internal CollectionTypeElement(SchemaElement parentElement)
: base(parentElement)
{
}
#endregion
protected override bool HandleAttribute(XmlReader reader)
{
if (base.HandleAttribute(reader))
{
return true;
}
else if (CanHandleAttribute(reader, XmlConstants.ElementType))
{
HandleElementTypeAttribute(reader);
return true;
}
return false;
}
protected void HandleElementTypeAttribute(XmlReader reader)
{
Debug.Assert(reader != null);
string type;
if (!Utils.GetString(Schema, reader, out type))
return;
_unresolvedType = type;
}
protected override bool HandleElement(XmlReader reader)
{
if (CanHandleElement(reader, XmlConstants.CollectionType))
{
HandleCollectionTypeElement(reader);
return true;
}
else if (CanHandleElement(reader, XmlConstants.ReferenceType))
{
HandleReferenceTypeElement(reader);
return true;
}
else if (CanHandleElement(reader, XmlConstants.TypeRef))
{
HandleTypeRefElement(reader);
return true;
}
else if (CanHandleElement(reader, XmlConstants.RowType))
{
HandleRowTypeElement(reader);
return true;
}
return false;
}
protected void HandleCollectionTypeElement(XmlReader reader)
{
Debug.Assert(reader != null);
var subElement = new CollectionTypeElement(this);
subElement.Parse(reader);
_typeSubElement = subElement;
}
protected void HandleReferenceTypeElement(XmlReader reader)
{
Debug.Assert(reader != null);
var subElement = new ReferenceTypeElement(this);
subElement.Parse(reader);
_typeSubElement = subElement;
}
protected void HandleTypeRefElement(XmlReader reader)
{
Debug.Assert(reader != null);
var subElement = new TypeRefElement(this);
subElement.Parse(reader);
_typeSubElement = subElement;
}
protected void HandleRowTypeElement(XmlReader reader)
{
Debug.Assert(reader != null);
var subElement = new RowTypeElement(this);
subElement.Parse(reader);
_typeSubElement = subElement;
}
internal override void ResolveTopLevelNames()
{
if (_typeSubElement != null)
{
_typeSubElement.ResolveTopLevelNames();
}
// Can't be "else if" because element could have attribute AND sub-element,
// in which case semantic validation won't work unless it has resolved both (so _type is not null)
if( _unresolvedType != null)
{
base.ResolveTopLevelNames();
}
}
internal override void WriteIdentity(StringBuilder builder)
{
if (UnresolvedType != null && !UnresolvedType.Trim().Equals(String.Empty))
{
builder.Append("Collection(" + UnresolvedType + ")");
}
else
{
builder.Append("Collection(");
_typeSubElement.WriteIdentity(builder);
builder.Append(")");
}
}
internal override TypeUsage GetTypeUsage()
{
if (_typeUsage != null)
{
return _typeUsage;
}
Debug.Assert(_typeSubElement != null, "For attributes typeusage should have been resolved");
if (_typeSubElement != null)
{
CollectionType collectionType = new CollectionType(_typeSubElement.GetTypeUsage());
collectionType.AddMetadataProperties(this.OtherContent);
_typeUsage = TypeUsage.Create(collectionType);
}
return _typeUsage;
}
internal override bool ResolveNameAndSetTypeUsage(Converter.ConversionCache convertedItemCache, Dictionary newGlobalItems)
{
if (_typeUsage == null)
{
if (_typeSubElement != null) //Has sub-elements
{
return _typeSubElement.ResolveNameAndSetTypeUsage(convertedItemCache, newGlobalItems);
}
else //Does not have sub-elements; try to resolve
{
if (_type is ScalarType) //Create and store type usage for scalar type
{
_typeUsageBuilder.ValidateAndSetTypeUsage(_type as ScalarType, false);
_typeUsage = TypeUsage.Create(new CollectionType(_typeUsageBuilder.TypeUsage));
return true;
}
else //Try to resolve edm type. If not now, it will resolve in the second pass
{
EdmType edmType = (EdmType)Converter.LoadSchemaElement(_type, _type.Schema.ProviderManifest, convertedItemCache, newGlobalItems);
if (edmType != null)
{
_typeUsageBuilder.ValidateAndSetTypeUsage(edmType, false); //use typeusagebuilder so dont lose facet information
_typeUsage = TypeUsage.Create(new CollectionType(_typeUsageBuilder.TypeUsage));
}
return _typeUsage != null;
}
}
}
return true;
}
internal override void Validate()
{
base.Validate();
if (_type != null && _type is ScalarType == false && _typeUsageBuilder.HasUserDefinedFacets)
{
//Non-scalar return type should not have Facets
AddError(ErrorCode.ModelFuncionFacetOnNonScalarType, EdmSchemaErrorSeverity.Error, Strings.FacetsOnNonScalarType(_type.FQName));
}
if (_type == null && _typeUsageBuilder.HasUserDefinedFacets)
{
//Type attribute not specified but facets exist
AddError(ErrorCode.ModelFunctionIncorrectlyPlacedFacet, EdmSchemaErrorSeverity.Error, Strings.FacetDeclarationRequiresTypeAttribute);
}
if (_type == null && _typeSubElement == null)
{
//Type not declared as either attribute or subelement
AddError(ErrorCode.ModelFunctionTypeNotDeclared, EdmSchemaErrorSeverity.Error, Strings.TypeMustBeDeclared);
}
if (_type != null && _typeSubElement != null)
{
//Both attribute and sub-element declarations exist
AddError(ErrorCode.TypeDeclaredAsAttributeAndElement, EdmSchemaErrorSeverity.Error, Strings.TypeDeclaredAsAttributeAndElement);
}
if (_typeSubElement != null)
{
_typeSubElement.Validate();
}
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- InvariantComparer.cs
- DefaultBindingPropertyAttribute.cs
- TextSpanModifier.cs
- InteropBitmapSource.cs
- UIElement3D.cs
- XmlUtf8RawTextWriter.cs
- XmlSerializationWriter.cs
- Or.cs
- DifferencingCollection.cs
- AspProxy.cs
- StrokeSerializer.cs
- PaintEvent.cs
- httpapplicationstate.cs
- NativeMethods.cs
- BindableTemplateBuilder.cs
- LayoutExceptionEventArgs.cs
- ClassHandlersStore.cs
- PropertyCondition.cs
- TableRow.cs
- AutomationPatternInfo.cs
- WindowsProgressbar.cs
- InputMethodStateTypeInfo.cs
- MarshalByRefObject.cs
- DocumentsTrace.cs
- ControlBindingsCollection.cs
- FontEditor.cs
- DodSequenceMerge.cs
- ResourceExpression.cs
- IApplicationTrustManager.cs
- WindowsIdentity.cs
- KeyedHashAlgorithm.cs
- ReceiveContent.cs
- ActivationServices.cs
- StateFinalizationDesigner.cs
- Command.cs
- DetailsViewPageEventArgs.cs
- CodeFieldReferenceExpression.cs
- DataGridViewCellCancelEventArgs.cs
- CultureInfoConverter.cs
- CheckBoxBaseAdapter.cs
- TextRunCacheImp.cs
- ControlPropertyNameConverter.cs
- ControlBindingsCollection.cs
- MappingItemCollection.cs
- StateDesigner.CommentLayoutGlyph.cs
- BehaviorEditorPart.cs
- ToolStripItemEventArgs.cs
- LingerOption.cs
- UserCancellationException.cs
- COSERVERINFO.cs
- BypassElement.cs
- Misc.cs
- StylusPointProperties.cs
- RequestCacheEntry.cs
- _IPv6Address.cs
- ScalarOps.cs
- PrePrepareMethodAttribute.cs
- ComponentEditorForm.cs
- RuntimeArgument.cs
- Timer.cs
- HttpRawResponse.cs
- XhtmlTextWriter.cs
- GeneralTransform.cs
- PropertyGridView.cs
- CustomValidator.cs
- List.cs
- XmlChoiceIdentifierAttribute.cs
- SafeUserTokenHandle.cs
- FilterEventArgs.cs
- MulticastOption.cs
- EntityDataSourceDataSelectionPanel.cs
- Border.cs
- MessageDispatch.cs
- TextEditorThreadLocalStore.cs
- DataAdapter.cs
- SafeCryptoHandles.cs
- UserNamePasswordValidator.cs
- FixedPageProcessor.cs
- QilXmlWriter.cs
- Vector.cs
- HttpContextServiceHost.cs
- MexNamedPipeBindingCollectionElement.cs
- TypeDelegator.cs
- ProtocolViolationException.cs
- Timeline.cs
- AutomationIdentifier.cs
- NativeMethods.cs
- DurationConverter.cs
- FormViewUpdatedEventArgs.cs
- X509CertificateCollection.cs
- CommonDialog.cs
- FunctionParameter.cs
- SocketException.cs
- DictionaryManager.cs
- EntityDesignPluralizationHandler.cs
- GroupBox.cs
- EdmSchemaAttribute.cs
- ISAPIWorkerRequest.cs
- ParallelTimeline.cs
- Camera.cs