source: trunk/autoquest-core-usability/src/main/java/de/ugoe/cs/autoquest/usability/UsabilityDefectDescription.java @ 1301

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