View Javadoc
1   package org.andromda.core.mapping;
2   
3   import java.io.FileNotFoundException;
4   import java.io.FileReader;
5   import java.util.ArrayList;
6   import java.util.Collection;
7   import java.util.Iterator;
8   import java.util.LinkedHashSet;
9   import java.util.List;
10  import org.andromda.core.common.ExceptionUtils;
11  import org.andromda.core.common.ResourceUtils;
12  import org.apache.commons.lang.StringUtils;
13  
14  /**
15   * A single child mapping instance belonging to a Mappings instance. It doesn't make sense to instantiate this class by
16   * itself.
17   *
18   * @author Chad Brandon
19   * @author Wouter Zoons
20   * @see org.andromda.core.mapping.Mappings
21   */
22  public class Mapping
23  {
24      /**
25       * Stores the from elements.
26       */
27      private final Collection<String> froms = new LinkedHashSet<String>();
28  
29      /**
30       * Adds the <code>from</code> type to the mapping.
31       *
32       * @param from the type that we are mapping from.
33       */
34      public void addFrom(final String from)
35      {
36          ExceptionUtils.checkNull("from", from);
37          froms.add(from);
38      }
39  
40      /**
41       * Return the Collection of froms.
42       *
43       * @return Collection
44       */
45      public Collection<String> getFroms()
46      {
47          return froms;
48      }
49  
50      /**
51       * Returns the to type for this mapping.
52       *
53       * @return String the to type
54       */
55      public String getTo()
56      {
57          final StringBuilder to = new StringBuilder();
58          if (StringUtils.isNotBlank(this.to))
59          {
60              to.append(this.to);
61          }
62          if (!this.paths.isEmpty())
63          {
64              try
65              {
66                  for (final String path : paths)
67                  {
68                      to.append(
69                          ResourceUtils.getContents(
70                              new FileReader(this.mappings.getCompletePath(path))));
71                  }
72              }
73              catch (final FileNotFoundException exception)
74              {
75                  throw new MappingsException(exception);
76              }
77          }
78          return to.toString();
79      }
80  
81      /**
82       * Stores any paths used by this mapping.
83       */
84      private final List<String> paths = new ArrayList<String>();
85  
86      /**
87       * Adds the path to the listof paths.
88       * @param path
89       */
90      public void addPath(final String path)
91      {
92          this.paths.add(path);
93      }
94  
95      /**
96       * Stores the to mapping.
97       */
98      private String to;
99  
100     /**
101      * Sets the type for this mapping.
102      *
103      * @param to the value to which the from
104      *        values are mapped.
105      */
106     public void setTo(final String to)
107     {
108         this.to = to;
109     }
110 
111     /**
112      * The parent of this instance.
113      */
114     private Mappings mappings;
115 
116     /**
117      * Sets the mappings to which this Mapping instance
118      * belongs.
119      *
120      * @param mappings the owning mappings.
121      */
122     final void setMappings(final Mappings mappings)
123     {
124         this.mappings = mappings;
125     }
126 
127     /**
128      * Returns a String representation of this mapping in the form of <code>from1, from2, from3 --> to</code>.
129      *
130      * @return a String representing the mapping instance
131      */
132     public String toString()
133     {
134         final StringBuilder buffer = new StringBuilder(512); // 512 should be enough for resizing not to occur
135 
136         for (Iterator<String> fromIterator = this.froms.iterator(); fromIterator.hasNext();)
137         {
138             final String from = fromIterator.next();
139             buffer.append(from);
140 
141             if (fromIterator.hasNext())
142             {
143                 buffer.append(", ");
144             }
145         }
146 
147         buffer.append(" --> ").append(this.to);
148 
149         return buffer.toString();
150     }
151 }