/** * Imports all styles and loads them into the catalog * * @param factory * @param styles */ void importStyles(CatalogFactory factory, Map styles) { for (Iterator s = styles.entrySet().iterator(); s.hasNext(); ) { Map.Entry entry = (Map.Entry) s.next(); StyleInfo style = factory.createStyle(); style.setName((String) entry.getKey()); style.setFilename((String) entry.getValue()); catalog.add(style); LOGGER.info("Loaded style '" + style.getName() + "'"); } }
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; }
protected StyleInfo newStyle(String name, String fileName) { StyleInfo s2 = catalog.getFactory().createStyle(); s2.setName(name); s2.setFilename(fileName); return s2; }
/** Copies a well known style out to the data directory and adds a catalog entry for it. */ void initializeStyle(Catalog catalog, String styleName, String sld) throws IOException { // copy the file out to the data directory if necessary Resource styleResource = resourceLoader.get(Paths.path("styles", sld)); if (!Resources.exists(styleResource)) { try (InputStream in = GeoServerLoader.class.getResourceAsStream(sld); OutputStream out = styleResource.out()) { IOUtils.copy(in, out); } } // create a style for it StyleInfo s = catalog.getFactory().createStyle(); s.setName(styleName); s.setFilename(sld); catalog.add(s); }
@Test public void testAddStyle() throws Exception { File f = new File(testData.getDataDirectoryRoot(), "styles/foostyle.xml"); assertFalse(f.exists()); StyleInfo s = catalog.getFactory().createStyle(); s.setName("foostyle"); s.setFilename("foostyle.sld"); catalog.add(s); assertTrue(f.exists()); }
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()); } } }
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; }
/** 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 testModifyStyle() throws Exception { testAddStyle(); StyleInfo s = catalog.getStyleByName("foostyle"); s.setFilename("foostyle2.sld"); catalog.save(s); File f = new File(testData.getDataDirectoryRoot(), "styles/foostyle.xml"); Document dom = dom(f); assertXpathEvaluatesTo("foostyle2.sld", "/style/filename", dom); }
@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); }
@Test public void testProxyListBehaviour() throws Exception { catalog.add(s); StyleInfo s2 = catalog.getFactory().createStyle(); s2.setName("a" + s.getName()); s2.setFilename("a.sld"); catalog.add(s2); List<StyleInfo> styles = catalog.getStyles(); assertEquals(2, styles.size()); // test immutability Comparator<StyleInfo> comparator = new Comparator<StyleInfo>() { public int compare(StyleInfo o1, StyleInfo o2) { return o1.getName().compareTo(o2.getName()); } }; try { Collections.sort(styles, comparator); fail("Expected runtime exception, immutable collection"); } catch (RuntimeException e) { assertTrue(true); } styles = new ArrayList<StyleInfo>(styles); Collections.sort(styles, comparator); assertEquals("a" + s.getName(), styles.get(0).getName()); assertEquals(s.getName(), styles.get(1).getName()); }
@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 testModifyLayerGroup() throws Exception { testAddLayerGroup(); LayerGroupInfo lg = catalog.getLayerGroupByName("lg"); StyleInfo s = catalog.getFactory().createStyle(); s.setName("foostyle2"); s.setFilename("foostyle2.sld"); catalog.add(s); lg.getStyles().set(0, s); catalog.save(lg); File f = new File(testData.getDataDirectoryRoot(), "layergroups/lg.xml"); Document dom = dom(f); assertXpathEvaluatesTo(s.getId(), "/layerGroup/styles/style/id", dom); }
@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()); }
@Test public void testStyle() throws Exception { Catalog catalog = new CatalogImpl(); CatalogFactory cFactory = catalog.getFactory(); StyleInfo s1 = cFactory.createStyle(); s1.setName("foo"); s1.setFilename("foo.sld"); ByteArrayOutputStream out = out(); persister.save(s1, out); ByteArrayInputStream in = in(out); StyleInfo s2 = persister.load(in, StyleInfo.class); assertEquals(s1, s2); Document dom = dom(in(out)); assertEquals("style", dom.getDocumentElement().getNodeName()); catalog.add(s2); assertNull(s2.getWorkspace()); }
@Test public void testModifyStyle() { addStyle(); StyleInfo s2 = catalog.getStyleByName(s.getName()); s2.setName(null); s2.setFilename(null); StyleInfo s3 = catalog.getStyleByName(s.getName()); assertEquals(s, s3); try { catalog.save(s2); fail("setting name to null should fail"); } catch (Exception e) { } s2.setName("s2Name"); try { catalog.save(s2); fail("setting filename to null should fail"); } catch (Exception e) { } s2.setFilename("s2Filename"); catalog.save(s2); s3 = catalog.getStyleByName("styleName"); assertNull(s3); s3 = catalog.getStyleByName(s2.getName()); assertEquals(s2, s3); }
@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 testModifyLayerDefaultStyle() { // create new style CatalogFactory factory = catalog.getFactory(); StyleInfo s2 = factory.createStyle(); s2.setName("styleName2"); s2.setFilename("styleFilename2"); catalog.add(s2); // change the layer style addLayer(); LayerInfo l2 = catalog.getLayerByName(l.getName()); l2.setDefaultStyle(catalog.getStyleByName("styleName2")); catalog.save(l2); // get back and compare with itself LayerInfo l3 = catalog.getLayerByName(l.getName()); LayerInfo l4 = catalog.getLayerByName(l.getName()); assertEquals(l3, l4); }
@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")); }