String layerName = getNodeValue(n_layerName); //.split("_styleLayerName")[0];
String styleName = getNodeValue(n_styleName);
LOGGER.info("PutStyles SLD:\nLayer: " + layerName + ", style: " + styleName);
// store the SLD
StyleInfoImpl style = new StyleInfoImpl(getCatalog());
style.setId(styleName);
// make the SLD file in the data_dir/styles directory
File data_dir = GeoserverDataDirectory.getGeoserverDataDirectory();
File style_dir;
try {
style_dir = GeoserverDataDirectory.findConfigDir(data_dir, "styles");
} catch (ConfigurationException cfe) {
LOGGER.warning("no style dir found, creating new one");
//if for some bizarre reason we don't fine the dir, make a new one.
style_dir = new File(data_dir, "styles");
}
File styleFile = new File(style_dir, styleName + ".sld"); // styleName.sld
//styleFile.createNewFile();
LOGGER.info("Saving new SLD file to " + styleFile.getPath());
// populate it with the style code
StringBuffer sldText = new StringBuffer();
sldText.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
sldText.append("<StyledLayerDescriptor version=\"1.0.0\"\n");
sldText.append(
" xsi:schemaLocation=\"http://www.opengis.net/sld StyledLayerDescriptor.xsd\"\n");
sldText.append(
" xmlns=\"http://www.opengis.net/sld\" xmlns:ogc=\"http://www.opengis.net/ogc\"\n");
sldText.append(" xmlns:xlink=\"http://www.w3.org/1999/xlink\"\n");
sldText.append(" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n");
FileOutputStream style_fos = new FileOutputStream(styleFile); // save the sld to a file
String sldBody = serviceRequest.getSldBody();
int start = sldBody.indexOf("<NamedLayer>");
int end = sldBody.indexOf("</NamedLayer>");
sldText.append(sldBody.substring(start, end));
sldText.append("</NamedLayer>\n");
sldText.append("</StyledLayerDescriptor>");
style_fos.write(sldText.toString().getBytes());
style_fos.flush();
style_fos.close();
style.setFilename(styleFile.getAbsolutePath());
// update the data config to tell it about our new style
getCatalog().add(style);
// SLD is set up now, so tell the feature type to use it