Ignore:
Timestamp:
2016-06-01T00:55:28+02:00 (10 years ago)
Author:
donvip
Message:

sonar - fix many issues

File:
1 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr/CadastreInterface.java

    r32205 r32211  
    1212import java.net.MalformedURLException;
    1313import java.net.URL;
     14import java.nio.charset.StandardCharsets;
     15import java.util.ArrayList;
    1416import java.util.Date;
    15 import java.util.Vector;
     17import java.util.List;
    1618
    1719import javax.swing.JComboBox;
     
    2628
    2729public class CadastreInterface {
    28     public boolean downloadCanceled = false;
    29     public HttpURLConnection urlConn = null;
     30    public boolean downloadCanceled;
     31    public HttpURLConnection urlConn;
    3032
    3133    private String cookie;
    32     private String interfaceRef = null;
    33     private String lastWMSLayerName = null;
     34    private String interfaceRef;
     35    private String lastWMSLayerName;
    3436    private URL searchFormURL;
    35     private Vector<String> listOfCommunes = new Vector<>();
    36     private Vector<String> listOfTA = new Vector<>();
    37     class PlanImage {
     37    private List<String> listOfCommunes = new ArrayList<>();
     38    private List<String> listOfTA = new ArrayList<>();
     39    static class PlanImage {
    3840        String name;
    3941        String ref;
     
    4345        }
    4446    }
    45     private Vector<PlanImage> listOfFeuilles = new Vector<>();
     47    private List<PlanImage> listOfFeuilles = new ArrayList<>();
    4648    private long cookieTimestamp;
    4749
    48     final String baseURL = "http://www.cadastre.gouv.fr";
    49     final String cImageFormat = "Cette commune est au format ";
    50     final String cCommuneListStart = "<select name=\"codeCommune\"";
    51     final String cCommuneListEnd = "</select>";
    52     final String c0ptionListStart = "<option value=\"";
    53     final String cOptionListEnd = "</option>";
    54     final String cBBoxCommunStart = "new GeoBox(";
    55     final String cBBoxCommunEnd = ")";
    56 
    57     final String cInterfaceVector = "afficherCarteCommune.do";
    58     final String cInterfaceRasterTA = "afficherCarteTa.do";
    59     final String cInterfaceRasterFeuille = "afficherCarteFeuille.do";
    60     final String cImageLinkStart = "<a href=\"#\" class=\"raster\" onClick=\"popup('afficherCarteFeuille.do?f=";
    61     final String cTAImageLinkStart = "<a href=\"#\" class=\"raster\" onClick=\"popup('afficherCarteTa.do?f=";
    62     final String cImageNameStart = ">Feuille ";
    63     final String cTAImageNameStart = "Tableau d'assemblage <strong>";
    64 
    65     final static long cCookieExpiration = 30 * 60 * 1000; // 30 minutes expressed in milliseconds
    66 
    67     final  int cRetriesGetCookie = 10; // 10 times every 3 seconds means 30 seconds trying to get a cookie
     50    static final String BASE_URL = "http://www.cadastre.gouv.fr";
     51    static final String C_IMAGE_FORMAT = "Cette commune est au format ";
     52    static final String C_COMMUNE_LIST_START = "<select name=\"codeCommune\"";
     53    static final String C_COMMUNE_LIST_END = "</select>";
     54    static final String C_OPTION_LIST_START = "<option value=\"";
     55    static final String C_OPTION_LIST_END = "</option>";
     56    static final String C_BBOX_COMMUN_START = "new GeoBox(";
     57    static final String C_BBOX_COMMUN_END = ")";
     58
     59    static final String C_INTERFACE_VECTOR = "afficherCarteCommune.do";
     60    static final String C_INTERFACE_RASTER_TA = "afficherCarteTa.do";
     61    static final String C_INTERFACE_RASTER_FEUILLE = "afficherCarteFeuille.do";
     62    static final String C_IMAGE_LINK_START = "<a href=\"#\" class=\"raster\" onClick=\"popup('afficherCarteFeuille.do?f=";
     63    static final String C_TA_IMAGE_LINK_START = "<a href=\"#\" class=\"raster\" onClick=\"popup('afficherCarteTa.do?f=";
     64    static final String C_IMAGE_NAME_START = ">Feuille ";
     65    static final String C_TA_IMAGE_NAME_START = "Tableau d'assemblage <strong>";
     66
     67    static final long COOKIE_EXPIRATION = 30 * 60 * 1000L; // 30 minutes expressed in milliseconds
     68
     69    static final int RETRIES_GET_COOKIE = 10; // 10 times every 3 seconds means 30 seconds trying to get a cookie
    6870
    6971    public boolean retrieveInterface(WMSLayer wmsLayer) throws DuplicateLayerException, WMSException {
    70         if (wmsLayer.getName().equals(""))
     72        if (wmsLayer.getName().isEmpty())
    7173            return false;
    7274        boolean isCookieExpired = isCookieExpired();
     
    9092            openInterface();
    9193        } catch (IOException e) {
     94            Main.error(e);
    9295            JOptionPane.showMessageDialog(Main.parent,
    9396                    tr("Town/city {0} not found or not available\n" +
     
    105108     */
    106109    private void getCookie() throws IOException {
    107         boolean sucess = false;
    108         int retries = cRetriesGetCookie;
     110        boolean success = false;
     111        int retries = RETRIES_GET_COOKIE;
    109112        try {
    110             searchFormURL = new URL(baseURL + "/scpc/accueil.do");
    111             while (sucess == false && retries > 0) {
     113            searchFormURL = new URL(BASE_URL + "/scpc/accueil.do");
     114            while (!success && retries > 0) {
    112115                urlConn = (HttpURLConnection)searchFormURL.openConnection();
    113116                urlConn.setRequestProperty("Connection", "close");
     
    116119                if (urlConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
    117120                    Main.info("GET "+searchFormURL);
    118                     BufferedReader in = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
    119                     while(in.readLine() != null) {}  // read the buffer otherwise we sent POST too early
    120                     sucess = true;
     121                    BufferedReader in = new BufferedReader(new InputStreamReader(urlConn.getInputStream(), StandardCharsets.UTF_8));
     122                    while(in.readLine() != null) {
     123                        // read the buffer otherwise we sent POST too early
     124                    }
     125                    success = true;
    121126                    String headerName;
    122127                    for (int i=1; (headerName = urlConn.getHeaderFieldKey(i))!=null; i++) {
     
    155160    public boolean isCookieExpired() {
    156161        long now = new Date().getTime();
    157         if ((now - cookieTimestamp) > cCookieExpiration) {
     162        if ((now - cookieTimestamp) > COOKIE_EXPIRATION) {
    158163            Main.info("cookie received at "+new Date(cookieTimestamp)+" expired (now is "+new Date(now)+")");
    159164            return true;
     
    182187        // second attempt either from known codeCommune (e.g. from cache) or from ComboBox
    183188        if (interfaceRef == null) {
    184             if (!wmsLayer.getCodeCommune().equals("")) {
     189            if (!wmsLayer.getCodeCommune().isEmpty()) {
    185190                // codeCommune is already known (from previous request or from cache on disk)
    186191                interfaceRef = postForm(wmsLayer, wmsLayer.getCodeCommune());
    187192            } else {
    188193                if (listOfCommunes.size() > 1) {
    189                     // commune unknown, prompt the list of communes from
    190                     // server and try with codeCommune
    191                     String selected = selectMunicipalityDialog(wmsLayer);
     194                    // commune unknown, prompt the list of communes from server and try with codeCommune
     195                    String selected = selectMunicipalityDialog();
    192196                    if (selected != null) {
    193                         String newCodeCommune = selected.substring(1, selected.indexOf(">")-2);
    194                         String newLocation = selected.substring(selected.indexOf(">")+1, selected.lastIndexOf(" - "));
     197                        String newCodeCommune = selected.substring(1, selected.indexOf('>') - 2);
     198                        String newLocation = selected.substring(selected.indexOf('>') + 1, selected.lastIndexOf(" - "));
    195199                        wmsLayer.setCodeCommune(newCodeCommune);
    196200                        wmsLayer.setLocation(newLocation);
     
    205209                    int res = selectFeuilleDialog();
    206210                    if (res != -1) {
    207                         wmsLayer.setCodeCommune(listOfFeuilles.elementAt(res).name);
     211                        wmsLayer.setCodeCommune(listOfFeuilles.get(res).name);
    208212                        checkLayerDuplicates(wmsLayer);
    209213                        interfaceRef = buildRasterFeuilleInterfaceRef(wmsLayer.getCodeCommune());
     
    220224        try {
    221225            // finally, open the interface on server side giving access to the wms server
    222             String lines = null;
    223             String ln = null;
    224             URL interfaceURL = new URL(baseURL + "/scpc/"+interfaceRef);
     226            URL interfaceURL = new URL(BASE_URL + "/scpc/"+interfaceRef);
    225227            urlConn = (HttpURLConnection)interfaceURL.openConnection();
    226228            urlConn.setRequestMethod("GET");
     
    231233            }
    232234            Main.info("GET "+interfaceURL);
    233             BufferedReader in = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
     235            BufferedReader in = new BufferedReader(new InputStreamReader(urlConn.getInputStream(), StandardCharsets.UTF_8));
    234236            // read the buffer otherwise we sent POST too early
     237            StringBuilder lines = new StringBuilder();
     238            String ln;
    235239            while ((ln = in.readLine()) != null) {
    236                 lines += ln;
     240                if (Main.isDebugEnabled()) {
     241                    lines.append(ln);
     242                }
    237243            }
    238244            if (Main.isDebugEnabled()) {
    239                 Main.debug(lines);
     245                Main.debug(lines.toString());
    240246            }
    241247        } catch (MalformedURLException e) {
     
    267273    private String postForm(WMSLayer wmsLayer, String codeCommune) throws IOException {
    268274        try {
    269             String ln = null;
    270             String lines = null;
    271275            listOfCommunes.clear();
    272276            listOfTA.clear();
     
    276280            content += "&nomvoie=";
    277281            content += "&lieuDit=";
    278             if (codeCommune == "") {
    279                 content += "&ville=" + new String(java.net.URLEncoder.encode(wmsLayer.getLocation(), "UTF-8"));
     282            if (codeCommune.isEmpty()) {
     283                content += "&ville=" + java.net.URLEncoder.encode(wmsLayer.getLocation(), "UTF-8");
    280284                content += "&codePostal=";
    281285            } else {
     
    286290            content += "&nbResultatParPage=10";
    287291            content += "&x=0&y=0";
    288             searchFormURL = new URL(baseURL + "/scpc/rechercherPlan.do");
     292            searchFormURL = new URL(BASE_URL + "/scpc/rechercherPlan.do");
    289293            urlConn = (HttpURLConnection)searchFormURL.openConnection();
    290294            urlConn.setRequestMethod("POST");
     
    293297            setCookie();
    294298            try (OutputStream wr = urlConn.getOutputStream()) {
    295                 wr.write(content.getBytes());
     299                wr.write(content.getBytes(StandardCharsets.UTF_8));
    296300                Main.info("POST "+content);
    297301                wr.flush();
    298302            }
    299             try (BufferedReader rd = new BufferedReader(new InputStreamReader(urlConn.getInputStream()))) {
     303            String ln;
     304            StringBuilder sb = new StringBuilder();
     305            try (BufferedReader rd = new BufferedReader(new InputStreamReader(urlConn.getInputStream(), StandardCharsets.UTF_8))) {
    300306                while ((ln = rd.readLine()) != null) {
    301                     lines += ln;
    302                 }
    303             }
     307                    sb.append(ln);
     308                }
     309            }
     310            String lines = sb.toString();
    304311            urlConn.disconnect();
    305312            if (lines != null) {
    306                 if (lines.indexOf(cImageFormat) != -1) {
    307                     int i = lines.indexOf(cImageFormat);
    308                     int j = lines.indexOf(".", i);
    309                     wmsLayer.setRaster(lines.substring(i+cImageFormat.length(), j).equals("image"));
    310                 }
    311                 if (!wmsLayer.isRaster() && lines.indexOf(cInterfaceVector) != -1) {  // "afficherCarteCommune.do"
     313                if (lines.indexOf(C_IMAGE_FORMAT) != -1) {
     314                    int i = lines.indexOf(C_IMAGE_FORMAT);
     315                    int j = lines.indexOf('.', i);
     316                    wmsLayer.setRaster("image".equals(lines.substring(i+C_IMAGE_FORMAT.length(), j)));
     317                }
     318                if (!wmsLayer.isRaster() && lines.indexOf(C_INTERFACE_VECTOR) != -1) {  // "afficherCarteCommune.do"
    312319                    // shall be something like: interfaceRef = "afficherCarteCommune.do?c=X2269";
    313                     lines = lines.substring(lines.indexOf(cInterfaceVector),lines.length());
    314                     lines = lines.substring(0, lines.indexOf("'"));
     320                    lines = lines.substring(lines.indexOf(C_INTERFACE_VECTOR),lines.length());
     321                    lines = lines.substring(0, lines.indexOf('\''));
    315322                    Main.info("interface ref.:"+lines);
    316323                    return lines;
    317                 } else if (wmsLayer.isRaster() && lines.indexOf(cInterfaceRasterTA) != -1) { // "afficherCarteTa.do"
     324                } else if (wmsLayer.isRaster() && lines.indexOf(C_INTERFACE_RASTER_TA) != -1) { // "afficherCarteTa.do"
    318325                    // list of values parsed in listOfFeuilles (list all non-georeferenced images)
    319326                    lines = getFeuillesList();
    320327                    if (!downloadCanceled) {
    321328                        parseFeuillesList(lines);
    322                         if (listOfFeuilles.size() > 0) {
     329                        if (!listOfFeuilles.isEmpty()) {
    323330                            int res = selectFeuilleDialog();
    324331                            if (res != -1) {
    325                                 wmsLayer.setCodeCommune(listOfFeuilles.elementAt(res).name);
     332                                wmsLayer.setCodeCommune(listOfFeuilles.get(res).name);
    326333                                checkLayerDuplicates(wmsLayer);
    327334                                interfaceRef = buildRasterFeuilleInterfaceRef(wmsLayer.getCodeCommune());
    328                                 wmsLayer.setCodeCommune(listOfFeuilles.elementAt(res).ref);
    329                                 lines = buildRasterFeuilleInterfaceRef(listOfFeuilles.elementAt(res).ref);
     335                                wmsLayer.setCodeCommune(listOfFeuilles.get(res).ref);
     336                                lines = buildRasterFeuilleInterfaceRef(listOfFeuilles.get(res).ref);
    330337                                Main.info("interface ref.:"+lines);
    331338                                return lines;
     
    334341                    }
    335342                    return null;
    336                 } else if (lines.indexOf(cCommuneListStart) != -1 && lines.indexOf(cCommuneListEnd) != -1) {
     343                } else if (lines.indexOf(C_COMMUNE_LIST_START) != -1 && lines.indexOf(C_COMMUNE_LIST_END) != -1) {
    337344                    // list of values parsed in listOfCommunes
    338                     int i = lines.indexOf(cCommuneListStart);
    339                     int j = lines.indexOf(cCommuneListEnd, i);
     345                    int i = lines.indexOf(C_COMMUNE_LIST_START);
     346                    int j = lines.indexOf(C_COMMUNE_LIST_END, i);
    340347                    parseCommuneList(lines.substring(i, j));
    341348                }
    342349            }
    343350        } catch (MalformedURLException e) {
    344             throw (IOException) new IOException(
    345                 "Illegal url.").initCause(e);
    346         } catch (Exception e){
    347             e.printStackTrace();
     351            throw (IOException) new IOException("Illegal url.").initCause(e);
     352        } catch (DuplicateLayerException e){
     353            Main.error(e);
    348354        }
    349355        return null;
     
    351357
    352358    private void parseCommuneList(String input) {
    353         if (input.indexOf(c0ptionListStart) != -1) {
     359        if (input.indexOf(C_OPTION_LIST_START) != -1) {
    354360            while (input.indexOf("<option value=\"") != -1) {
    355                 int i = input.indexOf(c0ptionListStart);
    356                 int j = input.indexOf(cOptionListEnd, i+c0ptionListStart.length());
    357                 int k = input.indexOf("\"", i+c0ptionListStart.length());
    358                 if (j != -1 && k > (i + c0ptionListStart.length())) {
    359                     String lov = new String(input.substring(i+c0ptionListStart.length()-1, j));
    360                     if (lov.indexOf(">") != -1) {
     361                int i = input.indexOf(C_OPTION_LIST_START);
     362                int j = input.indexOf(C_OPTION_LIST_END, i+C_OPTION_LIST_START.length());
     363                int k = input.indexOf('"', i+C_OPTION_LIST_START.length());
     364                if (j != -1 && k > (i + C_OPTION_LIST_START.length())) {
     365                    String lov = input.substring(i+C_OPTION_LIST_START.length()-1, j);
     366                    if (lov.indexOf('>') != -1) {
    361367                        Main.info("parse "+lov);
    362368                        listOfCommunes.add(lov);
     
    364370                        Main.error("unable to parse commune string:"+lov);
    365371                }
    366                 input = input.substring(j+cOptionListEnd.length());
     372                input = input.substring(j+C_OPTION_LIST_END.length());
    367373            }
    368374        }
     
    372378        // get all images in one html page
    373379        String ln = null;
    374         String lines = null;
     380        StringBuilder lines = new StringBuilder();
    375381        HttpURLConnection urlConn2 = null;
    376382        try {
    377             URL getAllImagesURL = new URL(baseURL + "/scpc/listerFeuillesParcommune.do?keepVolatileSession=&offset=2000");
     383            URL getAllImagesURL = new URL(BASE_URL + "/scpc/listerFeuillesParcommune.do?keepVolatileSession=&offset=2000");
    378384            urlConn2 = (HttpURLConnection)getAllImagesURL.openConnection();
    379385            setCookie(urlConn2);
    380386            urlConn2.connect();
    381387            Main.info("GET "+getAllImagesURL);
    382             try (BufferedReader rd = new BufferedReader(new InputStreamReader(urlConn2.getInputStream()))) {
     388            try (BufferedReader rd = new BufferedReader(new InputStreamReader(urlConn2.getInputStream(), StandardCharsets.UTF_8))) {
    383389                while ((ln = rd.readLine()) != null) {
    384                     lines += ln;
     390                    lines.append(ln);
    385391                }
    386392            }
     
    390396            Main.error(e);
    391397        }
    392         return lines;
     398        return lines.toString();
    393399    }
    394400
     
    398404        String inputTA = input;
    399405        if (Main.pref.getBoolean("cadastrewms.useTA", false)) {
    400             while (inputTA.indexOf(cTAImageLinkStart) != -1) {
    401                 inputTA = inputTA.substring(inputTA.indexOf(cTAImageLinkStart) + cTAImageLinkStart.length());
    402                 String refTA = inputTA.substring(0, inputTA.indexOf("'"));
    403                 String nameTA = inputTA.substring(inputTA.indexOf(cTAImageNameStart) + cTAImageNameStart.length());
    404                 nameTA = nameTA.substring(0, nameTA.indexOf("<"));
     406            while (inputTA.indexOf(C_TA_IMAGE_LINK_START) != -1) {
     407                inputTA = inputTA.substring(inputTA.indexOf(C_TA_IMAGE_LINK_START) + C_TA_IMAGE_LINK_START.length());
     408                String refTA = inputTA.substring(0, inputTA.indexOf('\''));
     409                String nameTA = inputTA.substring(inputTA.indexOf(C_TA_IMAGE_NAME_START) + C_TA_IMAGE_NAME_START.length());
     410                nameTA = nameTA.substring(0, nameTA.indexOf('<'));
    405411                listOfFeuilles.add(new PlanImage(nameTA, refTA));
    406412            }
    407413        }
    408414        // get "Feuilles"
    409         while (input.indexOf(cImageLinkStart) != -1) {
    410             input = input.substring(input.indexOf(cImageLinkStart)+cImageLinkStart.length());
    411             String refFeuille = input.substring(0, input.indexOf("'"));
     415        while (input.indexOf(C_IMAGE_LINK_START) != -1) {
     416            input = input.substring(input.indexOf(C_IMAGE_LINK_START)+C_IMAGE_LINK_START.length());
     417            String refFeuille = input.substring(0, input.indexOf('\''));
    412418            String nameFeuille = input.substring(
    413                     input.indexOf(cImageNameStart)+cImageNameStart.length(),
     419                    input.indexOf(C_IMAGE_NAME_START)+C_IMAGE_NAME_START.length(),
    414420                    input.indexOf(" -"));
    415421            listOfFeuilles.add(new PlanImage(nameFeuille, refFeuille));
     
    417423    }
    418424
    419     private String selectMunicipalityDialog(WMSLayer wmsLayer) {
     425    private String selectMunicipalityDialog() {
    420426        JPanel p = new JPanel(new GridBagLayout());
    421427        String[] communeList = new String[listOfCommunes.size() + 1];
    422428        communeList[0] = tr("Choose from...");
    423429        for (int i = 0; i < listOfCommunes.size(); i++) {
    424             communeList[i + 1] = listOfCommunes.elementAt(i).substring(listOfCommunes.elementAt(i).indexOf(">")+1);
     430            communeList[i + 1] = listOfCommunes.get(i).substring(listOfCommunes.get(i).indexOf('>')+1);
    425431        }
    426432        JComboBox<String> inputCommuneList = new JComboBox<>(communeList);
    427433        p.add(inputCommuneList, GBC.eol().fill(GBC.HORIZONTAL).insets(10, 0, 0, 0));
    428434        JOptionPane pane = new JOptionPane(p, JOptionPane.INFORMATION_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null);
    429         //pane.createDialog(Main.parent, tr("Select commune")).setVisible(true);
    430435        // this below is a temporary workaround to fix the "always on top" issue
    431436        JDialog dialog = pane.createDialog(Main.parent, tr("Select commune"));
     
    435440        if (!Integer.valueOf(JOptionPane.OK_OPTION).equals(pane.getValue()))
    436441            return null;
    437         return listOfCommunes.elementAt(inputCommuneList.getSelectedIndex()-1);
     442        return listOfCommunes.get(inputCommuneList.getSelectedIndex()-1);
    438443    }
    439444
    440445    private int selectFeuilleDialog() {
    441446        JPanel p = new JPanel(new GridBagLayout());
    442         Vector<String> imageNames = new Vector<>();
     447        List<String> imageNames = new ArrayList<>();
    443448        for (PlanImage src : listOfFeuilles) {
    444449            imageNames.add(src.name);
    445450        }
    446         JComboBox<String> inputFeuilleList = new JComboBox<>(imageNames);
     451        JComboBox<String> inputFeuilleList = new JComboBox<>(imageNames.toArray(new String[]{}));
    447452        p.add(inputFeuilleList, GBC.eol().fill(GBC.HORIZONTAL).insets(10, 0, 0, 0));
    448453        JOptionPane pane = new JOptionPane(p, JOptionPane.INFORMATION_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null);
    449         //pane.createDialog(Main.parent, tr("Select Feuille")).setVisible(true);
    450454        // this below is a temporary workaround to fix the "always on top" issue
    451455        JDialog dialog = pane.createDialog(Main.parent, tr("Select Feuille"));
     
    455459        if (!Integer.valueOf(JOptionPane.OK_OPTION).equals(pane.getValue()))
    456460            return -1;
    457         int result = inputFeuilleList.getSelectedIndex();
    458         return result;
    459     }
    460 
    461     private String buildRasterFeuilleInterfaceRef(String codeCommune) {
    462         return cInterfaceRasterFeuille + "?f=" + codeCommune;
     461        return inputFeuilleList.getSelectedIndex();
     462    }
     463
     464    private static String buildRasterFeuilleInterfaceRef(String codeCommune) {
     465        return C_INTERFACE_RASTER_FEUILLE + "?f=" + codeCommune;
    463466    }
    464467
     
    474477        if (interfaceRef == null)
    475478            return;
    476         String ln = null;
    477         String line = null;
    478479        // send GET opening normally the small window with the commune overview
    479         String content = baseURL + "/scpc/" + interfaceRef;
     480        String content = BASE_URL + "/scpc/" + interfaceRef;
    480481        content += "&dontSaveLastForward&keepVolatileSession=";
    481482        searchFormURL = new URL(content);
     
    488489        }
    489490        Main.info("GET "+searchFormURL);
    490         try (BufferedReader in = new BufferedReader(new InputStreamReader(urlConn.getInputStream()))) {
     491        String ln;
     492        StringBuilder sb = new StringBuilder();
     493        try (BufferedReader in = new BufferedReader(new InputStreamReader(urlConn.getInputStream(), StandardCharsets.UTF_8))) {
    491494            while ((ln = in.readLine()) != null) {
    492                 line += ln;
     495                sb.append(ln);
    493496            }
    494497        }
    495498        urlConn.disconnect();
     499        String line = sb.toString();
    496500        parseBBoxCommune(wmsLayer, line);
    497501        if (wmsLayer.isRaster() && !wmsLayer.isAlreadyGeoreferenced()) {
     
    500504    }
    501505
    502     private void parseBBoxCommune(WMSLayer wmsLayer, String input) {
    503         if (input.indexOf(cBBoxCommunStart) != -1) {
    504             input = input.substring(input.indexOf(cBBoxCommunStart));
    505             int i = input.indexOf(",");
    506             double minx = Double.parseDouble(input.substring(cBBoxCommunStart.length(), i));
    507             int j = input.indexOf(",", i+1);
     506    private static void parseBBoxCommune(WMSLayer wmsLayer, String input) {
     507        if (input.indexOf(C_BBOX_COMMUN_START) != -1) {
     508            input = input.substring(input.indexOf(C_BBOX_COMMUN_START));
     509            int i = input.indexOf(',');
     510            double minx = Double.parseDouble(input.substring(C_BBOX_COMMUN_START.length(), i));
     511            int j = input.indexOf(',', i+1);
    508512            double miny = Double.parseDouble(input.substring(i+1, j));
    509             int k = input.indexOf(",", j+1);
     513            int k = input.indexOf(',', j+1);
    510514            double maxx = Double.parseDouble(input.substring(j+1, k));
    511             int l = input.indexOf(cBBoxCommunEnd, k+1);
     515            int l = input.indexOf(C_BBOX_COMMUN_END, k+1);
    512516            double maxy = Double.parseDouble(input.substring(k+1, l));
    513517            wmsLayer.setCommuneBBox( new EastNorthBound(new EastNorth(minx,miny), new EastNorth(maxx,maxy)));
     
    515519    }
    516520
    517     private void parseGeoreferences(WMSLayer wmsLayer, String input) {
     521    private static void parseGeoreferences(WMSLayer wmsLayer, String input) {
    518522        /* commented since cadastre WMS changes mid july 2013
    519523         * until new GeoBox coordinates parsing is solved */
     
    547551//                wmsLayer.Y0 = Y0;
    548552//            }
    549 //            Main.info("parse georef:"+unknown_yet+","+angle+","+scale_origin+","+dpi+","+fX+","+
    550 //                    fY+","+X0+","+Y0);
     553//            Main.info("parse georef:"+unknown_yet+","+angle+","+scale_origin+","+dpi+","+fX+","+fY+","+X0+","+Y0);
    551554//        }
    552555    }
    553556
    554     private void checkLayerDuplicates(WMSLayer wmsLayer) throws DuplicateLayerException {
     557    private static void checkLayerDuplicates(WMSLayer wmsLayer) throws DuplicateLayerException {
    555558        if (Main.map != null) {
    556559            for (Layer l : Main.map.mapView.getAllLayers()) {
    557                 if (l instanceof WMSLayer && l.getName().equals(wmsLayer.getName()) && (l != wmsLayer)) {
     560                if (l instanceof WMSLayer && l.getName().equals(wmsLayer.getName()) && (!l.equals(wmsLayer))) {
    558561                    Main.info("Try to grab into a new layer when "+wmsLayer.getName()+" is already opened.");
    559562                    // remove the duplicated layer
     
    569572            urlConn.setConnectTimeout(1);
    570573            urlConn.setReadTimeout(1);
    571             //urlConn.disconnect();
    572574        }
    573575        downloadCanceled = true;
Note: See TracChangeset for help on using the changeset viewer.