- made SCL parser more tolerant (added warnings)

pull/6/head
Michael Zillgith 10 years ago
parent ad0b4cbd7a
commit 8521a42f0d

@ -3,7 +3,7 @@ package com.libiec61850.scl.model;
/* /*
* DataModelValue.java * DataModelValue.java
* *
* Copyright 2013 Michael Zillgith * Copyright 2013, 2015 Michael Zillgith
* *
* This file is part of libIEC61850. * This file is part of libIEC61850.
* *
@ -44,7 +44,26 @@ public class DataModelValue {
switch (type) { switch (type) {
case ENUMERATED: case ENUMERATED:
EnumerationType enumType = (EnumerationType) sclType; EnumerationType enumType = (EnumerationType) sclType;
try {
this.value = (Object) (new Integer(enumType.getOrdByEnumString(value))); this.value = (Object) (new Integer(enumType.getOrdByEnumString(value)));
} catch (IllegalValueException e) {
try {
this.value = Integer.parseInt(value);
if (enumType.isValidOrdValue(Integer.parseInt(value)) == false) {
throw new IllegalValueException(value +
" is not a valid value of type " + sclType.getId());
}
System.out.println("WARNING: Initialization of ENUM with ordinal value!");
}
catch (NumberFormatException nfe) {
throw new IllegalValueException(value +
" is not a valid value of type " + sclType.getId());
}
}
break; break;
case INT8: case INT8:
@ -55,28 +74,56 @@ public class DataModelValue {
case INT32U: case INT32U:
case INT24U: case INT24U:
case INT64: case INT64:
if (value.isEmpty())
String trimmedValue = value.trim();
if (trimmedValue != value) {
System.out.println("WARNING: value initializer contains leading or trailing whitespace");
}
if (trimmedValue.isEmpty())
this.value = new Long(0); this.value = new Long(0);
else else
this.value = new Long(value); this.value = Long.decode(trimmedValue);
break; break;
case BOOLEAN: case BOOLEAN:
if (value.toLowerCase().equals("true"))
trimmedValue = value.trim();
if (trimmedValue != value) {
System.out.println("WARNING: value initializer contains leading or trailing whitespace");
}
if (trimmedValue.toLowerCase().equals("true"))
this.value = new Boolean(true); this.value = new Boolean(true);
else else
this.value = new Boolean(false); this.value = new Boolean(false);
break; break;
case FLOAT32: case FLOAT32:
if (value.isEmpty())
trimmedValue = value.trim();
if (trimmedValue != value) {
System.out.println("WARNING: value initializer contains leading or trailing whitespace");
}
if (trimmedValue.isEmpty())
this.value = new Float(0); this.value = new Float(0);
else else
this.value = new Float(value); this.value = new Float(trimmedValue);
break; break;
case FLOAT64: case FLOAT64:
if (value.isEmpty())
trimmedValue = value.trim();
if (trimmedValue != value) {
System.out.println("WARNING: value initializer contains leading or trailing whitespace");
}
if (trimmedValue.isEmpty())
this.value = new Double(0); this.value = new Double(0);
else else
this.value = new Double(value); this.value = new Double(trimmedValue);
break; break;
case UNICODE_STRING_255: case UNICODE_STRING_255:
this.value = value; this.value = value;
@ -90,10 +137,14 @@ public class DataModelValue {
this.value = (Object) value; this.value = (Object) value;
break; break;
case CHECK: case CHECK:
System.out.println("Warning: Initialization of CHECK is unsupported!\n"); System.out.println("Warning: Initialization of CHECK is unsupported!");
case CODEDENUM: case CODEDENUM:
this.value = null; this.value = null;
System.out.println("Warning: Initialization of CODEDENUM is unsupported!\n"); System.out.println("Warning: Initialization of CODEDENUM is unsupported!");
break;
case QUALITY:
this.value = null;
System.out.println("Warning: Initialization of QUALITY is unsupported!");
break; break;
default: default:
throw new IllegalValueException("Unsupported type " + type.toString() + " value: " + value); throw new IllegalValueException("Unsupported type " + type.toString() + " value: " + value);

@ -80,4 +80,13 @@ public class EnumerationType extends SclType {
throw new IllegalValueException("Enum has no value " + enumString); throw new IllegalValueException("Enum has no value " + enumString);
} }
public boolean isValidOrdValue(int ordValue) {
for (EnumerationValue enumValue : enumValues) {
if (enumValue.getOrd() == ordValue)
return true;
}
return false;
}
} }

Loading…
Cancel
Save