1 | // Copyright 2012 Georg-August-Universität Göttingen, Germany |
---|
2 | // |
---|
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
---|
4 | // you may not use this file except in compliance with the License. |
---|
5 | // You may obtain a copy of the License at |
---|
6 | // |
---|
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
---|
8 | // |
---|
9 | // Unless required by applicable law or agreed to in writing, software |
---|
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
---|
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
12 | // See the License for the specific language governing permissions and |
---|
13 | // limitations under the License. |
---|
14 | |
---|
15 | package de.ugoe.cs.autoquest.usability; |
---|
16 | |
---|
17 | import java.io.IOException; |
---|
18 | import java.io.InputStream; |
---|
19 | import java.util.ArrayList; |
---|
20 | import java.util.Collection; |
---|
21 | import java.util.List; |
---|
22 | import java.util.Map; |
---|
23 | |
---|
24 | import javax.xml.bind.JAXBContext; |
---|
25 | import javax.xml.bind.JAXBElement; |
---|
26 | import javax.xml.bind.Unmarshaller; |
---|
27 | |
---|
28 | /** |
---|
29 | * TODO comment |
---|
30 | * |
---|
31 | * @version $Revision: $ $Date: 18.07.2012$ |
---|
32 | * @author 2012, last modified by $Author: pharms$ |
---|
33 | */ |
---|
34 | public enum UsabilityDefectDescription { |
---|
35 | |
---|
36 | INEFFICIENT_ACTIONS, |
---|
37 | TEXT_FIELD_INPUT_RATIO, |
---|
38 | TEXT_FIELD_INPUT_REPETITIONS, |
---|
39 | TEXT_FIELD_NO_LETTER_OR_DIGIT_RATIO, |
---|
40 | COOCCURENCE_SUCCEED, |
---|
41 | COOCCURENCE_PRECED, |
---|
42 | HIGH_EVENT_COVERAGE, |
---|
43 | HIGH_TARGET_DISTANCE, |
---|
44 | MISSING_FEEDBACK, |
---|
45 | UNUSED_GUI_ELEMENTS; |
---|
46 | |
---|
47 | /** */ |
---|
48 | private static final String DEFAULT_MESSAGES_FILE = "defectDescriptions_en.xml"; |
---|
49 | |
---|
50 | /** */ |
---|
51 | private static DefectDescriptions sDefectDescriptions; |
---|
52 | |
---|
53 | /** */ |
---|
54 | private DefectDescription defectDescription; |
---|
55 | |
---|
56 | /** |
---|
57 | * |
---|
58 | */ |
---|
59 | private UsabilityDefectDescription() { |
---|
60 | init(); |
---|
61 | } |
---|
62 | |
---|
63 | /** |
---|
64 | * |
---|
65 | */ |
---|
66 | @SuppressWarnings("unchecked") |
---|
67 | private void init() { |
---|
68 | synchronized (this.getClass()) { |
---|
69 | if (sDefectDescriptions == null) { |
---|
70 | InputStream inputStream = |
---|
71 | ClassLoader.getSystemResourceAsStream(DEFAULT_MESSAGES_FILE); |
---|
72 | |
---|
73 | try { |
---|
74 | String packageName = DefectDescriptions.class.getPackage().getName(); |
---|
75 | JAXBContext jaxbContext = JAXBContext.newInstance(packageName); |
---|
76 | Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); |
---|
77 | |
---|
78 | sDefectDescriptions = |
---|
79 | ((JAXBElement<DefectDescriptions>) unmarshaller.unmarshal(inputStream)) |
---|
80 | .getValue(); |
---|
81 | } |
---|
82 | catch (Exception e) { |
---|
83 | throw new RuntimeException |
---|
84 | ("error while initializing usability defect descriptions", e); |
---|
85 | } |
---|
86 | finally { |
---|
87 | if (inputStream != null) { |
---|
88 | try { |
---|
89 | inputStream.close(); |
---|
90 | } |
---|
91 | catch (IOException e) { |
---|
92 | // ignore |
---|
93 | } |
---|
94 | } |
---|
95 | } |
---|
96 | } |
---|
97 | } |
---|
98 | |
---|
99 | for (DefectDescription description : sDefectDescriptions.getDefectDescription()) { |
---|
100 | if (this.name().equals(description.getDefectId())) { |
---|
101 | defectDescription = description; |
---|
102 | break; |
---|
103 | } |
---|
104 | } |
---|
105 | |
---|
106 | if (defectDescription == null) { |
---|
107 | throw new RuntimeException |
---|
108 | ("error while initializing usability defect descriptions. No " + |
---|
109 | "description text available for description " + this.name()); |
---|
110 | } |
---|
111 | } |
---|
112 | |
---|
113 | /** |
---|
114 | * |
---|
115 | */ |
---|
116 | public String[] getDescriptionParameters() { |
---|
117 | List<String> parameters = new ArrayList<String>(); |
---|
118 | |
---|
119 | for (Object fragment : defectDescription.getTextFragmentOrParameterFragment()) { |
---|
120 | if (fragment instanceof ParameterFragment) { |
---|
121 | parameters.add(((ParameterFragment) fragment).getParameterName()); |
---|
122 | } |
---|
123 | } |
---|
124 | |
---|
125 | return parameters.toArray(new String[parameters.size()]); |
---|
126 | } |
---|
127 | |
---|
128 | /** |
---|
129 | * |
---|
130 | */ |
---|
131 | public String toString(Map<String, Object> parameters) throws IllegalArgumentException { |
---|
132 | StringBuffer result = new StringBuffer(); |
---|
133 | |
---|
134 | for (Object fragment : defectDescription.getTextFragmentOrParameterFragment()) { |
---|
135 | if (result.length() > 0) { |
---|
136 | result.append(" "); |
---|
137 | } |
---|
138 | |
---|
139 | if (fragment instanceof ParameterFragment) { |
---|
140 | Object value = null; |
---|
141 | if (parameters != null) { |
---|
142 | value = parameters.get(((ParameterFragment) fragment).getParameterName()); |
---|
143 | } |
---|
144 | |
---|
145 | if (value != null) { |
---|
146 | if (value instanceof Collection<?>) { |
---|
147 | int counter = 1; |
---|
148 | for (Object elem : ((Collection<?>) value)) { |
---|
149 | result.append('\n'); |
---|
150 | result.append(counter++); |
---|
151 | result.append(".: "); |
---|
152 | result.append(elem); |
---|
153 | } |
---|
154 | } |
---|
155 | else { |
---|
156 | result.append(value.toString()); |
---|
157 | } |
---|
158 | } |
---|
159 | else { |
---|
160 | throw new IllegalArgumentException |
---|
161 | ("required parameter \"" + |
---|
162 | ((ParameterFragment) fragment).getParameterName() + |
---|
163 | "\" for usability defect description " + this.name() + " not provided"); |
---|
164 | } |
---|
165 | } |
---|
166 | else { |
---|
167 | result.append(getFragmentString(fragment)); |
---|
168 | } |
---|
169 | } |
---|
170 | |
---|
171 | return result.toString(); |
---|
172 | } |
---|
173 | |
---|
174 | /** |
---|
175 | * |
---|
176 | */ |
---|
177 | public List<Object> toFragmentList(Map<String, Object> parameters) |
---|
178 | throws IllegalArgumentException |
---|
179 | { |
---|
180 | List<Object> result = new ArrayList<Object>(); |
---|
181 | |
---|
182 | for (Object fragment : defectDescription.getTextFragmentOrParameterFragment()) { |
---|
183 | if (fragment instanceof ParameterFragment) { |
---|
184 | Object value = null; |
---|
185 | if (parameters != null) { |
---|
186 | value = parameters.get(((ParameterFragment) fragment).getParameterName()); |
---|
187 | } |
---|
188 | |
---|
189 | if (value != null) { |
---|
190 | result.add(value); |
---|
191 | } |
---|
192 | else { |
---|
193 | throw new IllegalArgumentException |
---|
194 | ("required parameter \"" + |
---|
195 | ((ParameterFragment) fragment).getParameterName() + |
---|
196 | "\" for usability defect description " + this.name() + " not provided"); |
---|
197 | } |
---|
198 | } |
---|
199 | else { |
---|
200 | result.add(getFragmentString(fragment)); |
---|
201 | } |
---|
202 | } |
---|
203 | |
---|
204 | return result; |
---|
205 | } |
---|
206 | |
---|
207 | /** |
---|
208 | * |
---|
209 | */ |
---|
210 | public String getBriefDescription() { |
---|
211 | return defectDescription.briefText; |
---|
212 | } |
---|
213 | |
---|
214 | /* |
---|
215 | * (non-Javadoc) |
---|
216 | * |
---|
217 | * @see java.lang.Enum#toString() |
---|
218 | */ |
---|
219 | @Override |
---|
220 | public String toString() { |
---|
221 | StringBuffer result = new StringBuffer(); |
---|
222 | |
---|
223 | int paramCount = 1; |
---|
224 | for (Object fragment : defectDescription.getTextFragmentOrParameterFragment()) { |
---|
225 | if (result.length() > 0) { |
---|
226 | result.append(" "); |
---|
227 | } |
---|
228 | |
---|
229 | if (fragment instanceof ParameterFragment) { |
---|
230 | result.append("<parameter"); |
---|
231 | result.append(paramCount++); |
---|
232 | result.append(">"); |
---|
233 | } |
---|
234 | else { |
---|
235 | result.append(getFragmentString(fragment)); |
---|
236 | } |
---|
237 | } |
---|
238 | |
---|
239 | return result.toString(); |
---|
240 | } |
---|
241 | |
---|
242 | /** |
---|
243 | * |
---|
244 | */ |
---|
245 | private String getFragmentString(Object fragment) { |
---|
246 | String fragmentStr = fragment.toString().trim(); |
---|
247 | |
---|
248 | fragmentStr = fragmentStr.replaceAll("\n", " "); |
---|
249 | |
---|
250 | while (fragmentStr.indexOf(" ") > -1) { |
---|
251 | fragmentStr = fragmentStr.replaceAll(" ", " "); |
---|
252 | } |
---|
253 | |
---|
254 | return fragmentStr; |
---|
255 | } |
---|
256 | |
---|
257 | } |
---|