public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (!(obj instanceof StyleInfo)) return false; final StyleInfo other = (StyleInfo) obj; if (filename == null) { if (other.getFilename() != null) return false; } else if (!filename.equals(other.getFilename())) return false; if (id == null) { if (other.getId() != null) return false; } else if (!id.equals(other.getId())) return false; if (name == null) { if (other.getName() != null) return false; } else if (!name.equals(other.getName())) return false; if (workspace == null) { if (other.getWorkspace() != null) return false; } else if (!workspace.equals(other.getWorkspace())) return false; if (format == null) { if (other.getFormat() != null) return false; } else { if (!format.equals(other.getFormat())) return false; } if (languageVersion == null) { if (other.getFormatVersion() != null) return false; } else if (!languageVersion.equals(other.getFormatVersion())) return false; return true; }
private void renameStyle(StyleInfo s, String newName) throws IOException { // rename style definition file Resource style = dd.style(s); StyleHandler format = Styles.handler(s.getFormat()); Resource target = uniqueResource(style, newName, format.getFileExtension()); renameRes(style, target.name()); s.setFilename(target.name()); // rename generated sld if appropriate if (!SLDHandler.FORMAT.equals(format.getFormat())) { Resource sld = style.parent().get(FilenameUtils.getBaseName(style.name()) + ".sld"); if (sld.getType() == Type.RESOURCE) { LOGGER.fine("Renaming style resource " + s.getName() + " to " + newName); Resource generated = uniqueResource(sld, newName, "sld"); renameRes(sld, generated.name()); } } }
/** * Serializes a style to configuration optionally formatting the style when writing it. * * @param info The configuration for the style. * @param style The style object. * @param format Whether to format the style */ public void writeSLD(StyleInfo info, StyledLayerDescriptor style, boolean format) throws IOException { synchronized (sldCache) { Resource styleFile = dataDir().style(info); BufferedOutputStream out = new BufferedOutputStream(styleFile.out()); try { Styles.handler(info.getFormat()) .encode(style, info.getFormatVersion(), format, out); clear(info); } finally { out.close(); } } }
StyleHandler styleHandler() { String format = styleModel.getObject().getFormat(); return Styles.handler(format); }
/** * Serializes a style to configuration optionally formatting the style when writing it. * * @param info The configuration for the style. * @param style The style object. * @param format Whether to format the style */ public void writeStyle(StyleInfo info, Style style, boolean format) throws IOException { synchronized (styleCache) { Resource styleFile = dataDir().style(info); BufferedOutputStream out = new BufferedOutputStream(styleFile.out()); try { Styles.handler(info.getFormat()) .encode(Styles.sld(style), info.getFormatVersion(), format, out); clear(info); } finally { out.close(); } } }
/** * Retrieve the StyleInfo as a GeoTools Style object. Note this is just the data structure as * written, the matching external graphics are unmodified and may not be (yet) available on the * local system. * * @param s The style * @return A {@link Resource} */ protected @Nonnull Style parsedStyleResources(StyleInfo s) throws IOException { final Resource styleResource = style(s); if (styleResource.getType() == Type.UNDEFINED) { throw new FileNotFoundException("No such resource: " + s.getFilename()); } final DefaultResourceLocator locator = new ResourceAwareResourceLocator(); locator.setSourceUrl(Resources.toURL(styleResource)); StyledLayerDescriptor sld = Styles.handler(s.getFormat()) .parse(styleResource, s.getFormatVersion(), locator, null); final Style style = Styles.style(sld); assert style != null; return style; }
@Override public void writeInternal(Object object, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { if (object instanceof RestWrapper) { object = ((RestWrapper<?>) object).getObject(); } if (object instanceof StyleInfo) { StyleInfo style = (StyleInfo) object; // optimization, if the requested format is the same as the native format // of the style, stream the file directly from the disk, otherwise encode // the style in the requested format if (handler.getFormat().equalsIgnoreCase(style.getFormat())) { copyDefinition(style, outputMessage.getBody()); return; } } Style style = object instanceof StyleInfo ? ((StyleInfo) object).getStyle() : (Style) object; StyledLayerDescriptor sld = Styles.sld(style); // TODO: support pretty print somehow - probably a hint handler.encode(sld, version, false, outputMessage.getBody()); }
/** * Retrieve the styled layer descriptor prepared for direct GeoTools use. All file references * have been made absolute. * * @param s The style * @return A {@link StyledLayerDescriptor} */ public @Nonnull StyledLayerDescriptor parsedSld(final StyleInfo s) throws IOException { final Resource styleResource = style(s); if (styleResource.getType() == Type.UNDEFINED) { throw new IOException( "No such resource: " + s.getFilename() + (s.getWorkspace() != null ? " in workspace " + s.getWorkspace() : "")); } File input = styleResource.file(); DefaultResourceLocator locator = new GeoServerResourceLocator(); locator.setSourceUrl(Resources.toURL(styleResource)); EntityResolver entityResolver = getEntityResolver(); final StyledLayerDescriptor sld = Styles.handler(s.getFormat()) .parse(input, s.getFormatVersion(), locator, getEntityResolver()); return sld; }
if ("sld".equalsIgnoreCase(si.getFormat())) { String filename = si.getFilename(); String filenameCss = filename.substring(0, filename.lastIndexOf('.')) + ".css";
@Test public void testParseStyleTwiceLock() throws Exception { StyleInfo style = getCatalog().getStyles().get(0); FileSystemResourceStore store = (FileSystemResourceStore) getDataDirectory().getResourceStore(); store.setLockProvider(new MemoryLockProvider()); // parse twice to check we are not locking on it Resource resource = getDataDirectory().style(style); Styles.handler(style.getFormat()) .parse(resource, style.getFormatVersion(), new DefaultResourceLocator(), null); Styles.handler(style.getFormat()) .parse(resource, style.getFormatVersion(), new DefaultResourceLocator(), null); }
String format = style.getFormat(); style.setFormat(format); Version version = Styles.handler(format).version(rawStyle);
@Override protected void onSubmit(AjaxRequestTarget target, Form<?> form) { // we need to force validation or the value won't be converted styles.processInput(); StyleInfo style = (StyleInfo) styles.getConvertedInput(); if (style != null) { try { // same here, force validation or the field won't be updated stylePage.editor.reset(); stylePage.setRawStyle(stylePage.readFile(style)); stylePage.getStyleInfo().setFormat(style.getFormat()); target.appendJavaScript( String.format( "if (document.gsEditors) { document.gsEditors.editor.setOption('mode', '%s'); }", stylePage.styleHandler().getCodeMirrorEditMode())); clearFeedbackMessages(); } catch (Exception e) { clearFeedbackMessages(); stylePage.error( "Errors occurred loading the '" + style.getName() + "' style"); LOGGER.log( Level.WARNING, "Errors occurred loading the '" + style.getName() + "' style", e); } target.add(stylePage); } } };
/** Helper method that parses the file associated with a style. */ private Style parseStyleFile(StyleInfo styleInfo, InputStream input) throws Exception { StyleHandler styleHandler = Styles.handler(styleInfo.getFormat()); StyledLayerDescriptor styleDescriptor = styleHandler.parse(input, styleInfo.getFormatVersion(), null, null); return Styles.style(styleDescriptor); }
stylePage.getGeoServerApplication().getApplicationContext()); StyleInfo si = new StyleInfoImpl(stylePage.getCatalog()); String format = stylePage.getStyleInfo().getFormat(); si.setFormat(format); String styleName = "tmp" + UUID.randomUUID().toString(); String styleFileName = styleName + '.' + Styles.handler(si.getFormat()).getFileExtension(); si.setFilename(styleFileName); si.setName(styleName);
assertNotNull(style); assertEquals("sld", style.getFormat()); assertEquals(SLDHandler.VERSION_11, style.getFormatVersion());
/** * Test recovery of a CSS Style generated by the old CSS Extension, when the StyleInfo has no * declared <format> and its filename points to a derived SLD. */ @Test public void testRecoverLostCssStyle() throws Exception { StyleInfo styleInfo = catalog.getStyleByName(oldCssStyle); StyleEditPage edit = new StyleEditPage(styleInfo); tester.startPage(edit); tester.assertRenderedPage(StyleEditPage.class); tester.assertNoErrorMessage(); // Assert that the page displays the format as css tester.assertModelValue("styleForm:context:panel:format", "css"); // Assert that the editor text area contains css String editorContents = (String) tester.getComponentFromLastRenderedPage( "styleForm:styleEditor:editorContainer:editorParent:editor") .getDefaultModelObject(); Styles.handler("css").parse(editorContents, null, null, null); // Assert that the catalog's StyleInfo is now a css style StyleInfo si = catalog.getStyleByName(oldCssStyle); assertEquals("css", si.getFormat()); assertEquals(oldCssStyle + ".css", si.getFilename()); }
/** * Test recovery of a CSS Style generated by the old CSS Extension, when the StyleInfo declares * <format>sld</format> and its filename points to a derived SLD. */ @Test public void testRecoverLostCssStyleWithFormatSetToSLD() throws Exception { StyleInfo styleInfo = catalog.getStyleByName(oldCssStyleWithFormatSLD); StyleEditPage edit = new StyleEditPage(styleInfo); tester.startPage(edit); tester.assertRenderedPage(StyleEditPage.class); tester.assertNoErrorMessage(); // Assert that the page displays the format as css tester.assertModelValue("styleForm:context:panel:format", "css"); // Assert that the editor text area contains css String editorContents = (String) tester.getComponentFromLastRenderedPage( "styleForm:styleEditor:editorContainer:editorParent:editor") .getDefaultModelObject(); Styles.handler("css").parse(editorContents, null, null, null); // Assert that the catalog's StyleInfo is now a css style StyleInfo si = catalog.getStyleByName(oldCssStyleWithFormatSLD); assertEquals("css", si.getFormat()); assertEquals(oldCssStyleWithFormatSLD + ".css", si.getFilename()); }
/** * Test that the recovery code does not overwrite generated SLD styles if they were subsequently * edited. */ @Test public void testIgnoreCssStyleIfSLDWasEdited() throws Exception { StyleInfo styleInfo = catalog.getStyleByName(oldCssStyleWithSLDManuallyEdited); StyleEditPage edit = new StyleEditPage(styleInfo); tester.startPage(edit); tester.assertRenderedPage(StyleEditPage.class); tester.assertNoErrorMessage(); // Assert that the page displays the format as SLD tester.assertModelValue("styleForm:context:panel:format", "sld"); // Assert that the editor text area contains SLD String editorContents = (String) tester.getComponentFromLastRenderedPage( "styleForm:styleEditor:editorContainer:editorParent:editor") .getDefaultModelObject(); Styles.handler("sld").parse(editorContents, null, null, null); // Assert that the catalog's StyleInfo is still a SLD style StyleInfo si = catalog.getStyleByName(oldCssStyleWithSLDManuallyEdited); assertEquals("sld", si.getFormat()); assertEquals(oldCssStyleWithSLDManuallyEdited + ".sld", si.getFilename()); } }