Skip to content
Permalink
Browse files

fix: throw exception on invalid string for color

In `ColorUtil.stringToColor()` throw an IllegalArgumentException if the String input cannot be turned into a color. Previously this method returned `Color.BLACK` when unable to parse a color string. Xml loaders all catch the exception and set the color being set at the time to black to avoid blocking errors loading files.
  • Loading branch information...
rhwood committed May 18, 2019
1 parent 8332082 commit d5aaa0e42cf268da9d09ee3f893336317bbdd051
@@ -81,8 +81,12 @@ public void load(Element element, Object o) {
if (element.getAttribute("scale") != null) {
scale = element.getAttribute("scale").getDoubleValue();
}
if (element.getAttribute("color") != null) {
color = ColorUtil.stringToColor(element.getAttribute("color").getValue());
try {
if (element.getAttribute("color") != null) {
color = ColorUtil.stringToColor(element.getAttribute("color").getValue());
}
} catch (IllegalArgumentException e) {
log.error("Invalid color {}; using black", element.getAttribute("color").getValue());
}
} catch (org.jdom2.DataConversionException e) {
log.error("failed to convert positional attribute");
@@ -9294,7 +9294,7 @@ public void setDefaultAlternativeTrackColor(@Nonnull Color color) {
*/
public void setTurnoutCircleColor(Color color) {
if (color == null) {
turnoutCircleColor = ColorUtil.stringToColor(getDefaultTrackColor());
turnoutCircleColor = getDefaultTrackColorColor();
} else {
turnoutCircleColor = color;
JmriColorChooser.addRecentColor(color);
@@ -9306,7 +9306,7 @@ public void setTurnoutCircleColor(Color color) {
*/
public void setTurnoutCircleThrownColor(Color color) {
if (color == null) {
turnoutCircleThrownColor = ColorUtil.stringToColor(getDefaultTrackColor());
turnoutCircleThrownColor = getDefaultTrackColorColor();
} else {
turnoutCircleThrownColor = color;
JmriColorChooser.addRecentColor(color);
@@ -80,7 +80,7 @@ public LayoutTrack(@Nonnull String ident, @Nonnull Point2D c, @Nonnull LayoutEdi
this.ident = ident;
this.center = c;
this.layoutEditor = layoutEditor;
defaultTrackColor = ColorUtil.stringToColor(layoutEditor.getDefaultTrackColor());
defaultTrackColor = layoutEditor.getDefaultTrackColorColor();
}

/**
@@ -152,13 +152,28 @@ public void loadLayoutBlocks(Element layoutblocks) {

if (b != null) {
// set attributes
Color color = ColorUtil.stringToColor(e.getAttribute("trackcolor").getValue());
b.setBlockTrackColor(color);
color = ColorUtil.stringToColor(e.getAttribute("occupiedcolor").getValue());
b.setBlockOccupiedColor(color);
Color color = Color.BLACK;
try {
color = ColorUtil.stringToColor(e.getAttribute("trackcolor").getValue());
b.setBlockTrackColor(color);
} catch (IllegalArgumentException ex) {
log.error("Invalid trackcolor {}; using black", e.getAttribute("trackcolor").getValue());
}
try {
color = ColorUtil.stringToColor(e.getAttribute("occupiedcolor").getValue());
b.setBlockOccupiedColor(color);
} catch (IllegalArgumentException ex) {
color = Color.BLACK;
log.error("Invalid occupiedcolor {}; using black", e.getAttribute("occupiedcolor").getValue(), color);
}
Attribute a = e.getAttribute("extracolor");
if (a != null) {
b.setBlockExtraColor(ColorUtil.stringToColor(a.getValue()));
try {
b.setBlockExtraColor(ColorUtil.stringToColor(a.getValue()));
} catch (IllegalArgumentException ex) {
b.setBlockExtraColor(Color.BLACK);
log.error("Invalid extracolor {}; using black", a.getValue(), color);
}
}
a = e.getAttribute("occupancysensor");
if (a != null) {
@@ -353,29 +353,49 @@ public boolean load(Element shared, Element perNode) {
panel.setXScale(xScale);
panel.setYScale(yScale);

String defaultColor = ColorUtil.ColorDarkGray;
if ((a = shared.getAttribute("defaulttrackcolor")) != null) {
defaultColor = a.getValue();
String color = ColorUtil.ColorDarkGray;
try {
if ((a = shared.getAttribute("defaulttrackcolor")) != null) {
color = a.getValue();
}
panel.setDefaultTrackColor(ColorUtil.stringToColor(color));
} catch (IllegalArgumentException e) {
panel.setDefaultTrackColor(Color.BLACK);
log.error("Invalid defaulttrackcolor {}; using black", color);
}
panel.setDefaultTrackColor(ColorUtil.stringToColor(defaultColor));

String defaultTextColor = ColorUtil.ColorBlack;
if ((a = shared.getAttribute("defaulttextcolor")) != null) {
defaultTextColor = a.getValue();
color = ColorUtil.ColorBlack;
try {
if ((a = shared.getAttribute("defaulttextcolor")) != null) {
color = a.getValue();
}
panel.setDefaultTextColor(ColorUtil.stringToColor(color));
} catch (IllegalArgumentException e) {
panel.setDefaultTextColor(Color.BLACK);
log.error("Invalid defaulttextcolor {}; using black", color);
}
panel.setDefaultTextColor(ColorUtil.stringToColor(defaultTextColor));

String turnoutCircleColor = "track"; //default to using use default track color for circle color
if ((a = shared.getAttribute("turnoutcirclecolor")) != null) {
turnoutCircleColor = a.getValue();
color = "track"; //default to using use default track color for circle color
try {
if ((a = shared.getAttribute("turnoutcirclecolor")) != null) {
color = a.getValue();
}
panel.setTurnoutCircleColor(ColorUtil.stringToColor(color));
} catch (IllegalArgumentException e) {
panel.setTurnoutCircleColor(Color.BLACK);
log.error("Invalid color {}; using black", color);
}
panel.setTurnoutCircleColor(ColorUtil.stringToColor(turnoutCircleColor));

String turnoutCircleThrownColor = turnoutCircleColor; //default to using use turnoutCircleColor
if ((a = shared.getAttribute("turnoutcirclethrowncolor")) != null) {
turnoutCircleThrownColor = a.getValue();
// default to using turnout circle color just set
try {
if ((a = shared.getAttribute("turnoutcirclethrowncolor")) != null) {
color = a.getValue();
}
panel.setTurnoutCircleThrownColor(ColorUtil.stringToColor(color));
} catch (IllegalArgumentException e) {
panel.setTurnoutCircleThrownColor(Color.BLACK);
log.error("Invalid color {}; using black", color);
}
panel.setTurnoutCircleThrownColor(ColorUtil.stringToColor(turnoutCircleThrownColor));

try { // the "turnoutfillcontrolcircles" attribute has a default="no" value in the schema;
// it will always return a "no" attribute if the attribute is not present.
@@ -557,15 +577,30 @@ public boolean load(Element shared, Element perNode) {

// set default track color
if ((a = shared.getAttribute("defaulttrackcolor")) != null) {
panel.setDefaultTrackColor(ColorUtil.stringToColor(a.getValue()));
try {
panel.setDefaultTrackColor(ColorUtil.stringToColor(a.getValue()));
} catch (IllegalArgumentException e) {
panel.setDefaultTrackColor(Color.BLACK);
log.error("Invalid color {}; using black", a.getValue());
}
}
// set default track color
// set default occupied track color
if ((a = shared.getAttribute("defaultoccupiedtrackcolor")) != null) {
panel.setDefaultOccupiedTrackColor(ColorUtil.stringToColor(a.getValue()));
try {
panel.setDefaultOccupiedTrackColor(ColorUtil.stringToColor(a.getValue()));
} catch (IllegalArgumentException e) {
panel.setDefaultOccupiedTrackColor(Color.BLACK);
log.error("Invalid color {}; using black", a.getValue());
}
}
// set default track color
// set default alternative track color
if ((a = shared.getAttribute("defaultalternativetrackcolor")) != null) {
panel.setDefaultAlternativeTrackColor(ColorUtil.stringToColor(a.getValue()));
try {
panel.setDefaultAlternativeTrackColor(ColorUtil.stringToColor(a.getValue()));
} catch (IllegalArgumentException e) {
panel.setDefaultAlternativeTrackColor(Color.BLACK);
log.error("Invalid color {}; using black", a.getValue());
}
}
try {
int red = shared.getAttribute("redBackground").getIntValue();
@@ -1,5 +1,6 @@
package jmri.jmrit.display.layoutEditor.configurexml;

import java.awt.Color;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.List;
@@ -116,12 +117,22 @@ public void load(Element element, Object o) {

a = element.getAttribute("lineColor");
if (a != null) {
s.setLineColor(ColorUtil.stringToColor(a.getValue()));
try {
s.setLineColor(ColorUtil.stringToColor(a.getValue()));
} catch (IllegalArgumentException e) {
s.setLineColor(Color.BLACK);
log.error("Invalid lineColor {}; using black", a.getValue());
}
}

a = element.getAttribute("fillColor");
if (a != null) {
s.setFillColor(ColorUtil.stringToColor(a.getValue()));
try {
s.setFillColor(ColorUtil.stringToColor(a.getValue()));
} catch (IllegalArgumentException e) {
s.setFillColor(Color.BLACK);
log.error("Invalid fillColor {}; using black", a.getValue());
}
}

Element pointsElement = element.getChild("points");
@@ -1,5 +1,6 @@
package jmri.jmrit.display.layoutEditor.configurexml;

import java.awt.Color;
import java.awt.geom.Point2D;
import java.util.HashMap;
import java.util.List;
@@ -373,7 +374,12 @@ public void load(Element element, Object o) {
}
a = decorationElement.getAttribute("color");
if (a != null) {
l.setArrowColor(ColorUtil.stringToColor(a.getValue()));
try {
l.setArrowColor(ColorUtil.stringToColor(a.getValue()));
} catch (IllegalArgumentException e) {
l.setArrowColor(Color.BLACK);
log.error("Invalid color {}; using black", a.getValue());
}
}
a = decorationElement.getAttribute("linewidth");
if (a != null) {
@@ -429,7 +435,12 @@ public void load(Element element, Object o) {

a = decorationElement.getAttribute("color");
if (a != null) {
l.setBridgeColor(ColorUtil.stringToColor(a.getValue()));
try {
l.setBridgeColor(ColorUtil.stringToColor(a.getValue()));
} catch (IllegalArgumentException e) {
l.setBridgeColor(Color.BLACK);
log.error("Invalid color {}; using black", a.getValue());
}
}

a = decorationElement.getAttribute("linewidth");
@@ -471,7 +482,12 @@ public void load(Element element, Object o) {

a = decorationElement.getAttribute("color");
if (a != null) {
l.setBumperColor(ColorUtil.stringToColor(a.getValue()));
try {
l.setBumperColor(ColorUtil.stringToColor(a.getValue()));
} catch (IllegalArgumentException e) {
l.setBumperColor(Color.BLACK);
log.error("Invalid color {}; using black", a.getValue());
}
}

a = decorationElement.getAttribute("linewidth");
@@ -530,7 +546,12 @@ public void load(Element element, Object o) {

a = decorationElement.getAttribute("color");
if (a != null) {
l.setTunnelColor(ColorUtil.stringToColor(a.getValue()));
try {
l.setTunnelColor(ColorUtil.stringToColor(a.getValue()));
} catch (IllegalArgumentException e) {
l.setTunnelColor(Color.BLACK);
log.error("Invalid color {}; using black", a.getValue());
}
}

a = decorationElement.getAttribute("linewidth");
@@ -33,7 +33,6 @@
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.JTextArea;
import javax.swing.SpinnerNumberModel;
@@ -45,7 +44,6 @@
import jmri.InstanceManager;
import jmri.Manager;
import jmri.NamedBean;
import jmri.jmrit.catalog.DefaultCatalogTreeManager;
import jmri.jmrit.display.CoordinateEdit;
import jmri.jmrit.display.Editor;
import jmri.jmrit.display.Positionable;
@@ -754,9 +752,17 @@ private void makeFileMenu() {
});
}

public void setDefaultTextColor(Color color) {
defaultTextColor = color;
}

/**
* @param color the string containing a color settable using {@link jmri.util.ColorUtil#stringToColor(String)}
* @deprecated since 4.15.7; use {@link #setDefaultTextColor(Color)} instead
*/
@Deprecated
public void setDefaultTextColor(String color) {
defaultTextColor = ColorUtil.stringToColor(color);
JmriColorChooser.addRecentColor(ColorUtil.stringToColor(color));
setDefaultTextColor(ColorUtil.stringToColor(color));
}

public String getDefaultTextColor() {
@@ -11,6 +11,8 @@
import jmri.configurexml.XmlAdapter;
import jmri.jmrit.display.PanelMenu;
import jmri.jmrit.display.switchboardEditor.SwitchboardEditor;
import jmri.util.ColorUtil;

import org.jdom2.Attribute;
import org.jdom2.Element;
import org.slf4j.Logger;
@@ -240,9 +242,14 @@ public boolean load(Element shared, Element perNode) {
}
panel.setColumns(columns);

String defaultTextColor = "black";
Color defaultTextColor = Color.BLACK;
if (shared.getAttribute("defaulttextcolor") != null) {
defaultTextColor = shared.getAttribute("defaulttextcolor").getValue();
String color = shared.getAttribute("defaulttextcolor").getValue();
try {
defaultTextColor = ColorUtil.stringToColor(color);
} catch (IllegalArgumentException ex) {
log.error("Invalid defaulttextcolor {} using black", color);
}
}
panel.setDefaultTextColor(defaultTextColor);
// set color if needed
@@ -178,7 +178,12 @@ public boolean load(Element shared, Element perNode) {
eep.setDispatcherIntegration(true);
}
if (shared.getChild("colourwhilesetting") != null) {
eep.setSettingRouteColor(ColorUtil.stringToColor(shared.getChild("colourwhilesetting").getText())); // NOI18N
try {
eep.setSettingRouteColor(ColorUtil.stringToColor(shared.getChild("colourwhilesetting").getText())); // NOI18N
} catch (IllegalArgumentException e) {
eep.setSettingRouteColor(Color.BLACK);
log.error("Invalid color {}; using black", shared.getChild("colourwhilesetting").getText());
}
int settingTimer = 2000;
try {
settingTimer = Integer.parseInt(shared.getChild("settingTimer").getText()); // NOI18N
@@ -284,31 +289,6 @@ public boolean load(Element shared, Element perNode) {
return true;
}

/**
* Get a descriptive name for a given color value.
*
* @param color Integer value of a color to display on screen
* @return lower case color name in English; None if color entered is null
* @deprecated since 4.9.4; use {@link jmri.util.ColorUtil#colorToColorName(Color)} instead
*/
@Deprecated
public static String colorToString(Color color) {
return ColorUtil.colorToColorName(color);
}

/**
* Get a color value for a color name.
*
* @param string String describing a color
* @return integer representing a screen color
* @deprecated since 4.9.4; use {@link jmri.util.ColorUtil#stringToColor(String)} instead
*
*/
@Deprecated
public static Color stringToColor(String string) {
return ColorUtil.stringToColor(string);
}

@Override
public int loadOrder() {
return InstanceManager.getDefault(EntryExitPairs.class).getXMLOrder();

0 comments on commit d5aaa0e

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