Mercurial > dive4elements > gnv-client
diff gnv-artifacts/src/main/java/de/intevation/gnv/utils/MetaWriter.java @ 647:bb0aa1f81280
Added missing class MetaWriter from last commit.
gnv-artifacts/trunk@734 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Fri, 05 Mar 2010 07:18:52 +0000 |
parents | |
children | 6eccb68a8b99 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gnv-artifacts/src/main/java/de/intevation/gnv/utils/MetaWriter.java Fri Mar 05 07:18:52 2010 +0000 @@ -0,0 +1,203 @@ +package de.intevation.gnv.utils; + +import de.intevation.artifacts.ArtifactNamespaceContext; +import de.intevation.artifacts.CallContext; + +import de.intevation.artifactdatabase.XMLUtils; + +import de.intevation.gnv.artifacts.context.GNVArtifactContext; +import de.intevation.gnv.wms.LayerInfo; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; + +import org.apache.log4j.Logger; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; + +/** + * @author Ingo Weinzierl (ingo.weinzierl@intevation.de) + */ +public class MetaWriter { + + private static Logger logger = Logger.getLogger(MetaWriter.class); + + public static final String NODE_MAPSERVER = "mapserver"; + public static final String NODE_SERVER = "server"; + public static final String NODE_MAP = "map"; + + public static final String META_FILE_NAME = "meta.xml"; + public static final String ISOLINES_NAME = "isolines.shp"; + public static final String POLYGON_NAME = "polygons.shp"; + + private MetaWriter() { + } + + public static Document writeHorizontalcrosssectionMeta( + CallContext context, + String uuid, + String path, + String paramType) + { + Document meta = XMLUtils.newDocument(); + XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator( + meta, + ArtifactNamespaceContext.NAMESPACE_URI, + ArtifactNamespaceContext.NAMESPACE_PREFIX); + + Element root = creator.create("meta"); + meta.appendChild(root); + + writeAbstractMeta(context, meta, root); + writePolygonMeta(context, meta, root, uuid, path, paramType); + writeIsolineMeta(context, meta, root, uuid, path, paramType); + + try { + File metaFile = new File(path, META_FILE_NAME); + + if (!metaFile.createNewFile() || !metaFile.canWrite()) { + logger.error("Error while writing meta file: "+metaFile.toString()); + return null; + } + + OutputStream out = null; + boolean success = false; + try { + out = new FileOutputStream(metaFile); + success = XMLUtils.toStream(meta, out); + } + finally { + if (out != null) { + try { out.close(); } + catch (IOException ioe) {} + } + } + + if (!success && metaFile.exists()) { + metaFile.delete(); + } + + return success ? meta : null; + } + catch (FileNotFoundException fnfe) { + logger.error(fnfe); + } + catch (IOException ioe) { + logger.error(ioe, ioe); + } + + return meta; + } + + + public static void writeAbstractMeta( + CallContext callContext, + Document document, + Element meta + ) { + XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator( + document, + ArtifactNamespaceContext.NAMESPACE_URI, + ArtifactNamespaceContext.NAMESPACE_PREFIX); + + GNVArtifactContext context = + (GNVArtifactContext) callContext.globalContext(); + + String server = (String) + context.get(GNVArtifactContext.MAPSERVER_SERVER_PATH_KEY); + + String map = (String) + context.get(GNVArtifactContext.MAPSERVER_MAP_PATH_KEY); + + logger.debug("MAPSERVER PATH: " + server); + logger.debug("MAP PATH: " + map); + + Element mapserver = creator.create(NODE_MAPSERVER); + Element serverPath = creator.create(NODE_SERVER); + Element mapPath = creator.create(NODE_MAP); + + mapPath.setTextContent(map); + serverPath.setTextContent(server); + + mapserver.appendChild(serverPath); + mapserver.appendChild(mapPath); + meta.appendChild(mapserver); + } + + + public static void writePolygonMeta( + CallContext context, + Document document, + Element meta, + String uuid, + String path, + String paramType + ) { + XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator( + document, + ArtifactNamespaceContext.NAMESPACE_URI, + ArtifactNamespaceContext.NAMESPACE_PREFIX); + + Element layer = creator.create(LayerInfo.LAYER); + Element model = creator.create(LayerInfo.LAYER_MODEL); + Element name = creator.create(LayerInfo.LAYER_NAME); + Element type = creator.create(LayerInfo.LAYER_TYPE); + Element status = creator.create(LayerInfo.LAYER_STATUS); + Element data = creator.create(LayerInfo.LAYER_DATA); + + model.setTextContent(paramType); + name.setTextContent(uuid); + type.setTextContent("POLYGON"); + status.setTextContent("DEFAULT"); + data.setTextContent(POLYGON_NAME); + + layer.appendChild(model); + layer.appendChild(name); + layer.appendChild(type); + layer.appendChild(status); + layer.appendChild(data); + + meta.appendChild(layer); + } + + + public static void writeIsolineMeta( + CallContext context, + Document document, + Element meta, + String uuid, + String path, + String paramType + ) { + XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator( + document, + ArtifactNamespaceContext.NAMESPACE_URI, + ArtifactNamespaceContext.NAMESPACE_PREFIX); + + Element layer = creator.create(LayerInfo.LAYER); + Element model = creator.create(LayerInfo.LAYER_MODEL); + Element name = creator.create(LayerInfo.LAYER_NAME); + Element type = creator.create(LayerInfo.LAYER_TYPE); + Element status = creator.create(LayerInfo.LAYER_STATUS); + Element data = creator.create(LayerInfo.LAYER_DATA); + + model.setTextContent(paramType+"_isolines"); + name.setTextContent(uuid); + type.setTextContent("LINE"); + status.setTextContent("DEFAULT"); + data.setTextContent(ISOLINES_NAME); + + layer.appendChild(model); + layer.appendChild(name); + layer.appendChild(type); + layer.appendChild(status); + layer.appendChild(data); + + meta.appendChild(layer); + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :