Skip to content
32 changes: 22 additions & 10 deletions core/src/org/sbml/jsbml/AbstractNamedSBaseWithUnit.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import org.apache.log4j.Logger;
import org.sbml.jsbml.Unit.Kind;
import org.sbml.jsbml.util.TreeNodeChangeEvent;
import org.sbml.jsbml.validator.SyntaxChecker;
import org.sbml.jsbml.validator.offline.factory.SBMLErrorFactory;

/**
* This simple implementation of the interfaces
Expand Down Expand Up @@ -283,7 +285,7 @@ public void setUnits(Kind unitKind) {
@Override
public void setUnits(String units) {
if ((units != null) && (units.trim().length() == 0)) {
units = null; // If we pass the empty String or null, the value is reset.
units = null;
}

String oldUnits = unitsID;
Expand All @@ -292,24 +294,34 @@ public void setUnits(String units) {
unitsID = null;
} else {
units = units.trim();
unitsID = units;

boolean illegalArgument = false;
// Use the generic SId syntax checker with the current level and version
boolean isSyntaxValid = SyntaxChecker.isValidId(units, getLevel(), getVersion());
boolean isReferenceValid = isSyntaxValid && Unit.isValidUnit(getModel(), units);

if (!Unit.isValidUnit(getModel(), units)) {
illegalArgument = true; // TODO - make use of the offline validation once attributes validation is in place.
}
if (illegalArgument) {
if (!isSyntaxValid || !isReferenceValid) {
Comment thread
dyrpsf marked this conversation as resolved.
Outdated
if (!isReadingInProgress()) {
throw new IllegalArgumentException(MessageFormat.format(
JSBML.ILLEGAL_UNIT_EXCEPTION_MSG, units));
SBMLDocument doc = getSBMLDocument();
if (doc != null && doc.getErrorLog() != null) {
int errorCode = !isSyntaxValid ? 10311 : 10313;

doc.getErrorLog().add(SBMLErrorFactory.createError(
errorCode,
getLevel(),
getVersion()
));
Comment thread
dyrpsf marked this conversation as resolved.
Outdated
} else {
throw new IllegalArgumentException(MessageFormat.format(
JSBML.ILLEGAL_UNIT_EXCEPTION_MSG, units));
}
Comment thread
dyrpsf marked this conversation as resolved.
} else {
logger.info(MessageFormat.format(JSBML.ILLEGAL_UNIT_EXCEPTION_MSG, units));
}
}
unitsID = units;
}

if (oldUnits != unitsID) {
if (oldUnits != null ? !oldUnits.equals(unitsID) : unitsID != null) {
firePropertyChange(TreeNodeChangeEvent.units, oldUnits, unitsID);
}
}
Expand Down