/*
 * COPYRIGHT (c) 2010 by Institute of Computer Science, 
 * Foundation for Research and Technology - Hellas
 * Contact: 
 *      POBox 1385, Heraklio Crete, GR-700 13 GREECE
 *      Tel:+30-2810-391632
 *      Fax: +30-2810-391638
 *      E-mail: isl@ics.forth.gr
 *      http://www.ics.forth.gr/isl/cci.html
 * 
 *  This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 
 * Unported License (http://creativecommons.org/licenses/by-sa/3.0/)
*/


import java.io.*;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

import gr.forth.ics.swkm.model2.Model;
import gr.forth.ics.swkm.model2.ModelBuilder;
import gr.forth.ics.swkm.model2.io.Format;
import gr.forth.ics.swkm.model2.io.RdfIO;
import gr.forth.ics.swkm.model2.validation.ValidationHandler;
import gr.forth.ics.swkm.model2.validation.ValidationProblem;
import gr.forth.ics.swkm.model2.validation.Validator;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;



public class RdfsParser {
	public ArrayList<String> uri;
	public ArrayList<String> namespace;
	ArrayList<String> filerdfs;
	Model model;
	
	public boolean validate(String path) throws FileNotFoundException, IOException {
	
		File data = new File(path);
		
		RdfIO.read(data, Format.RDFXML).withBase(path).into(model);
		//Validator.defaultValidator().validateAndFailOnFirstError(model); 
		final boolean[] hasErrors = new boolean[1];
		hasErrors[0]=false;
        Validator.defaultValidator().validate(model, new ValidationHandler() {
            int i = 1;
            public void handleError(ValidationProblem error) {
                System.out.println(i + ") " + error);
                i++;	
				hasErrors[0] = true;
            }

            public void handleWarning(ValidationProblem warning) {
                System.out.println("Warning: " + warning);
            }
        });
		return hasErrors[0];
    }
	
	
	public boolean validateRdfs() {
		final boolean[] hasErrors = new boolean[1];
		hasErrors[0]=false;
		try{
		model = ModelBuilder.newSparse().withTypeInference().build();
		for (int i=0; i<uri.size(); i++) {
			File schema = new File(filerdfs.get(i));
			RdfIO.read(schema, Format.RDFXML).withBase(uri.get(i)).into(model);
		}
		Validator.defaultValidator().validate(model, new ValidationHandler() {
            int i = 1;
            public void handleError(ValidationProblem error) {
                System.out.println(i + ") " + error);
                i++;
				hasErrors[0] = true;
            }

            public void handleWarning(ValidationProblem warning) {
                System.out.println("Warning: " + warning);
            }
        });	
		}catch(Exception e){
			System.out.println("Read/Validate Exception "+e);
		}
		return hasErrors[0];
	}
	
	
	public RdfsParser(){
		filerdfs = new ArrayList<String>();
		uri = new ArrayList<String>();
		namespace = new ArrayList<String>();
		ArrayList<String> paths = read_paths("./Paths.txt");
		
		for (int i=0; i<paths.size(); i=i+3) {
		
			filerdfs.add(paths.get(i));
			uri.add(paths.get(i+1));
			namespace.add(paths.get(i+2));
			
		}
		if (validateRdfs()) System.exit(0);
		
	}
	
	private ArrayList<String> read_paths(String filepath){
		ArrayList<String> paths = new ArrayList<String>();
		try{
			FileInputStream fstream = new FileInputStream(filepath);
			BufferedReader in = new BufferedReader(new InputStreamReader(fstream));
			String line = in.readLine();
			String[] lineparts;
            while (line != null)
			{
            	if((!line.contains("%%"))&&line.trim().length()!=0){
            		lineparts = line.split("=");
            		paths.add(lineparts[1].trim());
            	}	
            	line = in.readLine();
			}
            return paths;
		}catch(Exception e){
			System.out.println("Reading Paths failed "+e);
			JOptionPane.showMessageDialog(new JFrame(), "Path file incorrect");
			System.exit(0);
		}	
		return paths;
	}

}
