Parser createSld11Parser( Object input, ResourceLocator resourceLocator, EntityResolver entityResolver) { if (resourceLocator == null && input instanceof File) { // setup for resolution of relative paths final java.net.URL surl = URLs.fileToUrl((File) input); DefaultResourceLocator defResourceLocator = new DefaultResourceLocator(); defResourceLocator.setSourceUrl(surl); resourceLocator = defResourceLocator; } final ResourceLocator locator = resourceLocator; SLDConfiguration sld; if (locator != null) { sld = new SLDConfiguration() { protected void configureContext( org.picocontainer.MutablePicoContainer container) { container.registerComponentInstance(ResourceLocator.class, locator); }; }; } else { sld = new SLDConfiguration(); } Parser parser = new Parser(sld); if (entityResolver != null) { parser.setEntityResolver(entityResolver); } return parser; }
@Override protected URL makeRelativeURL(String uri, String query) { if (SystemUtils.IS_OS_WINDOWS && uri.contains("\\")) { uri = uri.replace('\\', '/'); } return super.makeRelativeURL(uri, query); } }
@Override protected URL validateRelativeURL(URL relativeUrl) { if (relativeUrl.getProtocol().equalsIgnoreCase("resource")) { String path = relativeUrl.getPath(); if (resourceLoader.get(path).getType() != Type.UNDEFINED) { return relativeUrl; } else { return null; } } else { return super.validateRelativeURL(relativeUrl); } }
public void testRelativeFileURL() throws Exception { DefaultResourceLocator locator = new DefaultResourceLocator(); locator.setSourceUrl(getClass().getResource("test-data/blob.gif")); checkURL(locator.locateResource("blob.gif")); checkURL(locator.locateResource("file:blob.gif")); checkURL(locator.locateResource("file://blob.gif")); checkURL(locator.locateResource("file://./blob.gif")); }
public SLDParser(StyleFactory factory, FilterFactory filterFactory) { this.factory = factory; this.ff = filterFactory; this.onlineResourceLocator = new DefaultResourceLocator(); }
/** * 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; }
String query = getQueryPart(url); URL relativeUrl = makeRelativeURL(f.getPath(), query); if (relativeUrl != null) { URL validated = validateRelativeURL(relativeUrl); if (validated != null) { url = validated; LOGGER.fine("Looks like " + uri + " is a relative path.."); if (sourceUrl != null) { url = makeRelativeURL(uri, null);
public void testPreserveURLQuery() throws Exception { DefaultResourceLocator locator = new DefaultResourceLocator(); locator.setSourceUrl(getClass().getResource("test-data/blob.gif")); // Confirm still able to resolve to a File URL url = locator.locateResource("blob.gif?query=parameter"); assertEquals("query=parameter", url.getQuery()); File file = URLs.urlToFile(url); assertTrue(file.exists()); }
@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); }
/** Internal setter for source url. */ void setSourceUrl(URL sourceUrl) { if (onlineResourceLocator instanceof DefaultResourceLocator) { ((DefaultResourceLocator) onlineResourceLocator).setSourceUrl(sourceUrl); } }
protected void configureContext( org.picocontainer.MutablePicoContainer container) { DefaultResourceLocator locator = new DefaultResourceLocator(); locator.setSourceUrl(surl); container.registerComponentInstance(ResourceLocator.class, locator); }; };
public void testPreserveURLQueryWithColors() throws Exception { DefaultResourceLocator locator = new DefaultResourceLocator(); locator.setSourceUrl(getClass().getResource("test-data/blob.gif")); // Confirm still able to resolve to a File URL url = locator.locateResource("blob.gif?fill=#ff0000&stroke=#000000"); final String external = url.toExternalForm(); assertTrue(external.indexOf('?') > 0); assertEquals("fill=#ff0000&stroke=#000000", external.split("\\?")[1]); File file = URLs.urlToFile(url); assertTrue(file.exists()); }
@Override protected void configureContext(MutablePicoContainer container) { super.configureContext(container); container.registerComponentImplementation(StyleFactory.class, StyleFactoryImpl.class); container.registerComponentInstance(ResourceLocator.class, new DefaultResourceLocator()); }
/** * 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; }
URL relativeUrl = makeRelativeURL(f.getPath()); if (relativeUrl != null) { f = DataUtilities.urlToFile(relativeUrl); LOGGER.fine("Looks like " + uri + " is a relative path.."); if (sourceUrl != null) { url = makeRelativeURL(uri);
/** * Sets the resource locator so that relative external graphic files can be found. * * @param styleReader the new resource locator * @param url the url */ public static void setResourcelocator(SLDParser styleReader, URL url) { DefaultResourceLocator resourceLocator = new DefaultResourceLocator(); resourceLocator.setSourceUrl(url); styleReader.setOnLineResourceLocator(resourceLocator); }
public void testInvalidPath() throws Exception { DefaultResourceLocator locator = new DefaultResourceLocator(); URL testURL = new URL( locator.setSourceUrl(testURL); assertEquals(new URL("file://test"), locator.locateResource("file://test"));
/** * Configures the sld context. * * <p>The following factories are registered: * * <ul> * <li>{@link StyleFactoryImpl} under {@link StyleFactory} * </ul> */ protected void configureContext(MutablePicoContainer container) { super.configureContext(container); container.registerComponentImplementation(StyleFactory.class, StyleFactoryImpl.class); container.registerComponentInstance(ResourceLocator.class, new DefaultResourceLocator()); }
/** * Internal setter for source url. */ void setSourceUrl(URL sourceUrl) { this.sourceUrl = sourceUrl; if (onlineResourceLocator instanceof DefaultResourceLocator) { ((DefaultResourceLocator) onlineResourceLocator).setSourceUrl(sourceUrl); } }
@Override public StyledLayerDescriptor parse(Object input, Version version, @Nullable ResourceLocator resourceLocator, EntityResolver entityResolver) throws IOException { if (resourceLocator == null && input instanceof File) { resourceLocator = new DefaultResourceLocator(); ((DefaultResourceLocator)resourceLocator).setSourceUrl(DataUtilities.fileToURL((File) input)); } return Ysld.parse(toReader(input), Collections.singletonList(zoomFinder), resourceLocator, uomMapper); }