[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 | |
---|
| 35 | TEXT_FIELD_INPUT_RATIO, |
---|
| 36 | TEXT_FIELD_INPUT_REPETITIONS, |
---|
| 37 | TEXT_FIELD_NO_LETTER_OR_DIGIT_RATIO; |
---|
[442] | 38 | |
---|
[561] | 39 | /** */ |
---|
| 40 | private static final String DEFAULT_MESSAGES_FILE = "defectDescriptions_en.xml"; |
---|
[442] | 41 | |
---|
[561] | 42 | /** */ |
---|
| 43 | private static DefectDescriptions sDefectDescriptions; |
---|
[442] | 44 | |
---|
[561] | 45 | /** */ |
---|
| 46 | private DefectDescription defectDescription; |
---|
[442] | 47 | |
---|
[561] | 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 | } |
---|
[442] | 93 | } |
---|
[561] | 94 | |
---|
| 95 | for (DefectDescription description : sDefectDescriptions.getDefectDescription()) { |
---|
| 96 | if (this.name().equals(description.getDefectId())) { |
---|
| 97 | defectDescription = description; |
---|
| 98 | break; |
---|
[442] | 99 | } |
---|
| 100 | } |
---|
[561] | 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 | } |
---|
[442] | 107 | } |
---|
| 108 | |
---|
[561] | 109 | /** |
---|
[442] | 110 | * |
---|
| 111 | */ |
---|
[561] | 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()]); |
---|
[475] | 122 | } |
---|
[561] | 123 | |
---|
| 124 | /** |
---|
[475] | 125 | * |
---|
| 126 | */ |
---|
[561] | 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 | } |
---|
[442] | 154 | } |
---|
[561] | 155 | |
---|
| 156 | return result.toString(); |
---|
[442] | 157 | } |
---|
| 158 | |
---|
[561] | 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(); |
---|
[442] | 185 | } |
---|
| 186 | |
---|
[561] | 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; |
---|
[442] | 203 | } |
---|
| 204 | |
---|
| 205 | } |
---|