Skaros Ilias
Skaros Ilias

Reputation: 1068

how to catch a "White spaces are required between publicId and systemId." error

I have a working java application that is harvesting an API end point. The API is supplying xml files, which i parse. The problem is that I am getting this strange error at some files

[Fatal Error] :1:55: White spaces are required between publicId and systemId.

causing the application to hung, trying to read that one file.
My main problem is that it is not throwing an exception, so I have no means of catching it (to either drop it, or try to re-do the process) and that I am not getting a line of the code that this error is coming from. When I try to load the file on the browser, it usually takes a long time to load, but eventually it is loaded.

I am guessing that the problem is on file fetching, which is this method

public String getData(String url){
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        HttpGet request = new HttpGet(url);
        // add request header
        request.addHeader("User-Agent", "MySuperUserAgent");

        try {
            HttpResponse response = httpClient.execute(request);
            BufferedReader rd;
            try{    
                rd = new BufferedReader(
                           new InputStreamReader(response.getEntity().getContent()));
            }catch(NullPointerException npe){           
                return null;
            }catch(Exception BRE){          
                return null;
            }

            String line = "";
            HttpEntity entity = response.getEntity();

            if (entity != null) {
                line=EntityUtils.toString(entity);
            }               

            httpClient.close();
            rd.close();
            return line.replace("", "");
        } catch (UnsupportedOperationException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } catch (NullPointerException e){
            return null;
        } catch (InterruptedException e) {
            e.printStackTrace();
            return null;
        }           
    }

Strangest of all is that if I run the same file again, maybe the second time around it might process it properly!
Is there any way to overcome this problem, or at least skip this file and let the program do something, rather than hanging there!

EDIT This is the api respond

Upvotes: 1

Views: 2228

Answers (1)

azatris
azatris

Reputation: 21

I wish there was a more elegant solution to this, but I doubt it. I think an elegant solution would rely on solving the underlying problem why this error is thrown in the first place.

The class used here is org.apache.commons.lang3.exception.ExceptionUtils.

try {
    JAXBContext jaxbContext = createJaxbContext();
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
    JAXBElement<E> jaxbElement = unmarshaller.unmarshal(new StreamSource(fileInputStream), expectedClass);
} catch (UnmarshalException e) {
    if (ExceptionUtils.getStackTrace(e.getLinkedException()).contains("White spaces are required between publicId and systemId.")) {
        // exception handling
    }
}

Upvotes: 1

Related Questions