Main.java 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. package com.up.smfmc;
  2. import java.io.BufferedReader;
  3. import java.io.File;
  4. import java.io.FileOutputStream;
  5. import java.io.FileReader;
  6. import java.io.IOException;
  7. import java.util.ArrayList;
  8. import java.util.HashMap;
  9. import java.util.zip.ZipEntry;
  10. import java.util.zip.ZipOutputStream;
  11. import javax.xml.parsers.DocumentBuilder;
  12. import javax.xml.parsers.DocumentBuilderFactory;
  13. import javax.xml.parsers.ParserConfigurationException;
  14. import org.w3c.dom.DOMException;
  15. import org.w3c.dom.Document;
  16. import org.w3c.dom.Node;
  17. import org.w3c.dom.NodeList;
  18. import org.xml.sax.SAXException;
  19. /**
  20. *
  21. * @author Ricky
  22. */
  23. public class Main {
  24. /**
  25. * @param args the command line arguments
  26. */
  27. public static void main(String[] args) {
  28. try {
  29. String root = args[0];
  30. Document info = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(root + "/src/package-info.xml");
  31. Node n = info.getElementsByTagName("package-info").item(0);
  32. String mod = createModFile(findNodeByName(n.getChildNodes(), "id").getFirstChild().getNodeValue(), findNodeByName(n.getChildNodes(), "version").getFirstChild().getNodeValue(), diffDir(root + "/smf", "."));
  33. File dz = new File(root + "/dist");
  34. if (dz.exists()) {
  35. for (File f : dz.listFiles()) f.delete();
  36. } else {
  37. dz.mkdir();
  38. }
  39. ZipOutputStream out = new ZipOutputStream(new FileOutputStream(root + "/dist/mod.zip"));
  40. out.putNextEntry(new ZipEntry("package-info.xml"));
  41. BufferedReader pir = new BufferedReader(new FileReader(root + "/src/package-info.xml"));
  42. while (pir.ready()) out.write((pir.readLine() + "/n").getBytes());
  43. out.putNextEntry(new ZipEntry("readme.txt"));
  44. BufferedReader rir = new BufferedReader(new FileReader(root + "/src/readme.txt"));
  45. while (rir.ready()) out.write((rir.readLine() + "/n").getBytes());
  46. out.putNextEntry(new ZipEntry("modification.xml"));
  47. out.write(mod.getBytes());
  48. out.close();
  49. } catch (ParserConfigurationException | SAXException | IOException | DOMException e) {
  50. e.printStackTrace();
  51. }
  52. }
  53. public static Node findNodeByName(NodeList nl, String name) {
  54. for (int i = 0; i < nl.getLength(); i++) if (nl.item(i).getNodeName().equals(name)) return nl.item(i);
  55. return null;
  56. }
  57. public static String createModFile(String id, String version, ArrayList<Diff> diffs) {
  58. String ret = "<?xml version=\"1.0\"?>\n" +
  59. "<!DOCTYPE modification SYSTEM \"http://www.simplemachines.org/xml/modification\">\n" +
  60. "<modification xmlns=\"http://www.simplemachines.org/xml/modification\" xmlns:smf=\"http://www.simplemachines.org/\">\n" +
  61. " <id>" + id + "</id>\n" +
  62. " <version>" + version + "</version>\n";
  63. for (Diff d : diffs) ret += d.toString() + "\n";
  64. ret += "</modification>";
  65. return ret;
  66. }
  67. public static ArrayList<Diff> diffDir(String root, String dir) throws IOException {
  68. ArrayList<Diff> diffs = new ArrayList<>();
  69. for (File f : new File(root + "/" + dir).listFiles()) {
  70. if (f.isDirectory()) {
  71. diffs.addAll(diffDir(root, dir + "/" + f.getName()));
  72. } else {
  73. diffs.addAll(getDiffs(root, dir + "/" + f.getName()));
  74. }
  75. }
  76. return diffs;
  77. }
  78. public static ArrayList<Diff> getDiffs(String root, String file) throws IOException {
  79. if (!new File(root + "/" + file).exists()) return new ArrayList<>();
  80. ArrayList<String> s1 = new ArrayList<>();
  81. ArrayList<String> s2 = new ArrayList<>();
  82. BufferedReader f1r = new BufferedReader(new FileReader("smf/" + file));
  83. while (f1r.ready()) s1.add(f1r.readLine());
  84. BufferedReader f2r = new BufferedReader(new FileReader(root + "/" + file));
  85. while (f2r.ready()) s2.add(f2r.readLine());
  86. return diff(s1.toArray(new String[0]), s2.toArray(new String[0]), file);
  87. }
  88. public static ArrayList<Diff> diff(String[] lines1, String[] lines2, String file) {
  89. if (linesContain(lines1, "?>")) {
  90. while (!lines1[lines1.length - 1].equals("?>") || lines1[lines1.length - 1].equals("")) {
  91. String[] temp = new String[lines1.length - 1];
  92. System.arraycopy(lines1, 0, temp, 0, lines1.length - 1);
  93. lines1 = temp;
  94. }
  95. String[] temp = new String[lines1.length - 1];
  96. System.arraycopy(lines1, 0, temp, 0, lines1.length - 1);
  97. lines1 = temp;
  98. }
  99. if (linesContain(lines2, "?>")) {
  100. while (!lines2[lines2.length - 1].equals("?>") || lines2[lines2.length - 1].equals("")) {
  101. String[] temp = new String[lines2.length - 1];
  102. System.arraycopy(lines2, 0, temp, 0, lines2.length - 1);
  103. lines2 = temp;
  104. }
  105. String[] temp = new String[lines2.length - 1];
  106. System.arraycopy(lines2, 0, temp, 0, lines2.length - 1);
  107. lines2 = temp;
  108. }
  109. ArrayList<Diff> diffs = new ArrayList<>();
  110. int i1 = 0;
  111. int i2 = 0;
  112. while (i1 < lines1.length && i2 < lines2.length) {
  113. if (!lines1[i1].equals(lines2[i2])) {
  114. Diff d = null;
  115. int start = i2;
  116. //Test for insert
  117. while (!linesMatch(lines1, lines2, i1, ++i2, 3)) {
  118. if (i2 >= lines2.length) {
  119. //Test for replace
  120. int start1 = i1;
  121. i1++;
  122. i2 = start;
  123. while (!linesMatch(lines1, lines2, i1, ++i2, 3)) {
  124. if (i2 >= lines2.length) {
  125. i1++;
  126. if (i1 >= lines1.length) break;
  127. i2 = start;
  128. }
  129. }
  130. if (i2 < lines2.length) {
  131. d = new Diff(file, join(lines1, "\n", start1, i1 - 1), join(lines2, "\n", start, i2 - 1), Diff.Method.replace);
  132. }
  133. //no replace? end then
  134. if (d == null) {
  135. d = new Diff(file, null, join(lines2, "\n", start, lines2.length - 1), Diff.Method.end);
  136. }
  137. break;
  138. }
  139. }
  140. if (d == null) {
  141. d = new Diff(file, join(lines1, "\n", Math.max(0, i1 - 3), i1), join(lines2, "\n", start, i2 - 1), Diff.Method.before);
  142. }
  143. diffs.add(d);
  144. }
  145. i1++; i2++;
  146. }
  147. if (i2 < lines2.length) {
  148. diffs.add(new Diff(file, null, join(lines2, "\n", i2, lines2.length - 1), Diff.Method.end));
  149. }
  150. return diffs;
  151. }
  152. public static String join(String[] arr, String glue, int start, int end) {
  153. String ret = "";
  154. for (int x = start; x <= end; x++) ret += arr[x] + glue;
  155. return ret.substring(0, ret.length() - glue.length());
  156. }
  157. public static boolean linesMatch(String[] arr1, String[] arr2, int start1, int start2, int length) {
  158. if (start1 >= arr1.length || start2 >= arr2.length) {
  159. return false;
  160. }
  161. for (int x = 0; x < length; x++) {
  162. if (start1 + x < arr1.length && start2 + x < arr2.length) {
  163. if(!arr1[start1 + x].equals(arr2[start2 + x])) {
  164. return false;
  165. }
  166. }
  167. }
  168. return true;
  169. }
  170. public static boolean linesContain(String[] arr, String line) {
  171. for (int x = 0; x < arr.length; x++) {
  172. if(arr[x].equals(line)) {
  173. return true;
  174. }
  175. }
  176. return false;
  177. }
  178. }