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.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Path;

public class DomUtils {

    public static Document parse(File file) throws JDOMException, IOException {
        SAXBuilder saxBuilder = new SAXBuilder();
        Document original = saxBuilder.build(file);
        Path repo = file.getParentFile().getParentFile().toPath();
        for (Element include : original.getRootElement().getChildren("include")) {
            try {
                Path included;
                try {
                    included = repo.resolve(include.getAttributeValue("src")).normalize();
                    for (Element element : parse(included.toFile()).getRootElement().getChildren()) {
                        original.getRootElement().addContent(element.detach());
                    }
                } catch (FileNotFoundException e) {
                    included = file.getParentFile().toPath().resolve(include.getAttributeValue("src")).normalize();
                    for (Element element : parse(included.toFile()).getRootElement().getChildren()) {
                        original.getRootElement().addContent(element.detach());
                        Bukkit.getLogger().info(element.getName());
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return original;
    }
}

@@ -212,28 +212,18 @@
 java.io.
-FileNotFound
+IO
 Exceptio
@@ -241,49 +241,26 @@
 ava.
-io.IOException;%0Aimport java.nio.file.Path
+util.logging.Level
 ;%0A%0Ap
@@ -477,64 +477,35 @@
     
-Path repo = file.getParentFile().getParentFile().toPath(
+Bukkit.getLogger().info(%221%22
 );%0A 
@@ -602,111 +602,154 @@
     
-try %7B%0A                Path included;%0A                try %7B%0A                    included = repo.resolve(
+Bukkit.getLogger().info(%222%22);%0A            File path = file.getParentFile();%0A            Bukkit.getLogger().info(%223%22);%0A            String source = 
 incl
@@ -772,37 +772,24 @@
 Value(%22src%22)
-).normalize()
 ;%0A          
@@ -794,206 +794,194 @@
     
-        for (Element element : parse(included.toFile()).getRootElement().getChildren()) %7B%0A                        original.getRootElement().addContent(element.detach());%0A                
+Bukkit.getLogger().info(%224%22);%0A            while (source.contains(%22../%22)) %7B%0A                source = source.replace(%22../%22, %22%22);%0A                path = path.getParentFile();%0A
     
-%7D%0A
+  
       
+%7D%0A
     
@@ -990,164 +990,153 @@
     
-%7D catch (FileNotFoundException e) %7B%0A                    included = file.getParentFile().toPath().resolve(include.getAttributeValue(%22src%22)).normalize();%0A    
+  Bukkit.getLogger().info(%225%22);%0A            File including = new File(path, source);%0A            Bukkit.getLogger().info(%226%22);%0A            try %7B%0A
     
@@ -1186,19 +1186,11 @@
 clud
-ed.toFile()
+ing
 ).ge
@@ -1245,48 +1245,31 @@
     
-    original.getRootElement().addContent
+Bukkit.getLogger().info
 (ele
@@ -1277,16 +1277,22 @@
 ent.
-detach()
+getName() + %22%22
 );%0A 
@@ -1314,35 +1314,44 @@
     
-    Bukkit.getLogger().info
+original.getRootElement().addContent
 (ele
@@ -1359,41 +1359,18 @@
 ent.
-getName());%0A                    %7D
+detach());
 %0A   
@@ -1405,16 +1405,34 @@
  catch (
+JDOMException %7C IO
 Exceptio
@@ -1458,26 +1458,110 @@
     
-e.printStackTrace(
+Bukkit.getLogger().log(Level.WARNING, %22File '%22 + including.getName() + %22' was not included correctly!%22
 );%0A 

Work on includes, left debug messages inside (for later)