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