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")) {
            File path = file.getParentFile();
            String source = include.getAttributeValue("src");
            while (source.startsWith("../")) {
                source = source.replace("../", "");
                path = path.getParentFile();
            }
            File including = new File(path, source);
            try {
                for (Element element : parse(including).getRootElement().getChildren()) {
                    original.getRootElement().addContent(element.clone().detach());
                }
            } catch (JDOMException | IOException e) {
                Bukkit.getLogger().log(Level.WARNING, "File '" + including.getName() + "' was not found nor included!");
            }
        }
        return original;
    }
}

@@ -544,24 +544,59 @@
 nclude%22)) %7B%0A
+            boolean found = false;%0A
             
@@ -687,24 +687,461 @@
 lue(%22src%22);%0A
+            File including = new File(path, source);%0A            if (including.exists()) %7B%0A                found = true;%0A                try %7B%0A                    for (Element element : parse(including).getRootElement().getChildren()) %7B%0A                        original.getRootElement().addContent(element.clone().detach());%0A                    %7D%0A                %7D catch (JDOMException %7C IOException ignored) %7B%7D%0A            %7D else %7B%0A    
             
@@ -1171,24 +1171,28 @@
 h(%22../%22)) %7B%0A
+    
             
@@ -1251,67 +1251,503 @@
     
-path = path.getParentFile();%0A            %7D%0A            File
+%7D%0A                including = new File(path, source);%0A                if (including.exists()) %7B%0A                    found = true;%0A                    try %7B%0A                        for (Element element : parse(including).getRootElement().getChildren()) %7B%0A                            original.getRootElement().addContent(element.clone().detach());%0A                        %7D%0A                    %7D catch (JDOMException %7C IOException ignored) %7B%0A                    %7D%0A                %7D%0A               
  inc
@@ -1760,32 +1760,48 @@
  = new File(path
+.getParentFile()
 , source);%0A     
@@ -1799,38 +1799,130 @@
 e);%0A            
-try %7B%0A
+    if (including.exists()) %7B%0A                    found = true;%0A                    try %7B%0A        
                 
@@ -2007,32 +2007,40 @@
                 
+        
 original.getRoot
@@ -2095,34 +2095,44 @@
                 
-%7D%0A
+        %7D%0A  
             %7D ca
@@ -2119,32 +2119,38 @@
 %7D%0A              
+      
 %7D catch (JDOMExc
@@ -2170,17 +2170,23 @@
 ception 
-e
+ignored
 ) %7B%0A    
@@ -2196,16 +2196,78 @@
         
+     %7D%0A                %7D%0A            %7D%0A            if (!found)
  Bukkit.
@@ -2364,30 +2364,16 @@
 ded!%22);%0A
-            %7D%0A
         

Check in more locations for included files