*/
private Style findStyleOf(GetMapRequest request, MapLayerInfo layer, String styleName,
StyledLayer[] styledLayers) throws ServiceException, IOException {
Style style = null;
String layerName = layer.getName();
StyledLayer sl;
for (int i = 0; i < styledLayers.length; i++) {
sl = styledLayers[i];
if (layerName.equals(sl.getName())) {
if (sl instanceof UserLayer) {
Style[] styles = ((UserLayer) sl).getUserStyles();
// if the style name has not been specified, look it up
// the default style, otherwise lookup the one requested
for (int j = 0; style == null && styles != null && j < styles.length; j++) {
if (styleName == null || styleName.equals("") && styles[j].isDefault())
style = styles[j];
else if (styleName != null && styleName.equals(styles[j].getName()))
style = styles[j];
}
} else if (sl instanceof NamedLayer) {
Style[] styles = ((NamedLayer) sl).getStyles();
// if the style name has not been specified, look it up
// the default style, otherwise lookup the one requested
for (int j = 0; style == null && styles != null && j < styles.length; j++) {
if ((styleName == null || styleName.equals("")) && styles[j].isDefault())
style = styles[j];
else if (styleName != null && styleName.equals(styles[j].getName()))
style = styles[j];
}
if (style instanceof NamedStyle) {
style = findStyle(wms, request, style.getName());
}
} else {
throw new RuntimeException("Unknown layer type: " + sl);
}
break;
}
}
// fallback on the old GeoServer behaviour, if the style is not found find
// the first style that matches the type name
// TODO: would be nice to have a switch to turn this off since it's out of the spec
if (style == null && laxStyleMatchAllowed) {
for (int i = 0; i < styledLayers.length; i++) {
sl = styledLayers[i];
if (layerName.equals(sl.getName())) {
if (sl instanceof UserLayer) {
Style[] styles = ((UserLayer) sl).getUserStyles();
if ((null != styles) && (0 < styles.length)) {
style = styles[0];