Browse Source

TOOLS: Add Servlet for Geodata combination tool.

pull/567/head
Philippe Liard 14 years ago
committed by Mihaela Rosca
parent
commit
16ab3d5d1b
5 changed files with 131 additions and 7 deletions
  1. +31
    -0
      tools/java/data/pom.xml
  2. +14
    -7
      tools/java/data/src/com/google/i18n/phonenumbers/tools/CombineGeoData.java
  3. +46
    -0
      tools/java/data/src/com/google/i18n/phonenumbers/tools/CombineGeoDataServlet.java
  4. +18
    -0
      tools/java/data/webapp/WEB-INF/web.xml
  5. +22
    -0
      tools/java/data/webapp/index.html

+ 31
- 0
tools/java/data/pom.xml View File

@ -19,6 +19,11 @@
<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>
@ -50,6 +55,32 @@
</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>


+ 14
- 7
tools/java/data/src/com/google/i18n/phonenumbers/tools/CombineGeoData.java View 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();
}


+ 46
- 0
tools/java/data/src/com/google/i18n/phonenumbers/tools/CombineGeoDataServlet.java View File

@ -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();
}
}

+ 18
- 0
tools/java/data/webapp/WEB-INF/web.xml View File

@ -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>

+ 22
- 0
tools/java/data/webapp/index.html View File

@ -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>

Loading…
Cancel
Save