|
/*
* Copyright 2006, Dustin Metzgar
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.CodeDom;
using System.Collections.Generic;
namespace XmiCodeDomLib.Version1_2
{
[XmiParser("UML:Association")]
public class XmiAssociation : XmiBaseClass, IModelElement, IGeneralizableElement
{
private List<XmiAssociationEnd> _Connection = new List<XmiAssociationEnd>();
public List<XmiAssociationEnd> Connection
{
get { return _Connection; }
}
#region IModelElement Members
private string _Name = string.Empty;
private bool _IsSpecification;
private string _Visibility = string.Empty;
[XmiAttribute]
public string Name
{
get
{
return _Name;
}
set
{
_Name = value;
}
}
[XmiAttribute]
public bool IsSpecification
{
get
{
return _IsSpecification;
}
set
{
_IsSpecification = value;
}
}
[XmiAttribute]
public string Visibility
{
get
{
return _Visibility;
}
set
{
_Visibility = value;
}
}
#endregion
#region IGeneralizableElement Members
private bool _IsRoot;
private bool _IsLeaf;
private bool _IsAbstract;
[XmiAttribute]
public bool IsRoot
{
get
{
return _IsRoot;
}
set
{
_IsRoot = value;
}
}
[XmiAttribute]
public bool IsLeaf
{
get
{
return _IsLeaf;
}
set
{
_IsLeaf = value;
}
}
[XmiAttribute]
public bool IsAbstract
{
get
{
return _IsAbstract;
}
set
{
_IsAbstract = value;
}
}
#endregion
/// <summary>
/// Associations have to be added after all the classes have been added to the namespace.
/// </summary>
/// <param name="cn"></param>
public void AddToCodeModel(CodeNamespace cn, CodeDomHelper cdhelper)
{
// There are some kinds of associations which are not parameters between classes.
// Those are not currently handled by this code.
if (_Connection.Count == 2)
{
if (_Connection[1].IsNavigable)
AddConnection(_Connection[0], _Connection[1], cn, cdhelper);
if (_Connection[0].IsNavigable)
AddConnection(_Connection[1], _Connection[0], cn, cdhelper);
}
}
private void AddConnection(XmiAssociationEnd connFrom, XmiAssociationEnd connTo, CodeNamespace cn,
CodeDomHelper cdhelper)
{
XmiBaseClass typeTo, typeFrom;
if (connFrom.Type != null)
typeFrom = connFrom.Type;
else if (connFrom.Participant != null)
typeFrom = connFrom.Participant;
else
return;
if (connTo.Type != null)
typeTo = connTo.Type;
else if (connTo.Participant != null)
typeTo = connTo.Participant;
else
return;
if (cdhelper.Classes.ContainsKey(typeFrom.XmiIdRef))
{
string fromClass = cdhelper.Classes[typeFrom.XmiIdRef].Name;
fromClass = XmiRoot.GetSafeName(fromClass);
CodeTypeDeclaration ctdFrom = null;
foreach (CodeTypeDeclaration ctd in cn.Types)
{
if (ctd.Name == fromClass)
{
ctdFrom = ctd;
break;
}
}
if (ctdFrom != null)
{
CodeTypeReference ctr = cdhelper.ResolveTypeRef(typeTo);
XmiMultRange xmr = cdhelper.ResolveMultiplicity(connTo.Multiplicity);
string name = XmiRoot.GetSafeName(connTo.Name);
if (name == string.Empty)
name = XmiRoot.GetSafeName(Name);
if (name != string.Empty)
{
if (xmr.IsArray)
ctr.ArrayRank = 1;
CodeMemberField cmf = new CodeMemberField(ctr, "_" + name);
CodeMemberProperty cmp = new CodeMemberProperty();
cmp.Name = name;
cmp.Type = ctr;
cmp.Attributes = MemberAttributes.Public;
cmp.HasGet = true;
CodeThisReferenceExpression cThis = new CodeThisReferenceExpression();
CodeFieldReferenceExpression fieldRef = new CodeFieldReferenceExpression(cThis, "_" + name);
CodeMethodReturnStatement ret = new CodeMethodReturnStatement(fieldRef);
cmp.GetStatements.Add(ret);
cmp.HasSet = true;
CodePropertySetValueReferenceExpression cValue = new CodePropertySetValueReferenceExpression();
CodeAssignStatement setStmt = new CodeAssignStatement(fieldRef, cValue);
cmp.SetStatements.Add(setStmt);
ctdFrom.Members.Add(cmf);
ctdFrom.Members.Add(cmp);
}
}
}
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.