Coverage Report - com.timjohnstondev.unitconverter.logic.ConversionFactorParserHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
ConversionFactorParserHandler
100%
54/54
100%
14/14
2
 
 1  
 /**
 2  
  * Copyright 2009 Timothy Johnston Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
 3  
  * file except in compliance with the License. You may obtain a copy of the License at
 4  
  * 
 5  
  * http://www.apache.org/licenses/LICENSE-2.0
 6  
  * 
 7  
  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 8  
  * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 9  
  * specific language governing permissions and limitations under the License.
 10  
  */
 11  
 
 12  
 package com.timjohnstondev.unitconverter.logic;
 13  
 
 14  
 import java.util.Collections;
 15  
 import java.util.prefs.Preferences;
 16  
 import org.xml.sax.Attributes;
 17  
 import org.xml.sax.SAXException;
 18  
 import org.xml.sax.helpers.DefaultHandler;
 19  
 import com.timjohnstondev.unitconverter.Launcher;
 20  
 import com.timjohnstondev.unitconverter.model.ConversionFactor;
 21  
 import com.timjohnstondev.unitconverter.model.ConversionFormula;
 22  
 import com.timjohnstondev.unitconverter.model.Property;
 23  
 import com.timjohnstondev.unitconverter.model.PropertyList;
 24  
 
 25  
 /**
 26  
  * This {@code DefaultHandler} is made specifically for this application. It understands the XML file format being used
 27  
  * and the related application objects.
 28  
  */
 29  13
 public class ConversionFactorParserHandler extends DefaultHandler
 30  
 {
 31  
   private Property property;
 32  13
   private final PropertyList properties = new PropertyList();
 33  
 
 34  13
   private final String propertyElementName = "Property";
 35  13
   private final String nameAttributeName = "name";
 36  13
   private final String formulaBasedAttributeName = "formulaBased";
 37  13
   private final String factorElementName = "Factor";
 38  13
   private final String unitNameAttributeName = "unitName";
 39  13
   private final String unitSymbolAttributeName = "unitSymbol";
 40  13
   private final String toUnitSymbolAttributeName = "toUnitSymbol";
 41  13
   private final String formulaAttributeName = "formula";
 42  13
   private final String factorAttributeName = "factor";
 43  13
   private final String usesMolesAttributeName = "usesMoles";
 44  
 
 45  
   /**
 46  
    * Returns the {@link PropertyList} constructed by the parser.
 47  
    * 
 48  
    * @return the {@code PropertyList} constructed by the parser
 49  
    */
 50  
   public final PropertyList getPropertyList()
 51  
   {
 52  11
     getPropertyOrderPreferences();
 53  11
     return properties;
 54  
   }
 55  
 
 56  
   /**
 57  
    * Looks into the preference storage in JavaSoft Preference settings. If the property order settings are found, they
 58  
    * are used, else the defaults are used.
 59  
    */
 60  
   private void getPropertyOrderPreferences()
 61  
   {
 62  11
     final Preferences userPref = Preferences.userNodeForPackage(Launcher.class);
 63  
 
 64  11
     for (Property prop : properties)
 65  
     {
 66  209
       final String key = "sortorder." + prop.getName().replaceAll(" ", "").toLowerCase();
 67  209
       final int propertyOrder = userPref.getInt(key, Integer.MAX_VALUE);
 68  209
       prop.setOrder(propertyOrder);
 69  209
     }
 70  11
     Collections.sort(properties);
 71  11
   }
 72  
 
 73  
   @Override
 74  
   public final void startElement(final String uri, final String localName, final String qualifiedName,
 75  
       final Attributes attributes) throws SAXException
 76  
   {
 77  2200
     if (propertyElementName.equals(qualifiedName))
 78  
     {
 79  209
       boolean hasFormula = false;
 80  209
       final String isFormulaBased = attributes.getValue(formulaBasedAttributeName);
 81  209
       if ("true".equals(isFormulaBased))
 82  
       {
 83  11
         hasFormula = true;
 84  
       }
 85  209
       boolean usesMoles = false;
 86  209
       final String canUseMoles = attributes.getValue(usesMolesAttributeName);
 87  209
       if ("true".equals(canUseMoles))
 88  
       {
 89  44
         usesMoles = true;
 90  
       }
 91  209
       property = new Property(attributes.getValue(nameAttributeName), hasFormula, usesMoles);
 92  
     }
 93  2200
     if (factorElementName.equals(qualifiedName))
 94  
     {
 95  1969
       addConversion(attributes);
 96  
     }
 97  2200
   }
 98  
 
 99  
   private void addConversion(final Attributes attributes)
 100  
   {
 101  1969
     if (property.hasFormula())
 102  
     {
 103  132
       addConversionFormula(attributes);
 104  
     }
 105  
     else
 106  
     {
 107  1837
       addConversionFactor(attributes);
 108  
     }
 109  1969
   }
 110  
 
 111  
   private void addConversionFormula(final Attributes attributes)
 112  
   {
 113  132
     final String unitName = attributes.getValue(unitNameAttributeName);
 114  132
     final String unitSymbol = attributes.getValue(unitSymbolAttributeName);
 115  132
     final String toUnitSymbol = attributes.getValue(toUnitSymbolAttributeName);
 116  132
     final String formula = attributes.getValue(formulaAttributeName);
 117  132
     property.addConversion(new ConversionFormula(unitName, unitSymbol, toUnitSymbol, formula));
 118  132
   }
 119  
 
 120  
   private void addConversionFactor(final Attributes attributes)
 121  
   {
 122  1837
     final String unitName = attributes.getValue(unitNameAttributeName);
 123  1837
     final String unitSymbol = attributes.getValue(unitSymbolAttributeName);
 124  1837
     final String factor = attributes.getValue(factorAttributeName);
 125  1837
     property.addConversion(new ConversionFactor(unitName, unitSymbol, factor));
 126  1837
   }
 127  
 
 128  
   @Override
 129  
   public final void endElement(final String uri, final String localName, final String qualifiedName)
 130  
       throws SAXException
 131  
   {
 132  2200
     if (propertyElementName.equals(qualifiedName))
 133  
     {
 134  209
       properties.add(property);
 135  209
       property = null;
 136  
     }
 137  2200
   }
 138  
 }