001/* 002 * Copyright 2015 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.ametys.plugins.maps; 018 019import java.io.IOException; 020import java.util.Arrays; 021 022import org.ametys.plugins.repository.metadata.CompositeMetadata; 023import org.ametys.web.repository.page.ZoneItem; 024 025import org.apache.cocoon.ProcessingException; 026import org.apache.cocoon.environment.ObjectModelHelper; 027import org.apache.cocoon.environment.Request; 028import org.apache.cocoon.generation.AbstractGenerator; 029import org.apache.cocoon.xml.XMLUtils; 030import org.xml.sax.SAXException; 031 032/** 033 * Generates a map. 034 */ 035public class MapGenerator extends AbstractGenerator 036{ 037 @Override 038 public void generate() throws IOException, SAXException, ProcessingException 039 { 040 Request request = ObjectModelHelper.getRequest(objectModel); 041 ZoneItem zoneItem = (ZoneItem) request.getAttribute(ZoneItem.class.getName()); 042 CompositeMetadata serviceParameters = zoneItem.getServiceParameters(); 043 044 contentHandler.startDocument(); 045 XMLUtils.startElement(contentHandler, "map"); 046 047 saxMapServiceParameters(serviceParameters); 048 049 XMLUtils.endElement(contentHandler, "map"); 050 contentHandler.endDocument(); 051 } 052 053 /** 054 * Sax Map service Parameters 055 * @param serviceParameters the parameters of the service 056 * @throws SAXException if an error occurs while saxing the map service parameters 057 */ 058 protected void saxMapServiceParameters(CompositeMetadata serviceParameters) throws SAXException 059 { 060 if (serviceParameters == null) 061 { 062 return; 063 } 064 065 XMLUtils.createElement(contentHandler, "centerLat", serviceParameters.getString("centerLat")); 066 XMLUtils.createElement(contentHandler, "centerLng", serviceParameters.getString("centerLng")); 067 XMLUtils.createElement(contentHandler, "zoomLevel", serviceParameters.getString("zoomLevel")); 068 XMLUtils.createElement(contentHandler, "mapTypeId", serviceParameters.getString("mapTypeId")); 069 070 XMLUtils.startElement(contentHandler, "pois"); 071 072 CompositeMetadata pois = serviceParameters.getCompositeMetadata("pois"); 073 for (String poiName : pois.getMetadataNames()) 074 { 075 CompositeMetadata poi = pois.getCompositeMetadata(poiName); 076 String type = poi.getString("type"); 077 078 XMLUtils.startElement(contentHandler, "poi"); 079 080 XMLUtils.createElement(contentHandler, "title", poi.getString("title")); 081 XMLUtils.createElement(contentHandler, "description", poi.getString("description")); 082 XMLUtils.createElement(contentHandler, "type", type); 083 084 if ("marker".equals(type)) 085 { 086 XMLUtils.createElement(contentHandler, "icon", poi.getString("icon")); 087 XMLUtils.createElement(contentHandler, "lat", poi.getString("lat")); 088 XMLUtils.createElement(contentHandler, "lng", poi.getString("lng")); 089 } 090 else if ("polygon".equals(type)) 091 { 092 XMLUtils.createElement(contentHandler, "color", poi.getString("color")); 093 094 CompositeMetadata points = poi.getCompositeMetadata("points"); 095 096 XMLUtils.startElement(contentHandler, "points"); 097 098 String[] metadataNames = points.getMetadataNames(); 099 Arrays.sort(metadataNames); 100 101 for (String pointName : metadataNames) 102 { 103 CompositeMetadata point = points.getCompositeMetadata(pointName); 104 105 XMLUtils.startElement(contentHandler, "point"); 106 107 XMLUtils.createElement(contentHandler, "lat", point.getString("lat")); 108 XMLUtils.createElement(contentHandler, "lng", point.getString("lng")); 109 110 XMLUtils.endElement(contentHandler, "point"); 111 112 } 113 114 XMLUtils.endElement(contentHandler, "points"); 115 } 116 else 117 { 118 throw new IllegalArgumentException("Unknown type value for POI " + type); 119 } 120 121 XMLUtils.endElement(contentHandler, "poi"); 122 } 123 124 XMLUtils.endElement(contentHandler, "pois"); 125 } 126}