/** * Factory method which tries to parse an untyped value. * * @param pTyped * @param pUriStr * @return <code>Value</code> instance * @throws IllegalArgumentException if parsing failed. */ public static Value parse(boolean pTyped, URIString pUriStr) throws IllegalArgumentException { // TODO: tracing TRC.debug(uriStr.toString()); if (pTyped) return parseTypedValue(pUriStr); URIString uriStr = pUriStr.deepCopy(); Value value; try { StringValue strVal = (StringValue) StringValue.parse(uriStr); // string like value // is this string an instance reference? try { URI ref = URI.parseRef(new URIString(strVal.toString()), false); value = new ReferenceValue(ref); } catch (IllegalArgumentException e) { if ((value = DateTimeValue.parse(strVal.toString())) == null) { // if not dateTimeValue, it is stringvalue value = strVal; } } pUriStr.set(uriStr); return value; } catch (IllegalArgumentException e) { // non string like value if ((value = IntegerValue.parse(uriStr)) != null || (value = RealValue.parse(uriStr)) != null || (value = BooleanValue.parse(uriStr)) != null || (value = CharValue.parse(uriStr)) != null) { pUriStr.set(uriStr); return value; } String msg = "Failed to parse untyped value!\n" + uriStr.markPosition(); throw new IllegalArgumentException(msg); } }
private static Value parseTypedValue(URIString pUriStr) throws IllegalArgumentException { URIString uriStr = pUriStr.deepCopy(); int typeInfoPos = uriStr.getPos(); String typeInfo = parseTypeInfo(uriStr); if (typeInfo == null) { String msg = "typeInfo expected!\n" + uriStr.markPosition(); throw new IllegalArgumentException(msg); } int valuePos = uriStr.getPos(); Value val; try { if (typeInfo.equalsIgnoreCase(MOF.DT_STR)) { val = StringValue.parse(uriStr); } else if (typeInfo.equalsIgnoreCase(MOF.REFERENCE)) { val = parseTypedReference(uriStr); } else if (typeInfo.equalsIgnoreCase(MOF.DT_DATETIME)) { val = parseTypedDateTime(uriStr); } else if (typeInfo.equalsIgnoreCase(MOF.DT_CHAR16)) { val = CharValue.parse(uriStr); } else if (typeInfo.equalsIgnoreCase(MOF.DT_BOOL)) { val = BooleanValue.parse(uriStr); } else if (typeInfo.equalsIgnoreCase(MOF.DT_SINT8)) { val = IntegerValue.parseSigned(uriStr, 8); } else if (typeInfo.equalsIgnoreCase(MOF.DT_SINT16)) { val = IntegerValue.parseSigned(uriStr, 16); } else if (typeInfo.equalsIgnoreCase(MOF.DT_SINT32)) { val = IntegerValue.parseSigned(uriStr, 32); } else if (typeInfo.equalsIgnoreCase(MOF.DT_SINT64)) { val = IntegerValue.parseSigned(uriStr, 64); } else if (typeInfo.equalsIgnoreCase(MOF.DT_UINT8)) { val = IntegerValue.parseUnsigned(uriStr, 8); } else if (typeInfo.equalsIgnoreCase(MOF.DT_UINT16)) { val = IntegerValue.parseUnsigned(uriStr, 16); } else if (typeInfo.equalsIgnoreCase(MOF.DT_UINT32)) { val = IntegerValue.parseUnsigned(uriStr, 32); } else if (typeInfo.equalsIgnoreCase(MOF.DT_UINT64)) { val = IntegerValue.parseUnsigned(uriStr, 64); } else if (typeInfo.equalsIgnoreCase(MOF.DT_REAL32)) { val = RealValue.parseFloat(uriStr); } else if (typeInfo.equalsIgnoreCase(MOF.DT_REAL64)) { val = RealValue.parseDouble(uriStr); } else { val = null; } } catch (IllegalArgumentException e) { String msg = "Failed to parse " + typeInfo + " value!\n" + uriStr.markPosition(valuePos) + "Nested message:\n" + e.getMessage(); throw new IllegalArgumentException(msg); } if (val == null) { String msg = "Unknown type:" + typeInfo + "!\n" + uriStr.markPosition(typeInfoPos); throw new IllegalArgumentException(msg); } pUriStr.set(uriStr); return val; }