Commit 58227743 authored by Matija Obreza's avatar Matija Obreza
Browse files

Moved ParserForCLDR class into GeoRegionDataCLDR

parent cc9eb408
package org.genesys2.server.service.worker;
import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.genesys2.server.model.impl.GeoRegion;
import org.genesys2.util.ParserForCLDR;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import javax.annotation.Resource;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
......@@ -20,105 +25,147 @@ import java.util.zip.ZipFile;
@Component
public class GeoRegionDataCLDR {
private static final String CLDR_URL = "http://unicode.org/Public/cldr/26.0.1/core.zip";
public static final Log LOG = LogFactory.getLog(GeoRegionDataCLDR.class);
public List<GeoRegion> getGeoRigionDataCLDR() throws IOException, ParserConfigurationException, SAXException {
ZipFile zipFile = getCoreZip();
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
SAXParser parser = saxParserFactory.newSAXParser();
ParserForCLDR parserForCLDR = new ParserForCLDR();
final Set<String> supportedLocales = new HashSet<>();
supportedLocales.add("en");
supportedLocales.add("ar");
supportedLocales.add("de");
supportedLocales.add("fa");
supportedLocales.add("fr");
supportedLocales.add("pt");
supportedLocales.add("ru");
supportedLocales.add("zh");
supportedLocales.add("es");
ZipEntry zipEntry;
List<GeoRegion> geoRegionList = new ArrayList<>();
InputStream inputStream;
for (String language : supportedLocales) {
String fileName = "common/main/" + language + ".xml";
zipEntry = zipFile.getEntry(fileName);
inputStream = zipFile.getInputStream(zipEntry);
parser.parse(inputStream, parserForCLDR);
List<GeoRegion> geoRegionsWithAssigndParents = assignParents(parserForCLDR.getRegionList());
geoRegionList.addAll(geoRegionsWithAssigndParents);
inputStream.close();
}
return geoRegionList;
}
private List<GeoRegion> assignParents(Map<String, GeoRegion> geoRegions) {
String world = "001";
String continents = "002,019,142,150,009";
String forAfrica = "015,014,017,018,011";
String forAsia = "143,030,034,035,145";
String forEurope = "151,154,039,155";
String forOceania = "053,054,057,061";
String forAmerica = "419,003,021";
String forLatinAmerica = "029,013,005";
for(String isoCode : continents.split(",")){
geoRegions.get(isoCode).setParentGeoRegion(geoRegions.get(world));
}
for(String isoCode : forAfrica.split(",")){
geoRegions.get(isoCode).setParentGeoRegion(geoRegions.get(continents.split(",")[0]));
}
for(String isoCode : forAmerica.split(",")){
geoRegions.get(isoCode).setParentGeoRegion(geoRegions.get(continents.split(",")[1]));
}
for(String isoCode : forAsia.split(",")){
geoRegions.get(isoCode).setParentGeoRegion(geoRegions.get(continents.split(",")[2]));
}
for(String isoCode : forEurope.split(",")){
geoRegions.get(isoCode).setParentGeoRegion(geoRegions.get(continents.split(",")[3]));
}
for(String isoCode : forOceania.split(",")){
geoRegions.get(isoCode).setParentGeoRegion(geoRegions.get(continents.split(",")[4]));
}
for(String isoCode : forLatinAmerica.split(",")){
geoRegions.get(isoCode).setParentGeoRegion(geoRegions.get(forAmerica.split(",")[0]));
}
return new ArrayList<>(geoRegions.values());
}
private ZipFile getCoreZip() throws IOException {
URL url = new URL(CLDR_URL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
File tempFileForXml = File.createTempFile("core", ".zip");
connection.setRequestMethod("GET");
InputStream in = connection.getInputStream();
FileOutputStream out = new FileOutputStream(tempFileForXml);
copyZip(in, out, 1024);
ZipFile zipFile = new ZipFile(tempFileForXml);
out.close();
return zipFile;
}
private void copyZip(InputStream input, OutputStream output, int bufferSize) throws IOException {
byte[] buf = new byte[bufferSize];
int n = input.read(buf);
while (n >= 0) {
output.write(buf, 0, n);
n = input.read(buf);
}
output.flush();
}
private static final String CLDR_URL = "http://unicode.org/Public/cldr/26.0.1/core.zip";
public static final Log LOG = LogFactory.getLog(GeoRegionDataCLDR.class);
@Resource
private Set<String> supportedLocales;
public List<GeoRegion> getGeoRigionDataCLDR() throws IOException, ParserConfigurationException, SAXException {
ZipFile zipFile = getCoreZip();
LOG.info("Obtained CLDR zip file: " + zipFile);
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
saxParserFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
SAXParser parser = saxParserFactory.newSAXParser();
ParserForCLDR parserForCLDR = new ParserForCLDR();
ZipEntry zipEntry;
List<GeoRegion> geoRegionList = new ArrayList<>();
InputStream inputStream;
for (String language : supportedLocales) {
String fileName = "common/main/" + language + ".xml";
LOG.info("Examining " + fileName);
zipEntry = zipFile.getEntry(fileName);
inputStream = zipFile.getInputStream(zipEntry);
parser.parse(inputStream, parserForCLDR);
List<GeoRegion> geoRegionsWithAssigndParents = assignParents(parserForCLDR.getRegionList());
geoRegionList.addAll(geoRegionsWithAssigndParents);
inputStream.close();
}
return geoRegionList;
}
private List<GeoRegion> assignParents(Map<String, GeoRegion> geoRegions) {
String world = "001";
String continents = "002,019,142,150,009";
String forAfrica = "015,014,017,018,011";
String forAsia = "143,030,034,035,145";
String forEurope = "151,154,039,155";
String forOceania = "053,054,057,061";
String forAmerica = "419,003,021";
String forLatinAmerica = "029,013,005";
for (String isoCode : continents.split(",")) {
geoRegions.get(isoCode).setParentGeoRegion(geoRegions.get(world));
}
for (String isoCode : forAfrica.split(",")) {
geoRegions.get(isoCode).setParentGeoRegion(geoRegions.get(continents.split(",")[0]));
}
for (String isoCode : forAmerica.split(",")) {
geoRegions.get(isoCode).setParentGeoRegion(geoRegions.get(continents.split(",")[1]));
}
for (String isoCode : forAsia.split(",")) {
geoRegions.get(isoCode).setParentGeoRegion(geoRegions.get(continents.split(",")[2]));
}
for (String isoCode : forEurope.split(",")) {
geoRegions.get(isoCode).setParentGeoRegion(geoRegions.get(continents.split(",")[3]));
}
for (String isoCode : forOceania.split(",")) {
geoRegions.get(isoCode).setParentGeoRegion(geoRegions.get(continents.split(",")[4]));
}
for (String isoCode : forLatinAmerica.split(",")) {
geoRegions.get(isoCode).setParentGeoRegion(geoRegions.get(forAmerica.split(",")[0]));
}
return new ArrayList<>(geoRegions.values());
}
private ZipFile getCoreZip() throws IOException {
URL url = new URL(CLDR_URL);
LOG.info("Downloading " + url);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
File tempFileForXml = File.createTempFile("core", ".zip");
connection.setRequestMethod("GET");
InputStream in = connection.getInputStream();
FileOutputStream out = new FileOutputStream(tempFileForXml);
IOUtils.copy(in, out);
out.close();
LOG.info("Done copying CLDR data to file");
ZipFile zipFile = new ZipFile(tempFileForXml);
return zipFile;
}
public class ParserForCLDR extends DefaultHandler {
private GeoRegion geoRegion;
private Map<String, GeoRegion> regionMap;
// needs for avoid duplicate geoRegions in regionList
private List<String> isoCodeList;
private String xmlTag = "";
private Attributes attributes;
public Map<String, GeoRegion> getRegionList() {
return regionMap;
}
@Override
public void startDocument() throws SAXException {
regionMap = new HashMap<>();
isoCodeList = new ArrayList<>();
}
@Override
public void endDocument() throws SAXException {
super.endDocument();
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
geoRegion = new GeoRegion();
this.xmlTag = qName;
this.attributes = attributes;
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
super.endElement(uri, localName, qName);
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
String attribute = attributes.getValue("type");
if (this.xmlTag.equalsIgnoreCase("territory") && attribute.matches("[0-9]+") && !isoCodeList.contains(attribute)) {
geoRegion.setIsoCode(attribute);
geoRegion.setName(new String(ch, start, length));
isoCodeList.add(attribute);
regionMap.put(geoRegion.getIsoCode(), geoRegion);
}
}
}
}
package org.genesys2.util;
import org.genesys2.server.model.impl.GeoRegion;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ParserForCLDR extends DefaultHandler{
private GeoRegion geoRegion;
private Map<String, GeoRegion> regionMap;
//needs for avoid duplicate geoRegions in regionList
private List<String> isoCodeList;
private String xmlTag = "";
private Attributes attributes;
public Map<String, GeoRegion> getRegionList() {
return regionMap;
}
@Override
public void startDocument() throws SAXException {
regionMap = new HashMap<>();
isoCodeList = new ArrayList<>();
}
@Override
public void endDocument() throws SAXException {
super.endDocument();
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
geoRegion = new GeoRegion();
this.xmlTag = qName;
this.attributes = attributes;
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
super.endElement(uri, localName, qName);
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
String attribute = attributes.getValue("type");
if(this.xmlTag.equalsIgnoreCase("territory") && attribute.matches("[0-9]+") && !isoCodeList.contains(attribute)) {
geoRegion.setIsoCode(attribute);
geoRegion.setName(new String(ch, start, length));
isoCodeList.add(attribute);
regionMap.put(geoRegion.getIsoCode(), geoRegion);
}
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment