/** * Create an instance of Ucum services. Stream must point to a * valid ucum-essence file (see class documentation) * @throws UcumException */ public UcumEssenceService(InputStream stream) throws UcumException { super(); assert stream != null : paramError("factory", "stream", "must not be null"); try { model = new DefinitionParser().parse(stream); } catch (Exception e) { throw new UcumException(e); } }
private Prefix parsePrefix(XmlPullParser xpp) throws XmlPullParserException, IOException, UcumException { Prefix prefix = new Prefix(xpp.getAttributeValue(null, "Code"), xpp.getAttributeValue(null, "CODE")); xpp.next(); skipWhitespace(xpp); prefix.getNames().add(readElement(xpp, "name", "prefix "+prefix.getCode(), false)); prefix.setPrintSymbol(readElement(xpp, "printSymbol", "prefix "+prefix.getCode(), false)); checkAtElement(xpp, "value", "prefix "+prefix.getCode()); prefix.setValue(new Decimal(xpp.getAttributeValue(null, "value"), 24)); readElement(xpp, "value", "prefix "+prefix.getCode(), true); xpp.next(); skipWhitespace(xpp); return prefix; }
throw new XmlPullParserException("Unexpected text "+xpp.getText()); } else if (xpp.getName().equals("prefix")) root.getPrefixes().add(parsePrefix(xpp)); else if (xpp.getName().equals("base-unit")) root.getBaseUnits().add(parseBaseUnit(xpp)); else if (xpp.getName().equals("unit")) root.getDefinedUnits().add(parseUnit(xpp)); else throw new XmlPullParserException("unknown element name "+xpp.getName());
private String readText(XmlPullParser xpp) throws XmlPullParserException, IOException { StringBuilder bldr = new StringBuilder(); while (xpp.getEventType() != XmlPullParser.END_TAG) { if (xpp.getEventType() == XmlPullParser.TEXT) { bldr.append(xpp.getText()); xpp.next(); } else { xpp.next(); bldr.append(readText(xpp)); xpp.next(); skipWhitespace(xpp); } } return bldr.toString(); }
private String readElement(XmlPullParser xpp, String name, String context, boolean complex) throws XmlPullParserException, IOException { checkAtElement(xpp, name, context); xpp.next(); skipWhitespace(xpp); String val = null; if (complex) { val = readText(xpp); } else if (xpp.getEventType() == XmlPullParser.TEXT) { val = xpp.getText(); xpp.next(); skipWhitespace(xpp); } if (xpp.getEventType() != XmlPullParser.END_TAG) { throw new XmlPullParserException("Unexpected content reading "+context); } xpp.next(); skipWhitespace(xpp); return val; }
private DefinedUnit parseUnit(XmlPullParser xpp) throws XmlPullParserException, IOException, UcumException { DefinedUnit unit = new DefinedUnit(xpp.getAttributeValue(null, "Code"), xpp.getAttributeValue(null, "CODE")); unit.setMetric("yes".equals(xpp.getAttributeValue(null, "isMetric"))); unit.setSpecial("yes".equals(xpp.getAttributeValue(null, "isSpecial"))); unit.setClass_(xpp.getAttributeValue(null, "class")); xpp.next(); skipWhitespace(xpp); while (xpp.getEventType() == XmlPullParser.START_TAG && "name".equals(xpp.getName())) unit.getNames().add(readElement(xpp, "name", "unit "+unit.getCode(), false)); if (xpp.getEventType() == XmlPullParser.START_TAG && "printSymbol".equals(xpp.getName())) unit.setPrintSymbol(readElement(xpp, "printSymbol", "unit "+unit.getCode(), true)); unit.setProperty(readElement(xpp, "property", "unit "+unit.getCode(), false)); unit.setValue(parseValue(xpp, "unit "+unit.getCode())); xpp.next(); skipWhitespace(xpp); return unit; }
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; }
private Value parseValue(XmlPullParser xpp, String context) throws XmlPullParserException, UcumException, IOException { checkAtElement(xpp, "value", context); Decimal val = null; if (xpp.getAttributeValue(null, "value") != null) try { if (xpp.getAttributeValue(null, "value").contains(".")) val = new Decimal(xpp.getAttributeValue(null, "value"), 24); // unlimited precision for these else val = new Decimal(xpp.getAttributeValue(null, "value")); } catch (NumberFormatException e) { throw new XmlPullParserException("Error reading "+context+": "+e.getMessage()); } Value value = new Value(xpp.getAttributeValue(null, "Unit"), xpp.getAttributeValue(null, "UNIT"), val); value.setText(readElement(xpp, "value", context, true)); return value; }
public UcumModel parse(String filename) throws UcumException, XmlPullParserException, IOException, ParseException { return parse(new FileInputStream(new File(filename))); }
private DefinedUnit parseUnit(XmlPullParser xpp) throws XmlPullParserException, IOException, UcumException { DefinedUnit unit = new DefinedUnit(xpp.getAttributeValue(null, "Code"), xpp.getAttributeValue(null, "CODE")); unit.setMetric("yes".equals(xpp.getAttributeValue(null, "isMetric"))); unit.setSpecial("yes".equals(xpp.getAttributeValue(null, "isSpecial"))); unit.setClass_(xpp.getAttributeValue(null, "class")); xpp.next(); skipWhitespace(xpp); while (xpp.getEventType() == XmlPullParser.START_TAG && "name".equals(xpp.getName())) unit.getNames().add(readElement(xpp, "name", "unit "+unit.getCode(), false)); if (xpp.getEventType() == XmlPullParser.START_TAG && "printSymbol".equals(xpp.getName())) unit.setPrintSymbol(readElement(xpp, "printSymbol", "unit "+unit.getCode(), true)); unit.setProperty(readElement(xpp, "property", "unit "+unit.getCode(), false)); unit.setValue(parseValue(xpp, "unit "+unit.getCode())); xpp.next(); skipWhitespace(xpp); return unit; }
private String readElement(XmlPullParser xpp, String name, String context, boolean complex) throws XmlPullParserException, IOException { checkAtElement(xpp, name, context); xpp.next(); skipWhitespace(xpp); String val = null; if (complex) { val = readText(xpp); } else if (xpp.getEventType() == XmlPullParser.TEXT) { val = xpp.getText(); xpp.next(); skipWhitespace(xpp); } if (xpp.getEventType() != XmlPullParser.END_TAG) { throw new XmlPullParserException("Unexpected content reading "+context); } xpp.next(); skipWhitespace(xpp); return val; }
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; }
private String readText(XmlPullParser xpp) throws XmlPullParserException, IOException { StringBuilder bldr = new StringBuilder(); while (xpp.getEventType() != XmlPullParser.END_TAG) { if (xpp.getEventType() == XmlPullParser.TEXT) { bldr.append(xpp.getText()); xpp.next(); } else { xpp.next(); bldr.append(readText(xpp)); xpp.next(); skipWhitespace(xpp); } } return bldr.toString(); }
private Value parseValue(XmlPullParser xpp, String context) throws XmlPullParserException, UcumException, IOException { checkAtElement(xpp, "value", context); Decimal val = null; if (xpp.getAttributeValue(null, "value") != null) try { if (xpp.getAttributeValue(null, "value").contains(".")) val = new Decimal(xpp.getAttributeValue(null, "value"), 24); // unlimited precision for these else val = new Decimal(xpp.getAttributeValue(null, "value")); } catch (NumberFormatException e) { throw new XmlPullParserException("Error reading "+context+": "+e.getMessage()); } Value value = new Value(xpp.getAttributeValue(null, "Unit"), xpp.getAttributeValue(null, "UNIT"), val); value.setText(readElement(xpp, "value", context, true)); return value; }
public UcumModel parse(String filename) throws UcumException, XmlPullParserException, IOException, ParseException { return parse(new FileInputStream(new File(filename))); }
private Prefix parsePrefix(XmlPullParser xpp) throws XmlPullParserException, IOException, UcumException { Prefix prefix = new Prefix(xpp.getAttributeValue(null, "Code"), xpp.getAttributeValue(null, "CODE")); xpp.next(); skipWhitespace(xpp); prefix.getNames().add(readElement(xpp, "name", "prefix "+prefix.getCode(), false)); prefix.setPrintSymbol(readElement(xpp, "printSymbol", "prefix "+prefix.getCode(), false)); checkAtElement(xpp, "value", "prefix "+prefix.getCode()); prefix.setValue(new Decimal(xpp.getAttributeValue(null, "value"), 24)); readElement(xpp, "value", "prefix "+prefix.getCode(), true); xpp.next(); skipWhitespace(xpp); return prefix; }
throw new XmlPullParserException("Unexpected text "+xpp.getText()); } else if (xpp.getName().equals("prefix")) root.getPrefixes().add(parsePrefix(xpp)); else if (xpp.getName().equals("base-unit")) root.getBaseUnits().add(parseBaseUnit(xpp)); else if (xpp.getName().equals("unit")) root.getDefinedUnits().add(parseUnit(xpp)); else throw new XmlPullParserException("unknown element name "+xpp.getName());
/** * Create an instance of Ucum services. filename must point to a * valid ucum-essence file (see class documentation) * @throws UcumException */ public UcumEssenceService(String filename) throws UcumException { super(); assert new File(filename).exists() : paramError("factory", "file", "must exist"); try { model = new DefinitionParser().parse(filename); } catch (Exception e) { throw new UcumException(e); } }
/** * Create an instance of Ucum services. filename must point to a * valid ucum-essence file (see class documentation) * @throws UcumException */ public UcumEssenceService(String filename) throws UcumException { super(); assert new File(filename).exists() : paramError("factory", "file", "must exist"); try { model = new DefinitionParser().parse(filename); } catch (Exception e) { throw new UcumException(e); } }
/** * Create an instance of Ucum services. Stream must point to a * valid ucum-essence file (see class documentation) * @throws UcumException */ public UcumEssenceService(InputStream stream) throws UcumException { super(); assert stream != null : paramError("factory", "stream", "must not be null"); try { model = new DefinitionParser().parse(stream); } catch (Exception e) { throw new UcumException(e); } }