View Javadoc

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  public class ConversionFactorParserHandler extends DefaultHandler
30  {
31    private Property property;
32    private final PropertyList properties = new PropertyList();
33  
34    private final String propertyElementName = "Property";
35    private final String nameAttributeName = "name";
36    private final String formulaBasedAttributeName = "formulaBased";
37    private final String factorElementName = "Factor";
38    private final String unitNameAttributeName = "unitName";
39    private final String unitSymbolAttributeName = "unitSymbol";
40    private final String toUnitSymbolAttributeName = "toUnitSymbol";
41    private final String formulaAttributeName = "formula";
42    private final String factorAttributeName = "factor";
43    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      getPropertyOrderPreferences();
53      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      final Preferences userPref = Preferences.userNodeForPackage(Launcher.class);
63  
64      for (Property prop : properties)
65      {
66        final String key = "sortorder." + prop.getName().replaceAll(" ", "").toLowerCase();
67        final int propertyOrder = userPref.getInt(key, Integer.MAX_VALUE);
68        prop.setOrder(propertyOrder);
69      }
70      Collections.sort(properties);
71    }
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      if (propertyElementName.equals(qualifiedName))
78      {
79        boolean hasFormula = false;
80        final String isFormulaBased = attributes.getValue(formulaBasedAttributeName);
81        if ("true".equals(isFormulaBased))
82        {
83          hasFormula = true;
84        }
85        boolean usesMoles = false;
86        final String canUseMoles = attributes.getValue(usesMolesAttributeName);
87        if ("true".equals(canUseMoles))
88        {
89          usesMoles = true;
90        }
91        property = new Property(attributes.getValue(nameAttributeName), hasFormula, usesMoles);
92      }
93      if (factorElementName.equals(qualifiedName))
94      {
95        addConversion(attributes);
96      }
97    }
98  
99    private void addConversion(final Attributes attributes)
100   {
101     if (property.hasFormula())
102     {
103       addConversionFormula(attributes);
104     }
105     else
106     {
107       addConversionFactor(attributes);
108     }
109   }
110 
111   private void addConversionFormula(final Attributes attributes)
112   {
113     final String unitName = attributes.getValue(unitNameAttributeName);
114     final String unitSymbol = attributes.getValue(unitSymbolAttributeName);
115     final String toUnitSymbol = attributes.getValue(toUnitSymbolAttributeName);
116     final String formula = attributes.getValue(formulaAttributeName);
117     property.addConversion(new ConversionFormula(unitName, unitSymbol, toUnitSymbol, formula));
118   }
119 
120   private void addConversionFactor(final Attributes attributes)
121   {
122     final String unitName = attributes.getValue(unitNameAttributeName);
123     final String unitSymbol = attributes.getValue(unitSymbolAttributeName);
124     final String factor = attributes.getValue(factorAttributeName);
125     property.addConversion(new ConversionFactor(unitName, unitSymbol, factor));
126   }
127 
128   @Override
129   public final void endElement(final String uri, final String localName, final String qualifiedName)
130       throws SAXException
131   {
132     if (propertyElementName.equals(qualifiedName))
133     {
134       properties.add(property);
135       property = null;
136     }
137   }
138 }