1
1
package io .swagger .codegen .v3 .templates ;
2
2
3
- import com .github .jknack .handlebars .io .FileTemplateLoader ;
4
- import org .apache .commons .lang3 .StringUtils ;
3
+ import com .github .jknack .handlebars .io .URLTemplateLoader ;
5
4
6
5
import java .io .File ;
7
6
import java .io .IOException ;
8
7
import java .net .URL ;
9
8
10
- public class CodegenTemplateLoader extends FileTemplateLoader {
9
+ public class CodegenTemplateLoader extends URLTemplateLoader {
11
10
private String templateDir ;
12
11
private String customTemplateDir ;
13
12
14
- public CodegenTemplateLoader (String templateDir , String suffix ) {
15
- super (templateDir , suffix );
16
- this .templateDir = templateDir ;
13
+ public CodegenTemplateLoader () {
14
+ setSuffix (".mustache" );
17
15
}
18
16
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
19
43
public URL getResource (String location ) throws IOException {
20
44
if (this .customTemplateDir == null ) {
21
45
return this .getClass ().getResource (location );
22
46
}
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 );
26
48
if (file .exists ()) {
27
49
return file .toURI ().toURL ();
28
50
}
29
51
return this .getClass ().getResource (location );
30
52
}
31
53
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
-
39
54
public String getCustomTemplateDir () {
40
55
return customTemplateDir ;
41
56
}
@@ -48,4 +63,20 @@ public CodegenTemplateLoader customTemplateDir(String customTemplateDir) {
48
63
this .customTemplateDir = customTemplateDir ;
49
64
return this ;
50
65
}
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
+ }
51
82
}
0 commit comments