Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package pl;
- import java.io.File;
- import java.io.IOException;
- import java.io.InputStream;
- import java.net.URL;
- import java.util.Enumeration;
- import java.util.zip.ZipEntry;
- import java.util.zip.ZipFile;
- import javax.ws.rs.core.Context;
- import javax.ws.rs.core.UriInfo;
- import javax.ws.rs.GET;
- import javax.ws.rs.Path;
- import javax.ws.rs.PUT;
- import javax.ws.rs.PathParam;
- import javax.ws.rs.core.MediaType;
- import javax.xml.stream.XMLInputFactory;
- import javax.xml.stream.XMLStreamConstants;
- import javax.xml.stream.XMLStreamException;
- import javax.xml.stream.XMLStreamReader;
- import java.net.HttpURLConnection;
- import java.util.zip.ZipInputStream;
- import javax.xml.namespace.QName;
- /**
- * REST Web Service
- *
- * @author Kacper
- */
- @Path("pl")
- public class GUService {
- @Context
- private UriInfo context;
- @GET
- @Path("/{street}")
- public int getStreet(@PathParam("street") String street) throws IOException {
- try {
- return readZipFile(street, "", "");
- } catch (Exception ex) {
- return 0;
- }
- }
- @GET
- @Path("/{voivodeship}/{street}")
- public int getVoivodeship(@PathParam("voivodeship") String voivodeship, @PathParam("street") String street) {
- int voivodeshipCodeLength = voivodeship.length();
- try {
- switch (voivodeshipCodeLength) {
- case 2:
- return readZipFile(street, voivodeship, "");
- }
- } catch (Exception ex) {
- }
- return 0;
- }
- @GET
- @Path("/xx/yy/{street}")
- public void getVoivodeshipAndCounty(@PathParam("street") String street) {
- }
- public int readZipFile(String street, String voivodeship, String county) throws IOException, XMLStreamException {
- int result = 0;
- String url = "http://cuda.iti.pk.edu.pl/ULIC_Urzedowy_2018-01-18.zip";
- HttpURLConnection httpConnection = (HttpURLConnection) new URL(url).openConnection();
- InputStream stream = httpConnection.getInputStream();
- ZipInputStream zis = new ZipInputStream(stream);
- for (ZipEntry e; (e = zis.getNextEntry()) != null;) {
- if (e.getName().contains(".xml")) {
- XMLInputFactory inputFactory = XMLInputFactory.newInstance();
- XMLStreamReader xMLInputFactory = inputFactory.createXMLStreamReader(zis);
- while (xMLInputFactory.hasNext()) {
- xMLInputFactory.next();
- int event = xMLInputFactory.getEventType();
- if (event == XMLStreamConstants.START_ELEMENT) {
- try {
- String rowName = xMLInputFactory.getLocalName();
- if (rowName == "WOJ") {
- String text = xMLInputFactory.getElementText();
- result++;
- }
- // if ("row".equals(rowName)) {
- // String voivodeshipName = "";
- // String streetName = "";
- // boolean voivodeShipFound = false;
- // boolean streetFound = false;
- // while (!voivodeShipFound || !streetFound) {
- // try {
- // xMLInputFactory.next();
- //
- // if (xMLInputFactory.getEventType() == XMLStreamConstants.START_ELEMENT) {
- // String tagName = xMLInputFactory.getLocalName();
- //
- // if ("WOJ".equals(tagName)) {
- // voivodeShipFound = true;
- // voivodeshipName = xMLInputFactory.getElementText();
- // }
- //
- // if ("NAZWA_1".equals(tagName)) {
- // streetFound = true;
- // streetName = xMLInputFactory.getElementText();
- // }
- // }
- // } catch (Exception ex) {
- // int error = 1;
- // }
- //
- // }
- // if (voivodeshipName == voivodeship && streetName == street) {
- // result++;
- // }
- //
- // }
- } catch (Exception ex) {
- int test = 0;
- }
- }
- }
- }
- }
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement