001: package org.andromda.core.mapping;
002:
003: import java.io.FileNotFoundException;
004: import java.io.FileReader;
005: import java.util.ArrayList;
006: import java.util.Collection;
007: import java.util.Iterator;
008: import java.util.LinkedHashSet;
009: import java.util.List;
010:
011: import org.andromda.core.common.ExceptionUtils;
012: import org.andromda.core.common.ResourceUtils;
013:
014: /**
015: * A single child mapping instance belonging to a Mappings instance. It doesn't make sense to instantiate this class by
016: * itself.
017: *
018: * @author Chad Brandon
019: * @author Wouter Zoons
020: * @see org.andromda.core.mapping.Mappings
021: */
022: public class Mapping {
023: /**
024: * Stores the from elements.
025: */
026: private final Collection froms = new LinkedHashSet();
027:
028: /**
029: * Adds the <code>from</code> type to the mapping.
030: *
031: * @param from the type that we are mapping from.
032: */
033: public void addFrom(final String from) {
034: ExceptionUtils.checkNull("from", from);
035: froms.add(from);
036: }
037:
038: /**
039: * Return the Collection of froms.
040: *
041: * @return Collection
042: */
043: public Collection getFroms() {
044: return froms;
045: }
046:
047: /**
048: * Returns the to type for this mapping.
049: *
050: * @return String the to type
051: */
052: public String getTo() {
053: if (!this .paths.isEmpty()) {
054: try {
055: final StringBuffer pathsContents = new StringBuffer();
056: for (final Iterator iterator = this .paths.iterator(); iterator
057: .hasNext();) {
058: pathsContents.append(ResourceUtils
059: .getContents(new FileReader(this .mappings
060: .getCompletePath((String) iterator
061: .next()))));
062: }
063: this .to = pathsContents.toString();
064: } catch (final FileNotFoundException exception) {
065: throw new MappingsException(exception);
066: }
067: }
068: return this .to;
069: }
070:
071: /**
072: * Stores any paths used by this mapping.
073: */
074: private final List paths = new ArrayList();
075:
076: /**
077: * Adds the path to the listof paths.
078: * @param path
079: */
080: public void addPath(final String path) {
081: this .paths.add(path);
082: }
083:
084: /**
085: * Stores the to mapping.
086: */
087: private String to;
088:
089: /**
090: * Sets the type for this mapping.
091: *
092: * @param to the value to which the from
093: * values are mapped.
094: */
095: public void setTo(final String to) {
096: this .to = to;
097: }
098:
099: /**
100: * The parent of this instance.
101: */
102: private Mappings mappings;
103:
104: /**
105: * Sets the mappings to which this Mapping instance
106: * belongs.
107: *
108: * @param mappings the owning mappings.
109: */
110: final void setMappings(final Mappings mappings) {
111: this .mappings = mappings;
112: }
113:
114: /**
115: * Returns a String representation of this mapping in the form of <code>from1, from2, from3 --> to</code>.
116: *
117: * @return a String representing the mapping instance
118: */
119: public String toString() {
120: final StringBuffer buffer = new StringBuffer(512); // 512 should be enough for resizing not to occur
121:
122: for (Iterator fromIterator = this .froms.iterator(); fromIterator
123: .hasNext();) {
124: final String from = (String) fromIterator.next();
125: buffer.append(from);
126:
127: if (fromIterator.hasNext()) {
128: buffer.append(", ");
129: }
130: }
131:
132: buffer.append(" --> ").append(this.to);
133:
134: return buffer.toString();
135: }
136: }
|