@PostConstruct void init() throws SldException { if (getDirectory() != null) { try { if (getDirectory().getFile().exists()) { if (getDirectory().getFile().isDirectory()) { File[] sldFiles = getDirectory() .getFile() .listFiles( new FilenameFilter() { public boolean accept(File dir, String name) { return name.endsWith(".sld") || name.endsWith(".xml"); } }); for (File file : sldFiles) { RawSld raw = new RawSld(); raw.setXml(FileUtils.readFileToString(file, FILE_ENCODING)); String fileName = StringUtils.stripFilenameExtension(file.getName()); StyledLayerDescriptorInfo tmp = parseXml(fileName, raw.getXml()); raw.setName(fileName); raw.setTitle(tmp.getTitle()); raw.setVersion(tmp.getVersion()); log.info("added sld '{}' to service", fileName); allSlds.put(raw.getName(), raw); } } } } catch (Exception e) { // NOSONAR log.warn("Error while initilizing SLD service.", e); } } }
/** * Convert StyledLayerDescriptorInfo to raw xml. * * @param sldi * @return rawSld * @throws SldException */ public RawSld toXml(StyledLayerDescriptorInfo sldi) throws SldException { try { if (sldi.getVersion() == null) { sldi.setVersion("1.0.0"); } return parseSldI(sldi); } catch (JiBXException e) { throw new SldException("Validation error", e); } }
private String getTitle(StyledLayerDescriptorInfo sld, String fallback) { if (sld.getChoiceList() != null && sld.getChoiceList().size() > 0) { NamedLayerInfo nli = sld.getChoiceList().get(0).getNamedLayer(); if (nli != null && nli.getName() != null) { return nli.getName(); } UserLayerInfo uli = sld.getChoiceList().get(0).getUserLayer(); if (uli != null && uli.getName() != null) { return uli.getName(); } } return fallback; }
private RawSld parseSldI(StyledLayerDescriptorInfo sld) throws JiBXException { RawSld res = new RawSld(); IBindingFactory bfact; bfact = BindingDirectory.getFactory(StyledLayerDescriptorInfo.class); IMarshallingContext mctx = bfact.createMarshallingContext(); StringWriter writer = new StringWriter(); mctx.setOutput(writer); mctx.getXmlWriter().setIndentSpaces(2, "\n", ' '); mctx.marshalDocument(sld); res.setXml(writer.toString()); res.setName(sld.getName()); res.setVersion(sld.getVersion()); res.setTitle(sld.getTitle() == null ? getTitle(sld, "?") : sld.getTitle()); return res; }
private StyledLayerDescriptorInfo parseXml(String name, String raw) throws JiBXException { IBindingFactory bfact = BindingDirectory.getFactory(StyledLayerDescriptorInfo.class); IUnmarshallingContext uctx = bfact.createUnmarshallingContext(); Object object = uctx.unmarshalDocument(new StringReader(raw)); StyledLayerDescriptorInfo sld = (StyledLayerDescriptorInfo) object; if (sld.getName() == null) { sld.setName(name); } if (sld.getTitle() == null) { sld.setTitle(getTitle(sld, name)); } if (sld.getVersion() == null) { sld.setVersion("1.0.0"); } return sld; }