ingo@1058: package de.intevation.flys.artifacts.services; ingo@1058: ingo@1058: import org.apache.log4j.Logger; ingo@1058: sascha@2098: import java.util.Map; sascha@2098: import java.util.HashMap; sascha@2098: ingo@1058: import org.w3c.dom.Document; sascha@2098: import org.w3c.dom.Node; ingo@1058: import org.w3c.dom.Element; ingo@1058: sascha@2098: import javax.xml.xpath.XPathConstants; sascha@2098: ingo@1774: import com.vividsolutions.jts.geom.Envelope; ingo@1058: ingo@1058: import de.intevation.artifacts.CallMeta; ingo@1058: import de.intevation.artifacts.GlobalContext; ingo@1058: ingo@1058: import de.intevation.artifacts.common.ArtifactNamespaceContext; ingo@1058: import de.intevation.artifacts.common.utils.Config; ingo@1058: import de.intevation.artifacts.common.utils.XMLUtils; ingo@1058: import de.intevation.artifacts.common.utils.XMLUtils.ElementCreator; ingo@1058: sascha@2607: import de.intevation.artifactdatabase.XMLService; ingo@1058: ingo@1058: import de.intevation.flys.model.River; ingo@1058: ingo@1058: import de.intevation.flys.utils.GeometryUtils; ingo@1058: ingo@1058: /** ingo@1058: * This service provides information about the supported rivers by this ingo@1058: * application. ingo@1058: * ingo@1058: * @author Ingo Weinzierl ingo@1058: */ sascha@2607: public class MapInfoService extends XMLService { ingo@1058: ingo@1058: /** XPath that points to the river.*/ ingo@1058: public static final String XPATH_RIVER = "/mapinfo/river/text()"; ingo@1058: ingo@1058: public static final String XPATH_RIVER_PROJECTION = sascha@2098: "/artifact-database/floodmap/river[@name=$river]/srid/@value"; ingo@1058: ingo@1058: public static final String XPATH_RIVER_BACKGROUND = ingo@1058: "/artifact-database/floodmap/river[@name='%RIVER%']/background-wms"; ingo@1058: ingo@1058: public static final String XPATH_RIVER_WMS = sascha@2098: "/artifact-database/floodmap/river[@name=$river]/river-wms/@url"; ingo@1058: ingo@1058: ingo@1058: /** The logger used in this service.*/ ingo@1058: private static Logger logger = Logger.getLogger(MapInfoService.class); ingo@1058: ingo@1058: ingo@1058: /** ingo@1058: * The default constructor. ingo@1058: */ ingo@1058: public MapInfoService() { ingo@1058: } ingo@1058: sascha@2098: protected static String getStringXPath( sascha@3076: String query, sascha@2098: Map variables sascha@2098: ) { sascha@2098: return (String)XMLUtils.xpath( sascha@2098: Config.getConfig(), query, XPathConstants.STRING, sascha@2098: null, variables); sascha@2098: } sascha@2098: sascha@2098: protected static Node getNodeXPath( sascha@3076: String query, sascha@2098: Map variables sascha@2098: ) { sascha@2098: return (Node)XMLUtils.xpath( sascha@2098: Config.getConfig(), query, XPathConstants.NODE, sascha@2098: null, variables); sascha@2098: } ingo@1058: sascha@2607: @Override sascha@2607: public Document processXML( ingo@1058: Document data, ingo@1058: GlobalContext globalContext, ingo@1058: CallMeta callMeta ingo@1058: ) { ingo@1058: logger.debug("MapInfoService.process"); ingo@1058: ingo@1058: Document result = XMLUtils.newDocument(); ingo@1058: ElementCreator cr = new ElementCreator(result, null, null); ingo@1058: ingo@1058: Element mapinfo = cr.create("mapinfo"); ingo@1058: result.appendChild(mapinfo); ingo@1058: ingo@1058: String river = extractRiver(data); ingo@1058: if (river == null || river.length() == 0) { ingo@1058: logger.warn("Cannot generate information: river is empty!"); ingo@1058: return result; ingo@1058: } ingo@1058: ingo@1058: Element root = cr.create("river"); ingo@1058: cr.addAttr(root, "name", river); ingo@1058: mapinfo.appendChild(root); ingo@1058: ingo@2078: Envelope env = GeometryUtils.getRiverBoundary(river); ingo@2078: if (env != null) { ingo@2078: String bounds = GeometryUtils.jtsBoundsToOLBounds(env); ingo@2078: logger.debug("River '" + river + "' bounds: " + bounds); ingo@1058: ingo@1058: Element bbox = cr.create("bbox"); ingo@1058: cr.addAttr(bbox, "value", bounds); ingo@1058: root.appendChild(bbox); ingo@1058: } ingo@1058: sascha@2098: Map vars = new HashMap(); sascha@2098: vars.put("river", river); sascha@2098: sascha@2098: String sridStr = getStringXPath(XPATH_RIVER_PROJECTION, vars); sascha@2098: ingo@1058: if (sridStr != null && sridStr.length() > 0) { ingo@1058: Element srid = cr.create("srid"); ingo@1058: cr.addAttr(srid, "value", sridStr); ingo@1058: root.appendChild(srid); ingo@1058: } ingo@1058: sascha@2098: Element back = (Element)getNodeXPath(XPATH_RIVER_BACKGROUND, vars); ingo@1058: if (back != null) { ingo@1058: Element background = cr.create("background-wms"); ingo@1058: cr.addAttr(background, "url", back.getAttribute("url")); ingo@1058: cr.addAttr(background, "layers", back.getAttribute("layers")); ingo@1058: root.appendChild(background); ingo@1058: } ingo@1058: sascha@2098: String wmsStr = getStringXPath(XPATH_RIVER_WMS, vars); ingo@1058: if (wmsStr != null && wmsStr.length() > 0) { ingo@1058: Element wms = cr.create("river-wms"); ingo@1058: cr.addAttr(wms, "url", wmsStr); ingo@1058: root.appendChild(wms); ingo@1058: } ingo@1058: ingo@1058: return result; ingo@1058: } ingo@1058: ingo@1058: ingo@1058: protected String extractRiver(Document data) { ingo@1058: return XMLUtils.xpathString( ingo@1058: data, XPATH_RIVER, ArtifactNamespaceContext.INSTANCE); ingo@1058: } ingo@1058: } ingo@1058: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :