Mercurial > dive4elements > gnv-client
view gnv-artifacts/src/main/java/de/intevation/gnv/artifacts/context/GNVArtifactContextFactory.java @ 592:3c939c95c477
Removed accidental checkin of Test-Shapefiles
gnv-artifacts/trunk@647 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Tim Englich <tim.englich@intevation.de> |
---|---|
date | Fri, 29 Jan 2010 13:28:28 +0000 |
parents | 1bf058f1a2d1 |
children | b248531fa20b |
line wrap: on
line source
/** * */ package de.intevation.gnv.artifacts.context; import java.awt.Color; import java.awt.Paint; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.File; import java.util.Properties; import java.util.HashMap; import java.util.ArrayList; import java.util.Collections; import java.awt.Dimension; import org.apache.log4j.Logger; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import de.intevation.gnv.geobackend.base.connectionpool.ConnectionPoolFactory; import de.intevation.gnv.geobackend.base.query.container.QueryContainerFactory; import de.intevation.gnv.geobackend.base.query.container.exception.QueryContainerException; import de.intevation.gnv.chart.XMLChartTheme; import de.intevation.gnv.artifacts.cache.CacheFactory; import de.intevation.gnv.raster.Palette; import de.intevation.gnv.raster.Filter; import de.intevation.gnv.raster.PaletteManager; import de.intevation.artifacts.ArtifactContextFactory; import de.intevation.artifactdatabase.Config; import de.intevation.artifactdatabase.XMLUtils; /** * @author Tim Englich (tim.englich@intevation.de) * @author Ingo Weinzierl (iweinzierl@intevation.de) * @author Sascha L. Teichmann (sascha.teichmann@intevation.de) */ public class GNVArtifactContextFactory implements ArtifactContextFactory { /** * the logger, used to log exceptions and additonaly information */ private static Logger log = Logger.getLogger(GNVArtifactContext.class); public static final String XPATH_GEOBACKEND_CONFIGURATION = "artifact-database/geo-backend/backend-configuration"; public static final String XPATH_GEOBACKEND_QUERYCONFIGURATION = "artifact-database/geo-backend/query-configuration"; private final static String CACHECONFIGNODEPATH = "/artifact-database/ehcache/configuration"; private final static String CHARTCONFIGNODEPATH = "/artifact-database/gnv/charttemplate/configuration"; public final static String PALETTES_PATH = "/artifact-database/gnv/palettes"; public final static String PALETTE_ITEMS = "palette"; public final static String HORIZONTAL_CROSS_SECTION_PROFILE_SAMPLES = "/artifact-database/gnv/horizontal-cross-section-profile/samples/@number"; public final static String HORIZONTAL_CROSS_SECTION_GROUND_INTERPOLATION = "/artifact-database/gnv/horizontal-cross-section/ground/@interpolation"; public final static String HORIZONTAL_CROSS_SECTION_SAMPLES = "/artifact-database/gnv/horizontal-cross-section/samples/@number"; public final static String HORIZONTAL_CROSS_SECTION_RESULT_SHAPEFILE_PATH = "/artifact-database/gnv/horizontal-cross-section/result-shapefile-directory/@path"; public final static String VERTICAL_CROSS_SECTION_SAMPLES = "/artifact-database/gnv/vertical-cross-section/samples"; public final static String VERTICAL_CROSS_SECTION_FILTERS = "/artifact-database/gnv/vertical-cross-section/filters/filter"; public final static String VERTICAL_CROSS_SECTION_GROUND_INTERPOLATION = "/artifact-database/gnv/vertical-cross-section/ground/@interpolation"; public final static String VERTICAL_CROSS_SECTION_GROUND_FILL_COLOR = "/artifact-database/gnv/vertical-cross-section/ground/@fill-color"; /** * Constructor */ public GNVArtifactContextFactory() { super(); log.debug("GNVArtifactContextFactory.Constructor"); } /** * @see de.intevation.artifacts.ArtifactContextFactory#createArtifactContext(org.w3c.dom.Document) */ public Object createArtifactContext(Document config) { GNVArtifactContext returnValue = null; try { log.debug("GNVArtifactContextFactory.createArtifactContext"); log.info("Initialisation of the Geo-BackendConnectionPool"); String backendConfigurationFile = Config.getStringXPath(config, XPATH_GEOBACKEND_CONFIGURATION); backendConfigurationFile = Config.replaceConfigDir( backendConfigurationFile); Properties properties = getProperties(backendConfigurationFile); ConnectionPoolFactory cpf = ConnectionPoolFactory.getInstance(); cpf.initializeConnectionPool(properties); log.info("Initialisation of the QueryContainer"); String queryConfigurationFile = Config.getStringXPath(config, XPATH_GEOBACKEND_QUERYCONFIGURATION); queryConfigurationFile = Config.replaceConfigDir( queryConfigurationFile); Properties queryProperties = getProperties(queryConfigurationFile); QueryContainerFactory qcf = QueryContainerFactory.getInstance(); qcf.initializeQueryContainer(queryProperties); log.info("Initialisation of the Cache"); String cacheConfigurationFile = Config.getStringXPath(config, CACHECONFIGNODEPATH); cacheConfigurationFile = Config.replaceConfigDir( cacheConfigurationFile); CacheFactory cf = CacheFactory.getInstance(); cf.initializeCache(cacheConfigurationFile); returnValue = new GNVArtifactContext(config); configurePalettes(config, returnValue); configureChartTemplate(config, returnValue); configureHorizontalCrossSectionProfile(config, returnValue); configureHorizontalCrossSection(config,returnValue); configureVerticalCrossSection(config, returnValue); } catch (FileNotFoundException e) { log.error(e, e); } catch (IOException e) { log.error(e, e); } catch (QueryContainerException e) { log.error(e, e); } return returnValue; } protected void configureVerticalCrossSection( Document config, GNVArtifactContext context ) { log.info("configuration of vertical cross section"); configureVerticalCrossSectionSamples(config, context); configureVerticalCrossSectionFilters(config, context); configureVerticalCrossSectionGroundInterpolation(config, context); configureVerticalCrossSectionGroundFillColor(config, context); } protected void configureVerticalCrossSectionGroundFillColor( Document config, GNVArtifactContext context ) { log.info("configuration of vertical cross section ground fill color"); String fillColor = Config.getStringXPath( config, VERTICAL_CROSS_SECTION_GROUND_FILL_COLOR); Paint fill = GNVArtifactContext.DEFAULT_VERTICAL_CROSS_SECTION_GROUND_FILL; if (fillColor != null && (fillColor = fillColor.trim()).length() != 0) { try { Color color = Color.decode(fillColor); log.info("ground fill color: #" + Integer.toHexString(color.getRGB())); fill = color; } catch (NumberFormatException nfe) { log.error("'" + fillColor + "' is not a valid color"); } } context.put( GNVArtifactContext .VERTICAL_CROSS_SECTION_GROUND_FILL_KEY, fill); } protected void configureVerticalCrossSectionGroundInterpolation( Document config, GNVArtifactContext context ) { log.info("configuration of vertical cross section ground interpolation"); String interpolation = Config.getStringXPath( config, VERTICAL_CROSS_SECTION_GROUND_INTERPOLATION); if (interpolation == null || (interpolation = interpolation.trim()).length() == 0) { interpolation = GNVArtifactContext .DEFAULT_VERTICAL_CROSS_SECTION_GROUND_INTERPOLATION; } log.info("ground interpolation: " + interpolation); context.put( GNVArtifactContext .VERTICAL_CROSS_SECTION_GROUND_INTERPOLATION_KEY, interpolation); } protected void configureVerticalCrossSectionFilters( Document config, GNVArtifactContext context ) { log.info("configuration of vertical cross section filters"); NodeList filters = Config.getNodeSetXPath( VERTICAL_CROSS_SECTION_FILTERS); ArrayList<Filter.Factory> filterFactories = new ArrayList<Filter.Factory>(); if (filters == null) { log.warn("no filters found"); } else { for (int i = 0, N = filters.getLength(); i < N; ++i) { Element filterElement = (Element)filters.item(i); String factoryName = filterElement.getAttribute("factory"); if ((factoryName = factoryName.trim()).length() > 0) { try { Class clazz = Class.forName(factoryName); Filter.Factory filterFactory = (Filter.Factory)clazz.newInstance(); filterFactories.add(filterFactory); } catch (ClassNotFoundException cnfe) { log.error("filter class not found", cnfe); } catch (InstantiationException ie) { log.error("cannot instantiate filter factory", ie); } catch (IllegalAccessException iae) { log.error("cannot access filter factory", iae); } catch (ClassCastException cce) { log.error("not a filter factory class", cce); } } else { log.error("No factory name given"); } } } log.info("number of filters: " + filterFactories.size()); context.put( GNVArtifactContext.VERTICAL_CROSS_SECTION_FILTER_FACTORIES_KEY, Collections.unmodifiableList(filterFactories)); } protected void configureVerticalCrossSectionSamples( Document config, GNVArtifactContext context ) { log.info("configuration of vertical cross section samples"); Element samples = (Element)Config.getNodeXPath( VERTICAL_CROSS_SECTION_SAMPLES); Dimension sampleSize = new Dimension( GNVArtifactContext.DEFAULT_VERTICAL_CROSS_SECTION_SAMPLES); if (samples == null) { log.warn("no samples found"); } else { String widthString = samples.getAttribute("width"); if ((widthString = widthString.trim()).length() > 0) { try { sampleSize.width = Math.max(1, Integer.parseInt(widthString)); } catch (NumberFormatException nfe) { log.error("invalid value for width: '" + widthString + "'"); } } String heightString = samples.getAttribute("height"); if ((heightString = heightString.trim()).length() > 0) { try { sampleSize.height = Math.max(1, Integer.parseInt(heightString)); } catch (NumberFormatException nfe) { log.error("invalid value for height: '" + heightString + "'"); } } } log.info("samples (width x height): " + sampleSize.width + " x " + sampleSize.height); context.put( GNVArtifactContext.VERTICAL_CROSS_SECTION_SAMPLES_KEY, sampleSize); } protected void configureHorizontalCrossSectionProfile( Document config, GNVArtifactContext context ) { log.info("configuration of horizontal cross section profile"); String numSamples = Config.getStringXPath( config, HORIZONTAL_CROSS_SECTION_PROFILE_SAMPLES); Integer samples = GNVArtifactContext.DEFAULT_HORIZONTAL_CROSS_SECTION_PROFILE_SAMPLES; if (numSamples == null) { log.warn("No number of samples found."); } else { try { samples = Integer.valueOf(numSamples); } catch (NumberFormatException nfe) { log.warn("Invalid integer for number of samples"); } } log.info("# horizontal cross section profile samples: " + samples); context.put( GNVArtifactContext.HORIZONTAL_CROSS_SECTION_PROFILE_SAMPLES_KEY, samples); } protected void configureHorizontalCrossSection( Document config, GNVArtifactContext context ) { log.info("configuration of horizontal cross section"); configureHorizontalCrossSectionSamples(config, context); configureHorizontalCrossSectionResultShapeFilePath(config, context); configureHorizontalCrossSectionGroundInterpolation(config, context); } protected void configureHorizontalCrossSectionGroundInterpolation( Document config, GNVArtifactContext context ) { log.info( "configuration of horizontal cross section ground interpolation"); String interpolation = Config.getStringXPath( config, HORIZONTAL_CROSS_SECTION_GROUND_INTERPOLATION); if (interpolation == null || (interpolation = interpolation.trim()).length() == 0) { interpolation = GNVArtifactContext .DEFAULT_HORIZONTAL_CROSS_SECTION_GROUND_INTERPOLATION; } log.info("ground interpolation: " + interpolation); context.put( GNVArtifactContext .HORIZONTAL_CROSS_SECTION_GROUND_INTERPOLATION_KEY, interpolation); } protected void configureHorizontalCrossSectionResultShapeFilePath( Document config, GNVArtifactContext context ) { log.info( "configuration of horizontal cross section result shape file path"); File dir = GNVArtifactContext. DEFAULT_HORIZONTAL_CROSS_SECTION_PROFILE_SHAPEFILE_PATH; String path = Config.getStringXPath( config, HORIZONTAL_CROSS_SECTION_RESULT_SHAPEFILE_PATH); if (path != null && (path = path.trim()).length() > 0) { dir = new File(Config.replaceConfigDir(path)); } else { log.warn("No 'result-shapefile-directory' given"); } log.info("writing shape files to '" + dir.getAbsolutePath() + "'"); context.put( GNVArtifactContext .HORIZONTAL_CROSS_SECTION_RESULT_SHAPEFILE_PATH_KEY, dir); } protected void configureHorizontalCrossSectionSamples( Document config, GNVArtifactContext context ) { log.info("configuration of horizontal cross section samples"); String numSamples = Config.getStringXPath( config, HORIZONTAL_CROSS_SECTION_SAMPLES); Integer samples = GNVArtifactContext.DEFAULT_HORIZONTAL_CROSS_SECTION_SAMPLES; if (numSamples == null) { log.warn("No number of samples found."); } else { try { samples = Integer.valueOf(numSamples); } catch (NumberFormatException nfe) { log.warn("Invalid integer for number of samples"); } } log.info("# horizontal cross section profile samples: " + samples); context.put( GNVArtifactContext.HORIZONTAL_CROSS_SECTION_SAMPLES_KEY, samples); } protected void configureChartTemplate( Document config, GNVArtifactContext context ) { log.info("Initialisation of chart template"); String chartConfigFile = Config.getStringXPath( config, CHARTCONFIGNODEPATH ); XMLChartTheme theme = new XMLChartTheme("XMLChartTheme"); if (chartConfigFile == null) { log.warn("no configuration file for chart template found"); } else { chartConfigFile = Config.replaceConfigDir(chartConfigFile); log.debug("Parse xml configuration of " + chartConfigFile); Document tmpl = XMLUtils.parseDocument(new File(chartConfigFile)); if (tmpl != null) { theme.applyXMLConfiguration(tmpl); } else { log.error( "Cannot load chart template from '" + chartConfigFile + "'"); } } context.put(GNVArtifactContext.CHART_TEMPLATE_KEY, theme); } protected void configurePalettes( Document config, GNVArtifactContext context ) { log.info("configure palettes"); HashMap<Integer, PaletteManager> palettes = new HashMap(); Element node = (Element)Config.getNodeXPath(config, PALETTES_PATH); if (node == null) { log.error("No palettes found"); } else { NodeList pals = node.getElementsByTagName(PALETTE_ITEMS); for (int i = 0, N = pals==null?0:pals.getLength(); i < N; ++i) { Element pal = (Element)pals.item(i); String name = pal.getAttribute("name"); String description = pal.getAttribute("description"); String filename = pal.getAttribute("file"); String parameterIds = pal.getAttribute("parameter-ids"); if (name == null || (name = name.trim()).length() == 0) { log.error("Palette has no 'name' attribute."); } else if (filename == null || (filename = filename.trim()).length() == 0) { log.error("Palette '" + name + "' has no 'file' attribute."); } else if (parameterIds == null || (parameterIds = parameterIds.trim()).length() == 0) { log.error("no parameter ids given for '" + name + "'"); } else { ArrayList<Integer> ids = new ArrayList<Integer>(); for (String idString: parameterIds.split("[\t ,]+")) { try { ids.add(Integer.valueOf(idString)); } catch (NumberFormatException nfe) { log.error( "parameter id '" + idString + "' is integer"); } } filename = Config.replaceConfigDir(filename); Document document = XMLUtils.parseDocument( new File(filename)); if (document == null) { log.error("Cannot load palette file '" + filename + "'"); } else { PaletteManager manager = new PaletteManager( name, description, new Palette(document)); for (Integer id: ids) { palettes.put(id, manager); } } } } // for all palettes } context.put(GNVArtifactContext.PALETTES_KEY, palettes); } /** * @param filePath * @return * @throws FileNotFoundException * @throws IOException */ private Properties getProperties(String filePath) throws FileNotFoundException, IOException { InputStream inputStream = null; try { inputStream = new FileInputStream(filePath); Properties properties = new Properties(); properties.load(inputStream); return properties; } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException ioe) {} } } } }