Skip to content
Permalink
Browse files

fix: Add validation to Z21 managers

  • Loading branch information...
rhwood committed Jul 14, 2019
1 parent e781ddc commit 172a6459fbe0721ae320bb4e7e678e9adff753da
@@ -1,6 +1,9 @@
package jmri.jmrix.roco.z21;

import java.util.Locale;
import jmri.Manager;
import jmri.Manager.NameValidity;
import jmri.NamedBean;
import jmri.ReporterManager;

/**
@@ -71,6 +74,57 @@ public static int getBitFromSystemName(String systemName, String prefix) {
return (num);
}

/**
* Validate a system name format.
*
* @param name the name to validate
* @param manager the manager requesting validation
* @param locale the locale for user messages
* @return name, unchanged
* @see jmri.Manager#validateSystemNameFormat(java.lang.String,
* java.util.Locale)
*/
public static String validateSystemNameFormat(String name, Manager manager, Locale locale) {
name = manager.validateSystemNamePrefix(name, locale);
String[] parts = name.substring(manager.getSystemNamePrefix().length()).split(":");
if (parts.length != 2) {
throw new NamedBean.BadSystemNameException(
Bundle.getMessage(Locale.ENGLISH, "SystemNameInvalidMissingParts", name),
Bundle.getMessage(locale, "SystemNameInvalidMissingParts", name));
}
int num;
try {
try {
num = Integer.parseInt(parts[0]);
} catch (NumberFormatException ex) {
// may have been base 16 instead of 10
num = Integer.parseInt(parts[0], 16);
}
if (num < 0 || num > 65535) {
throw new NamedBean.BadSystemNameException(
Bundle.getMessage(Locale.ENGLISH, "SystemNameInvalidCanAddress", name),
Bundle.getMessage(locale, "SystemNameInvalidCanAddress", name));
}
} catch (NumberFormatException ex) {
throw new NamedBean.BadSystemNameException(
Bundle.getMessage(Locale.ENGLISH, "SystemNameInvalidCanAddress", name),
Bundle.getMessage(locale, "SystemNameInvalidCanAddress", name));
}
try {
num = Integer.parseInt(parts[1]);
if (num < 1 || num > 8) {
throw new NamedBean.BadSystemNameException(
Bundle.getMessage(Locale.ENGLISH, "SystemNameInvalidPin", name),
Bundle.getMessage(locale, "SystemNameInvalidPin", name));
}
} catch (NumberFormatException ex) {
throw new NamedBean.BadSystemNameException(
Bundle.getMessage(Locale.ENGLISH, "SystemNameInvalidPin", name),
Bundle.getMessage(locale, "SystemNameInvalidPin", name));
}
return name;
}

/**
* Public static method to validate system name format.
* Logging of handled cases no higher than WARN.
@@ -1,6 +1,9 @@
package jmri.jmrix.roco.z21;

import java.util.Locale;
import jmri.Manager;
import jmri.Manager.NameValidity;
import jmri.NamedBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@@ -69,6 +72,62 @@ public static int getBitFromSystemName(String systemName, String prefix) {
return (-1);
}

/**
* Validate a system name format.
*
* @param name the name to validate
* @param manager the manager requesting validation
* @param locale the locale for user messages
* @return name, unchanged
* @see jmri.Manager#validateSystemNameFormat(java.lang.String,
* java.util.Locale)
*/
public static String validateSystemNameFormat(String name, Manager manager, Locale locale) {
name = manager.validateSystemNamePrefix(name, locale);
String[] parts = name.substring(manager.getSystemNamePrefix().length()).split(":");
if (parts.length != 2) {
try {
return manager.validateIntegerSystemNameFormat(name, 1, 160, locale);
} catch (NumberFormatException ex) {
// ignore -- will throw in next statement
}
throw new NamedBean.BadSystemNameException(
Bundle.getMessage(Locale.ENGLISH, "SystemNameInvalidMissingParts", name),
Bundle.getMessage(locale, "SystemNameInvalidMissingParts", name));
}
int num;
try {
try {
num = Integer.parseInt(parts[0]);
} catch (NumberFormatException ex) {
// may have been base 16 instead of 10
num = Integer.parseInt(parts[0], 16);
}
if (num < 1 || num > 20) {
throw new NamedBean.BadSystemNameException(
Bundle.getMessage(Locale.ENGLISH, "SystemNameInvalidRMAddress", name),
Bundle.getMessage(locale, "SystemNameInvalidRMAddress", name));
}
} catch (NumberFormatException ex) {
throw new NamedBean.BadSystemNameException(
Bundle.getMessage(Locale.ENGLISH, "SystemNameInvalidRMAddress", name),
Bundle.getMessage(locale, "SystemNameInvalidRMAddress", name));
}
try {
num = Integer.parseInt(parts[1]);
if (num < 1 || num > 8) {
throw new NamedBean.BadSystemNameException(
Bundle.getMessage(Locale.ENGLISH, "SystemNameInvalidPin", name),
Bundle.getMessage(locale, "SystemNameInvalidPin", name));
}
} catch (NumberFormatException ex) {
throw new NamedBean.BadSystemNameException(
Bundle.getMessage(Locale.ENGLISH, "SystemNameInvalidPin", name),
Bundle.getMessage(locale, "SystemNameInvalidPin", name));
}
return name;
}

/**
* Public static method to validate system name format.
* Logging of handled cases no higher than WARN.
@@ -1,5 +1,6 @@
package jmri.jmrix.roco.z21;

import java.util.Locale;
import jmri.JmriException;
import jmri.Sensor;
import org.slf4j.Logger;
@@ -132,20 +133,28 @@ public void message(Z21Message l) {
}

/**
* Validate Sensor system name format.
* Logging of handled cases no higher than WARN.
*
* @return VALID if system name has a valid format, else return INVALID
* {@inheritDoc}
*/
@Override
public NameValidity validSystemNameFormat(String systemName) {
if ( Z21RMBusAddress.validSystemNameFormat(systemName, 'S', getSystemPrefix()) == NameValidity.VALID ) {
return (Z21RMBusAddress.validSystemNameFormat(systemName, 'S', getSystemPrefix()));
public String validateSystemNameFormat(String name, Locale locale) {
name = validateSystemNamePrefix(name, locale);
if (name.substring(getSystemNamePrefix().length()).contains(":")) {
return Z21CanBusAddress.validateSystemNameFormat(name, this, locale);
} else {
return (Z21CanBusAddress.validSystemNameFormat(systemName, 'S', getSystemPrefix()));
return Z21RMBusAddress.validateSystemNameFormat(name, this, locale);
}
}

/**
* {@inheritDoc}
*/
@Override
public NameValidity validSystemNameFormat(String systemName) {
return Z21RMBusAddress.validSystemNameFormat(systemName, 'S', getSystemPrefix()) == NameValidity.VALID
? NameValidity.VALID
: Z21CanBusAddress.validSystemNameFormat(systemName, 'S', getSystemPrefix());
}

@Override
public boolean allowMultipleAdditions(String systemName) {
return true;
@@ -51,4 +51,11 @@ SendZ21MessageTitle = Send Z21 Message
# Z21 Sensor (Add to table pane) items
AddInputEntryToolTip = 3 ( module 1, input 3)<br>\
19:3 (Feedback module 19, input 3)<br>\
enter as number from 1 to 160 (inclusive).
Enter as address:pin where address is 0 to 65535 (or 0 to FFFF) and pin is 1 to 8<br>\
(some Z21 systems limit the address to the range 1 to 20)<br>\
Or enter as number from 1 to 160 (inclusive)

SystemNameInvalidMissingParts = {0} needs to include both an address and a pin
SystemNameInvalidCanAddress = {0} needs an address in the range 0 to 65535 or in the range 0 to FFFF
SystemNameInvalidCanAddress = {0} needs an address in the range 1 to 20
SystemNameInvalidPin = {0} needs a pin in the range 1 to 8

0 comments on commit 172a645

Please sign in to comment.
You can’t perform that action at this time.