protected void resolve(StyleInfo style) { setId(style); // resolve the workspace WorkspaceInfo ws = style.getWorkspace(); if (ws != null) { WorkspaceInfo resolved = ResolvingProxy.resolve(getCatalog(), ws); if (resolved != null) { resolved = unwrap(resolved); style.setWorkspace(resolved); } else { LOGGER.log( Level.INFO, "Failed to resolve workspace for style \"" + style.getName() + "\". This means the workspace has not yet been added to the catalog, keep the proxy around"); } } }
StyleInfo doCreateStyle(StyleHandler handler, StyleType styleType, ResourceInfo resource) throws IOException { // find a new style name String styleName = generateUniqueStyleName(resource); // variable replacement String styleData = generateStyle(handler, styleType, styleName); // let's store it StyleInfo style = catalog.getFactory().createStyle(); style.setName(styleName); style.setFilename(styleName + "." + handler.getFileExtension()); style.setFormat(handler.getFormat()); if (workspace != null) { style.setWorkspace(workspace); } catalog.getResourcePool().writeStyle(style, new ByteArrayInputStream(styleData.getBytes())); return style; }
@Test public void testModifyStyleChangeWorkspace2() throws Exception { testAddStyle(); // copy an sld into place FileUtils.copyURLToFile( getClass().getResource("default_line.sld"), new File(testData.getDataDirectoryRoot(), "styles/foostyle.sld")); assertTrue(new File(testData.getDataDirectoryRoot(), "styles/foostyle.xml").exists()); assertTrue(new File(testData.getDataDirectoryRoot(), "styles/foostyle.sld").exists()); StyleInfo s = catalog.getStyleByName("foostyle"); s.setWorkspace(catalog.getDefaultWorkspace()); catalog.save(s); assertFalse(new File(testData.getDataDirectoryRoot(), "styles/foostyle.xml").exists()); assertFalse(new File(testData.getDataDirectoryRoot(), "styles/foostyle.sld").exists()); assertTrue( new File(testData.getDataDirectoryRoot(), "workspaces/gs/styles/foostyle.xml") .exists()); assertTrue( new File(testData.getDataDirectoryRoot(), "workspaces/gs/styles/foostyle.sld") .exists()); }
StyleInfo backupStyle(StyleInfo s) throws IOException { StyleInfo backup = catalog.getFactory().createStyle(); new CatalogBuilder(catalog).updateStyle(backup, s); backup.setWorkspace(s.getWorkspace()); // find a unique name for the style String name = findUniqueStyleName( s.getWorkspace() == null ? null : s.getWorkspace().getName(), s.getName() + "_BACKUP"); backup.setName(name); // update it's file name backup.setFilename(name + "." + FilenameUtils.getExtension(s.getFilename())); // copy over the style contents try (BufferedReader reader = catalog.getResourcePool().readStyle(s)) { catalog.getResourcePool() .writeStyle(backup, new ByteArrayInputStream(IOUtils.toByteArray(reader))); } return backup; }
@Test public void testModifyStyleChangeWorkspaceToGlobal() throws Exception { testAddStyleWithWorkspace(); // copy an sld into place FileUtils.copyURLToFile( getClass().getResource("default_line.sld"), new File(testData.getDataDirectoryRoot(), "workspaces/gs/styles/foostyle.sld")); assertTrue( new File(testData.getDataDirectoryRoot(), "workspaces/gs/styles/foostyle.xml") .exists()); assertTrue( new File(testData.getDataDirectoryRoot(), "workspaces/gs/styles/foostyle.sld") .exists()); StyleInfo s = catalog.getStyleByName("foostyle"); s.setWorkspace(null); catalog.save(s); assertTrue(new File(testData.getDataDirectoryRoot(), "styles/foostyle.xml").exists()); assertTrue(new File(testData.getDataDirectoryRoot(), "styles/foostyle.sld").exists()); assertFalse( new File(testData.getDataDirectoryRoot(), "workspaces/gs/styles/foostyle.xml") .exists()); assertFalse( new File(testData.getDataDirectoryRoot(), "workspaces/gs/styles/foostyle.sld") .exists()); }
/** Keys for overriding default layer properties */ public static class StyleProperty<T> { T get(Map<StyleProperty, Object> map, T def) { return map != null && map.containsKey(this) ? (T) map.get(this) : def; } public static StyleProperty<String> FORMAT = new StyleProperty<String>(); public static StyleProperty<Version> FORMAT_VERSION = new StyleProperty<Version>(); public static StyleProperty<LegendInfo> LEGEND_INFO = new StyleProperty<LegendInfo>(); }
@Test public void testModifyStyleChangeWorkspace() throws Exception { testAddStyle(); StyleInfo s = catalog.getStyleByName("foostyle"); s.setFilename("foostyle2.sld"); s.setWorkspace(catalog.getDefaultWorkspace()); catalog.save(s); File f = new File(testData.getDataDirectoryRoot(), "styles/foostyle.xml"); assertFalse(f.exists()); f = new File(testData.getDataDirectoryRoot(), "workspaces/gs/styles/foostyle.xml"); Document dom = dom(f); assertXpathEvaluatesTo("foostyle2.sld", "/style/filename", dom); }
@Test public void testAddStyleWithWorkspace() throws Exception { File f = new File(testData.getDataDirectoryRoot(), "workspaces/gs/styles/foostyle.xml"); assertFalse(f.exists()); StyleInfo s = catalog.getFactory().createStyle(); s.setName("foostyle"); s.setFilename("foostyle.sld"); s.setWorkspace(catalog.getDefaultWorkspace()); catalog.add(s); assertTrue(f.exists()); Document dom = dom(f); assertXpathEvaluatesTo(catalog.getDefaultWorkspace().getId(), "/style/workspace/id", dom); }
s.setWorkspace(catalog.getDefaultWorkspace()); catalog.save(s);
s.setWorkspace(catalog.getDefaultWorkspace()); catalog.save(s);
@Test public void testRemoveStyleWithWorkspace() throws Exception { StyleInfo s = catalog.getFactory().createStyle(); s.setName("foostyle"); s.setFilename("foostyle.sld"); s.setWorkspace(catalog.getDefaultWorkspace()); catalog.add(s); File f = new File(testData.getDataDirectoryRoot(), "workspaces/gs/styles/foostyle.xml"); assertTrue(f.exists()); s = catalog.getStyleByName("foostyle"); assertNotNull(s); s = catalog.getStyleByName(catalog.getDefaultWorkspace(), "foostyle"); catalog.remove(s); assertFalse(f.exists()); }
@Test public void testGetStyles() { addWorkspace(); addStyle(); assertEquals(1, catalog.getStyles().size()); assertEquals(0, catalog.getStylesByWorkspace(ws.getName()).size()); assertEquals(0, catalog.getStylesByWorkspace(ws).size()); assertEquals(0, catalog.getStylesByWorkspace((WorkspaceInfo) null).size()); StyleInfo s2 = catalog.getFactory().createStyle(); s2.setName("styleNameWithWorkspace"); s2.setFilename("styleFilenameWithWorkspace"); s2.setWorkspace(ws); catalog.add(s2); assertEquals(2, catalog.getStyles().size()); assertEquals(1, catalog.getStylesByWorkspace(ws.getName()).size()); assertEquals(1, catalog.getStylesByWorkspace(ws).size()); assertEquals(1, catalog.getStylesByWorkspace((WorkspaceInfo) null).size()); }
style.setWorkspace(ws); catalog.save(style);
s2.setWorkspace(ws); catalog.add(s2); s3.setWorkspace(ws); try { catalog.add(s3);
@Test public void testModifyDefaultStyle() { addWorkspace(); addDefaultStyle(); StyleInfo s = catalog.getStyleByName(StyleInfo.DEFAULT_LINE); s.setName("foo"); try { catalog.save(s); fail("changing name of default style should fail"); } catch (Exception e) { } s = catalog.getStyleByName(StyleInfo.DEFAULT_LINE); s.setWorkspace(ws); try { catalog.save(s); fail("changing workspace of default style should fail"); } catch (Exception e) { } }
@Test public void testChangeStyleWorkspaceRelativeResources() throws Exception { // move style to a different workspace final Catalog catalog = getCatalog(); final StyleInfo style = catalog.getStyleByName("relative"); final WorkspaceInfo secondaryWs = catalog.getWorkspaceByName("secondary"); style.setWorkspace(secondaryWs); catalog.save(style); // check the referenced image and svg has been moved keeping the relative position final Resource relativeImage = getDataDirectory().getStyles(secondaryWs, "images", "rockFillSymbol.png"); assertEquals(Resource.Type.RESOURCE, relativeImage.getType()); final Resource relativeSvg = getDataDirectory().getStyles(secondaryWs, "images", "square16.svg"); assertEquals(Resource.Type.RESOURCE, relativeSvg.getType()); } }
@Test public void testGetStyleByNameWithWorkspace2() throws Exception { addWorkspace(); WorkspaceInfo ws2 = catalog.getFactory().createWorkspace(); ws2.setName("wsName2"); catalog.add(ws2); // add style with same name in each workspace StyleInfo s1 = catalog.getFactory().createStyle(); s1.setName("foo"); s1.setFilename("foo1.sld"); s1.setWorkspace(ws); catalog.add(s1); StyleInfo s2 = catalog.getFactory().createStyle(); s2.setName("foo"); s2.setFilename("foo2.sld"); s2.setWorkspace(ws2); catalog.add(s2); assertEquals(s1, catalog.getStyleByName("foo")); assertEquals(s1, catalog.getStyleByName(ws.getName(), "foo")); assertEquals(s1, catalog.getStyleByName(ws, "foo")); assertEquals(s2, catalog.getStyleByName(ws2.getName(), "foo")); assertEquals(s2, catalog.getStyleByName(ws2, "foo")); }
@Test public void testWorkspaceStyle() throws Exception { Catalog catalog = new CatalogImpl(); CatalogFactory cFactory = catalog.getFactory(); WorkspaceInfo ws = cFactory.createWorkspace(); ws.setName("foo"); StyleInfo s1 = cFactory.createStyle(); s1.setName("bar"); s1.setFilename("bar.sld"); s1.setWorkspace(ws); ByteArrayOutputStream out = out(); persister.save(s1, out); ByteArrayInputStream in = in(out); StyleInfo s2 = persister.load(in, StyleInfo.class); assertEquals("bar", s2.getName()); assertNotNull(s2.getWorkspace()); assertEquals("foo", s2.getWorkspace().getId()); Document dom = dom(in(out)); assertEquals("style", dom.getDocumentElement().getNodeName()); catalog.add(ws); catalog.add(s2); // Make sure the catalog resolves the workspace assertEquals("foo", s2.getWorkspace().getName()); }
@Test public void testGetStyleByNameWithWorkspace() { addWorkspace(); addStyle(); StyleInfo s2 = catalog.getFactory().createStyle(); s2.setName("styleNameWithWorkspace"); s2.setFilename("styleFilenameWithWorkspace"); s2.setWorkspace(ws); catalog.add(s2); assertNotNull(catalog.getStyleByName("styleNameWithWorkspace")); assertNotNull(catalog.getStyleByName(ws.getName(), "styleNameWithWorkspace")); assertNotNull(catalog.getStyleByName(ws, "styleNameWithWorkspace")); assertNull(catalog.getStyleByName((WorkspaceInfo) null, "styleNameWithWorkspace")); assertNull(catalog.getStyleByName(ws.getName(), "styleName")); assertNull(catalog.getStyleByName(ws, "styleName")); assertNotNull(catalog.getStyleByName((WorkspaceInfo) null, "styleName")); }
s2.setName("styleName"); s2.setFilename("styleFilename"); s2.setWorkspace(ws2); catalog.add(s2);