Java tutorial
/** * Copyright 2015 Google Inc. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.boazlev.waze.web; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.Collection; import java.util.logging.Level; import java.util.logging.Logger; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.lang3.StringEscapeUtils; import com.boazlev.waze.json.neig.Info; import com.boazlev.waze.json.neig.MyBase; import com.boazlev.waze.json.neig.PoiElement; import com.boazlev.waze.json.neig.PoiMarkerElement; import com.boazlev.waze.services.App; import com.boazlev.waze.services.AppImpl; import com.boazlev.waze.services.EnhancedBase; @SuppressWarnings("serial") public class WazeServlet extends HttpServlet { private static final Logger LOG = Logger.getLogger(WazeServlet.class.getName()); private App app = new AppImpl(); @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { LOG.info("doPost"); response.setContentType("text/xml; charset=UTF-8"); PrintWriter out = new PrintWriter(new OutputStreamWriter(response.getOutputStream(), "UTF8"), true); StringBuilder sb = new StringBuilder(); Collection<EnhancedBase> enhancedBases = app.get(); sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>").append("\n"); sb.append("<?xml-stylesheet type=\"text/xsl\" href=\"out.xslt\"?>").append("\n"); sb.append("<catalog>").append("\n"); if (enhancedBases != null) { for (EnhancedBase enhancedBase : enhancedBases) { String city = enhancedBase.getCity(); String neighborhood = enhancedBase.getNeighborhood(); LOG.info("Parsing - " + city + " / " + neighborhood); MyBase myBase = enhancedBase.getMyBase(); if (myBase != null) { PoiMarkerElement[] poimarkers = myBase.getPoimarkers(); for (PoiMarkerElement poiMarkerElement : poimarkers) { PoiElement[] pois = poiMarkerElement.getPois(); if (pois != null) { for (PoiElement poiElement : pois) { String id = poiElement.getId(); if ("project".equals(poiElement.getType())) { sb.append("<cd>").append("\n"); sb.append(toXmlData("xid", id)).append("\n"); Info info = poiElement.getInfo(); if (info != null) { sb.append(toXmlData("city", city)).append("\n"); sb.append(toXmlData("neighborhood", neighborhood)).append("\n"); sb.append(toXmlData("marketing", info.getMarketingstage())).append("\n"); sb.append(toXmlData("buildingStage", info.getBuildingstage())).append("\n"); sb.append(toXmlData("location", info.getLocation())).append("\n"); sb.append(toXmlData("lat", poiElement.getLat().toString())).append("\n"); sb.append(toXmlData("lng", poiElement.getLng().toString())).append("\n"); sb.append(toXmlData("description", info.getDescription())).append("\n"); } sb.append("</cd>").append("\n"); } } } } } } } sb.append("</catalog>"); out.println(sb.toString()); out.flush(); LOG.fine(sb.toString()); LOG.info("Done"); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doPost(req, resp); } private String toXmlData(String tag, String data) { StringBuilder sb = new StringBuilder(); sb.append("<").append(tag).append(">"); if (data != null) { sb.append(StringEscapeUtils.escapeXml11(data)); } sb.append("</").append(tag).append(">"); return sb.toString(); } }