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

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

Initial import.

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