Ignore:
Timestamp:
08/17/12 09:26:21 (12 years ago)
Author:
pharms
Message:
  • adapted to new coding style in quest
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/quest-core-usability/src/main/java/de/ugoe/cs/quest/usability/UsabilityDefectDescription.java

    r496 r561  
    1 //------------------------------------------------------------------------------------------------- 
    21// Module    : $RCSfile: UsabilityDefectDescriptions.java,v $ 
    32// Version   : $Revision: 0.0 $  $Author: pharms $  $Date: 18.07.2012 $ 
     
    54// Creation  : 2012 by pharms 
    65// Copyright : Patrick Harms, 2012 
    7 //------------------------------------------------------------------------------------------------- 
     6 
    87package de.ugoe.cs.quest.usability; 
    98 
     
    1817import javax.xml.bind.Unmarshaller; 
    1918 
    20 //------------------------------------------------------------------------------------------------- 
    2119/** 
    2220 * TODO comment 
     
    2523 * @author 2012, last modified by $Author: pharms$ 
    2624 */ 
    27 //------------------------------------------------------------------------------------------------- 
    28 public enum UsabilityDefectDescription 
    29 { 
    30   TEXT_FIELD_INPUT_RATIO, 
    31   TEXT_FIELD_INPUT_REPETITIONS, 
    32   TEXT_FIELD_NO_LETTER_OR_DIGIT_RATIO; 
     25public enum UsabilityDefectDescription { 
     26     
     27    TEXT_FIELD_INPUT_RATIO, 
     28    TEXT_FIELD_INPUT_REPETITIONS, 
     29    TEXT_FIELD_NO_LETTER_OR_DIGIT_RATIO; 
    3330 
    34   /** */ 
    35   private static final String DEFAULT_MESSAGES_FILE = "defectDescriptions_en.xml"; 
    36    
    37   /** */ 
    38   private static DefectDescriptions sDefectDescriptions; 
     31    /** */ 
     32    private static final String DEFAULT_MESSAGES_FILE = "defectDescriptions_en.xml"; 
    3933 
    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   } 
     34    /** */ 
     35    private static DefectDescriptions sDefectDescriptions; 
    5536 
    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); 
     37    /** */ 
     38    private DefectDescription defectDescription; 
    7039 
    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(); 
     40    /** 
     41     * TODO: comment 
     42     *  
     43     * @param name 
     44     * @param ordinal 
     45     */ 
     46    private UsabilityDefectDescription() { 
     47        init(); 
     48    } 
     49 
     50    /** 
     51     * TODO: comment 
     52     *  
     53     */ 
     54    @SuppressWarnings("unchecked") 
     55    private void init() { 
     56        synchronized (this.getClass()) { 
     57            if (sDefectDescriptions == null) { 
     58                InputStream inputStream = 
     59                    ClassLoader.getSystemResourceAsStream(DEFAULT_MESSAGES_FILE); 
     60 
     61                try { 
     62                    String packageName = DefectDescriptions.class.getPackage().getName(); 
     63                    JAXBContext jaxbContext = JAXBContext.newInstance(packageName); 
     64                    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); 
     65 
     66                    sDefectDescriptions = 
     67                        ((JAXBElement<DefectDescriptions>) unmarshaller.unmarshal(inputStream)) 
     68                            .getValue(); 
     69                } 
     70                catch (Exception e) { 
     71                    throw new RuntimeException 
     72                        ("error while initializing usability defect descriptions", e); 
     73                } 
     74                finally { 
     75                    if (inputStream != null) { 
     76                        try { 
     77                            inputStream.close(); 
     78                        } 
     79                        catch (IOException e) { 
     80                            // ignore 
     81                        } 
     82                    } 
     83                } 
     84            } 
    7985        } 
    80         catch (Exception e) 
    81         { 
    82           throw new RuntimeException("error while initializing usability defect descriptions", e); 
     86 
     87        for (DefectDescription description : sDefectDescriptions.getDefectDescription()) { 
     88            if (this.name().equals(description.getDefectId())) { 
     89                defectDescription = description; 
     90                break; 
     91            } 
    8392        } 
    84         finally 
    85         { 
    86           if (inputStream != null) 
    87           { 
    88             try 
    89             { 
    90               inputStream.close(); 
    91             } 
    92             catch (IOException e) 
    93             { 
    94               // ignore 
    95             } 
    96           } 
     93 
     94        if (defectDescription == null) { 
     95            throw new RuntimeException 
     96                ("error while initializing usability defect descriptions. No " + 
     97                 "description text available for description " + this.name()); 
    9798        } 
    98       } 
    9999    } 
    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   } 
    116100 
    117   //----------------------------------------------------------------------------------------------- 
    118   /** 
     101    /** 
    119102   *  
    120103   */ 
    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       } 
     104    public String[] getDescriptionParameters() { 
     105        List<String> parameters = new ArrayList<String>(); 
     106 
     107        for (Object fragment : defectDescription.getTextFragmentOrParameterFragment()) { 
     108            if (fragment instanceof ParameterFragment) { 
     109                parameters.add(((ParameterFragment) fragment).getParameterName()); 
     110            } 
     111        } 
     112 
     113        return parameters.toArray(new String[parameters.size()]); 
    132114    } 
    133      
    134     return parameters.toArray(new String[parameters.size()]); 
    135   } 
    136    
    137   //----------------------------------------------------------------------------------------------- 
    138   /** 
     115 
     116    /** 
    139117   *  
    140118   */ 
    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()); 
     119    public String toString(Map<String, String> parameters) throws IllegalArgumentException { 
     120        StringBuffer result = new StringBuffer(); 
     121 
     122        for (Object fragment : defectDescription.getTextFragmentOrParameterFragment()) { 
     123            if (result.length() > 0) { 
     124                result.append(" "); 
     125            } 
     126 
     127            if (fragment instanceof ParameterFragment) { 
     128                String value = null; 
     129                if (parameters != null) { 
     130                    value = parameters.get(((ParameterFragment) fragment).getParameterName()); 
     131                } 
     132 
     133                if (value != null) { 
     134                    result.append(value); 
     135                } 
     136                else { 
     137                    throw new IllegalArgumentException 
     138                        ("required parameter \"" + 
     139                         ((ParameterFragment) fragment).getParameterName() + 
     140                         "\" for usability defect description " + this.name() + " not provided"); 
     141                } 
     142            } 
     143            else { 
     144                result.append(getFragmentString(fragment)); 
     145            } 
    159146        } 
    160          
    161         if (value != null) 
    162         { 
    163           result.append(value); 
     147 
     148        return result.toString(); 
     149    } 
     150 
     151    /* 
     152     * (non-Javadoc) 
     153     *  
     154     * @see java.lang.Enum#toString() 
     155     */ 
     156    @Override 
     157    public String toString() { 
     158        StringBuffer result = new StringBuffer(); 
     159 
     160        int paramCount = 1; 
     161        for (Object fragment : defectDescription.getTextFragmentOrParameterFragment()) { 
     162            if (result.length() > 0) { 
     163                result.append(" "); 
     164            } 
     165 
     166            if (fragment instanceof ParameterFragment) { 
     167                result.append("<parameter"); 
     168                result.append(paramCount++); 
     169                result.append(">"); 
     170            } 
     171            else { 
     172                result.append(getFragmentString(fragment)); 
     173            } 
    164174        } 
    165         else 
    166         { 
    167           throw new IllegalArgumentException 
    168             ("required parameter \"" + ((ParameterFragment) fragment).getParameterName() + 
    169              "\" for usability defect description " + this.name() + " not provided"); 
     175 
     176        return result.toString(); 
     177    } 
     178 
     179    /** 
     180     * TODO: comment 
     181     *  
     182     * @param fragment 
     183     * @return 
     184     */ 
     185    private String getFragmentString(Object fragment) { 
     186        String fragmentStr = fragment.toString().trim(); 
     187 
     188        fragmentStr = fragmentStr.replaceAll("\n", " "); 
     189 
     190        while (fragmentStr.indexOf("  ") > -1) { 
     191            fragmentStr = fragmentStr.replaceAll("  ", " "); 
    170192        } 
    171       } 
    172       else 
    173       { 
    174         result.append(getFragmentString(fragment)); 
    175       } 
     193 
     194        return fragmentStr; 
    176195    } 
    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   } 
    235196 
    236197} 
Note: See TracChangeset for help on using the changeset viewer.