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

Last change on this file since 927 was 927, checked in by sherbold, 12 years ago
  • added copyright under the Apache License, Version 2.0
File size: 6.0 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     * TODO: comment
50     *
51     * @param name
52     * @param ordinal
53     */
54    private UsabilityDefectDescription() {
55        init();
56    }
57
58    /**
59     * TODO: comment
60     *
61     */
62    @SuppressWarnings("unchecked")
63    private void init() {
64        synchronized (this.getClass()) {
65            if (sDefectDescriptions == null) {
66                InputStream inputStream =
67                    ClassLoader.getSystemResourceAsStream(DEFAULT_MESSAGES_FILE);
68
69                try {
70                    String packageName = DefectDescriptions.class.getPackage().getName();
71                    JAXBContext jaxbContext = JAXBContext.newInstance(packageName);
72                    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
73
74                    sDefectDescriptions =
75                        ((JAXBElement<DefectDescriptions>) unmarshaller.unmarshal(inputStream))
76                            .getValue();
77                }
78                catch (Exception e) {
79                    throw new RuntimeException
80                        ("error while initializing usability defect descriptions", e);
81                }
82                finally {
83                    if (inputStream != null) {
84                        try {
85                            inputStream.close();
86                        }
87                        catch (IOException e) {
88                            // ignore
89                        }
90                    }
91                }
92            }
93        }
94
95        for (DefectDescription description : sDefectDescriptions.getDefectDescription()) {
96            if (this.name().equals(description.getDefectId())) {
97                defectDescription = description;
98                break;
99            }
100        }
101
102        if (defectDescription == null) {
103            throw new RuntimeException
104                ("error while initializing usability defect descriptions. No " +
105                 "description text available for description " + this.name());
106        }
107    }
108
109    /**
110   *
111   */
112    public String[] getDescriptionParameters() {
113        List<String> parameters = new ArrayList<String>();
114
115        for (Object fragment : defectDescription.getTextFragmentOrParameterFragment()) {
116            if (fragment instanceof ParameterFragment) {
117                parameters.add(((ParameterFragment) fragment).getParameterName());
118            }
119        }
120
121        return parameters.toArray(new String[parameters.size()]);
122    }
123
124    /**
125   *
126   */
127    public String toString(Map<String, String> parameters) throws IllegalArgumentException {
128        StringBuffer result = new StringBuffer();
129
130        for (Object fragment : defectDescription.getTextFragmentOrParameterFragment()) {
131            if (result.length() > 0) {
132                result.append(" ");
133            }
134
135            if (fragment instanceof ParameterFragment) {
136                String value = null;
137                if (parameters != null) {
138                    value = parameters.get(((ParameterFragment) fragment).getParameterName());
139                }
140
141                if (value != null) {
142                    result.append(value);
143                }
144                else {
145                    throw new IllegalArgumentException
146                        ("required parameter \"" +
147                         ((ParameterFragment) fragment).getParameterName() +
148                         "\" for usability defect description " + this.name() + " not provided");
149                }
150            }
151            else {
152                result.append(getFragmentString(fragment));
153            }
154        }
155
156        return result.toString();
157    }
158
159    /*
160     * (non-Javadoc)
161     *
162     * @see java.lang.Enum#toString()
163     */
164    @Override
165    public String toString() {
166        StringBuffer result = new StringBuffer();
167
168        int paramCount = 1;
169        for (Object fragment : defectDescription.getTextFragmentOrParameterFragment()) {
170            if (result.length() > 0) {
171                result.append(" ");
172            }
173
174            if (fragment instanceof ParameterFragment) {
175                result.append("<parameter");
176                result.append(paramCount++);
177                result.append(">");
178            }
179            else {
180                result.append(getFragmentString(fragment));
181            }
182        }
183
184        return result.toString();
185    }
186
187    /**
188     * TODO: comment
189     *
190     * @param fragment
191     * @return
192     */
193    private String getFragmentString(Object fragment) {
194        String fragmentStr = fragment.toString().trim();
195
196        fragmentStr = fragmentStr.replaceAll("\n", " ");
197
198        while (fragmentStr.indexOf("  ") > -1) {
199            fragmentStr = fragmentStr.replaceAll("  ", " ");
200        }
201
202        return fragmentStr;
203    }
204
205}
Note: See TracBrowser for help on using the repository browser.