package in.twizmwaz.cardinal.util;

import org.bukkit.Bukkit;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;

import java.io.File;
import java.io.IOException;
import java.util.logging.Level;

public class DomUtils {

    public static Document parse(File file) throws JDOMException, IOException {
        SAXBuilder saxBuilder = new SAXBuilder();
        Document original = saxBuilder.build(file);
        for (Element include : original.getRootElement().getChildren("include")) {
            boolean found = false;
            File path = file.getParentFile();
            String source = include.getAttributeValue("src");
            File including = new File(path, source);
            if (including.exists()) {
                found = true;
                try {
                    for (Element element : parse(including).getRootElement().getChildren()) {
                        original.getRootElement().addContent(element.clone().detach());
                    }
                } catch (JDOMException | IOException ignored) {}
            } else {
                while (source.startsWith("../")) {
                    source = source.replace("../", "");
                }
                including = new File(path, source);
                if (including.exists()) {
                    found = true;
                    try {
                        for (Element element : parse(including).getRootElement().getChildren()) {
                            original.getRootElement().addContent(element.clone().detach());
                        }
                    } catch (JDOMException | IOException ignored) {
                    }
                }
                including = new File(path.getParentFile(), source);
                if (including.exists()) {
                    found = true;
                    try {
                        for (Element element : parse(including).getRootElement().getChildren()) {
                            original.getRootElement().addContent(element.clone().detach());
                        }
                    } catch (JDOMException | IOException ignored) {
                    }
                }
            }
            if (!found) Bukkit.getLogger().log(Level.WARNING, "File '" + including.getName() + "' was not found nor included!");
        }
        return original;
    }
}
@@ -29,16 +29,56 @@
 .util;%0A%0A
+import com.google.common.collect.Lists;%0A
 import o
@@ -261,24 +261,47 @@
 OException;%0A
+import java.util.List;%0A
 import java.
@@ -524,24 +524,79 @@
 uild(file);%0A
+        List%3CString%3E toInclude = Lists.newArrayList();%0A
         for 
@@ -663,18 +663,16 @@
 clude%22))
- %7B
 %0A       
@@ -676,16 +676,120 @@
         
+toInclude.add(include.getAttributeValue(%22src%22));%0A        for (String include : toInclude) %7B%0A            
 boolean 
@@ -850,70 +850,8 @@
 ile(
-);%0A            String source = include.getAttributeValue(%22src%22
 );%0A 
@@ -885,37 +885,38 @@
  new File(path, 
-sourc
+includ
 e);%0A            
@@ -1306,21 +1306,22 @@
  while (
-sourc
+includ
 e.starts
@@ -1359,22 +1359,24 @@
     
-source = sourc
+include = includ
 e.re
@@ -1454,21 +1454,22 @@
 e(path, 
-sourc
+includ
 e);%0A    
@@ -1953,13 +1953,14 @@
 (), 
-sourc
+includ
 e);%0A

Use patch to fix CME