TOOLS: Add Servlet for Geodata combination tool.
authorphilip.liard@gmail.com <philip.liard@gmail.com@ee073f10-1060-11df-b6a4-87a95322a99c>
Thu, 22 Sep 2011 11:02:13 +0000 (11:02 +0000)
committerphilip.liard@gmail.com <philip.liard@gmail.com@ee073f10-1060-11df-b6a4-87a95322a99c>
Thu, 22 Sep 2011 11:02:13 +0000 (11:02 +0000)
git-svn-id: http://libphonenumber.googlecode.com/svn/trunk@357 ee073f10-1060-11df-b6a4-87a95322a99c

tools/java/data/pom.xml
tools/java/data/src/com/google/i18n/phonenumbers/tools/CombineGeoData.java
tools/java/data/src/com/google/i18n/phonenumbers/tools/CombineGeoDataServlet.java [new file with mode: 0644]
tools/java/data/webapp/WEB-INF/web.xml [new file with mode: 0644]
tools/java/data/webapp/index.html [new file with mode: 0644]

index d669c1a..0334282 100644 (file)
       <version>4.8.1</version>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>javax.servlet</groupId>
+      <artifactId>servlet-api</artifactId>
+      <version>2.5</version>
+    </dependency>
   </dependencies>
 
   <build>
           </execution>
         </executions>
       </plugin>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-war-plugin</artifactId>
+        <version>2.1.1</version>
+        <configuration>
+          <warSourceDirectory>webapp</warSourceDirectory>
+        </configuration>
+      </plugin>
+      <plugin>
+        <groupId>org.mortbay.jetty</groupId>
+        <artifactId>maven-jetty-plugin</artifactId>
+        <version>6.1.10</version>
+        <configuration>
+          <webAppSourceDirectory>webapp</webAppSourceDirectory>
+          <scanIntervalSeconds>10</scanIntervalSeconds>
+          <webAppConfig>
+            <contextPath>/</contextPath>
+          </webAppConfig>
+          <connectors>
+            <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
+              <port>8080</port>
+              <maxIdleTime>60000</maxIdleTime>
+            </connector>
+          </connectors>
+        </configuration>
+      </plugin>
     </plugins>
   </build>
 
index ed03281..385a3f4 100644 (file)
@@ -35,7 +35,7 @@ import java.util.logging.Logger;
 
 /**
  * Utility class that makes the geocoding data as small as possible. This class assumes the
- * geocoding data provided as input doesn't contain any gap thus should not be used with incomplete
+ * geocoding data provided as input doesn't contain any gaps thus should not be used with incomplete
  * data (missing prefixes).
  * <pre>
  * Example:        Can be combined as:
@@ -49,11 +49,17 @@ import java.util.logging.Logger;
 public class CombineGeoData {
   private final InputStream inputStream;
   private final OutputStream outputStream;
+  private final String outputLineSeparator;
   private static final Logger LOGGER = Logger.getLogger(CombineGeoData.class.getName());
 
-  public CombineGeoData(InputStream inputStream, OutputStream outputStream) {
+  public CombineGeoData(InputStream inputStream, OutputStream outputStream, String lineSeparator) {
     this.inputStream = inputStream;
     this.outputStream = outputStream;
+    this.outputLineSeparator = lineSeparator;
+  }
+
+  public CombineGeoData(InputStream inputStream, OutputStream outputStream) {
+    this(inputStream, outputStream, System.getProperty("line.separator"));
   }
 
   /**
@@ -70,7 +76,8 @@ public class CombineGeoData {
   }
 
   /**
-   * Parses the input text file expected to contain lines written as 'prefix|description'.
+   * Parses the input text file expected to contain lines written as 'prefix|description'. Note that
+   * description can be empty.
    *
    * @return the map of phone prefix data parsed.
    * @throws IOException
@@ -80,10 +87,10 @@ public class CombineGeoData {
     BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
 
     for (String line; (line = bufferedReader.readLine()) != null; ) {
-      if (!line.matches("\\d+|.+")) {
+      int indexOfPipe = line.indexOf('|');
+      if (indexOfPipe == -1) {
         continue;
       }
-      int indexOfPipe = line.indexOf('|');
       outputMap.put(line.substring(0, indexOfPipe), line.substring(indexOfPipe + 1));
     }
     return outputMap;
@@ -251,14 +258,14 @@ public class CombineGeoData {
 
   /**
    * Combines the geocoding data read from the provided input stream and writes it as a result to
-   * the provided output stream.
+   * the provided output stream. Uses the provided string as the line separator.
    */
   public void run() throws IOException {
     SortedMap<String, String> phonePrefixMap = parseInput();
     phonePrefixMap = combineMultipleTimes(phonePrefixMap);
     PrintWriter printWriter = new PrintWriter(new BufferedOutputStream(outputStream));
     for (Map.Entry<String, String> mapping : phonePrefixMap.entrySet()) {
-      printWriter.printf("%s|%s\n", mapping.getKey(), mapping.getValue());
+      printWriter.printf("%s|%s%s", mapping.getKey(), mapping.getValue(), outputLineSeparator);
     }
     printWriter.flush();
   }
diff --git a/tools/java/data/src/com/google/i18n/phonenumbers/tools/CombineGeoDataServlet.java b/tools/java/data/src/com/google/i18n/phonenumbers/tools/CombineGeoDataServlet.java
new file mode 100644 (file)
index 0000000..0011beb
--- /dev/null
@@ -0,0 +1,46 @@
+/*
+ * Copyright (C) 2011 The Libphonenumber Authors
+ *
+ * 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.
+ *
+ * @author Philippe Liard
+ */
+
+package com.google.i18n.phonenumbers.tools;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+/**
+ * A servlet that invokes the geocoding data combination tool.
+ */
+public class CombineGeoDataServlet extends HttpServlet {
+  @Override
+  public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
+    resp.setContentType("text/html;charset=UTF-8");
+    String input = req.getParameter("geodata");
+    resp.getOutputStream().print("<html><head>");
+    resp.getOutputStream().print(
+        "<meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\"></head>");
+    resp.getOutputStream().print("<body>");
+    CombineGeoData combineGeoData = new CombineGeoData(
+        new ByteArrayInputStream(input.getBytes()), resp.getOutputStream(), "<br>");
+    combineGeoData.run();
+    resp.getOutputStream().print("</body></html>");
+    resp.getOutputStream().flush();
+  }
+}
diff --git a/tools/java/data/webapp/WEB-INF/web.xml b/tools/java/data/webapp/WEB-INF/web.xml
new file mode 100644 (file)
index 0000000..c30d720
--- /dev/null
@@ -0,0 +1,18 @@
+<?xml version="1.0" encoding="utf-8"?>
+<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+  xmlns="http://java.sun.com/xml/ns/javaee"
+  xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
+  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
+  http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
+  <servlet>
+    <servlet-name>CombineGeoDataServlet</servlet-name>
+    <servlet-class>com.google.i18n.phonenumbers.tools.CombineGeoDataServlet</servlet-class>
+  </servlet>
+  <servlet-mapping>
+    <servlet-name>CombineGeoDataServlet</servlet-name>
+    <url-pattern>/combine</url-pattern>
+  </servlet-mapping>
+  <welcome-file-list>
+    <welcome-file>index.html</welcome-file>
+  </welcome-file-list>
+</web-app>
diff --git a/tools/java/data/webapp/index.html b/tools/java/data/webapp/index.html
new file mode 100644 (file)
index 0000000..987ca70
--- /dev/null
@@ -0,0 +1,22 @@
+<html>
+  <head><meta http-equiv="content-type" content="text/html; charset=UTF-8"></head>
+
+  <body>
+    <form action="/combine" method="post" accept-charset="UTF-8">
+      <h2>Geocoding Prefix Reducer</h2>
+
+      <p>Please paste your geocoding data below. Each line should be in the format
+      "prefix|description". Lines without a "|" symbol will be ignored.</p>
+
+      <u>Input example:</u><br>
+      331|Paris<br>
+      334|Marseilles<br>
+      <br>
+
+      <textarea name="geodata" rows="20" cols="80"></textarea>
+      <input type="submit" value="Submit"><br>
+      <a href="http://code.google.com/p/libphonenumber/">Back to libphonenumber</a>
+    </form>
+  </body>
+
+</html>