1 | package de.ugoe.cs.quest.usability; |
---|
2 | |
---|
3 | import java.io.IOException; |
---|
4 | import java.io.InputStream; |
---|
5 | import java.util.ArrayList; |
---|
6 | import java.util.List; |
---|
7 | import java.util.Map; |
---|
8 | |
---|
9 | import javax.xml.bind.JAXBContext; |
---|
10 | import javax.xml.bind.JAXBElement; |
---|
11 | import javax.xml.bind.Unmarshaller; |
---|
12 | |
---|
13 | /** |
---|
14 | * TODO comment |
---|
15 | * |
---|
16 | * @version $Revision: $ $Date: 18.07.2012$ |
---|
17 | * @author 2012, last modified by $Author: pharms$ |
---|
18 | */ |
---|
19 | public enum UsabilityDefectDescription { |
---|
20 | |
---|
21 | TEXT_FIELD_INPUT_RATIO, |
---|
22 | TEXT_FIELD_INPUT_REPETITIONS, |
---|
23 | TEXT_FIELD_NO_LETTER_OR_DIGIT_RATIO; |
---|
24 | |
---|
25 | /** */ |
---|
26 | private static final String DEFAULT_MESSAGES_FILE = "defectDescriptions_en.xml"; |
---|
27 | |
---|
28 | /** */ |
---|
29 | private static DefectDescriptions sDefectDescriptions; |
---|
30 | |
---|
31 | /** */ |
---|
32 | private DefectDescription defectDescription; |
---|
33 | |
---|
34 | /** |
---|
35 | * TODO: comment |
---|
36 | * |
---|
37 | * @param name |
---|
38 | * @param ordinal |
---|
39 | */ |
---|
40 | private UsabilityDefectDescription() { |
---|
41 | init(); |
---|
42 | } |
---|
43 | |
---|
44 | /** |
---|
45 | * TODO: comment |
---|
46 | * |
---|
47 | */ |
---|
48 | @SuppressWarnings("unchecked") |
---|
49 | private void init() { |
---|
50 | synchronized (this.getClass()) { |
---|
51 | if (sDefectDescriptions == null) { |
---|
52 | InputStream inputStream = |
---|
53 | ClassLoader.getSystemResourceAsStream(DEFAULT_MESSAGES_FILE); |
---|
54 | |
---|
55 | try { |
---|
56 | String packageName = DefectDescriptions.class.getPackage().getName(); |
---|
57 | JAXBContext jaxbContext = JAXBContext.newInstance(packageName); |
---|
58 | Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); |
---|
59 | |
---|
60 | sDefectDescriptions = |
---|
61 | ((JAXBElement<DefectDescriptions>) unmarshaller.unmarshal(inputStream)) |
---|
62 | .getValue(); |
---|
63 | } |
---|
64 | catch (Exception e) { |
---|
65 | throw new RuntimeException |
---|
66 | ("error while initializing usability defect descriptions", e); |
---|
67 | } |
---|
68 | finally { |
---|
69 | if (inputStream != null) { |
---|
70 | try { |
---|
71 | inputStream.close(); |
---|
72 | } |
---|
73 | catch (IOException e) { |
---|
74 | // ignore |
---|
75 | } |
---|
76 | } |
---|
77 | } |
---|
78 | } |
---|
79 | } |
---|
80 | |
---|
81 | for (DefectDescription description : sDefectDescriptions.getDefectDescription()) { |
---|
82 | if (this.name().equals(description.getDefectId())) { |
---|
83 | defectDescription = description; |
---|
84 | break; |
---|
85 | } |
---|
86 | } |
---|
87 | |
---|
88 | if (defectDescription == null) { |
---|
89 | throw new RuntimeException |
---|
90 | ("error while initializing usability defect descriptions. No " + |
---|
91 | "description text available for description " + this.name()); |
---|
92 | } |
---|
93 | } |
---|
94 | |
---|
95 | /** |
---|
96 | * |
---|
97 | */ |
---|
98 | public String[] getDescriptionParameters() { |
---|
99 | List<String> parameters = new ArrayList<String>(); |
---|
100 | |
---|
101 | for (Object fragment : defectDescription.getTextFragmentOrParameterFragment()) { |
---|
102 | if (fragment instanceof ParameterFragment) { |
---|
103 | parameters.add(((ParameterFragment) fragment).getParameterName()); |
---|
104 | } |
---|
105 | } |
---|
106 | |
---|
107 | return parameters.toArray(new String[parameters.size()]); |
---|
108 | } |
---|
109 | |
---|
110 | /** |
---|
111 | * |
---|
112 | */ |
---|
113 | public String toString(Map<String, String> parameters) throws IllegalArgumentException { |
---|
114 | StringBuffer result = new StringBuffer(); |
---|
115 | |
---|
116 | for (Object fragment : defectDescription.getTextFragmentOrParameterFragment()) { |
---|
117 | if (result.length() > 0) { |
---|
118 | result.append(" "); |
---|
119 | } |
---|
120 | |
---|
121 | if (fragment instanceof ParameterFragment) { |
---|
122 | String value = null; |
---|
123 | if (parameters != null) { |
---|
124 | value = parameters.get(((ParameterFragment) fragment).getParameterName()); |
---|
125 | } |
---|
126 | |
---|
127 | if (value != null) { |
---|
128 | result.append(value); |
---|
129 | } |
---|
130 | else { |
---|
131 | throw new IllegalArgumentException |
---|
132 | ("required parameter \"" + |
---|
133 | ((ParameterFragment) fragment).getParameterName() + |
---|
134 | "\" for usability defect description " + this.name() + " not provided"); |
---|
135 | } |
---|
136 | } |
---|
137 | else { |
---|
138 | result.append(getFragmentString(fragment)); |
---|
139 | } |
---|
140 | } |
---|
141 | |
---|
142 | return result.toString(); |
---|
143 | } |
---|
144 | |
---|
145 | /* |
---|
146 | * (non-Javadoc) |
---|
147 | * |
---|
148 | * @see java.lang.Enum#toString() |
---|
149 | */ |
---|
150 | @Override |
---|
151 | public String toString() { |
---|
152 | StringBuffer result = new StringBuffer(); |
---|
153 | |
---|
154 | int paramCount = 1; |
---|
155 | for (Object fragment : defectDescription.getTextFragmentOrParameterFragment()) { |
---|
156 | if (result.length() > 0) { |
---|
157 | result.append(" "); |
---|
158 | } |
---|
159 | |
---|
160 | if (fragment instanceof ParameterFragment) { |
---|
161 | result.append("<parameter"); |
---|
162 | result.append(paramCount++); |
---|
163 | result.append(">"); |
---|
164 | } |
---|
165 | else { |
---|
166 | result.append(getFragmentString(fragment)); |
---|
167 | } |
---|
168 | } |
---|
169 | |
---|
170 | return result.toString(); |
---|
171 | } |
---|
172 | |
---|
173 | /** |
---|
174 | * TODO: comment |
---|
175 | * |
---|
176 | * @param fragment |
---|
177 | * @return |
---|
178 | */ |
---|
179 | private String getFragmentString(Object fragment) { |
---|
180 | String fragmentStr = fragment.toString().trim(); |
---|
181 | |
---|
182 | fragmentStr = fragmentStr.replaceAll("\n", " "); |
---|
183 | |
---|
184 | while (fragmentStr.indexOf(" ") > -1) { |
---|
185 | fragmentStr = fragmentStr.replaceAll(" ", " "); |
---|
186 | } |
---|
187 | |
---|
188 | return fragmentStr; |
---|
189 | } |
---|
190 | |
---|
191 | } |
---|