Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.util.ArrayList;
- import javax.xml.bind.JAXBException;
- import org.docx4j.XmlUtils;
- import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
- import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
- import org.docx4j.openpackaging.parts.relationships.Namespaces;
- import org.docx4j.samples.AbstractSample;
- import org.docx4j.wml.P;
- import org.docx4j.wml.Tbl;
- public class AllemandWord extends AbstractSample {
- static int sequence = 0;
- public static String currentGermanWord = "";
- public static String currentFrenchWord = "";
- public static ArrayList<String> listOfGermanWords = new ArrayList<String>();
- public static ArrayList<String> listOfFrenchWords = new ArrayList<String>();
- public static ArrayList<String> alAll = new ArrayList<String>();
- public static ArrayList<String> frAll = new ArrayList<String>();
- public static ArrayList<String> alDer = new ArrayList<String>();
- public static ArrayList<String> frDer = new ArrayList<String>();
- public static ArrayList<String> alDas = new ArrayList<String>();
- public static ArrayList<String> frDas = new ArrayList<String>();
- public static ArrayList<String> alDie = new ArrayList<String>();
- public static ArrayList<String> frDie = new ArrayList<String>();
- static MainDocumentPart mdp;
- final static String tableauPt1 = "<w:tbl xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" xmlns:xml=\"http://www.w3.org/XML/1998/namespace\">"+ "<w:tblPr " + Namespaces.W_NAMESPACE_DECLARATION + ">"+ "<w:tblStyle w:val=\"TableGrid\"/>"+ "<w:tblW w:w=\"0\" w:type=\"auto\"/>"+ "<w:tblLook w:val=\"04A0\"/>"+ "</w:tblPr>"+ "<w:tblGrid>" + "<w:gridCol w:w=\"4606\"/>" + "<w:gridCol w:w=\"4606\"/>" +"</w:tblGrid>"+ "<w:tr w:rsidTr=\"00074D40\">"+ "<w:tc>"+ "<w:tcPr>"+ "<w:tcW w:type=\"dxa\" w:w=\"4606\"/>"+"</w:tcPr>";
- final static String tableauAlPt1 = "<w:p>"+ "<w:pPr>"+ "<w:jc w:val=\"right\"/>"+"</w:pPr>" + "<w:r>"+ "<w:t>";
- final static String tableauAlPt2 = "</w:t>"+"</w:r>"+"</w:p>";
- final static String tableauPt2 = "</w:tc>"+ "<w:tc>" + "<w:tcPr>"+ "<w:tcW w:type=\"dxa\" w:w=\"4606\"/>"+"</w:tcPr>";
- final static String tableauFrPt1 = "<w:p>"+ "<w:r>"+ "<w:t>";
- final static String tableauFrPt2 = "</w:t>"+"</w:r>"+"</w:p>";
- final static String tableauPt3 = "</w:tc>"+"</w:tr>"+"</w:tbl>";
- static String openXML = "<w:p xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\"/>";
- public static void main(String[] args) throws Exception {
- new File(System.getProperty("user.home") + "Allemand vocab").mkdir();
- Fenetre fen = new Fenetre();
- }
- public static void createTextFile(File file) {
- try {
- file.createNewFile();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public static void sortWords() {
- for (int i = 0; i < listOfGermanWords.size() ; i++) {
- String motAl = (String) listOfGermanWords.get(i);
- String motFr = (String) listOfFrenchWords.get(i);
- if (motAl.startsWith("Der ")) {
- alDer.add(motAl);
- frDer.add(motFr);
- } else if (motAl.startsWith("Das ")) {
- alDas.add(motAl);
- frDas.add(motFr);
- } else if (motAl.startsWith("Die ")) {
- alDie.add(motAl);
- frDie.add(motFr);
- } else {
- alAll.add(motAl);
- frAll.add(motFr);
- }
- }
- try {
- createDoc();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public static void writeToFile(File file, ArrayList<String> typeOfWords) {
- FileWriter fw = null;
- try {
- fw = new FileWriter(file.getAbsoluteFile(), true);
- } catch (IOException e) {}
- BufferedWriter bw = new BufferedWriter(fw);
- for (int i=0; i < typeOfWords.size(); i++) {
- try {
- bw.write(typeOfWords.get(i) + "\n");
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- try {
- bw.close();
- } catch (IOException e) {}
- }
- public static String[] readFromFile(File file) {
- ArrayList<String> plArray= new ArrayList<String>();
- try (BufferedReader br = new BufferedReader(new FileReader(file))) {
- String line;
- while ((line = br.readLine()) != null) {
- boolean checkDuplicate = false;
- if(file.getName().endsWith("Al.txt")) {
- for (int i = 0; i < plArray.size(); i++) {
- if (line.equals(plArray.get(i)))
- checkDuplicate = true;
- }
- }
- if (!checkDuplicate)
- plArray.add(line);
- else
- plArray.add("Duplication - ce mot était déjà entré");
- }
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- String[] plString = new String[plArray.size()];
- plString = plArray.toArray(plString);
- return plString;
- }
- public static void createTable(String[] al, String[] fr){
- String tableau = tableauPt1;
- for (int i = 0; i < al.length; i++) {
- tableau += tableauAlPt1;
- tableau += al[i];
- tableau += tableauAlPt2;
- }
- tableau += tableauPt2;
- for (int i = 0; i < fr.length; i++) {
- tableau += tableauFrPt1;
- tableau += fr[i];
- tableau += tableauFrPt2;
- }
- tableau += tableauPt3;
- try {
- System.out.println(tableau);
- Tbl tbl = (Tbl)XmlUtils.unmarshalString(tableau);
- if(al.length != 0) {
- try {
- mdp.addObject(tbl);
- P p2 = (P)XmlUtils.unmarshalString(openXML);
- mdp.addObject(p2);
- } catch (NullPointerException e) {
- System.out.println("null pointer exception");
- }
- }
- } catch (JAXBException e) {
- System.out.println("JAXB exception");
- }
- }
- public static void createDoc() throws Exception {
- System.out.println("Création des fichiers");
- new File(System.getProperty("user.home") + "/Allemand vocab").mkdir();
- new File(System.getProperty("user.home") + "/Allemand vocab/" + "AllemandVocab" + String.valueOf(sequence)).mkdir();
- String pathForFiles = System.getProperty("user.home") + "/Allemand vocab/" + "AllemandVocab"
- + String.valueOf(sequence) + "/";
- System.out.println(pathForFiles);
- File DerAl = new File(pathForFiles + "DerAl.txt");
- File DerFr = new File(pathForFiles + "DerFr.txt");
- File DasAl = new File(pathForFiles + "DasAl.txt");
- File DasFr = new File(pathForFiles + "DasFr.txt");
- File DieAl = new File(pathForFiles + "DieAl.txt");
- File DieFr = new File(pathForFiles + "DieFr.txt");
- File AllAl = new File(pathForFiles + "AllAl.txt");
- File AllFr = new File(pathForFiles + "AllFr.txt");
- createTextFile(DerAl);
- createTextFile(DerFr);
- createTextFile(DasAl);
- createTextFile(DasFr);
- createTextFile(DieAl);
- createTextFile(DieFr);
- createTextFile(AllAl);
- createTextFile(AllFr);
- System.out.println("Sauvegarde des mots");
- writeToFile(DerAl, alDer);
- writeToFile(DerFr, frDer);
- writeToFile(DasAl, alDas);
- writeToFile(DasFr, frDas);
- writeToFile(DieAl, alDie);
- writeToFile(DieFr, frDie);
- writeToFile(AllAl, alAll);
- writeToFile(AllFr, frAll);
- System.out.println("Création du document en cours");
- //Création des arrays
- String[] strAlAll = readFromFile(AllAl);
- String[] strFrAll = readFromFile(AllFr);
- /*String[] strAlDer = new String[alDer.size()];
- strAlDer = alDer.toArray(strAlDer);*/
- String[] strAlDer = readFromFile(DerAl);
- String[] strFrDer = readFromFile(DerFr);
- String[] strAlDas = readFromFile(DasAl);
- String[] strFrDas = readFromFile(DasFr);
- String[] strAlDie = readFromFile(DieAl);
- String[] strFrDie = readFromFile(DieFr);
- System.out.println("Création des arrays réussie");
- WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
- mdp = wordMLPackage.getMainDocumentPart();
- createTable(strAlDer, strFrDer);
- createTable(strAlDas, strFrDas);
- createTable(strAlDie, strFrDie);
- createTable(strAlAll, strFrAll);
- //Save the file
- String filename = System.getProperty("user.home") + "/Desktop/Allemand vocab " + sequence + ".docx";
- wordMLPackage.save(new java.io.File(filename) );
- System.out.println("Saved " + filename);
- System.exit(0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement