11package io .swagger .codegen .v3 .templates ;
22
3- import com .github .jknack .handlebars .io .FileTemplateLoader ;
4- import org .apache .commons .lang3 .StringUtils ;
3+ import com .github .jknack .handlebars .io .URLTemplateLoader ;
54
65import java .io .File ;
76import java .io .IOException ;
87import java .net .URL ;
98
10- public class CodegenTemplateLoader extends FileTemplateLoader {
9+ public class CodegenTemplateLoader extends URLTemplateLoader {
1110 private String templateDir ;
1211 private String customTemplateDir ;
1312
14- public CodegenTemplateLoader (String templateDir , String suffix ) {
15- super (templateDir , suffix );
16- this .templateDir = templateDir ;
13+ public CodegenTemplateLoader () {
14+ setSuffix (".mustache" );
1715 }
1816
17+ @ Override
18+ public String resolve (String uri ) {
19+ if (!uri .endsWith (this .getSuffix ())) {
20+ uri = uri + this .getSuffix ();
21+ }
22+ File templateFile = new File (uri );
23+ if (templateFile .exists ()) {
24+ return templateFile .toString ();
25+ }
26+ templateFile = new File (this .getPrefix () + this .normalize (uri ));
27+ if (templateFile .exists ()) {
28+ return templateFile .toString ();
29+ }
30+ if (this .customTemplateDir != null ) {
31+ templateFile = new File (this .customTemplateDir , this .normalize (uri ));
32+ if (templateFile .exists ()) {
33+ return templateFile .toString ();
34+ }
35+ }
36+ if (getClass ().getResource (this .getPrefix () + this .normalize (uri )) != null ) {
37+ return this .getPrefix () + this .normalize (uri );
38+ }
39+ return this .templateDir + this .normalize (uri );
40+ }
41+
42+ @ Override
1943 public URL getResource (String location ) throws IOException {
2044 if (this .customTemplateDir == null ) {
2145 return this .getClass ().getResource (location );
2246 }
23- String templateFile = resolveTemplateFile (this .templateDir , location );
24- templateFile = resolveTemplateFile (this .customTemplateDir , templateFile );
25- final File file = new File (this .customTemplateDir , templateFile );
47+ final File file = new File (location );
2648 if (file .exists ()) {
2749 return file .toURI ().toURL ();
2850 }
2951 return this .getClass ().getResource (location );
3052 }
3153
32- private String resolveTemplateFile (String templateDir , String templateFile ) {
33- if (templateFile .startsWith (templateDir )) {
34- templateFile = StringUtils .replaceOnce (templateFile , templateDir , StringUtils .EMPTY );
35- }
36- return templateFile ;
37- }
38-
3954 public String getCustomTemplateDir () {
4055 return customTemplateDir ;
4156 }
@@ -48,4 +63,20 @@ public CodegenTemplateLoader customTemplateDir(String customTemplateDir) {
4863 this .customTemplateDir = customTemplateDir ;
4964 return this ;
5065 }
66+
67+ public String getTemplateDir () {
68+ return templateDir ;
69+ }
70+
71+ public void setTemplateDir (String templateDir ) {
72+ this .templateDir = this .getPrefix () + templateDir ;
73+ if (!this .templateDir .endsWith ("/" )) {
74+ this .templateDir = this .templateDir + "/" ;
75+ }
76+ }
77+
78+ public CodegenTemplateLoader templateDir (String templateDir ) {
79+ setTemplateDir (templateDir );
80+ return this ;
81+ }
5182}
0 commit comments