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
16
17
18
19
20
21
22 public class Mapping
23 {
24
25
26
27 private final Collection<String> froms = new LinkedHashSet<String>();
28
29
30
31
32
33
34 public void addFrom(final String from)
35 {
36 ExceptionUtils.checkNull("from", from);
37 froms.add(from);
38 }
39
40
41
42
43
44
45 public Collection<String> getFroms()
46 {
47 return froms;
48 }
49
50
51
52
53
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
83
84 private final List<String> paths = new ArrayList<String>();
85
86
87
88
89
90 public void addPath(final String path)
91 {
92 this.paths.add(path);
93 }
94
95
96
97
98 private String to;
99
100
101
102
103
104
105
106 public void setTo(final String to)
107 {
108 this.to = to;
109 }
110
111
112
113
114 private Mappings mappings;
115
116
117
118
119
120
121
122 final void setMappings(final Mappings mappings)
123 {
124 this.mappings = mappings;
125 }
126
127
128
129
130
131
132 public String toString()
133 {
134 final StringBuilder buffer = new StringBuilder(512);
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 }