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

Last change on this file since 475 was 475, checked in by pharms, 12 years ago

improved code coverage for defect description class

File size: 6.6 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
33  /** */
34  private static final String DEFAULT_MESSAGES_FILE = "defectDescriptions_en.xml";
35 
36  /** */
37  private static DefectDescriptions sDefectDescriptions;
38
39  /** */
40  private DefectDescription mDefectDescription;
41 
42  //-----------------------------------------------------------------------------------------------
43  /**
44   * TODO: comment
45   *
46   * @param name
47   * @param ordinal
48   */
49  //-----------------------------------------------------------------------------------------------
50  private UsabilityDefectDescription()
51  {
52    init();
53  }
54
55  //-----------------------------------------------------------------------------------------------
56  /**
57   * TODO: comment
58   *
59   */
60  //-----------------------------------------------------------------------------------------------
61  @SuppressWarnings("unchecked")
62  private void init()
63  {
64    synchronized (this.getClass())
65    {
66      if (sDefectDescriptions == null)
67      {
68        InputStream inputStream = ClassLoader.getSystemResourceAsStream(DEFAULT_MESSAGES_FILE);
69
70        try
71        {
72          String packageName = DefectDescriptions.class.getPackage().getName();
73          JAXBContext jaxbContext = JAXBContext.newInstance(packageName);
74          Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
75       
76          sDefectDescriptions =
77            ((JAXBElement<DefectDescriptions>) unmarshaller.unmarshal(inputStream)).getValue();
78        }
79        catch (Exception e)
80        {
81          throw new RuntimeException("error while initializing usability defect descriptions", e);
82        }
83        finally
84        {
85          if (inputStream != null)
86          {
87            try
88            {
89              inputStream.close();
90            }
91            catch (IOException e)
92            {
93              // ignore
94            }
95          }
96        }
97      }
98    }
99   
100    for (DefectDescription description : sDefectDescriptions.getDefectDescription())
101    {
102      if (this.name().equals(description.getDefectId()))
103      {
104        mDefectDescription = description;
105        break;
106      }
107    }
108   
109    if (mDefectDescription == null)
110    {
111      throw new RuntimeException("error while initializing usability defect descriptions. No " +
112                                 "description text available for description " + this.name());
113    }
114  }
115
116  //-----------------------------------------------------------------------------------------------
117  /**
118   *
119   */
120  //-----------------------------------------------------------------------------------------------
121  public String[] getDescriptionParameters()
122  {
123    List<String> parameters = new ArrayList<String>();
124   
125    for (Object fragment : mDefectDescription.getTextFragmentOrParameterFragment())
126    {
127      if (fragment instanceof ParameterFragment)
128      {
129        parameters.add(((ParameterFragment) fragment).getParameterName());
130      }
131    }
132   
133    return parameters.toArray(new String[parameters.size()]);
134  }
135 
136  //-----------------------------------------------------------------------------------------------
137  /**
138   *
139   */
140  //-----------------------------------------------------------------------------------------------
141  public String toString(Map<String, String> parameters) throws IllegalArgumentException
142  {
143    StringBuffer result = new StringBuffer();
144   
145    for (Object fragment : mDefectDescription.getTextFragmentOrParameterFragment())
146    {
147      if (result.length() > 0)
148      {
149        result.append(" ");
150      }
151     
152      if (fragment instanceof ParameterFragment)
153      {
154        String value = null;
155        if (parameters != null)
156        {
157          value = parameters.get(((ParameterFragment) fragment).getParameterName());
158        }
159       
160        if (value != null)
161        {
162          result.append(value);
163        }
164        else
165        {
166          throw new IllegalArgumentException
167            ("required parameter \"" + ((ParameterFragment) fragment).getParameterName() +
168             "\" for usability defect description " + this.name() + " not provided");
169        }
170      }
171      else
172      {
173        result.append(getFragmentString(fragment));
174      }
175    }
176   
177    return result.toString();
178  }
179
180  //-----------------------------------------------------------------------------------------------
181  /* (non-Javadoc)
182   * @see java.lang.Enum#toString()
183   */
184  //-----------------------------------------------------------------------------------------------
185  @Override
186  public String toString()
187  {
188    StringBuffer result = new StringBuffer();
189   
190    int paramCount = 1;
191    for (Object fragment : mDefectDescription.getTextFragmentOrParameterFragment())
192    {
193      if (result.length() > 0)
194      {
195        result.append(" ");
196      }
197     
198      if (fragment instanceof ParameterFragment)
199      {
200        result.append("<parameter");
201        result.append(paramCount++);
202        result.append(">");
203      }
204      else
205      {
206        result.append(getFragmentString(fragment));
207      }
208    }
209   
210    return result.toString();
211  }
212
213  //-----------------------------------------------------------------------------------------------
214  /**
215   * TODO: comment
216   *
217   * @param fragment
218   * @return
219   */
220  //-----------------------------------------------------------------------------------------------
221  private String getFragmentString(Object fragment)
222  {
223    String fragmentStr = fragment.toString().trim();
224   
225    fragmentStr = fragmentStr.replaceAll("\n", " ");
226   
227    while (fragmentStr.indexOf("  ") > -1)
228    {
229      fragmentStr = fragmentStr.replaceAll("  ", " ");
230    }
231   
232    return fragmentStr;
233  }
234
235}
Note: See TracBrowser for help on using the repository browser.