source: trunk/quest-core-usability/src/main/java/de/ugoe/cs/quest/usability/UsabilityDefectDescription.java @ 496

Last change on this file since 496 was 496, checked in by pharms, 12 years ago
  • added further text entry based evaluation rule
File size: 6.7 KB
Line 
1//-------------------------------------------------------------------------------------------------
2// Module    : $RCSfile: UsabilityDefectDescriptions.java,v $
3// Version   : $Revision: 0.0 $  $Author: pharms $  $Date: 18.07.2012 $
4// Project   : UsabilityEvaluationManager
5// Creation  : 2012 by pharms
6// Copyright : Patrick Harms, 2012
7//-------------------------------------------------------------------------------------------------
8package de.ugoe.cs.quest.usability;
9
10import java.io.IOException;
11import java.io.InputStream;
12import java.util.ArrayList;
13import java.util.List;
14import java.util.Map;
15
16import javax.xml.bind.JAXBContext;
17import javax.xml.bind.JAXBElement;
18import javax.xml.bind.Unmarshaller;
19
20//-------------------------------------------------------------------------------------------------
21/**
22 * TODO comment
23 *
24 * @version $Revision: $ $Date: 18.07.2012$
25 * @author 2012, last modified by $Author: pharms$
26 */
27//-------------------------------------------------------------------------------------------------
28public enum UsabilityDefectDescription
29{
30  TEXT_FIELD_INPUT_RATIO,
31  TEXT_FIELD_INPUT_REPETITIONS,
32  TEXT_FIELD_NO_LETTER_OR_DIGIT_RATIO;
33
34  /** */
35  private static final String DEFAULT_MESSAGES_FILE = "defectDescriptions_en.xml";
36 
37  /** */
38  private static DefectDescriptions sDefectDescriptions;
39
40  /** */
41  private DefectDescription mDefectDescription;
42 
43  //-----------------------------------------------------------------------------------------------
44  /**
45   * TODO: comment
46   *
47   * @param name
48   * @param ordinal
49   */
50  //-----------------------------------------------------------------------------------------------
51  private UsabilityDefectDescription()
52  {
53    init();
54  }
55
56  //-----------------------------------------------------------------------------------------------
57  /**
58   * TODO: comment
59   *
60   */
61  //-----------------------------------------------------------------------------------------------
62  @SuppressWarnings("unchecked")
63  private void init()
64  {
65    synchronized (this.getClass())
66    {
67      if (sDefectDescriptions == null)
68      {
69        InputStream inputStream = ClassLoader.getSystemResourceAsStream(DEFAULT_MESSAGES_FILE);
70
71        try
72        {
73          String packageName = DefectDescriptions.class.getPackage().getName();
74          JAXBContext jaxbContext = JAXBContext.newInstance(packageName);
75          Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
76       
77          sDefectDescriptions =
78            ((JAXBElement<DefectDescriptions>) unmarshaller.unmarshal(inputStream)).getValue();
79        }
80        catch (Exception e)
81        {
82          throw new RuntimeException("error while initializing usability defect descriptions", e);
83        }
84        finally
85        {
86          if (inputStream != null)
87          {
88            try
89            {
90              inputStream.close();
91            }
92            catch (IOException e)
93            {
94              // ignore
95            }
96          }
97        }
98      }
99    }
100   
101    for (DefectDescription description : sDefectDescriptions.getDefectDescription())
102    {
103      if (this.name().equals(description.getDefectId()))
104      {
105        mDefectDescription = description;
106        break;
107      }
108    }
109   
110    if (mDefectDescription == null)
111    {
112      throw new RuntimeException("error while initializing usability defect descriptions. No " +
113                                 "description text available for description " + this.name());
114    }
115  }
116
117  //-----------------------------------------------------------------------------------------------
118  /**
119   *
120   */
121  //-----------------------------------------------------------------------------------------------
122  public String[] getDescriptionParameters()
123  {
124    List<String> parameters = new ArrayList<String>();
125   
126    for (Object fragment : mDefectDescription.getTextFragmentOrParameterFragment())
127    {
128      if (fragment instanceof ParameterFragment)
129      {
130        parameters.add(((ParameterFragment) fragment).getParameterName());
131      }
132    }
133   
134    return parameters.toArray(new String[parameters.size()]);
135  }
136 
137  //-----------------------------------------------------------------------------------------------
138  /**
139   *
140   */
141  //-----------------------------------------------------------------------------------------------
142  public String toString(Map<String, String> parameters) throws IllegalArgumentException
143  {
144    StringBuffer result = new StringBuffer();
145   
146    for (Object fragment : mDefectDescription.getTextFragmentOrParameterFragment())
147    {
148      if (result.length() > 0)
149      {
150        result.append(" ");
151      }
152     
153      if (fragment instanceof ParameterFragment)
154      {
155        String value = null;
156        if (parameters != null)
157        {
158          value = parameters.get(((ParameterFragment) fragment).getParameterName());
159        }
160       
161        if (value != null)
162        {
163          result.append(value);
164        }
165        else
166        {
167          throw new IllegalArgumentException
168            ("required parameter \"" + ((ParameterFragment) fragment).getParameterName() +
169             "\" for usability defect description " + this.name() + " not provided");
170        }
171      }
172      else
173      {
174        result.append(getFragmentString(fragment));
175      }
176    }
177   
178    return result.toString();
179  }
180
181  //-----------------------------------------------------------------------------------------------
182  /* (non-Javadoc)
183   * @see java.lang.Enum#toString()
184   */
185  //-----------------------------------------------------------------------------------------------
186  @Override
187  public String toString()
188  {
189    StringBuffer result = new StringBuffer();
190   
191    int paramCount = 1;
192    for (Object fragment : mDefectDescription.getTextFragmentOrParameterFragment())
193    {
194      if (result.length() > 0)
195      {
196        result.append(" ");
197      }
198     
199      if (fragment instanceof ParameterFragment)
200      {
201        result.append("<parameter");
202        result.append(paramCount++);
203        result.append(">");
204      }
205      else
206      {
207        result.append(getFragmentString(fragment));
208      }
209    }
210   
211    return result.toString();
212  }
213
214  //-----------------------------------------------------------------------------------------------
215  /**
216   * TODO: comment
217   *
218   * @param fragment
219   * @return
220   */
221  //-----------------------------------------------------------------------------------------------
222  private String getFragmentString(Object fragment)
223  {
224    String fragmentStr = fragment.toString().trim();
225   
226    fragmentStr = fragmentStr.replaceAll("\n", " ");
227   
228    while (fragmentStr.indexOf("  ") > -1)
229    {
230      fragmentStr = fragmentStr.replaceAll("  ", " ");
231    }
232   
233    return fragmentStr;
234  }
235
236}
Note: See TracBrowser for help on using the repository browser.