Skip to content

Commit

Permalink
fix: more validation method overrides for specific systems
Browse files Browse the repository at this point in the history
  • Loading branch information
rhwood committed Jul 12, 2019
1 parent c4efdfe commit e8e825d
Show file tree
Hide file tree
Showing 11 changed files with 253 additions and 66 deletions.
38 changes: 2 additions & 36 deletions java/src/jmri/jmrix/anyma/AnymaDMX_SystemConnectionMemo.java
Expand Up @@ -112,7 +112,7 @@ public int getChannelFromSystemName(String systemName) {
if (k > offset) { // k = position of "L" char in name
try {
result = Integer.parseInt(systemName.substring(k));
} catch (Exception e) {
} catch (NumberFormatException e) {
log.warn("invalid character in channel number field of anyma dmx system name: {}", systemName);
}
}
Expand Down Expand Up @@ -189,9 +189,7 @@ public NameValidity validSystemNameFormat(String systemName, char type) {
} else {
log.debug("number field out of range in anyma dmx system name: {}", systemName);
}
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
} catch (NumberFormatException e) {
log.debug("invalid character in number field of anyma dmx system name: {}", systemName);
}
} else {
Expand All @@ -203,38 +201,6 @@ public NameValidity validSystemNameFormat(String systemName, char type) {
return result;
}

/**
* Public static method to construct a anyma dmx system name from type
* character, node address, and channel number.
* <p>
* If the supplied character is not valid, or the node address is out of the
* 0 - 127 range, or the channel number is out of the 1 - 512 range, an
* error message is logged and the null string "" is returned.
*
* @return a system name in the DaLnnnxxx format
*/
public String makeSystemName(String type, int nAddress, int channelNum) {
log.debug("* makeSystemName('{}', {}, {})", type, nAddress, channelNum);
String nName = "";
if (type.equals("L")) {
if ((nAddress >= 0) && (nAddress < 1000)) {
if ((channelNum >= 1) && (channelNum <= 512)) {
nName = getSystemPrefix() + nAddress + type + Integer.toString(channelNum);
} else {
log.warn("invalid channel number proposed for system name");
return nName;
}
} else {
log.warn("invalid node address proposed for system name");
return nName;
}
} else {
log.error("invalid type character proposed for system name");
return nName;
}
return nName;
}

/**
* Public static method to validate anyma dmx system name for configuration.
* Does validate node number and system prefix.
Expand Down
85 changes: 85 additions & 0 deletions java/src/jmri/jmrix/cmri/CMRISystemConnectionMemo.java
@@ -1,11 +1,13 @@
package jmri.jmrix.cmri;

import java.util.Locale;
import java.util.ResourceBundle;
import javax.annotation.Nonnull;
import javax.annotation.CheckReturnValue;
import jmri.InstanceManager;
import jmri.Light;
import jmri.Manager.NameValidity;
import jmri.NamedBean;
import jmri.Sensor;
import jmri.Turnout;
import jmri.jmrix.AbstractNode;
Expand Down Expand Up @@ -413,6 +415,89 @@ public NameValidity validSystemNameFormat(String systemName, char type) {
return NameValidity.VALID;
}

/**
* Validate system name format. Does not check whether that node is defined
* on current system.
*
* @param systemName the system name
* @param type the device type
* @param locale the Locale for user messages
* @return systemName unmodified
* @throws IllegalArgumentException if unable to validate systemName
*/
public String validateSystemNameFormat(String systemName, char type, Locale locale) throws IllegalArgumentException {
String prefix = getSystemPrefix() + type;
if (!systemName.startsWith(prefix)) {
throw new NamedBean.BadSystemNameException(
Bundle.getMessage(Locale.ENGLISH, "InvalidSystemNameInvalidPrefix", systemName),
Bundle.getMessage(locale, "InvalidSystemNameInvalidPrefix", systemName));
}
String address = systemName.substring(prefix.length());
int node = 0;
int bit = 0;
if (!address.contains("B") && !address.contains(":")) {
// This is a CLnnnxxx pattern address
int num;
try {
num = Integer.parseInt(address);
node = num / 1000;
bit = num - ((num / 1000) * 1000);
} catch (NumberFormatException e) {
throw new NamedBean.BadSystemNameException(
Bundle.getMessage(Locale.ENGLISH, "InvalidSystemNameNotInteger", systemName, prefix),
Bundle.getMessage(locale, "InvalidSystemNameNotInteger", systemName, prefix));
}
} else {
// This is a CLnBxxx or CLn:xxx pattern address
String[] parts = address.split("B");
if (parts.length != 2) {
parts = address.split(":");
if (parts.length != 2) {
if (address.indexOf(":") == 0 && address.indexOf("B") == 0) {
// no node
throw new NamedBean.BadSystemNameException(
Bundle.getMessage(Locale.ENGLISH, "InvalidSystemNameNodeInvalid", systemName, ""),
Bundle.getMessage(locale, "InvalidSystemNameNodeInvalid", systemName, ""));
} else {
// no bit
throw new NamedBean.BadSystemNameException(
Bundle.getMessage(Locale.ENGLISH, "InvalidSystemNameBitInvalid", systemName, ""),
Bundle.getMessage(locale, "InvalidSystemNameBitInvalid", systemName, ""));
}
}
}
try {
node = Integer.parseInt(parts[0]);
} catch (NumberFormatException e) {
log.debug("invalid character in node address field of CMRI system name: {}", systemName);
throw new NamedBean.BadSystemNameException(
Bundle.getMessage(Locale.ENGLISH, "InvalidSystemNameNodeInvalid", systemName, parts[0]),
Bundle.getMessage(locale, "InvalidSystemNameNodeInvalid", systemName, parts[0]));
}
try {
bit = Integer.parseInt(parts[1]);
} catch (NumberFormatException ex) {
log.debug("invalid character in bit number field of CMRI system name: {}", systemName);
throw new NamedBean.BadSystemNameException(
Bundle.getMessage(Locale.ENGLISH, "InvalidSystemNameBitInvalid", systemName, parts[1]),
Bundle.getMessage(locale, "InvalidSystemNameBitInvalid", systemName, parts[1]));
}
}
if (node < 0 || node >= 128) {
log.debug("node address field out of range in CMRI system name: {}", systemName);
throw new NamedBean.BadSystemNameException(
Bundle.getMessage(Locale.ENGLISH, "InvalidSystemNameNodeInvalid", systemName, node),
Bundle.getMessage(locale, "InvalidSystemNameNodeInvalid", systemName, node));
}
if (bit < 1 || bit > 2048) {
log.debug("bit number field out of range in CMRI system name: {}", systemName);
throw new NamedBean.BadSystemNameException(
Bundle.getMessage(Locale.ENGLISH, "InvalidSystemNameBitInvalid", systemName, bit),
Bundle.getMessage(locale, "InvalidSystemNameBitInvalid", systemName, bit));
}
return systemName;
}

/**
* Test if a C/MRI input bit is free for assignment. Test is not performed
* if the node address is invalid or bit number is greater than 2048.
Expand Down
8 changes: 6 additions & 2 deletions java/src/jmri/jmrix/cmri/CmriBundle.properties
Expand Up @@ -94,11 +94,15 @@ AddOutputEntryToolTip = 3 (Node 0, Bit 3)<br>\
4:3 (Node 4, Bit 3)<br>\
4B30 (Node 4, Bit 30)<br>\
Node: 0 - 127<br>\
Max. value for Bit: 999 (9999 using B-pattern)
Bit: 1 - 2048 (1 - 999 when not using separator)

AddInputEntryToolTip = 1003 (Node 1, Bit 3)<br>\
4003 (Node 4, Bit 3)<br>\
4:3 (Node 4, Bit 3)<br>\
4B30 (Node 4, Bit 30)<br>\
Node: 0 - 127<br>\
Max. value for Bit: 999 (9999 using B-pattern)
Bit: 1 - 2048 (1 - 999 when not using separator)

# Validation errors
InvalidSystemNameNodeInvalid = "{0}" has "{1}" for Node; must be number in range 0 - 127"
InvalidSystemNameBitInvalid = "{0}" has "{1}" for Bit; must be number in range 1 - 2048"
11 changes: 10 additions & 1 deletion java/src/jmri/jmrix/cmri/serial/SerialLightManager.java
@@ -1,5 +1,6 @@
package jmri.jmrix.cmri.serial;

import java.util.Locale;
import jmri.Light;
import jmri.jmrix.cmri.CMRISystemConnectionMemo;
import jmri.managers.AbstractLightManager;
Expand Down Expand Up @@ -84,12 +85,20 @@ public void notifyLightCreationError(String conflict, int bitNum) {
javax.swing.JOptionPane.INFORMATION_MESSAGE, null);
}

/**
* {@inheritDoc}
*/
@Override
public String validateSystemNameFormat(String systemName, Locale locale) {
return _memo.validateSystemNameFormat(super.validateSystemNameFormat(systemName, locale), typeLetter(), locale);
}

/**
* {@inheritDoc}
*/
@Override
public NameValidity validSystemNameFormat(String systemName) {
return _memo.validSystemNameFormat(systemName, 'L');
return _memo.validSystemNameFormat(systemName, typeLetter());
}

/**
Expand Down
11 changes: 10 additions & 1 deletion java/src/jmri/jmrix/cmri/serial/SerialSensorManager.java
@@ -1,5 +1,6 @@
package jmri.jmrix.cmri.serial;

import java.util.Locale;
import jmri.JmriException;
import jmri.Sensor;
import jmri.jmrix.AbstractNode;
Expand Down Expand Up @@ -251,12 +252,20 @@ public String getNextValidAddress(String curAddress, String prefix) {
}
}

/**
* {@inheritDoc}
*/
@Override
public String validateSystemNameFormat(String systemName, Locale locale) {
return _memo.validateSystemNameFormat(super.validateSystemNameFormat(systemName, locale), typeLetter(), locale);
}

/**
* {@inheritDoc}
*/
@Override
public NameValidity validSystemNameFormat(String systemName) {
return _memo.validSystemNameFormat(systemName, 'S');
return _memo.validSystemNameFormat(systemName, typeLetter());
}

/**
Expand Down
11 changes: 10 additions & 1 deletion java/src/jmri/jmrix/cmri/serial/SerialTurnoutManager.java
@@ -1,5 +1,6 @@
package jmri.jmrix.cmri.serial;

import java.util.Locale;
import javax.swing.JOptionPane;
import jmri.JmriException;
import jmri.Turnout;
Expand Down Expand Up @@ -353,12 +354,20 @@ public String getNextValidAddress(String curAddress, String prefix) throws JmriE
}
}

/**
* {@inheritDoc}
*/
@Override
public String validateSystemNameFormat(String systemName, Locale locale) {
return _memo.validateSystemNameFormat(super.validateSystemNameFormat(systemName, locale), typeLetter(), locale);
}

/**
* {@inheritDoc}
*/
@Override
public NameValidity validSystemNameFormat(String systemName) {
return _memo.validSystemNameFormat(systemName, 'T');
return _memo.validSystemNameFormat(systemName, typeLetter());
}

/**
Expand Down
6 changes: 5 additions & 1 deletion java/src/jmri/jmrix/oaktree/OakTreeBundle.properties
Expand Up @@ -14,4 +14,8 @@ AddOutputEntryToolTip=1003 (Output node 1, pin 3)
AddInputEntryToolTip=22016 (Sensor node 22, pin 16)<br>\
22B16 (Sensor node 22, pin 16)<br>\
Valid Node range: 0 - 127<br>\
Valid Bit range: 001-999
Valid Bit range: 1 - 2048 (001 - 999 when not using separator)

# Validation errors
InvalidSystemNameNodeInvalid = "{0}" has "{1}" for Node; must be number in range 0 - 127"
InvalidSystemNameBitInvalid = "{0}" has "{1}" for Bit; must be number in range 1 - 2048"

0 comments on commit e8e825d

Please sign in to comment.