ingo@1058: package de.intevation.flys.artifacts.services; ingo@1058: ingo@1058: import org.apache.log4j.Logger; ingo@1058: ingo@1058: import org.w3c.dom.Document; ingo@1058: import org.w3c.dom.Element; ingo@1058: 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: ingo@1058: import de.intevation.artifactdatabase.DefaultService; ingo@1058: ingo@1058: import de.intevation.flys.model.River; ingo@1058: ingo@1058: import de.intevation.flys.model.RiverAxis; 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: */ ingo@1058: public class MapInfoService extends DefaultService { 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 = ingo@1058: "/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 = ingo@1058: "/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: ingo@1058: ingo@1058: public Document process( 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: ingo@1058: String xpathS = XPATH_RIVER_PROJECTION.replace("%RIVER%", river); ingo@1058: String sridStr = Config.getStringXPath(xpathS); 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: ingo@1058: String xpathB = XPATH_RIVER_BACKGROUND.replace("%RIVER%", river); ingo@1058: Element back = (Element) Config.getNodeXPath(xpathB); 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: ingo@1058: String xpathWMS = XPATH_RIVER_WMS.replace("%RIVER%", river); ingo@1058: String wmsStr = Config.getStringXPath(xpathWMS); 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 :