17
17
* See the License for the specific language governing permissions and
18
18
* limitations under the License.
19
19
*/
20
- package org .zaproxy .zap .extension ;
20
+ package org .zaproxy .zap .extension . api ;
21
21
22
22
import java .io .IOException ;
23
+ import java .io .UncheckedIOException ;
23
24
import java .util .ArrayList ;
25
+ import java .util .Arrays ;
24
26
import java .util .List ;
27
+ import java .util .Locale ;
28
+ import java .util .ResourceBundle ;
25
29
30
+ import org .zaproxy .zap .extension .api .AbstractAPIGenerator ;
26
31
import org .zaproxy .zap .extension .api .ApiImplementor ;
27
32
import org .zaproxy .zap .extension .api .JavaAPIGenerator ;
28
33
import org .zaproxy .zap .extension .api .NodeJSAPIGenerator ;
@@ -36,6 +41,8 @@ public class ApiGenerator {
36
41
37
42
private static final String JAVA_OUTPUT_DIR = "../zap-api-java/subprojects/zap-clientapi/src/main/java/org/zaproxy/clientapi/gen" ;
38
43
44
+ private static final String PHP_OUTPUT_DIR = "../zaproxy/php/api/zapv2/src/Zap" ;
45
+
39
46
private static final String PYTHON_OUTPUT_DIR = "../zap-api-python/src/zapv2/" ;
40
47
41
48
private static final String NODE_OUTPUT_DIR = "../zap-api-nodejs/src/" ;
@@ -63,26 +70,62 @@ public static List<ApiImplementor> getApiImplementors() {
63
70
* @param args
64
71
*/
65
72
public static void main (String [] args ) {
66
- try {
67
- JavaAPIGenerator japi = new JavaAPIGenerator (JAVA_OUTPUT_DIR , true );
68
- japi .generateAPIFiles (getApiImplementors ());
73
+ List <ApiGeneratorWrapper > generators = Arrays .asList (
74
+ wrapper (JavaAPIGenerator .class , JAVA_OUTPUT_DIR ),
75
+ wrapper (NodeJSAPIGenerator .class , NODE_OUTPUT_DIR ),
76
+ wrapper (PhpAPIGenerator .class , PHP_OUTPUT_DIR ),
77
+ wrapper (PythonAPIGenerator .class , PYTHON_OUTPUT_DIR )
78
+ // wrapper(WikiAPIGenerator.class, "../zaproxy-wiki")
79
+ );
80
+ getApiImplementors ().forEach (api -> {
81
+ ResourceBundle bundle = ResourceBundle .getBundle (
82
+ api .getClass ().getPackage ().getName () + ".resources.Messages" ,
83
+ Locale .ENGLISH ,
84
+ api .getClass ().getClassLoader (),
85
+ ResourceBundle .Control .getControl (ResourceBundle .Control .FORMAT_PROPERTIES ));
69
86
70
- NodeJSAPIGenerator napi = new NodeJSAPIGenerator (NODE_OUTPUT_DIR , true );
71
- napi .generateAPIFiles (getApiImplementors ());
72
-
73
- PhpAPIGenerator phapi = new PhpAPIGenerator ("../zaproxy/php/api/zapv2/src/Zap" , true );
74
- phapi .generateAPIFiles (getApiImplementors ());
87
+ generators .forEach (generator -> generator .generate (api , bundle ));
88
+ });
89
+ }
90
+
91
+ private static ApiGeneratorWrapper wrapper (Class <? extends AbstractAPIGenerator > clazz , String outputDir ) {
92
+ return new ApiGeneratorWrapper (clazz , outputDir );
93
+ }
75
94
76
- PythonAPIGenerator pyapi = new PythonAPIGenerator (PYTHON_OUTPUT_DIR , true );
77
- pyapi .generateAPIFiles (getApiImplementors ());
95
+ private static class ApiGeneratorWrapper {
78
96
79
- //WikiAPIGenerator wapi = new WikiAPIGenerator("../zaproxy-wiki", true);
80
- //wapi.generateAPIFiles(getApiImplementors());
81
-
82
- } catch (IOException e ) {
83
- e .printStackTrace ();
97
+ private final Class <? extends AbstractAPIGenerator > clazz ;
98
+ private final String outputDir ;
99
+
100
+ public ApiGeneratorWrapper (Class <? extends AbstractAPIGenerator > clazz , String outputDir ) {
101
+ this .clazz = clazz ;
102
+ this .outputDir = outputDir ;
84
103
}
85
104
105
+ public void generate (ApiImplementor api , ResourceBundle bundle ) {
106
+ AbstractAPIGenerator generator ;
107
+ try {
108
+ generator = createInstance (bundle );
109
+ } catch (Exception e ) {
110
+ throw new RuntimeException (e );
111
+ }
112
+
113
+ try {
114
+ generator .generateAPIFiles (Arrays .asList (api ));
115
+ } catch (IOException e ) {
116
+ throw new UncheckedIOException (e );
117
+ }
118
+ }
119
+
120
+ private AbstractAPIGenerator createInstance (ResourceBundle bundle ) throws Exception {
121
+ try {
122
+ return clazz .getDeclaredConstructor (String .class , boolean .class , ResourceBundle .class )
123
+ .newInstance (outputDir , true , bundle );
124
+ } catch (NoSuchMethodException e ) {
125
+ System .out .println ("Defaulting to generator without ResourceBundle, no descriptions will be included." );
126
+ return clazz .getDeclaredConstructor (String .class , boolean .class ).newInstance (outputDir , true );
127
+ }
128
+ }
86
129
}
87
130
88
131
}
0 commit comments