source: trunk/autoquest-core-usability-evaluation/src/main/java/de/ugoe/cs/autoquest/usability/evaluation/result/UsabilityDefectXmlDescriptionResolver.java @ 1040

Last change on this file since 1040 was 1040, checked in by adeicke, 11 years ago
  • Removed lombok related annotations and util class
  • Added comments and formating due to match project defaults
  • Property svn:mime-type set to text/plain
File size: 3.8 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.evaluation.result;
16
17import java.io.IOException;
18import java.io.InputStream;
19
20import javax.xml.bind.JAXBContext;
21import javax.xml.bind.JAXBElement;
22import javax.xml.bind.Unmarshaller;
23
24import com.google.common.base.Optional;
25import com.google.common.base.Predicate;
26import com.google.common.collect.Iterables;
27
28import de.ugoe.cs.autoquest.usability.DefectDescription;
29import de.ugoe.cs.autoquest.usability.DefectDescriptions;
30import de.ugoe.cs.autoquest.usability.evaluation.rule.set.UsabilityRule;
31
32/**
33 * <p>
34 * TODO comment
35 * </p>
36 *
37 * @author Alexander Deicke
38 */
39public class UsabilityDefectXmlDescriptionResolver implements UsabilityDefectDescriptionResolver {
40
41    private static final String DEFAULT_MESSAGES_FILE = "defectDescriptions_en.xml";
42
43    private static final UsabilityDefectXmlDescriptionResolver instance =
44        new UsabilityDefectXmlDescriptionResolver();
45
46    private DefectDescriptions defectDescriptions;
47
48    private UsabilityDefectXmlDescriptionResolver() {
49        loadDescriptions();
50    }
51
52    @SuppressWarnings("unchecked")
53    private void loadDescriptions() {
54        InputStream inputStream = ClassLoader.getSystemResourceAsStream(DEFAULT_MESSAGES_FILE);
55        try {
56            String packageName = DefectDescriptions.class.getPackage().getName();
57            JAXBContext jaxbContext = JAXBContext.newInstance(packageName);
58            Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
59
60            defectDescriptions =
61                ((JAXBElement<DefectDescriptions>) unmarshaller.unmarshal(inputStream)).getValue();
62        }
63        catch (Exception e) {
64            throw new RuntimeException("error while initializing usability defect descriptions", e);
65        }
66        finally {
67            if (inputStream != null) {
68                try {
69                    inputStream.close();
70                }
71                catch (IOException e) {
72                    // ignore
73                }
74            }
75        }
76    }
77
78    public static UsabilityDefectXmlDescriptionResolver instance() {
79        return instance;
80    }
81
82    @Override
83    public DefectDescription descriptionFor(final UsabilityRule usabilityRule) {
84        Optional<DefectDescription> guidlineDescription =
85            Iterables.tryFind(defectDescriptions.getDefectDescription(),
86                              new Predicate<DefectDescription>() {
87
88                                  public boolean apply(DefectDescription defectDescription) {
89                                      return usabilityRule.ruleIdentifier()
90                                          .equals(defectDescription.getDefectId());
91                                  }
92
93                              });
94        if (!guidlineDescription.isPresent())
95            throw new RuntimeException(
96                                       "error while initializing usability defect descriptions. No " +
97                                           "description text available for description " +
98                                           usabilityRule.ruleIdentifier());
99        return guidlineDescription.get();
100    }
101
102}
Note: See TracBrowser for help on using the repository browser.