001: package org.andromda.core.translation.library;
002:
003: import java.util.LinkedHashMap;
004: import java.util.Map;
005:
006: import org.andromda.core.common.ExceptionUtils;
007: import org.apache.commons.lang.StringUtils;
008: import org.apache.commons.lang.builder.ToStringBuilder;
009:
010: /**
011: * A Translation "fragment" of a translation file. This fragment belongs to a Translation object.
012: *
013: * @author Chad Brandon
014: * @see org.andromda.core.translation.library.Translation
015: */
016: public class Fragment {
017: private String name;
018: private String handlerMethod;
019:
020: /**
021: * The Translation to which this Fragment belongs.
022: */
023: private Translation translation;
024:
025: /**
026: * The possible kinds available to this Fragment
027: */
028: private Map kinds = new LinkedHashMap();
029:
030: /**
031: * It doesn't make any sense to instatiate this object explicitly. It intended to be instantiated as part of a
032: * Translation.
033: *
034: * @see org.andromda.core.translation.library.Translation
035: */
036: public Fragment() {
037: // Here for documentation purposes
038: }
039:
040: /**
041: * Gets the name of this fragment
042: *
043: * @return the name of this fragment.
044: */
045: public String getName() {
046: return name;
047: }
048:
049: /**
050: * Sets the name of this fragment.
051: *
052: * @param name the name to set.
053: */
054: public void setName(final String name) {
055: this .name = StringUtils.trimToEmpty(name);
056: }
057:
058: /**
059: * Returns the kinds contained within this translation fragment
060: *
061: * @return Map the Kinds keyed by name.
062: */
063: public Map getKinds() {
064: return this .kinds;
065: }
066:
067: /**
068: * Returns the body for the fragment kind with the specified name.
069: *
070: * @param name the name of the kind to get.
071: * @return FragmentKind
072: */
073: public String getKind(String name) {
074: // clean the name first
075: name = StringUtils.trimToEmpty(name);
076: ExceptionUtils.checkEmpty("name", name);
077: String kind = StringUtils.trimToEmpty((String) kinds.get(name));
078: if (kind == null) {
079: throw new LibraryException("No kind '"
080: + name
081: + "' could be found for the translation fragment '"
082: + this .getName()
083: + "' check the fragment '"
084: + this .getName()
085: + "' in translation template --> '"
086: + getTranslation().getLibraryTranslation()
087: .getTemplate() + "'");
088: }
089: return kind;
090: }
091:
092: /**
093: * Adds the specified kind having the specified name and body to the Fragment.
094: *
095: * @param name the name of the kind of expression.
096: * @param body the body of the kind of expression.
097: */
098: public void addKind(final String name, final String body) {
099: kinds.put(StringUtils.trimToEmpty(name), body);
100: }
101:
102: /**
103: * Returns the name of the handler method.
104: *
105: * @return Returns the handlerMethod.
106: */
107: public String getHandlerMethod() {
108: return this .handlerMethod;
109: }
110:
111: /**
112: * Sets the name of the handler method. This method is the method within the Translator that handles the processing
113: * of the fragment.
114: *
115: * @param handlerMethod The handlerMethod to set.
116: * @see org.andromda.core.translation.Translator
117: */
118: public void setHandlerMethod(final String handlerMethod) {
119: this .handlerMethod = handlerMethod;
120: }
121:
122: /**
123: * Gets the Translation to which this Fragment belongs.
124: *
125: * @return Translation
126: */
127: public Translation getTranslation() {
128: final String methodName = "Fragment.getTranslation";
129:
130: // should never happen, but it doesn't hurt to be safe
131: if (this .translation == null) {
132: throw new LibraryException(methodName
133: + " - translation can not be null");
134: }
135: return translation;
136: }
137:
138: /**
139: * Sets the Translation to which this Fragment belongs.
140: *
141: * @param translation
142: */
143: public void setTranslation(final Translation translation) {
144: this .translation = translation;
145: }
146:
147: /**
148: * @see java.lang.Object#toString()
149: */
150: public String toString() {
151: return ToStringBuilder.reflectionToString(this);
152: }
153: }
|