private BaseUnit parseBaseUnit(XmlPullParser xpp) throws XmlPullParserException, IOException { BaseUnit base = new BaseUnit(xpp.getAttributeValue(null, "Code"), xpp.getAttributeValue(null, "CODE")); base.setDim(xpp.getAttributeValue(null, "dim").charAt(0)); xpp.next(); skipWhitespace(xpp); base.getNames().add(readElement(xpp, "name", "base-unit "+base.getCode(), false)); base.setPrintSymbol(readElement(xpp, "printSymbol", "base-unit "+base.getCode(), false)); base.setProperty(readElement(xpp, "property", "base-unit "+base.getCode(), false)); xpp.next(); skipWhitespace(xpp); return base; }
@Override public String validateInProperty(String unit, String property) { assert checkStringParam(unit) : paramError("validate", "unit", "must not be null or empty"); assert checkStringParam(property) : paramError("validateInProperty", "property", "must not be null or empty"); try { Term term = new ExpressionParser(model).parse(unit); Canonical can = new Converter(model, handlers).convert(term); String cu = new ExpressionComposer().compose(can, false); if (can.getUnits().size() == 1) { if (property.equals(can.getUnits().get(0).getBase().getProperty())) return null; else return "unit "+unit+" is of the property type "+can.getUnits().get(0).getBase().getProperty()+" ("+cu+"), not "+property+" as required."; } // defined special case if ("concentration".equals(property) && ("g/L".equals(cu) || "mol/L".equals(cu))) return null; return "unit "+unit+" has the base units "+cu+", and are not from the property "+property+" as required."; } catch (Exception e) { return e.getMessage(); } }
public BaseUnit getBaseUnit(String code) { for (BaseUnit unit : getBaseUnits()) { if (unit.getCode().equals(code)) return unit; } return null; }
@Override public String validateInProperty(String unit, String property) { assert checkStringParam(unit) : paramError("validate", "unit", "must not be null or empty"); assert checkStringParam(property) : paramError("validateInProperty", "property", "must not be null or empty"); try { Term term = new ExpressionParser(model).parse(unit); Canonical can = new Converter(model, handlers).convert(term); String cu = new ExpressionComposer().compose(can, false); if (can.getUnits().size() == 1) { if (property.equals(can.getUnits().get(0).getBase().getProperty())) return null; else return "unit "+unit+" is of the property type "+can.getUnits().get(0).getBase().getProperty()+" ("+cu+"), not "+property+" as required."; } // defined special case if ("concentration".equals(property) && ("g/L".equals(cu) || "mol/L".equals(cu))) return null; return "unit "+unit+" has the base units "+cu+", and are not from the property "+property+" as required."; } catch (Exception e) { return e.getMessage(); } }
private BaseUnit parseBaseUnit(XmlPullParser xpp) throws XmlPullParserException, IOException { BaseUnit base = new BaseUnit(xpp.getAttributeValue(null, "Code"), xpp.getAttributeValue(null, "CODE")); base.setDim(xpp.getAttributeValue(null, "dim").charAt(0)); xpp.next(); skipWhitespace(xpp); base.getNames().add(readElement(xpp, "name", "base-unit "+base.getCode(), false)); base.setPrintSymbol(readElement(xpp, "printSymbol", "base-unit "+base.getCode(), false)); base.setProperty(readElement(xpp, "property", "base-unit "+base.getCode(), false)); xpp.next(); skipWhitespace(xpp); return base; }
public String compose(Canonical can, boolean value) { StringBuilder b = new StringBuilder(); if (value) b.append(can.getValue().asDecimal()); boolean first = true; for (CanonicalUnit c : can.getUnits()) { if (first) first = false; else b.append("."); b.append(c.getBase().getCode()); if (c.getExponent() != 1) b.append(c.getExponent()); } return b.toString(); }
public BaseUnit getBaseUnit(String code) { for (BaseUnit unit : getBaseUnits()) { if (unit.getCode().equals(code)) return unit; } return null; }
public String compose(Canonical can, boolean value) { StringBuilder b = new StringBuilder(); if (value) b.append(can.getValue().asDecimal()); boolean first = true; for (CanonicalUnit c : can.getUnits()) { if (first) first = false; else b.append("."); b.append(c.getBase().getCode()); if (c.getExponent() != 1) b.append(c.getExponent()); } return b.toString(); }