View Javadoc
1   package org.andromda.core.cartridge.template;
2   
3   import java.util.ArrayList;
4   import java.util.Collection;
5   import java.util.LinkedHashSet;
6   import java.util.Set;
7   import org.andromda.core.common.ExceptionUtils;
8   import org.andromda.core.metafacade.MetafacadeBase;
9   import org.apache.commons.lang.StringUtils;
10  
11  /**
12   * Defines the <modelElements/> element within a <template/> within an XML cartridge descriptor. This allows
13   * the grouping of model elements by criteria defined within the nested {@link ModelElement}instances.
14   *
15   * @author Chad Brandon
16   * @author Michail Plushnikov
17   * @see Template
18   * @see ModelElement
19   */
20  public class ModelElements
21  {
22      private String variable;
23      private final Collection<ModelElement> modelElements = new ArrayList<ModelElement>();
24  
25      /**
26       * The variable name to make the model element available to the template engine. For example if you have the
27       * modelElement &lt;&lt;entity&gt;&gt; defined within your &lt;&lt;modelElements&gt;&gt; element you may want to
28       * define this value as <code>entity</code>. If on the other hand the outputToSingleFile flag is set to true you'd
29       * probably want to make it available as <code>entities</code>.
30       *
31       * @return Returns the variable.
32       */
33      public String getVariable()
34      {
35          return variable;
36      }
37  
38      /**
39       * @param variable The variable to set.
40       */
41      public void setVariable(String variable)
42      {
43          variable = StringUtils.trimToEmpty(variable);
44          ExceptionUtils.checkEmpty("variable", variable);
45          this.variable = variable;
46      }
47  
48      /**
49       * Adds a modelElement to the collection of <code>modelElements</code>.
50       *
51       * @param modelElement the new ModelElement to add.
52       */
53      public void addModelElement(final ModelElement modelElement)
54      {
55          ExceptionUtils.checkNull("modelElement", modelElement);
56          modelElements.add(modelElement);
57      }
58  
59      /**
60       * Gets all metafacade instances from each ModelElement belonging to this ModelElements instance.
61       *
62       * @return Collection of all metafacades.
63       */
64      public Set<MetafacadeBase> getAllMetafacades()
65      {
66          final Set<MetafacadeBase> allMetafacades = new LinkedHashSet<MetafacadeBase>();
67          for (ModelElement modelElement : modelElements)
68          {
69              allMetafacades.addAll(modelElement.getMetafacades());
70          }
71          return allMetafacades;
72      }
73  
74      /**
75       * Returns all model elements belonging to this model elements instance.
76       *
77       * @return Collection of all {@link ModelElement}instances.
78       */
79      public Collection<ModelElement> getModelElements()
80      {
81          return this.modelElements;
82      }
83  
84      /**
85       * Returns true if this instance has no <code>modelElements</code> stored within it.
86       *
87       * @return true/false
88       */
89      public boolean isEmpty()
90      {
91          return this.modelElements.isEmpty();
92      }
93  }