Java tutorial
/* * The MIT License * * Copyright 2014 Alvin Natawiguna. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ package TwitterAnalytics; import TwitterAnalytics.StringMatcher.MatcherAlgo; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; 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 twitter4j.Status; /** * * @author Alvin Natawiguna */ public class Analyze extends HttpServlet { private List<String> keywords; private final HashMap<String, List<String>> searchPattern = new HashMap<>(); private MatcherAlgo matchMethod; private final List<Category> category = new ArrayList<>(); private final TwitterAPI twitter = TwitterAPI.getSingleton(); private final Category unknown = new Category("unknown"); private void clearCategory() { unknown.clear(); for (Category cat : category) { cat.clear(); } category.clear(); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { processRequest(request, response); } catch (Exception e) { e.printStackTrace(); } } protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, Exception { clearCategory(); keywords = Arrays.asList(request.getParameter("twitterKeywords").trim().split(";")); searchPattern.clear(); String checkMacet = request.getParameter("checkMacet"); if (checkMacet.equalsIgnoreCase("ok")) { searchPattern.put("macet", Arrays.asList(request.getParameter("keywordMacet").trim().split(";"))); category.add(new Category("macet")); } String checkKecelakaan = request.getParameter("checkKecelakaan"); if (checkKecelakaan.equalsIgnoreCase("ok")) { searchPattern.put("kecelakaan", Arrays.asList(request.getParameter("keywordKecelakaan").trim().split(";"))); category.add(new Category("kecelakaan")); } String checkTutup = request.getParameter("checkTutup"); if (checkTutup.equalsIgnoreCase("ok")) { searchPattern.put("tutup", Arrays.asList(request.getParameter("keywordTutup").trim().split(";"))); category.add(new Category("tutup")); } String matchingAlgorithm = request.getParameter("matchingAlgorithm"); if (matchingAlgorithm.equalsIgnoreCase("KMP")) { matchMethod = MatcherAlgo.KMP; } else if (matchingAlgorithm.equalsIgnoreCase("BM")) { matchMethod = MatcherAlgo.BM; } else { throw new ServletException("Unknown string matching method!"); } twitter.getTweets(keywords); parseCategories(); prepareResponse(response); } private void prepareResponse(HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); try (PrintWriter out = response.getWriter()) { /* TODO output your page here. You may use following sample code. */ out.println("<!DOCTYPE html>"); out.println("<html>"); out.println("<head>"); out.println("<title>Servlet NewServlet</title>"); out.println("</head>"); out.println("<body>"); for (Category k : category) { out.println(k.toString()); out.println("<br />"); } out.println(unknown.toString()); out.println("<br />"); out.println("</body>"); out.println("</html>"); out.flush(); out.close(); } } private void parseCategories() { StringMatcher mr = new StringMatcher(); for (Status st : twitter.getStatusCache()) { boolean match = false; int j = 0; while (!match && j < category.size()) { match = category.get(j).isMatch(st.getText(), matchMethod); if (match) { StatusView svw = new StatusView(st); category.get(j).add(svw.getStatus(), "https://twitter.com/" + svw.getUsername() + "/status/" + svw.getId(), mr.extractLocation(svw.getStatus())); } j++; } if (j == category.size()) { unknown.add(st.getText(), "https://twitter.com/" + st.getUser().getScreenName() + "/status/" + st.getId(), mr.extractLocation(st.getText())); } } } }