Main.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 org.w3c.dom.Document;
  14. import org.w3c.dom.Node;
  15. import org.w3c.dom.NodeList;
  16. /**
  17. *
  18. * @author Ricky
  19. */
  20. public class Main {
  21. /**
  22. * @param args the command line arguments
  23. */
  24. public static void main(String[] args) {
  25. try {
  26. String root = args[0];
  27. Document info = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(root + "/src/package-info.xml");
  28. Node n = info.getElementsByTagName("package-info").item(0);
  29. String mod = createModFile(findNodeByName(n.getChildNodes(), "id").getFirstChild().getNodeValue(), findNodeByName(n.getChildNodes(), "version").getFirstChild().getNodeValue(), diffDir(root + "/smf", "."));
  30. File dz = new File(root + "/dist");
  31. if (dz.exists()) {
  32. for (File f : dz.listFiles()) f.delete();
  33. } else {
  34. dz.mkdir();
  35. }
  36. ZipOutputStream out = new ZipOutputStream(new FileOutputStream(root + "/dist/mod.zip"));
  37. out.putNextEntry(new ZipEntry("package-info.xml"));
  38. BufferedReader pir = new BufferedReader(new FileReader(root + "/src/package-info.xml"));
  39. while (pir.ready()) out.write((pir.readLine() + "/n").getBytes());
  40. out.putNextEntry(new ZipEntry("readme.txt"));
  41. BufferedReader rir = new BufferedReader(new FileReader(root + "/src/readme.txt"));
  42. while (rir.ready()) out.write((rir.readLine() + "/n").getBytes());
  43. out.putNextEntry(new ZipEntry("modification.xml"));
  44. out.write(mod.getBytes());
  45. out.close();
  46. } catch (Exception e) {
  47. e.printStackTrace();
  48. }
  49. }
  50. public static Node findNodeByName(NodeList nl, String name) {
  51. for (int i = 0; i < nl.getLength(); i++) if (nl.item(i).getNodeName().equals(name)) return nl.item(i);
  52. return null;
  53. }
  54. public static String createModFile(String id, String version, ArrayList<Diff> diffs) {
  55. String ret = "<?xml version=\"1.0\"?>\n" +
  56. "<!DOCTYPE modification SYSTEM \"http://www.simplemachines.org/xml/modification\">\n" +
  57. "<modification xmlns=\"http://www.simplemachines.org/xml/modification\" xmlns:smf=\"http://www.simplemachines.org/\">\n" +
  58. " <id>" + id + "</id>\n" +
  59. " <version>" + version + "</version>\n";
  60. for (Diff d : diffs) ret += d.toString() + "\n";
  61. ret += "</modification>";
  62. return ret;
  63. }
  64. public static ArrayList<Diff> diffDir(String root, String dir) throws IOException {
  65. ArrayList<Diff> diffs = new ArrayList<>();
  66. for (File f : new File(root + "/" + dir).listFiles()) {
  67. if (f.isDirectory()) {
  68. diffs.addAll(diffDir(root, dir + "/" + f.getName()));
  69. } else {
  70. diffs.addAll(getDiffs(root, dir + "/" + f.getName()));
  71. }
  72. }
  73. return diffs;
  74. }
  75. public static ArrayList<Diff> getDiffs(String root, String file) throws IOException {
  76. if (!new File(root + "/" + file).exists()) return new ArrayList<>();
  77. ArrayList<String> s1 = new ArrayList<>();
  78. ArrayList<String> s2 = new ArrayList<>();
  79. BufferedReader f1r = new BufferedReader(new FileReader("smf/" + file));
  80. while (f1r.ready()) s1.add(f1r.readLine());
  81. BufferedReader f2r = new BufferedReader(new FileReader(root + "/" + file));
  82. while (f2r.ready()) s2.add(f2r.readLine());
  83. return diff(s1.toArray(new String[0]), s2.toArray(new String[0]), file);
  84. }
  85. public static ArrayList<Diff> diff(String[] lines1, String[] lines2, String file) {
  86. ArrayList<Diff> diffs = new ArrayList<>();
  87. int i1 = 0;
  88. int i2 = 0;
  89. while (i1 < lines1.length && i2 < lines2.length) {
  90. if (!lines1[i1].equals(lines2[i2])) {
  91. Diff d = null;
  92. int start = i2;
  93. //Test for insert
  94. while (!linesMatch(lines1, lines2, i1, ++i2, 2)) {
  95. if (i2 >= lines2.length) {
  96. //Test for replace
  97. //no replace? end then
  98. if (d == null) {
  99. d = new Diff(file, null, join(lines2, "\n", start, lines2.length - 1), Diff.Method.end);
  100. }
  101. break;
  102. }
  103. }
  104. // lines1[71].equals(lines2[75]);
  105. if (d == null) {
  106. d = new Diff(file, join(lines1, "\n", Math.max(0, i1 - 4), i1), join(lines2, "\n", start, i2 - 1), Diff.Method.before);
  107. }
  108. diffs.add(d);
  109. }
  110. i1++; i2++;
  111. }
  112. if (i2 < lines2.length) {
  113. diffs.add(new Diff(file, null, join(lines2, "\n", i2, lines2.length - 1), Diff.Method.end));
  114. }
  115. return diffs;
  116. }
  117. public static String join(String[] arr, String glue, int start, int end) {
  118. String ret = "";
  119. for (int x = start; x <= end; x++) ret += arr[x] + glue;
  120. return ret.substring(0, ret.length() - glue.length());
  121. }
  122. public static boolean linesMatch(String[] arr1, String[] arr2, int start1, int start2, int length) {
  123. for (int x = 0; x < length; x++) if (start1 + x < arr1.length && start2 + x < arr2.length) if(!arr1[start1 + x].equals(arr2[start2 + x])) return false;
  124. return true;
  125. }
  126. }