| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 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 | |
|
| 27 | |
|
| 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 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
public final PropertyList getPropertyList() |
| 51 | |
{ |
| 52 | 11 | getPropertyOrderPreferences(); |
| 53 | 11 | return properties; |
| 54 | |
} |
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 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 | |
} |