<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>
/**
* 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:
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"));
}
/**
}
/**
- * 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
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;
/**
* 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();
}
--- /dev/null
+/*
+ * 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();
+ }
+}
--- /dev/null
+<?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>
--- /dev/null
+<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>