Advertisement
Guest User

Untitled

a guest
Jan 21st, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.09 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package pl;
  7.  
  8. import java.io.File;
  9. import java.io.IOException;
  10. import java.io.InputStream;
  11. import java.net.URL;
  12. import java.util.Enumeration;
  13. import java.util.zip.ZipEntry;
  14. import java.util.zip.ZipFile;
  15. import javax.ws.rs.core.Context;
  16. import javax.ws.rs.core.UriInfo;
  17.  
  18. import javax.ws.rs.GET;
  19. import javax.ws.rs.Path;
  20. import javax.ws.rs.PUT;
  21. import javax.ws.rs.PathParam;
  22. import javax.ws.rs.core.MediaType;
  23. import javax.xml.stream.XMLInputFactory;
  24. import javax.xml.stream.XMLStreamConstants;
  25. import javax.xml.stream.XMLStreamException;
  26. import javax.xml.stream.XMLStreamReader;
  27.  
  28. import java.net.HttpURLConnection;
  29. import java.util.zip.ZipInputStream;
  30. import javax.xml.namespace.QName;
  31.  
  32. /**
  33.  * REST Web Service
  34.  *
  35.  * @author Kacper
  36.  */
  37. @Path("pl")
  38. public class GUService {
  39.  
  40.     @Context
  41.     private UriInfo context;
  42.  
  43.     @GET
  44.     @Path("/{street}")
  45.     public int getStreet(@PathParam("street") String street) throws IOException {
  46.         try {
  47.             return readZipFile(street, "", "");
  48.         } catch (Exception ex) {
  49.             return 0;
  50.         }
  51.     }
  52.  
  53.     @GET
  54.     @Path("/{voivodeship}/{street}")
  55.     public int getVoivodeship(@PathParam("voivodeship") String voivodeship, @PathParam("street") String street) {
  56.         int voivodeshipCodeLength = voivodeship.length();
  57.  
  58.         try {
  59.             switch (voivodeshipCodeLength) {
  60.                 case 2:
  61.                     return readZipFile(street, voivodeship, "");
  62.             }
  63.         } catch (Exception ex) {
  64.  
  65.         }
  66.  
  67.         return 0;
  68.     }
  69.  
  70.     @GET
  71.     @Path("/xx/yy/{street}")
  72.     public void getVoivodeshipAndCounty(@PathParam("street") String street) {
  73.  
  74.     }
  75.  
  76.     public int readZipFile(String street, String voivodeship, String county) throws IOException, XMLStreamException {
  77.         int result = 0;
  78.  
  79.         String url = "http://cuda.iti.pk.edu.pl/ULIC_Urzedowy_2018-01-18.zip";
  80.         HttpURLConnection httpConnection = (HttpURLConnection) new URL(url).openConnection();
  81.         InputStream stream = httpConnection.getInputStream();
  82.         ZipInputStream zis = new ZipInputStream(stream);
  83.         for (ZipEntry e; (e = zis.getNextEntry()) != null;) {
  84.             if (e.getName().contains(".xml")) {
  85.                 XMLInputFactory inputFactory = XMLInputFactory.newInstance();
  86.                 XMLStreamReader xMLInputFactory = inputFactory.createXMLStreamReader(zis);
  87.  
  88.                 while (xMLInputFactory.hasNext()) {
  89.                     xMLInputFactory.next();
  90.                     int event = xMLInputFactory.getEventType();
  91.  
  92.                     if (event == XMLStreamConstants.START_ELEMENT) {
  93.                         try {
  94.                             String rowName = xMLInputFactory.getLocalName();
  95.                             if (rowName == "WOJ") {
  96.                                 String text = xMLInputFactory.getElementText();
  97.                                 result++;
  98.                             }
  99. //                            if ("row".equals(rowName)) {
  100. //                                String voivodeshipName = "";
  101. //                                String streetName = "";
  102. //                                boolean voivodeShipFound = false;
  103. //                                boolean streetFound = false;
  104.  
  105. //                                while (!voivodeShipFound || !streetFound) {
  106. //                                    try {
  107. //                                        xMLInputFactory.next();
  108. //
  109. //                                        if (xMLInputFactory.getEventType() == XMLStreamConstants.START_ELEMENT) {
  110. //                                            String tagName = xMLInputFactory.getLocalName();
  111. //
  112. //                                            if ("WOJ".equals(tagName)) {
  113. //                                                voivodeShipFound = true;
  114. //                                                voivodeshipName = xMLInputFactory.getElementText();
  115. //                                            }
  116. //
  117. //                                            if ("NAZWA_1".equals(tagName)) {
  118. //                                                streetFound = true;
  119. //                                                streetName = xMLInputFactory.getElementText();
  120. //                                            }
  121. //                                        }
  122. //                                    } catch (Exception ex) {
  123. //                                        int error = 1;
  124. //                                    }
  125. //
  126. //                                }
  127. //                                if (voivodeshipName == voivodeship && streetName == street) {
  128. //                                    result++;
  129. //                                }
  130. //
  131. //                            }
  132.                         } catch (Exception ex) {
  133.                             int test = 0;
  134.                         }
  135.  
  136.                     }
  137.                 }
  138.             }
  139.         }
  140.  
  141.         return result;
  142.  
  143.     }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement