public void error(String errMsg) throws UcumException { throw new UcumException("Error processing unit '"+source+"': "+ errMsg +"' at position "+Integer.toString(start)); }
result.add(pairToQty(p)); } catch (UcumException e) { throw new PathEngineException(e.getMessage(), e);
} catch (UcumException e) { System.err.println("Could not create UCUM validation service:"); e.printStackTrace();
private boolean checkAnnotation(char ch) throws UcumException { if (ch == '{') { StringBuilder b = new StringBuilder(); while (ch != '}') { ch = nextChar(); if (!Utilities.isAsciiChar(ch)) throw new UcumException("Error processing unit'"+source+"': Annotation contains non-ascii characters"); if (ch == 0) throw new UcumException("Error processing unit'"+source+"': unterminated annotation"); b.append(ch); } // got to the end of the annotation - need to do it again token = b.toString(); type = TokenType.ANNOTATION; return true; } else return false; }
result.add(pairToQty(p)); } catch (UcumException e) { throw new PathEngineException(e.getMessage(), e);
private boolean checkNumber(char ch) throws UcumException { if (ch == '+' || ch == '-') { token = String.valueOf(ch); ch = peekChar(); while ((ch >= '0' && ch <= '9')) { token = token + ch; index++; ch = peekChar(); } if (token.length() == 1) { throw new UcumException("Error processing unit'"+source+"': unexpected character '"+ch+"' at position "+Integer.toString(start)+": a + or - must be followed by at least one digit"); } type = TokenType.NUMBER; return true; } else return false; }
result.add(pairToQty(p)); } catch (UcumException e) { throw new PathEngineException(e.getMessage(), e);
private void setValueScientific(String value) throws UcumException { int i = value.indexOf("e"); String s = value.substring(0, i); String e = value.substring(i+1); if (Utilities.noString(s) || s.equals("-") || !Utilities.isDecimal(s)) throw new UcumException("'"+value+"' is not a valid decimal (numeric)"); if (Utilities.noString(e) || e.equals("-") || !Utilities.isInteger(e)) throw new UcumException("'"+value+"' is not a valid decimal (exponent)"); setValueDecimal(s); scientific = true; // now adjust for exponent if (e.charAt(0) == '-') i = 1; else i = 0; while (i < e.length()) { if (!Character.isDigit(e.charAt(i))) throw new UcumException(""+value+"' is not a valid decimal"); i++; } i = Integer.parseInt(e); decimal = decimal + i; }
result.add(pairToQty(p)); } catch (UcumException e) { throw new PathEngineException(e.getMessage(), 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); } }
dec = i; else if (!Character.isDigit(value.charAt(i))) throw new UcumException("'"+value+"' is not a valid decimal"); digits = value; } else if (dec == value.length() -1) throw new UcumException("'"+value+"' is not a valid decimal"); else { decimal = dec;
public int asInteger() throws UcumException { if (!isWholeNumber()) throw new UcumException("Unable to represent "+toString()+" as an integer"); if (comparesTo(new Decimal(Integer.MIN_VALUE)) < 0) throw new UcumException("Unable to represent "+toString()+" as a signed 8 byte integer"); if (comparesTo(new Decimal(Integer.MAX_VALUE)) > 0) throw new UcumException("Unable to represent "+toString()+" as a signed 8 byte integer"); return Integer.parseInt(asDecimal()); }
public Term parse(String code) throws UcumException { Lexer lexer = new Lexer(code); Term res = parseTerm(lexer, true); if (!lexer.finished()) throw new UcumException("Expression was not parsed completely. Syntax Error?"); return res; }
/** * 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); } }
throw new UcumException("Attempt to divide "+toString()+" by zero");
public void consume() throws UcumException { token = null; type = TokenType.NONE; start = index; if (index < source.length()) { char ch = nextChar(); if (!(checkSingle(ch, '/', TokenType.SOLIDUS) || checkSingle(ch, '.', TokenType.PERIOD) || checkSingle(ch, '(', TokenType.OPEN) || checkSingle(ch, ')', TokenType.CLOSE) || checkAnnotation(ch) || checkNumber(ch) || checkNumberOrSymbol(ch))) throw new UcumException("Error processing unit '"+source+"': unexpected character '"+ch+"' at position "+Integer.toString(start)); } }
@Override public String getCanonicalUnits(String unit) throws UcumException { assert checkStringParam(unit) : paramError("getCanonicalUnits", "unit", "must not be null or empty"); try { Term term = new ExpressionParser(model).parse(unit); return new ExpressionComposer().compose(new Converter(model, handlers).convert(term), false); } catch (Exception e) { throw new UcumException("Error processing "+unit+": "+e.getMessage(), e); } }
@Override public Decimal convert(Decimal value, String sourceUnit, String destUnit) throws UcumException { assert value != null : paramError("convert", "value", "must not be null"); assert checkStringParam(sourceUnit) : paramError("convert", "sourceUnit", "must not be null or empty"); assert checkStringParam(destUnit) : paramError("convert", "destUnit", "must not be null or empty"); if (sourceUnit.equals(destUnit)) return value; Canonical src = new Converter(model, handlers).convert(new ExpressionParser(model).parse(sourceUnit)); Canonical dst = new Converter(model, handlers).convert(new ExpressionParser(model).parse(destUnit)); String s = new ExpressionComposer().compose(src, false); String d = new ExpressionComposer().compose(dst, false); if (!s.equals(d)) throw new UcumException("Unable to convert between units "+sourceUnit+" and "+destUnit+" as they do not have matching canonical forms ("+s+" and "+d+" respectively)"); Decimal canValue = value.multiply(src.getValue()); // System.out.println(value.toPlainString()+sourceUnit+" =("+src.getValue().toPlainString()+")= "+ // canValue.toPlainString()+s+" =("+dst.getValue().toPlainString()+")= "+ // canValue.divide(dst.getValue())+destUnit); return canValue.divide(dst.getValue()); }
public void error(String errMsg) throws UcumException { throw new UcumException("Error processing unit '"+source+"': "+ errMsg +"' at position "+Integer.toString(start)); }
private Canonical expandDefinedUnit(String indent, DefinedUnit unit) throws UcumException { String u = unit.getValue().getUnit(); if (unit.isSpecial()) { if (!handlers.exists(unit.getCode())) throw new UcumException("Not handled yet (special unit)"); else u = handlers.get(unit.getCode()).getUnits(); } Term t = new ExpressionParser(model).parse(u); debug(indent, "now handle", t); Canonical result = normalise(indent+" ", t); result.multiplyValue(unit.getValue().getValue()); return result; }