192 lines
6.9 KiB
Java
192 lines
6.9 KiB
Java
package outils;
|
|
|
|
import java.io.BufferedOutputStream;
|
|
import java.io.BufferedReader;
|
|
import java.io.BufferedWriter;
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.io.InputStreamReader;
|
|
import java.io.OutputStream;
|
|
import java.io.Reader;
|
|
import java.net.URI;
|
|
import java.net.URISyntaxException;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.nio.file.FileSystem;
|
|
import java.nio.file.FileSystems;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Path;
|
|
import java.nio.file.Paths;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
public class resources {
|
|
|
|
public resources() {
|
|
|
|
}
|
|
|
|
/**
|
|
* Liste des resources
|
|
* @throws URISyntaxException
|
|
* @throws IOException
|
|
*/
|
|
public void ListResources() throws URISyntaxException, IOException {
|
|
System.out.println();
|
|
System.out.println("Ci-dessous la liste des ressources.");
|
|
System.out.println("Une ressource peut être composée par plusieurs fichiers (.xml, .pdf, .ods, odt)");
|
|
System.out.println();
|
|
URI uri = this.getClass().getResource("").toURI();
|
|
Map<String, String> env = new HashMap<>();
|
|
try (FileSystem zipfs = FileSystems.newFileSystem(uri, env)) {
|
|
for (Path path : zipfs.getRootDirectories()) {
|
|
Files.list(path.resolve("")).filter(s -> s.toString().endsWith(".xml"))
|
|
.forEach(p -> System.out.println("\t* " + p));
|
|
Files.list(path.resolve("")).filter(s -> s.toString().endsWith(".pdf"))
|
|
.forEach(p -> System.out.println("\t* " + p));
|
|
Files.list(path.resolve("")).filter(s -> s.toString().endsWith(".ods"))
|
|
.forEach(p -> System.out.println("\t* " + p));
|
|
Files.list(path.resolve("")).filter(s -> s.toString().endsWith(".odt"))
|
|
.forEach(p -> System.out.println("\t* " + p));
|
|
}
|
|
}
|
|
System.out.println();
|
|
System.out.println("Pour obtenir les fichiers d'une seule ressource, vous devez utiliser la commande \"-get\".");
|
|
System.out.println("\tExemple : \"-get pyramide\".");
|
|
System.out.println("Pour analyser les fichiers des étudiants avec une ressource, vous devez taper le nom de la ressource.");
|
|
System.out.println("\tExemple du code complet avec la commande : \"java -jar analyseCalc.jar pyramide\".");
|
|
System.out.println();
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param fichierResource
|
|
* @param path
|
|
* @throws IOException
|
|
*/
|
|
public void getLicence(String path) throws IOException {
|
|
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
|
|
InputStream is = classLoader.getResourceAsStream("licence.txt");
|
|
|
|
if(is!=null) {
|
|
Reader initialReader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
|
|
char[] arr = new char[8 * 1024];
|
|
StringBuilder buffer = new StringBuilder();
|
|
int numCharsRead;
|
|
while ((numCharsRead = initialReader.read(arr, 0, arr.length)) != -1) {
|
|
buffer.append(arr, 0, numCharsRead);
|
|
}
|
|
initialReader.close();
|
|
|
|
Path outputFilePath = Paths.get(path + "/" + "licence.txt");
|
|
BufferedWriter fichier = Files.newBufferedWriter(outputFilePath, StandardCharsets.UTF_8);
|
|
fichier.write(buffer.toString());
|
|
fichier.close();
|
|
System.out.println("Ecriture terminé du fichier : " + "licence.txt" );
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Ecrit dans le réperoire courant le fichier resource demandé
|
|
* @param fichierResource
|
|
* @param path
|
|
* @throws IOException
|
|
*/
|
|
public void getResource(String fichierResource, String path) throws IOException {
|
|
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
|
|
InputStream is = classLoader.getResourceAsStream(fichierResource +".xml");
|
|
|
|
if(is!=null) {
|
|
Reader initialReader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
|
|
char[] arr = new char[8 * 1024];
|
|
StringBuilder buffer = new StringBuilder();
|
|
int numCharsRead;
|
|
while ((numCharsRead = initialReader.read(arr, 0, arr.length)) != -1) {
|
|
buffer.append(arr, 0, numCharsRead);
|
|
}
|
|
initialReader.close();
|
|
|
|
Path outputFilePath = Paths.get(path + "/" + fichierResource +".xml");
|
|
BufferedWriter fichier = Files.newBufferedWriter(outputFilePath, StandardCharsets.UTF_8);
|
|
fichier.write(buffer.toString());
|
|
fichier.close();
|
|
System.out.println("Ecriture terminé du fichier : " + fichierResource + ".xml" );
|
|
getExercice(fichierResource,path,".pdf");
|
|
getExercice(fichierResource,path,".ods");
|
|
getExercice(fichierResource,path,"(vide).ods");
|
|
getExercice(fichierResource,path,".odt");
|
|
}else {
|
|
System.out.println("ERREUR, il n'y a pas de resource nommée :" + fichierResource );
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Ecrit dans le réperoire courant le fichier exercice demandé
|
|
* @param fichierResource
|
|
* @param path
|
|
* @throws IOException
|
|
*/
|
|
private void getExercice(String fichierResource, String path, String ext) throws IOException {
|
|
|
|
try {
|
|
File myFile = new File((fichierResource+ext));
|
|
if (!myFile.exists()) {
|
|
// In JAR
|
|
InputStream inputStream = ClassLoader.getSystemClassLoader().getResourceAsStream(fichierResource+ext);
|
|
if(inputStream!=null) {
|
|
// Copy file
|
|
Path p = Paths.get(path+"/"+fichierResource+ext);
|
|
OutputStream out = new BufferedOutputStream(Files.newOutputStream(p));
|
|
byte[] buffer = new byte[1024];
|
|
int length;
|
|
while ((length = inputStream.read(buffer)) > 0) {
|
|
out.write(buffer, 0, length);
|
|
}
|
|
out.close();
|
|
inputStream.close();
|
|
System.out.println("Ecriture terminé du fichier : " + fichierResource + ext );
|
|
}
|
|
}else {
|
|
System.out.println("Il n'y a pas de ressource : " + fichierResource + ext );
|
|
}
|
|
} catch (IOException ex) {
|
|
System.err.println(ex);
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void getHelp(String path) throws IOException {
|
|
|
|
try {
|
|
File myFile = new File(("help.pdf"));
|
|
if (!myFile.exists()) {
|
|
// In JAR
|
|
InputStream inputStream = ClassLoader.getSystemClassLoader().getResourceAsStream("help.pdf");
|
|
// Copy file
|
|
Path p = Paths.get(path+"/help.pdf");
|
|
OutputStream out = new BufferedOutputStream(Files.newOutputStream(p));
|
|
byte[] buffer = new byte[1024];
|
|
int length;
|
|
while ((length = inputStream.read(buffer)) > 0) {
|
|
out.write(buffer, 0, length);
|
|
}
|
|
out.close();
|
|
inputStream.close();
|
|
System.out.println("Vous avez dans le répertoire de travail le fichier \"help.pdf\"." );
|
|
}
|
|
} catch (IOException ex) {
|
|
System.err.println(ex);
|
|
}
|
|
|
|
}
|
|
|
|
|
|
}
|