// Copyright 2012 Georg-August-Universität Göttingen, Germany // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package de.ugoe.cs.autoquest.usability; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.Map; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBElement; import javax.xml.bind.Unmarshaller; /** * TODO comment * * @version $Revision: $ $Date: 18.07.2012$ * @author 2012, last modified by $Author: pharms$ */ public enum UsabilityDefectDescription { INEFFICIENT_ACTIONS, TEXT_FIELD_INPUT_RATIO, TEXT_FIELD_INPUT_REPETITIONS, TEXT_FIELD_NO_LETTER_OR_DIGIT_RATIO, COOCCURENCE_SUCCEED, COOCCURENCE_PRECED, HIGH_EVENT_COVERAGE, HIGH_TARGET_DISTANCE, MISSING_FEEDBACK, UNUSED_GUI_ELEMENTS; /** */ private static final String DEFAULT_MESSAGES_FILE = "defectDescriptions_en.xml"; /** */ private static DefectDescriptions sDefectDescriptions; /** */ private DefectDescription defectDescription; /** * */ private UsabilityDefectDescription() { init(); } /** * */ @SuppressWarnings("unchecked") private void init() { synchronized (this.getClass()) { if (sDefectDescriptions == null) { InputStream inputStream = ClassLoader.getSystemResourceAsStream(DEFAULT_MESSAGES_FILE); try { String packageName = DefectDescriptions.class.getPackage().getName(); JAXBContext jaxbContext = JAXBContext.newInstance(packageName); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); sDefectDescriptions = ((JAXBElement) unmarshaller.unmarshal(inputStream)) .getValue(); } catch (Exception e) { throw new RuntimeException ("error while initializing usability defect descriptions", e); } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { // ignore } } } } } for (DefectDescription description : sDefectDescriptions.getDefectDescription()) { if (this.name().equals(description.getDefectId())) { defectDescription = description; break; } } if (defectDescription == null) { throw new RuntimeException ("error while initializing usability defect descriptions. No " + "description text available for description " + this.name()); } } /** * */ public String[] getDescriptionParameters() { List parameters = new ArrayList(); for (Object fragment : defectDescription.getTextFragmentOrParameterFragment()) { if (fragment instanceof ParameterFragment) { parameters.add(((ParameterFragment) fragment).getParameterName()); } } return parameters.toArray(new String[parameters.size()]); } /** * */ public String toString(Map parameters) throws IllegalArgumentException { StringBuffer result = new StringBuffer(); for (Object fragment : defectDescription.getTextFragmentOrParameterFragment()) { if (result.length() > 0) { result.append(" "); } if (fragment instanceof ParameterFragment) { Object value = null; if (parameters != null) { value = parameters.get(((ParameterFragment) fragment).getParameterName()); } if (value != null) { if (value instanceof Collection) { int counter = 1; for (Object elem : ((Collection) value)) { result.append('\n'); result.append(counter++); result.append(".: "); result.append(elem); } } else { result.append(value.toString()); } } else { throw new IllegalArgumentException ("required parameter \"" + ((ParameterFragment) fragment).getParameterName() + "\" for usability defect description " + this.name() + " not provided"); } } else { result.append(getFragmentString(fragment)); } } return result.toString(); } /** * */ public List toFragmentList(Map parameters) throws IllegalArgumentException { List result = new ArrayList(); for (Object fragment : defectDescription.getTextFragmentOrParameterFragment()) { if (fragment instanceof ParameterFragment) { Object value = null; if (parameters != null) { value = parameters.get(((ParameterFragment) fragment).getParameterName()); } if (value != null) { result.add(value); } else { throw new IllegalArgumentException ("required parameter \"" + ((ParameterFragment) fragment).getParameterName() + "\" for usability defect description " + this.name() + " not provided"); } } else { result.add(getFragmentString(fragment)); } } return result; } /** * */ public String getBriefDescription() { return defectDescription.briefText; } /* * (non-Javadoc) * * @see java.lang.Enum#toString() */ @Override public String toString() { StringBuffer result = new StringBuffer(); int paramCount = 1; for (Object fragment : defectDescription.getTextFragmentOrParameterFragment()) { if (result.length() > 0) { result.append(" "); } if (fragment instanceof ParameterFragment) { result.append(""); } else { result.append(getFragmentString(fragment)); } } return result.toString(); } /** * */ private String getFragmentString(Object fragment) { String fragmentStr = fragment.toString().trim(); fragmentStr = fragmentStr.replaceAll("\n", " "); while (fragmentStr.indexOf(" ") > -1) { fragmentStr = fragmentStr.replaceAll(" ", " "); } return fragmentStr; } }