[1040] | 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 |
|
---|
[1030] | 15 | package de.ugoe.cs.autoquest.usability.evaluation.result;
|
---|
| 16 |
|
---|
| 17 | import java.io.IOException;
|
---|
| 18 | import java.io.InputStream;
|
---|
| 19 |
|
---|
| 20 | import javax.xml.bind.JAXBContext;
|
---|
| 21 | import javax.xml.bind.JAXBElement;
|
---|
| 22 | import javax.xml.bind.Unmarshaller;
|
---|
| 23 |
|
---|
| 24 | import com.google.common.base.Optional;
|
---|
| 25 | import com.google.common.base.Predicate;
|
---|
| 26 | import com.google.common.collect.Iterables;
|
---|
| 27 |
|
---|
| 28 | import de.ugoe.cs.autoquest.usability.DefectDescription;
|
---|
| 29 | import de.ugoe.cs.autoquest.usability.DefectDescriptions;
|
---|
| 30 | import de.ugoe.cs.autoquest.usability.evaluation.rule.set.UsabilityRule;
|
---|
| 31 |
|
---|
[1040] | 32 | /**
|
---|
| 33 | * <p>
|
---|
| 34 | * TODO comment
|
---|
| 35 | * </p>
|
---|
| 36 | *
|
---|
| 37 | * @author Alexander Deicke
|
---|
| 38 | */
|
---|
[1030] | 39 | public class UsabilityDefectXmlDescriptionResolver implements UsabilityDefectDescriptionResolver {
|
---|
[1040] | 40 |
|
---|
[1030] | 41 | private static final String DEFAULT_MESSAGES_FILE = "defectDescriptions_en.xml";
|
---|
[1040] | 42 |
|
---|
| 43 | private static final UsabilityDefectXmlDescriptionResolver instance =
|
---|
| 44 | new UsabilityDefectXmlDescriptionResolver();
|
---|
| 45 |
|
---|
[1030] | 46 | private DefectDescriptions defectDescriptions;
|
---|
[1040] | 47 |
|
---|
[1030] | 48 | private UsabilityDefectXmlDescriptionResolver() {
|
---|
| 49 | loadDescriptions();
|
---|
| 50 | }
|
---|
[1040] | 51 |
|
---|
[1030] | 52 | @SuppressWarnings("unchecked")
|
---|
| 53 | private void loadDescriptions() {
|
---|
[1040] | 54 | InputStream inputStream = ClassLoader.getSystemResourceAsStream(DEFAULT_MESSAGES_FILE);
|
---|
[1030] | 55 | try {
|
---|
| 56 | String packageName = DefectDescriptions.class.getPackage().getName();
|
---|
| 57 | JAXBContext jaxbContext = JAXBContext.newInstance(packageName);
|
---|
| 58 | Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
|
---|
| 59 |
|
---|
| 60 | defectDescriptions =
|
---|
[1040] | 61 | ((JAXBElement<DefectDescriptions>) unmarshaller.unmarshal(inputStream)).getValue();
|
---|
[1030] | 62 | }
|
---|
| 63 | catch (Exception e) {
|
---|
[1040] | 64 | throw new RuntimeException("error while initializing usability defect descriptions", e);
|
---|
[1030] | 65 | }
|
---|
| 66 | finally {
|
---|
| 67 | if (inputStream != null) {
|
---|
| 68 | try {
|
---|
| 69 | inputStream.close();
|
---|
| 70 | }
|
---|
| 71 | catch (IOException e) {
|
---|
| 72 | // ignore
|
---|
| 73 | }
|
---|
| 74 | }
|
---|
| 75 | }
|
---|
| 76 | }
|
---|
[1040] | 77 |
|
---|
[1030] | 78 | public static UsabilityDefectXmlDescriptionResolver instance() {
|
---|
| 79 | return instance;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | @Override
|
---|
| 83 | public DefectDescription descriptionFor(final UsabilityRule usabilityRule) {
|
---|
[1040] | 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());
|
---|
[1030] | 99 | return guidlineDescription.get();
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | }
|
---|