package uk.ac.ebi.pride.archive.px;

import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.SAXException;
import uk.ac.ebi.pride.tools.ErrorHandlerIface;
import uk.ac.ebi.pride.tools.GenericSchemaValidator;
import uk.ac.ebi.pride.tools.ValidationErrorHandler;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.List;

/**
 * Class to validate a PX XML file according to the PX XML schema.
 *
 * @author Tobias Ternent
 */

public class ValidateMessage {

    private static final Logger logger = LoggerFactory.getLogger(ValidateMessage.class);
    private static final String SCHEMA_LOCATION = "http://proteomecentral.proteomexchange.org/schemas/proteomeXchange-1.3.0.xsd";

    /**
     * Method to validate a supplied PX XML file.
     *
     * @param file the PX XML file to be validated.
     * @return String which lists any errors that occurred during validation. If there were none, the String will be empty.
     * @throws SAXException
     * @throws MalformedURLException
     * @throws FileNotFoundException
     * @throws URISyntaxException
     */
    public static String validateMessage(File file) throws SAXException, MalformedURLException, FileNotFoundException, URISyntaxException{
        StringBuilder errorOutput = new StringBuilder();
        GenericSchemaValidator genericValidator = new GenericSchemaValidator();
        genericValidator.setSchema(new URI(SCHEMA_LOCATION));
        logger.info("XML schema validation on " + file.getName());
        ErrorHandlerIface handler = new ValidationErrorHandler();
        genericValidator.setErrorHandler(handler);
        BufferedReader br = new BufferedReader(new FileReader(file));
        genericValidator.validate(br);
        List<String> errorMsgs = handler.getErrorMessages(); // ToDo: make ErrorHandlerIface type safe
        for (String content : errorMsgs) {
            errorOutput.append(content);
            errorOutput.append(System.getProperty("line.separator"));
        }
        return errorOutput.toString();
    }
}

@@ -34,48 +34,8 @@
 x;%0A%0A
-import org.apache.commons.io.FileUtils;%0A
 impo
@@ -393,29 +393,8 @@
 on;%0A
-import java.net.URL;%0A
 impo
@@ -550,18 +550,16 @@
 sage %7B%0A%0A
-  
   privat
@@ -637,18 +637,16 @@
 class);%0A
-  
   privat
@@ -697,24 +697,51 @@
 p://
-proteomecentral.
+ftp.pride.ebi.ac.uk/pride/resources/schema/
 prot
@@ -755,20 +755,8 @@
 ange
-.org/schemas
 /pro
@@ -785,18 +785,14 @@
 %22;%0A%0A
-  
   /**%0A
-  
    *
@@ -842,14 +842,10 @@
 %0A   
-  *%0A  
+*%0A
    *
@@ -890,18 +890,16 @@
 idated.%0A
-  
    * @re
@@ -1011,26 +1011,24 @@
 e empty.%0A   
-  
 * @throws SA
@@ -1033,34 +1033,32 @@
 SAXException%0A   
- 
- 
 * @throws Malfor
@@ -1065,34 +1065,32 @@
 medURLException%0A
-  
    * @throws Fil
@@ -1104,26 +1104,24 @@
 ndException%0A
-  
    * @throws
@@ -1147,15 +1147,11 @@
 %0A   
-  
 */%0A
-  
   pu
@@ -1283,20 +1283,16 @@
 eption%7B%0A
-    
     Stri
@@ -1332,28 +1332,24 @@
 gBuilder();%0A
-    
     GenericS
@@ -1408,36 +1408,32 @@
 alidator();%0A    
- 
-   
 genericValidator
@@ -1466,28 +1466,24 @@
 LOCATION));%0A
-    
     logger.i
@@ -1537,20 +1537,16 @@
 ));%0A    
-  
-  
 ErrorHan
@@ -1595,20 +1595,16 @@
 dler();%0A
-    
     gene
@@ -1646,20 +1646,16 @@
 r);%0A    
-   
- 
 Buffered
@@ -1704,28 +1704,24 @@
 der(file));%0A
-    
     genericV
@@ -1743,20 +1743,16 @@
 te(br);%0A
-    
     List
@@ -1842,20 +1842,16 @@
 pe safe%0A
-    
     for 
@@ -1883,30 +1883,24 @@
 gs) %7B%0A      
-     
- 
 errorOutput.
@@ -1916,22 +1916,16 @@
 ntent);%0A
-      
       er
@@ -1988,18 +1988,10 @@
     
-  
-  %7D%0A    
+%7D%0A
     
@@ -2021,16 +2021,14 @@
 ring();%0A
-  
   %7D%0A%7D%0A

Update schema location, removed whitespace, unused imports.