package org.hildan.livedoc.springmvc.controller;

import java.util.List;

import org.hildan.livedoc.core.pojo.JSONDoc;
import org.hildan.livedoc.core.pojo.JSONDoc.MethodDisplay;
import org.hildan.livedoc.core.scanner.JSONDocScanner;
import org.hildan.livedoc.springmvc.scanner.Spring3JSONDocScanner;
import org.hildan.livedoc.springmvc.scanner.Spring4JSONDocScanner;
import org.springframework.core.SpringVersion;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class JSONDocController {
    private String version;

    private String basePath;

    private List<String> packages;

    private JSONDocScanner jsondocScanner;

    private boolean playgroundEnabled = true;

    private MethodDisplay displayMethodAs = MethodDisplay.URI;

    public final static String JSONDOC_DEFAULT_PATH = "/jsondoc";

    private final static Integer SPRING_VERSION_3_X = 3;

    public JSONDocController(String version, String basePath, List<String> packages) {
        this.version = version;
        this.basePath = basePath;
        this.packages = packages;
        String springVersion = SpringVersion.getVersion();
        if (springVersion != null && !springVersion.isEmpty()) {
            Integer majorSpringVersion = Integer.parseInt(springVersion.split("\\.")[0]);
            if (majorSpringVersion > SPRING_VERSION_3_X) {
                this.jsondocScanner = new Spring4JSONDocScanner();
            } else {
                this.jsondocScanner = new Spring3JSONDocScanner();
            }
        } else {
            try {
                Class.forName("org.springframework.web.bind.annotation.RestController");
                this.jsondocScanner = new Spring4JSONDocScanner();

            } catch (ClassNotFoundException e) {
                this.jsondocScanner = new Spring3JSONDocScanner();
            }
        }
    }

    public boolean isPlaygroundEnabled() {
        return playgroundEnabled;
    }

    public void setPlaygroundEnabled(boolean playgroundEnabled) {
        this.playgroundEnabled = playgroundEnabled;
    }

    public MethodDisplay getDisplayMethodAs() {
        return displayMethodAs;
    }

    public void setDisplayMethodAs(MethodDisplay displayMethodAs) {
        this.displayMethodAs = displayMethodAs;
    }

    @RequestMapping(value = JSONDocController.JSONDOC_DEFAULT_PATH, method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE)
    public @ResponseBody
    JSONDoc getApi() {
        return jsondocScanner.getJSONDoc(version, basePath, packages, playgroundEnabled, displayMethodAs);
    }

}

@@ -732,16 +732,17 @@
 oller %7B%0A
+%0A
     priv
@@ -1082,32 +1082,38 @@
  Integer SPRING_
+MAJOR_
 VERSION_3_X = 3;
@@ -1105,18 +1105,16 @@
 ERSION_3
-_X
  = 3;%0A%0A 
@@ -1215,95 +1215,637 @@
 this
-.version = version;%0A        this.basePath = basePath;%0A        this.packages = packages;
+(version, basePath, packages, getSpringJsonDocScanner());%0A    %7D%0A%0A    public JSONDocController(String version, String basePath, List%3CString%3E packages, JSONDocScanner jsonDocScanner) %7B%0A        this.version = version;%0A        this.basePath = basePath;%0A        this.packages = packages;%0A        this.jsondocScanner = jsonDocScanner;%0A    %7D%0A%0A    private static JSONDocScanner getSpringJsonDocScanner() %7B%0A        if (isSpring4OrMoreOnClasspath()) %7B%0A            return new Spring4JSONDocScanner();%0A        %7D else %7B%0A            return new Spring3JSONDocScanner();%0A        %7D%0A    %7D%0A%0A    private static boolean isSpring4OrMoreOnClasspath() %7B
 %0A   
@@ -2067,20 +2067,23 @@
         
-if (
+return 
 majorSpr
@@ -2102,16 +2102,22 @@
  SPRING_
+MAJOR_
 VERSION_
@@ -2121,165 +2121,83 @@
 ON_3
-_X) %7B%0A                this.jsondocScanner = new Spring4JSONDocScanner();%0A            %7D else %7B%0A                this.jsondocScanner = new Spring3JSONDocScanner
+;%0A        %7D else %7B%0A            return isRestControllerAnnotationOnClassPath
 ();%0A
@@ -2208,35 +2208,86 @@
     
+%7D%0A
     %7D%0A
+%0A
     
-    %7D else %7B%0A    
+private static boolean isRestControllerAnnotationOnClassPath() %7B%0A
     
@@ -2296,20 +2296,16 @@
   try %7B%0A
-    
         
@@ -2397,193 +2397,91 @@
     
-    this.jsondocScanner = new Spring4JSONDocScanner();%0A%0A            %7D catch (ClassNotFoundException e) %7B%0A                this.jsondocScanner = new Spring3JSONDocScanner();%0A            %7D
+return true;%0A        %7D catch (ClassNotFoundException e) %7B%0A            return false;
 %0A   

Make JsonDocScanner customizable in JsonDocController