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

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

refactored to integrate it into QUEST

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