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; }
/** * 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(); } } }
/** * 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; }
/** * 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; }
@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); }
.parse( newStyle.file(), s.getFormatVersion(), null, getCatalog().getResourcePool().getEntityResolver()); .parse( oldStyle.file(), s.getFormatVersion(), null, getCatalog().getResourcePool().getEntityResolver());
@Test public void testPutAsSLD11Raw() throws Exception { String xml = newSLD11XML(); // check it's version 1.0 before StyleInfo infoBefore = catalog.getStyleByName("Ponds"); assertThat(infoBefore.getFormatVersion(), equalTo(SLDHandler.VERSION_10)); // put a SLD 1.1 in raw mode (the content type is ignored in raw mode, but mimicking was // gsconfig does here) MockHttpServletResponse response = putAsServletResponse( RestBaseController.ROOT_PATH + "/styles/Ponds?raw=true", xml, "application/vnd.ogc.se+xml"); assertEquals(200, response.getStatus()); // now it should have been "upgraded" to 1.1 StyleInfo infoAfter = catalog.getStyleByName("Ponds"); assertThat(infoAfter.getFormatVersion(), equalTo(SLDHandler.VERSION_11)); }
/** 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); }
@Test public void testImportStyleWithCorrectVersion() throws Exception { File dir = unpack("shape/archsites_epsg_prj.zip"); // add an sld file String sld = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<StyledLayerDescriptor xmlns=\"http://www.opengis.net/sld\" xmlns:ogc=\"http://www.opengis.net/ogc\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" version=\"1.1.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" xsi:schemaLocation=\"http://www.opengis.net/sld http://schemas.opengis.net/sld/1.1.0/StyledLayerDescriptor.xsd\" xmlns:se=\"http://www.opengis.net/se\">" + " <NamedLayer>" + " <UserStyle>" + " <se:FeatureTypeStyle>" + " <se:Rule>" + " <se:PolygonSymbolizer>" + " <se:Fill>" + " <se:SvgParameter name=\"fill\">#d7191c</se:SvgParameter>" + " </se:Fill>" + " <se:Stroke>" + " <se:SvgParameter name=\"stroke\">#000000</se:SvgParameter>" + " </se:Stroke>" + " </se:PolygonSymbolizer>" + " </se:Rule>" + " </se:FeatureTypeStyle>" + " </UserStyle>" + " </NamedLayer>" + "</StyledLayerDescriptor>"; StyleInfo info = writeStyleAndImport(sld, "archsites.sld", dir); assertEquals(SLDHandler.VERSION_11, info.getFormatVersion()); }
@Test public void testPutVersionBackAndForth() throws Exception { // go from 1.0 to 1.1 testPutAsSLD11Raw(); String xml = newSLDXML(); // put a SLD 1.0 in raw mode (the content type is ignored in raw mode, but mimicking was // gsconfig does here) MockHttpServletResponse response = putAsServletResponse( RestBaseController.ROOT_PATH + "/styles/Ponds?raw=true", xml, "application/vnd.ogc.sld+xml"); assertEquals(200, response.getStatus()); // now it should have been modified back to 1.0 StyleInfo infoAfter = catalog.getStyleByName("Ponds"); assertThat(infoAfter.getFormatVersion(), equalTo(SLDHandler.VERSION_10)); }
assertEquals(SLDHandler.VERSION_11, style.getFormatVersion());