diff --git a/tools/java/data/pom.xml b/tools/java/data/pom.xml index d669c1ac0..0334282a3 100644 --- a/tools/java/data/pom.xml +++ b/tools/java/data/pom.xml @@ -19,6 +19,11 @@ 4.8.1 test + + javax.servlet + servlet-api + 2.5 + @@ -50,6 +55,32 @@ + + org.apache.maven.plugins + maven-war-plugin + 2.1.1 + + webapp + + + + org.mortbay.jetty + maven-jetty-plugin + 6.1.10 + + webapp + 10 + + / + + + + 8080 + 60000 + + + + diff --git a/tools/java/data/src/com/google/i18n/phonenumbers/tools/CombineGeoData.java b/tools/java/data/src/com/google/i18n/phonenumbers/tools/CombineGeoData.java index ed0328130..385a3f463 100644 --- a/tools/java/data/src/com/google/i18n/phonenumbers/tools/CombineGeoData.java +++ b/tools/java/data/src/com/google/i18n/phonenumbers/tools/CombineGeoData.java @@ -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). *
  * 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 phonePrefixMap = parseInput();
     phonePrefixMap = combineMultipleTimes(phonePrefixMap);
     PrintWriter printWriter = new PrintWriter(new BufferedOutputStream(outputStream));
     for (Map.Entry 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
index 000000000..0011beb68
--- /dev/null
+++ b/tools/java/data/src/com/google/i18n/phonenumbers/tools/CombineGeoDataServlet.java
@@ -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("");
+    resp.getOutputStream().print(
+        "");
+    resp.getOutputStream().print("");
+    CombineGeoData combineGeoData = new CombineGeoData(
+        new ByteArrayInputStream(input.getBytes()), resp.getOutputStream(), "
"); + combineGeoData.run(); + resp.getOutputStream().print(""); + 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 index 000000000..c30d720ae --- /dev/null +++ b/tools/java/data/webapp/WEB-INF/web.xml @@ -0,0 +1,18 @@ + + + + CombineGeoDataServlet + com.google.i18n.phonenumbers.tools.CombineGeoDataServlet + + + CombineGeoDataServlet + /combine + + + index.html + + diff --git a/tools/java/data/webapp/index.html b/tools/java/data/webapp/index.html new file mode 100644 index 000000000..987ca7004 --- /dev/null +++ b/tools/java/data/webapp/index.html @@ -0,0 +1,22 @@ + + + + +
+

Geocoding Prefix Reducer

+ +

Please paste your geocoding data below. Each line should be in the format + "prefix|description". Lines without a "|" symbol will be ignored.

+ + Input example:
+ 331|Paris
+ 334|Marseilles
+
+ + +
+ Back to libphonenumber +
+ + +