internal virtual void FromXml( SecurityDocument doc, int position, bool allowInternalOnly )
{
if (doc == null)
throw new ArgumentNullException("doc");
Contract.EndContractBlock();
if (!doc.GetTagForElement( position ).Equals(s_str_PermissionSet))
throw new ArgumentException(String.Format( null, Environment.GetResourceString( "Argument_InvalidXMLElement" ), "PermissionSet", this.GetType().FullName) );
Reset();
m_allPermissionsDecoded = false;
Exception savedException = null;
String strUnrestricted = doc.GetAttributeForElement( position, "Unrestricted" );
if (strUnrestricted != null)
m_Unrestricted = strUnrestricted.Equals( "True" ) || strUnrestricted.Equals( "true" ) || strUnrestricted.Equals( "TRUE" );
else
m_Unrestricted = false;
ArrayList childrenIndices = doc.GetChildrenPositionForElement( position );
int childCount = childrenIndices.Count;
for (int i = 0; i < childCount; ++i)
{
int childIndex = (int)childrenIndices[i];
if (IsPermissionTag( doc.GetTagForElement( childIndex ), allowInternalOnly ))
{
try
{
String className = doc.GetAttributeForElement( childIndex, "class" );
PermissionToken token;
Object objectToInsert;
if (className != null)
{
token = PermissionToken.GetToken( className );
if (token == null)
{
objectToInsert = CreatePerm( doc.GetElement( childIndex, true ) );
if (objectToInsert != null)
{
#if _DEBUG
PermissionToken tokenDebug = PermissionToken.GetToken( (IPermission)objectToInsert );
Contract.Assert((tokenDebug != null), "PermissionToken.GetToken returned null ");
Contract.Assert( (tokenDebug.m_type & PermissionTokenType.BuiltIn) != 0, "This should only be called for built-ins" );
#endif
Contract.Assert( objectToInsert.GetType().Module.Assembly == System.Reflection.Assembly.GetExecutingAssembly(),
"PermissionToken.GetToken returned null for non-mscorlib permission" );
token = PermissionToken.GetToken( (IPermission)objectToInsert );
Contract.Assert((token != null), "PermissionToken.GetToken returned null ");
Contract.Assert( (token.m_type & PermissionTokenType.DontKnow) == 0, "We should always know the permission type when getting a token from an instance" );
}
}
else
{
objectToInsert = ((ISecurityElementFactory)new SecurityDocumentElement(doc, childIndex)).CreateSecurityElement();
}
}
else
{
IPermission ip = CreatePerm( doc.GetElement( childIndex, true ) );
if (ip == null)
{
token = null;
objectToInsert = null;
}
else
{
token = PermissionToken.GetToken( ip );
Contract.Assert( PermissionToken.IsTokenProperlyAssigned( ip, token ),
"PermissionToken was improperly assigned" );
objectToInsert = ip;
}
}
if (token != null && objectToInsert != null)
{
if (m_permSet == null)
m_permSet = new TokenBasedSet();
IPermission permInSlot = null;
if (this.m_permSet.GetItem( token.m_index ) != null)
{
// If there is already something in that slot, let's union them
// together.
if (this.m_permSet.GetItem( token.m_index ) is IPermission)
permInSlot = (IPermission)this.m_permSet.GetItem( token.m_index );
else
permInSlot = CreatePerm( this.m_permSet.GetItem( token.m_index ) );
}
if (permInSlot != null)
{
if (objectToInsert is IPermission)
objectToInsert = permInSlot.Union((IPermission)objectToInsert);
else
objectToInsert = permInSlot.Union(CreatePerm( objectToInsert ));
}
if(m_Unrestricted && objectToInsert is IPermission)
objectToInsert = null;
this.m_permSet.SetItem( token.m_index, objectToInsert );
}
}
catch (Exception e)
{
#if _DEBUG
if (debug)
DEBUG_WRITE( "error while decoding permission set =\n" + e.ToString() );
#endif
if (savedException == null)
savedException = e;
}
}
}
if (savedException != null)
throw savedException;
}