View Javadoc

1   package org.andromda.core.uml14;
2   
3   import java.util.Collection;
4   import java.util.Collections;
5   import java.util.Iterator;
6   import java.util.Vector;
7   
8   import org.andromda.core.common.DbMappingTable;
9   import org.andromda.core.common.ScriptHelper;
10  import org.omg.uml.UmlPackage;
11  import org.omg.uml.foundation.core.ModelElement;
12  import org.omg.uml.foundation.core.TaggedValue;
13  import org.omg.uml.modelmanagement.Model;
14  
15  /***
16   * Implements the minimum set of API, for UML v1.4 based models, needed
17   * by AndroMDA to perform code generation.
18   * 
19   * @author Anthony Mowers
20   */
21  public class UMLDefaultHelper
22  	implements ScriptHelper
23  {
24  	protected UmlPackage model;
25  	protected DbMappingTable typeMappings;
26  
27  	public void setModel(Object model)
28  	{
29  		this.model = (UmlPackage) model;
30  	}
31  	
32      public Object getModel()
33      {
34          return this.model;
35      }
36      
37  	public void setTypeMappings(DbMappingTable mappings)
38  	{
39  		this.typeMappings = mappings;
40  	}
41  	
42  
43  	public String getName(Object object)
44  	{
45  		if ((object == null) || !(object instanceof ModelElement))
46  		{
47  			return null;
48  		}
49  		ModelElement modelElement = (ModelElement) object;
50          if (object instanceof TaggedValue)
51          {
52              return getTaggedValueName((TaggedValue)object);
53          }
54  
55  		return modelElement.getName();
56  	}
57  
58  
59  	public String getPackageName(Object object)
60  	{
61  		if ((object == null) || !(object instanceof ModelElement))
62  		{
63  			return null;
64  		}
65  
66  		ModelElement modelElement = (ModelElement) object;
67  		String packageName = "";
68  
69  		for (ModelElement namespace = modelElement.getNamespace();
70  			(namespace instanceof
71  			org.omg.uml.modelmanagement.UmlPackage)
72  			 && !
73  			(namespace instanceof Model);
74  			namespace = namespace.getNamespace())
75  		{
76  			packageName = "".equals(packageName) ?
77  				namespace.getName()
78  				 : namespace.getName() + "." + packageName;
79  		}
80  
81  		returng> packageName;
82  	}
83  	
84  	public Collection getModelElements()
85  	{
86  		return model.getCore().getModelElement().refAllOfType();
87  	}
88  
89  	public Collection getStereotypeNames(Object object)
90  	{
91  		if ((object == null) || !(object instanceof ModelElement))
92  		{
93  			return Collections.EMPTY_LIST;
94  		}
95  
96  		ModelElement modelElement = (ModelElement) object;
97  		Collection stereoTypeNames = new Vector();
98  
99  		Collection stereotypes = modelElement.getStereotype();
100 		for (Iterator i = stereotypes.iterator(); i.hasNext(); )
101 		{
102 			ModelElement stereotype = (ModelElement) i.next();
103 			stereoTypeNames.add(stereotype.getName());
104 		}
105 
106 		return stereoTypeNames;
107 	}
108 
109     private String getTaggedValueName(TaggedValue tgv)
110     {
111         String tgvName = tgv.getName();
112             
113         // sometimes the tag name is on the TagDefinition
114         if ( (tgvName == null) && (tgv.getType() != null) )
115         {
116             tgvName = tgv.getType().getName();
117                 
118             // sometimes it is the TagType
119             if (tgvName == null)
120             {
121                 tgvName = tgv.getType().getTagType();
122             }
123         }
124                     
125         return tgvName;
126     }
127     
128 }