View Javadoc

1   package org.ocltf.translation.library;
2   
3   import java.util.HashMap;
4   import java.util.Map;
5   
6   import org.apache.commons.lang.StringUtils;
7   import org.apache.commons.lang.builder.ToStringBuilder;
8   import org.ocltf.utils.ExceptionUtils;
9   
10  
11  /***
12   * A Translation "fragment" of a translation file.  
13   * This fragment belongs to a Translation object.
14   * 
15   * @see org.ocltf.translation.library.Translation
16   * 
17   * @author Chad Brandon
18   */
19  public class Fragment {
20  
21  	private String name;
22      private String handlerMethod;
23      
24  	/***
25  	 * The Translation to which this 
26  	 * Fragment belongs.
27  	 */
28  	private Translation translation;
29  
30  	/***
31  	 * The possible kinds available to this
32  	 * Fragment
33  	 */
34  	private Map kinds = new HashMap();
35  	
36  	/***
37  	 * It doesn't make any sense to instatiate this
38  	 * object explicitly. It intended to be instantiated
39  	 * as part of a Translation.
40  	 * 
41  	 * @see org.ocltf.translation.library.Translation
42  	 */
43  	public Fragment() {}
44  	
45  	/***
46  	 * Gets the name of this fragment
47  	 * @return
48  	 */
49  	public String getName() {
50  		return name;
51  	}
52  	
53  	/***
54  	 * Sets the name of this fragment.
55  	 * @param name
56  	 */
57  	public void setName(String name) {
58  		this.name = StringUtils.trimToEmpty(name);;
59  	}
60  	
61  	/***
62  	 * Returns the kinds contained within
63  	 * this translation fragment
64  	 * 
65  	 * @return Map the Kinds keyed by name.
66  	 */
67  	public Map getKinds() {
68  		return this.kinds;
69  	}
70  	
71  	/***
72  	 * Returns the body for the fragment kind with the
73  	 * specified name.
74  	 * 
75       * @param name the name of the kind to get.
76  	 * @return FragmentKind
77  	 */
78  	public String getKind(String name) {
79  		String methodName = "getKind";
80  		
81  		//clean the name first
82  		name = StringUtils.trimToEmpty(name);
83  		
84  		ExceptionUtils.checkEmpty(methodName, "name", name);
85  		String kind = StringUtils.trimToEmpty((String)kinds.get(name));
86  		if (kind == null) {
87  			throw new LibraryException(methodName
88  				+ " - no kind (" + name 
89  				+ ") could be found for the translation fragment ("
90  				+ this.getName() + ") check the fragment (" 
91  				+ this.getName() + ") in translation file --> " 
92  				+ getTranslation().getLibraryTranslation().getFile());
93  		}
94  		return kind;
95  	}
96  	
97  	/***
98  	 * Adds the specified kind having the specified 
99  	 * name and body to the Fragment.
100 	 * 
101 	 * @param name the name of the kind of expression.
102 	 * @param body the body of the kind of expression.
103 	 */
104 	public void addKind(String name, String body) {
105     	kinds.put(StringUtils.trimToEmpty(name), body);
106 	}
107 	
108     /***
109      * Returns the name of the handler method.
110      * 
111      * @return Returns the handlerMethod.
112      */
113     public String getHandlerMethod() {
114         return this.handlerMethod;
115     }
116 
117     /***
118      * Sets the name of the handler method.  This method
119      * is the method within the Translator that handles
120      * the processing of the fragment.
121      * 
122      * @see org.ocltf.translation.Translator
123      * 
124      * @param handlerMethod The handlerMethod to set.
125      */
126     public void setHandlerMethod(String handlerMethod) {
127         this.handlerMethod = handlerMethod;
128     }
129 
130     
131 	/***
132 	 * Gets the Translation to which this Fragment belongs.
133 	 * 
134 	 * @return Translation 
135 	 */
136 	public Translation getTranslation() {
137 		String methodName = "getTranslation";
138 		//should never happen, but it doesn't hurt to be safe
139 		if (this.translation == null) {
140 			throw new LibraryException(methodName
141 				+ " - translation can not be null");
142 		}
143 		return translation;
144 	}
145 
146 	/***
147 	 * Sets the Translation to which this Fragment belongs.
148 	 * 
149 	 * @param translation
150 	 */
151 	public void setTranslation(Translation translation) {
152 		this.translation = translation;
153 	}
154 	
155     /***
156      * @see java.lang.Object#toString()
157      */
158 	public String toString() {
159 		return ToStringBuilder.reflectionToString(this);
160 	}
161 
162 }