<commit_before>package edu.unibonn.i4matcher.controllers;

import java.io.*;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;

import javax.servlet.http.HttpServletResponse;
//import javax.json;

import edu.unibonn.i4matcher.SparqlQuery;
import org.apache.jena.query.QueryParseException;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

//import com.hp.hpl.jena.rdf.model.Model;
import org.apache.jena.rdf.model.Model;

import edu.unibonn.i4matcher.model.FileMeta;
import edu.unibonn.i4matcher.helpers.*;
import edu.unibonn.i4matcher.Matcher;
@Controller
@RequestMapping("/controller")
public class FileController {
	LinkedList<FileMeta> files = new LinkedList<FileMeta>();
	FileMeta fileMeta = null;
	/***************************************************
	 * URL: /rest/controller/upload  
	 * upload(): receives files
	 * @param request : MultipartHttpServletRequest auto passed
	 * @param response : HttpServletResponse auto passed
	 * @return LinkedList<FileMeta> as json format
	 ****************************************************/
	@RequestMapping(value="/upload", method = RequestMethod.POST, produces = "application/json")
	public @ResponseBody Response upload(MultipartHttpServletRequest request, HttpServletResponse response) {
 		System.out.println(request.getRequestHeaders().toString());
		//1. build an iterator
		 Iterator<String> itr =  request.getFileNames();
		 MultipartFile mpf;

		 while(itr.hasNext()){

			 mpf = request.getFile(itr.next()); 
			 System.out.println(mpf.getOriginalFilename() +" uploaded! "+files.size());

			 if(files.size() >= 10)
				 files.pop();
			 
			 fileMeta = new FileMeta();
			 fileMeta.setFileName(mpf.getOriginalFilename());
			 fileMeta.setFileSize(mpf.getSize()/1024+" Kb");
             try {
                 fileMeta.setBytes(mpf.getBytes());
             } catch (IOException e) {
                 // TODO Auto-generated catch block
                 e.printStackTrace();
             }

			 try {
				 String schema = DocumentIdentifier.getFileType(mpf.getOriginalFilename());
				 XSDValidator validator = new XSDValidator(schema+".xsd");
                 fileMeta.setFileType(schema);
				 //InputStream is = new ByteArrayInputStream(mpf.getBytes());

				 validator.validateAgainstXSD(new ByteArrayInputStream(fileMeta.getBytes()));
				 Kreker pecker = new Kreker();
				 byte[] ttl = pecker.krekerize(new ByteArrayInputStream(fileMeta.getBytes()), schema);
                 fileMeta.setTtl(ttl);
				 //is.close();
			 } catch (Exception ex){
				 ex.printStackTrace();
			 }

			 files.add(fileMeta);
			 
		 }
        Matcher matcher = new Matcher();
        try {
            Model model = matcher.match2Files(files);
            TripleStoreWriter writer = new TripleStoreWriter();
            String ret = writer.write(model);
            Response resp = new Response(ret);
            System.out.println(resp.toString());
            return resp;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new Response("Error!");
 
	}
	/***************************************************
	 * URL: /rest/controller/get?query={qry}
	 * get(): get file as an attachment
	 * @param response : passed by the server
	 * @param qry : value from the URL
	 * @return void
	 ****************************************************/
	@RequestMapping(value = "/get" +
			"", method = RequestMethod.GET)
	 public void get(HttpServletResponse response,
					 //@PathVariable String value
					 @RequestParam(value = "query") String qry) {
//		 FileMeta getFile = files.get(Integer.parseInt(value));
		SparqlQuery sq = new SparqlQuery();<commit_msg>made fixes<commit_after>package edu.unibonn.i4matcher.controllers;

import java.io.*;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;

import javax.servlet.http.HttpServletResponse;
//import javax.json;

import edu.unibonn.i4matcher.SparqlQuery;
import org.apache.jena.query.QueryParseException;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

//import com.hp.hpl.jena.rdf.model.Model;
import org.apache.jena.rdf.model.Model;

import edu.unibonn.i4matcher.model.FileMeta;
import edu.unibonn.i4matcher.helpers.*;
import edu.unibonn.i4matcher.Matcher;
@Controller
@RequestMapping("/controller")
public class FileController {
	LinkedList<FileMeta> files = new LinkedList<FileMeta>();
	FileMeta fileMeta = null;
	/***************************************************
	 * URL: /rest/controller/upload  
	 * upload(): receives files
	 * @param request : MultipartHttpServletRequest auto passed
	 * @param response : HttpServletResponse auto passed
	 * @return LinkedList<FileMeta> as json format
	 ****************************************************/
	@RequestMapping(value="/upload", method = RequestMethod.POST, produces = "application/json")
	public @ResponseBody Response upload(MultipartHttpServletRequest request, HttpServletResponse response) {
 		System.out.println(request.getRequestHeaders().toString());
		//1. build an iterator
		 Iterator<String> itr =  request.getFileNames();
		 MultipartFile mpf;

		 while(itr.hasNext()){

			 mpf = request.getFile(itr.next()); 
			 System.out.println(mpf.getOriginalFilename() +" uploaded! "+files.size());

			 if(files.size() >= 10)
				 files.pop();
			 
			 fileMeta = new FileMeta();
			 fileMeta.setFileName(mpf.getOriginalFilename());
			 fileMeta.setFileSize(mpf.getSize()/1024+" Kb");
             try {
                 fileMeta.setBytes(mpf.getBytes());
             } catch (IOException e) {
                 // TODO Auto-generated catch block
                 e.printStackTrace();
             }

			 try {
				 String schema = DocumentIdentifier.getFileType(mpf.getOriginalFilename());
				 XSDValidator validator = new XSDValidator(schema+".xsd");
                 fileMeta.setFileType(schema);
				 //InputStream is = new ByteArrayInputStream(mpf.getBytes());

				 validator.validateAgainstXSD(new ByteArrayInputStream(fileMeta.getBytes()));
				 Kreker pecker = new Kreker();
				 byte[] ttl = pecker.krekerize(new ByteArrayInputStream(fileMeta.getBytes()), schema);
                 fileMeta.setTtl(ttl);
				 //is.close();
			 } catch (Exception ex){
				 ex.printStackTrace();
			 }

			 files.add(fileMeta);
			 
		 }
        Matcher matcher = new Matcher();
        try {
            Model model = matcher.match2Files(files);
            TripleStoreWriter writer = new TripleStoreWriter();
            String ret = writer.write(model);
            Response resp = new Response(ret);
            System.out.println(resp.toString());
            return resp;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new Response("Error!");
 
	}
	/***************************************************
	 * URL: /rest/controller/get?query={qry}
	 * get(): get file as an attachment
	 * @param response : passed by the server
	 * @param qry : value from the URL
	 * @return void
	 ****************************************************/
	@RequestMapping(value = "/get" +
			"", method = RequestMethod.GET, produces = "application/json")
	 public void get(HttpServletResponse response,
					 //@PathVariable String value
					 @RequestParam(value = "query") String qry) {
//		 FileMeta getFile = files.get(Integer.parseInt(value));
		SparqlQuery sq = new SparqlQuery();
{'fraction_non_alphanumeric': 0.16717440974866718, 'fraction_numerical': 0.0033003300330033004, 'mean_word_length': 3.5861466821885912, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2147965', 'n_tokens_mistral': 2378, 'n_tokens_neox': 2128, 'n_words': 448}