Advertisement
Guest User

Untitled

a guest
Mar 4th, 2016
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.29 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.BufferedWriter;
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileReader;
  6. import java.io.FileWriter;
  7. import java.io.IOException;
  8. import java.util.ArrayList;
  9.  
  10. import javax.xml.bind.JAXBException;
  11.  
  12. import org.docx4j.XmlUtils;
  13. import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
  14. import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
  15. import org.docx4j.openpackaging.parts.relationships.Namespaces;
  16. import org.docx4j.samples.AbstractSample;
  17. import org.docx4j.wml.P;
  18. import org.docx4j.wml.Tbl;
  19.  
  20. public class AllemandWord extends AbstractSample {
  21.  
  22. static int sequence = 0;
  23. public static String currentGermanWord = "";
  24. public static String currentFrenchWord = "";
  25. public static ArrayList<String> listOfGermanWords = new ArrayList<String>();
  26. public static ArrayList<String> listOfFrenchWords = new ArrayList<String>();
  27.  
  28. public static ArrayList<String> alAll = new ArrayList<String>();
  29. public static ArrayList<String> frAll = new ArrayList<String>();
  30. public static ArrayList<String> alDer = new ArrayList<String>();
  31. public static ArrayList<String> frDer = new ArrayList<String>();
  32. public static ArrayList<String> alDas = new ArrayList<String>();
  33. public static ArrayList<String> frDas = new ArrayList<String>();
  34. public static ArrayList<String> alDie = new ArrayList<String>();
  35. public static ArrayList<String> frDie = new ArrayList<String>();
  36.  
  37. static MainDocumentPart mdp;
  38. 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>";
  39. final static String tableauAlPt1 = "<w:p>"+ "<w:pPr>"+ "<w:jc w:val=\"right\"/>"+"</w:pPr>" + "<w:r>"+ "<w:t>";
  40. final static String tableauAlPt2 = "</w:t>"+"</w:r>"+"</w:p>";
  41. final static String tableauPt2 = "</w:tc>"+ "<w:tc>" + "<w:tcPr>"+ "<w:tcW w:type=\"dxa\" w:w=\"4606\"/>"+"</w:tcPr>";
  42. final static String tableauFrPt1 = "<w:p>"+ "<w:r>"+ "<w:t>";
  43. final static String tableauFrPt2 = "</w:t>"+"</w:r>"+"</w:p>";
  44. final static String tableauPt3 = "</w:tc>"+"</w:tr>"+"</w:tbl>";
  45.  
  46. static String openXML = "<w:p xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\"/>";
  47.  
  48. public static void main(String[] args) throws Exception {
  49.  
  50. new File(System.getProperty("user.home") + "Allemand vocab").mkdir();
  51. Fenetre fen = new Fenetre();
  52. }
  53.  
  54. public static void createTextFile(File file) {
  55. try {
  56. file.createNewFile();
  57. } catch (IOException e) {
  58. e.printStackTrace();
  59. }
  60. }
  61.  
  62. public static void sortWords() {
  63. for (int i = 0; i < listOfGermanWords.size() ; i++) {
  64. String motAl = (String) listOfGermanWords.get(i);
  65. String motFr = (String) listOfFrenchWords.get(i);
  66. if (motAl.startsWith("Der ")) {
  67. alDer.add(motAl);
  68. frDer.add(motFr);
  69. } else if (motAl.startsWith("Das ")) {
  70. alDas.add(motAl);
  71. frDas.add(motFr);
  72. } else if (motAl.startsWith("Die ")) {
  73. alDie.add(motAl);
  74. frDie.add(motFr);
  75. } else {
  76. alAll.add(motAl);
  77. frAll.add(motFr);
  78. }
  79. }
  80. try {
  81. createDoc();
  82. } catch (Exception e) {
  83. e.printStackTrace();
  84. }
  85. }
  86.  
  87. public static void writeToFile(File file, ArrayList<String> typeOfWords) {
  88. FileWriter fw = null;
  89. try {
  90. fw = new FileWriter(file.getAbsoluteFile(), true);
  91. } catch (IOException e) {}
  92. BufferedWriter bw = new BufferedWriter(fw);
  93. for (int i=0; i < typeOfWords.size(); i++) {
  94. try {
  95. bw.write(typeOfWords.get(i) + "\n");
  96. } catch (IOException e) {
  97. e.printStackTrace();
  98. }
  99. }
  100.  
  101. try {
  102. bw.close();
  103. } catch (IOException e) {}
  104.  
  105. }
  106.  
  107. public static String[] readFromFile(File file) {
  108. ArrayList<String> plArray= new ArrayList<String>();
  109. try (BufferedReader br = new BufferedReader(new FileReader(file))) {
  110. String line;
  111. while ((line = br.readLine()) != null) {
  112. boolean checkDuplicate = false;
  113.  
  114. if(file.getName().endsWith("Al.txt")) {
  115. for (int i = 0; i < plArray.size(); i++) {
  116. if (line.equals(plArray.get(i)))
  117. checkDuplicate = true;
  118. }
  119. }
  120.  
  121.  
  122. if (!checkDuplicate)
  123. plArray.add(line);
  124. else
  125. plArray.add("Duplication - ce mot était déjà entré");
  126. }
  127. } catch (FileNotFoundException e) {
  128. e.printStackTrace();
  129. } catch (IOException e) {
  130. e.printStackTrace();
  131. }
  132. String[] plString = new String[plArray.size()];
  133. plString = plArray.toArray(plString);
  134. return plString;
  135. }
  136.  
  137. public static void createTable(String[] al, String[] fr){
  138. String tableau = tableauPt1;
  139. for (int i = 0; i < al.length; i++) {
  140. tableau += tableauAlPt1;
  141. tableau += al[i];
  142. tableau += tableauAlPt2;
  143. }
  144. tableau += tableauPt2;
  145. for (int i = 0; i < fr.length; i++) {
  146. tableau += tableauFrPt1;
  147. tableau += fr[i];
  148. tableau += tableauFrPt2;
  149. }
  150. tableau += tableauPt3;
  151. try {
  152. System.out.println(tableau);
  153. Tbl tbl = (Tbl)XmlUtils.unmarshalString(tableau);
  154. if(al.length != 0) {
  155. try {
  156. mdp.addObject(tbl);
  157. P p2 = (P)XmlUtils.unmarshalString(openXML);
  158. mdp.addObject(p2);
  159. } catch (NullPointerException e) {
  160. System.out.println("null pointer exception");
  161. }
  162. }
  163. } catch (JAXBException e) {
  164. System.out.println("JAXB exception");
  165. }
  166.  
  167. }
  168.  
  169. public static void createDoc() throws Exception {
  170. System.out.println("Création des fichiers");
  171. new File(System.getProperty("user.home") + "/Allemand vocab").mkdir();
  172. new File(System.getProperty("user.home") + "/Allemand vocab/" + "AllemandVocab" + String.valueOf(sequence)).mkdir();
  173. String pathForFiles = System.getProperty("user.home") + "/Allemand vocab/" + "AllemandVocab"
  174. + String.valueOf(sequence) + "/";
  175. System.out.println(pathForFiles);
  176. File DerAl = new File(pathForFiles + "DerAl.txt");
  177. File DerFr = new File(pathForFiles + "DerFr.txt");
  178. File DasAl = new File(pathForFiles + "DasAl.txt");
  179. File DasFr = new File(pathForFiles + "DasFr.txt");
  180. File DieAl = new File(pathForFiles + "DieAl.txt");
  181. File DieFr = new File(pathForFiles + "DieFr.txt");
  182. File AllAl = new File(pathForFiles + "AllAl.txt");
  183. File AllFr = new File(pathForFiles + "AllFr.txt");
  184. createTextFile(DerAl);
  185. createTextFile(DerFr);
  186. createTextFile(DasAl);
  187. createTextFile(DasFr);
  188. createTextFile(DieAl);
  189. createTextFile(DieFr);
  190. createTextFile(AllAl);
  191. createTextFile(AllFr);
  192.  
  193. System.out.println("Sauvegarde des mots");
  194.  
  195. writeToFile(DerAl, alDer);
  196. writeToFile(DerFr, frDer);
  197. writeToFile(DasAl, alDas);
  198. writeToFile(DasFr, frDas);
  199. writeToFile(DieAl, alDie);
  200. writeToFile(DieFr, frDie);
  201. writeToFile(AllAl, alAll);
  202. writeToFile(AllFr, frAll);
  203.  
  204. System.out.println("Création du document en cours");
  205.  
  206. //Création des arrays
  207. String[] strAlAll = readFromFile(AllAl);
  208. String[] strFrAll = readFromFile(AllFr);
  209. /*String[] strAlDer = new String[alDer.size()];
  210. strAlDer = alDer.toArray(strAlDer);*/
  211. String[] strAlDer = readFromFile(DerAl);
  212. String[] strFrDer = readFromFile(DerFr);
  213. String[] strAlDas = readFromFile(DasAl);
  214. String[] strFrDas = readFromFile(DasFr);
  215. String[] strAlDie = readFromFile(DieAl);
  216. String[] strFrDie = readFromFile(DieFr);
  217.  
  218. System.out.println("Création des arrays réussie");
  219.  
  220. WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
  221. mdp = wordMLPackage.getMainDocumentPart();
  222.  
  223. createTable(strAlDer, strFrDer);
  224. createTable(strAlDas, strFrDas);
  225. createTable(strAlDie, strFrDie);
  226. createTable(strAlAll, strFrAll);
  227.  
  228. //Save the file
  229. String filename = System.getProperty("user.home") + "/Desktop/Allemand vocab " + sequence + ".docx";
  230. wordMLPackage.save(new java.io.File(filename) );
  231. System.out.println("Saved " + filename);
  232. System.exit(0);
  233. }
  234.  
  235. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement