[1030] | 1 | package de.ugoe.cs.autoquest.usability.evaluation.result;
|
---|
| 2 |
|
---|
| 3 | import java.io.IOException;
|
---|
| 4 | import java.io.InputStream;
|
---|
| 5 |
|
---|
| 6 | import javax.xml.bind.JAXBContext;
|
---|
| 7 | import javax.xml.bind.JAXBElement;
|
---|
| 8 | import javax.xml.bind.Unmarshaller;
|
---|
| 9 |
|
---|
| 10 | import com.google.common.base.Optional;
|
---|
| 11 | import com.google.common.base.Predicate;
|
---|
| 12 | import com.google.common.collect.Iterables;
|
---|
| 13 |
|
---|
| 14 | import de.ugoe.cs.autoquest.usability.DefectDescription;
|
---|
| 15 | import de.ugoe.cs.autoquest.usability.DefectDescriptions;
|
---|
| 16 | import de.ugoe.cs.autoquest.usability.evaluation.rule.set.UsabilityRule;
|
---|
| 17 |
|
---|
| 18 | public class UsabilityDefectXmlDescriptionResolver implements UsabilityDefectDescriptionResolver {
|
---|
| 19 |
|
---|
| 20 | private static final String DEFAULT_MESSAGES_FILE = "defectDescriptions_en.xml";
|
---|
| 21 |
|
---|
| 22 | private static final UsabilityDefectXmlDescriptionResolver instance = new UsabilityDefectXmlDescriptionResolver();
|
---|
| 23 |
|
---|
| 24 | private DefectDescriptions defectDescriptions;
|
---|
| 25 |
|
---|
| 26 | private UsabilityDefectXmlDescriptionResolver() {
|
---|
| 27 | loadDescriptions();
|
---|
| 28 | }
|
---|
| 29 |
|
---|
| 30 | @SuppressWarnings("unchecked")
|
---|
| 31 | private void loadDescriptions() {
|
---|
| 32 | InputStream inputStream =
|
---|
| 33 | ClassLoader.getSystemResourceAsStream(DEFAULT_MESSAGES_FILE);
|
---|
| 34 | try {
|
---|
| 35 | String packageName = DefectDescriptions.class.getPackage().getName();
|
---|
| 36 | JAXBContext jaxbContext = JAXBContext.newInstance(packageName);
|
---|
| 37 | Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
|
---|
| 38 |
|
---|
| 39 | defectDescriptions =
|
---|
| 40 | ((JAXBElement<DefectDescriptions>) unmarshaller.unmarshal(inputStream))
|
---|
| 41 | .getValue();
|
---|
| 42 | }
|
---|
| 43 | catch (Exception e) {
|
---|
| 44 | throw new RuntimeException
|
---|
| 45 | ("error while initializing usability defect descriptions", e);
|
---|
| 46 | }
|
---|
| 47 | finally {
|
---|
| 48 | if (inputStream != null) {
|
---|
| 49 | try {
|
---|
| 50 | inputStream.close();
|
---|
| 51 | }
|
---|
| 52 | catch (IOException e) {
|
---|
| 53 | // ignore
|
---|
| 54 | }
|
---|
| 55 | }
|
---|
| 56 | }
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | public static UsabilityDefectXmlDescriptionResolver instance() {
|
---|
| 60 | return instance;
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | @Override
|
---|
| 64 | public DefectDescription descriptionFor(final UsabilityRule usabilityRule) {
|
---|
| 65 | Optional<DefectDescription> guidlineDescription = Iterables.tryFind(defectDescriptions.getDefectDescription(), new Predicate<DefectDescription>() {
|
---|
| 66 |
|
---|
| 67 | public boolean apply(DefectDescription defectDescription) {
|
---|
| 68 | return usabilityRule.ruleIdentifier().equals(defectDescription.getDefectId());
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | });
|
---|
| 72 | if(!guidlineDescription.isPresent())
|
---|
| 73 | throw new RuntimeException
|
---|
| 74 | ("error while initializing usability defect descriptions. No " +
|
---|
| 75 | "description text available for description " + usabilityRule.ruleIdentifier());
|
---|
| 76 | return guidlineDescription.get();
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | }
|
---|