|
| 1 | + |
| 2 | +package com.sparkpost.documentor; |
| 3 | + |
| 4 | +import java.io.File; |
| 5 | +import java.io.IOException; |
| 6 | +import java.net.MalformedURLException; |
| 7 | +import java.net.URL; |
| 8 | +import java.nio.charset.Charset; |
| 9 | +import java.util.Set; |
| 10 | +import java.util.regex.Matcher; |
| 11 | + |
| 12 | +import org.apache.commons.io.FileUtils; |
| 13 | +import org.apache.commons.io.IOUtils; |
| 14 | +import org.json.JSONException; |
| 15 | +import org.json.JSONObject; |
| 16 | + |
| 17 | +public class GenerateWebhookEventPojos { |
| 18 | + |
| 19 | + private static final String WEBHOOK_DOCS_URL = "https://api.sparkpost.com/api/v1/webhooks/events/documentation"; |
| 20 | + private static final String OUTPUT_PACKAGE = "com.sparkpost.model.webhook.event"; |
| 21 | + |
| 22 | + private String javaOutputPath = "gen/java/" + OUTPUT_PACKAGE.replaceAll("\\.", "/"); |
| 23 | + |
| 24 | + public static void main(String[] args) throws JSONException, MalformedURLException, IOException { |
| 25 | + GenerateWebhookEventPojos generator = new GenerateWebhookEventPojos(); |
| 26 | + generator.processInput(); |
| 27 | + } |
| 28 | + |
| 29 | + private void processInput() throws JSONException, MalformedURLException, IOException { |
| 30 | + |
| 31 | + JSONObject json = new JSONObject(IOUtils.toString(new URL(WEBHOOK_DOCS_URL), Charset.forName("UTF-8"))); |
| 32 | + //System.out.println("Fetched: " + json); |
| 33 | + |
| 34 | + JSONObject results = json.getJSONObject("results"); |
| 35 | + |
| 36 | + Set<String> keySet = results.keySet(); |
| 37 | + for (String key : keySet) { |
| 38 | + |
| 39 | + JSONObject events = results.getJSONObject(key); |
| 40 | + //String description = events.getString("description"); |
| 41 | + //String displayName = events.getString("display_name"); |
| 42 | + |
| 43 | + JSONObject subEventTypes = events.getJSONObject("events"); |
| 44 | + Set<String> eventInfo = subEventTypes.keySet(); |
| 45 | + System.out.println("package " + OUTPUT_PACKAGE + "." + convertToCamelCase(key, false)); |
| 46 | + for (String eventName : eventInfo) { |
| 47 | + //System.out.println("\tclass " + convertToCamelCase(eventName, true)); |
| 48 | + JSONObject classInfo = subEventTypes.getJSONObject(eventName); |
| 49 | + String contents = renderEventClass(classInfo); |
| 50 | + |
| 51 | + // Write class to file |
| 52 | + //writeFile(String contents, String directoryName, String fileName) |
| 53 | + String fileName = convertToCamelCase(classInfo.getString("display_name"), true) + "Event.java"; |
| 54 | + writeFile(contents, this.javaOutputPath, fileName); |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + public String renderEventClass(JSONObject eventInfo) { |
| 60 | + StringBuilder builder = new StringBuilder(); |
| 61 | + builder.append("package " + OUTPUT_PACKAGE + ";\n\n"); |
| 62 | + //System.out.println("TODO: render " + eventInfo); |
| 63 | + |
| 64 | + builder.append("import java.util.List;\n"); |
| 65 | + builder.append("import java.util.Map;\n"); |
| 66 | + builder.append("import com.yepher.jsondoc.annotations.Description;\n"); |
| 67 | + |
| 68 | + builder.append("import lombok.Data;\n"); |
| 69 | + builder.append("import lombok.EqualsAndHashCode;\n"); |
| 70 | + |
| 71 | + builder.append("\n\n/**\n").append(eventInfo.optString("description").trim()).append("\n*/\n\n"); |
| 72 | + builder.append("@Data\n"); |
| 73 | + builder.append("@EqualsAndHashCode()\n"); |
| 74 | + builder.append("public class " + convertToCamelCase(eventInfo.getString("display_name"), true) + "Event {\n\n"); |
| 75 | + |
| 76 | + JSONObject fields = eventInfo.getJSONObject("event"); |
| 77 | + Set<String> fieldNames = fields.keySet(); |
| 78 | + |
| 79 | + for (String fieldName : fieldNames) { |
| 80 | + JSONObject field = fields.getJSONObject(fieldName); |
| 81 | + |
| 82 | + //@Description( |
| 83 | + // value = "A suppression list - or exclusion list, as it is sometimes called - stores a recipient's opt-out preferences.", |
| 84 | + // sample = {"Array of SuppressionListEntrys"}) |
| 85 | + builder.append("\t@Description(\n"); |
| 86 | + builder.append("\t\tvalue = \"" + formatSample(field.getString("description")) + "\",\n"); |
| 87 | + |
| 88 | + String prettyFieldName = convertToCamelCase(fieldName, false); |
| 89 | + if (isFieldArray(field, "sampleValue")) { |
| 90 | + builder.append("\t\tsample = \"" + formatSample(field.getJSONArray("sampleValue").toString()) + "\")\n"); |
| 91 | + builder.append("\tprivate List<String> " + prettyFieldName + ";\n"); |
| 92 | + |
| 93 | + } else if (isFieldObject(field, "sampleValue")) { |
| 94 | + builder.append("\t\tsample = \"" + formatSample(field.getJSONObject("sampleValue").toString()) + "\")\n"); |
| 95 | + builder.append("\tprivate Map<String, String> " + prettyFieldName + ";\n"); |
| 96 | + |
| 97 | + } else if (isFieldString(field, "sampleValue")) { |
| 98 | + builder.append("\t\tsample = \"" + formatSample(field.getString("sampleValue").toString()) + "\")\n"); |
| 99 | + builder.append("\tprivate String " + prettyFieldName + ";\n"); |
| 100 | + |
| 101 | + } else if (isFieldInt(field, "sampleValue")) { |
| 102 | + builder.append("\t\tsample = \"" + field.getInt("sampleValue") + "\")\n"); |
| 103 | + builder.append("\tprivate int " + prettyFieldName + ";\n"); |
| 104 | + |
| 105 | + } else { |
| 106 | + builder.append("\t\tsample = \"" + "\")\n"); |
| 107 | + builder.append("\tUnknown Field Type: " + prettyFieldName + "\n"); |
| 108 | + |
| 109 | + } |
| 110 | + //System.out.println("Type: " + sampleObj.getClass().getSimpleName()); |
| 111 | + builder.append("\n"); |
| 112 | + |
| 113 | + } |
| 114 | + |
| 115 | + builder.append("}\n"); // class close curly brace |
| 116 | + System.out.println(builder); |
| 117 | + return builder.toString(); |
| 118 | + |
| 119 | + } |
| 120 | + |
| 121 | + private String formatSample(String input) { |
| 122 | + //String result = input.replaceAll("\"", "\\\""); |
| 123 | + String result = input.replaceAll("\"", Matcher.quoteReplacement("\\\"")); |
| 124 | + |
| 125 | + return result; |
| 126 | + } |
| 127 | + |
| 128 | + private boolean isFieldArray(JSONObject obj, String field) { |
| 129 | + |
| 130 | + try { |
| 131 | + obj.getJSONArray(field); |
| 132 | + return true; |
| 133 | + } catch (JSONException e) { |
| 134 | + return false; |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + private boolean isFieldString(JSONObject obj, String field) { |
| 139 | + try { |
| 140 | + obj.getString(field); |
| 141 | + return true; |
| 142 | + } catch (JSONException e) { |
| 143 | + return false; |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + private boolean isFieldObject(JSONObject obj, String field) { |
| 148 | + try { |
| 149 | + obj.getJSONObject(field); |
| 150 | + return true; |
| 151 | + } catch (JSONException e) { |
| 152 | + return false; |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + private boolean isFieldInt(JSONObject obj, String field) { |
| 157 | + try { |
| 158 | + obj.getInt(field); |
| 159 | + return true; |
| 160 | + } catch (JSONException e) { |
| 161 | + return false; |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + private String convertToCamelCase(String input, boolean capitalInital) { |
| 166 | + if (input == null) { |
| 167 | + return null; |
| 168 | + } |
| 169 | + |
| 170 | + if (input.length() == 0) { |
| 171 | + return input; |
| 172 | + } |
| 173 | + |
| 174 | + StringBuilder result = new StringBuilder(); |
| 175 | + String[] scratch = input.toLowerCase().split("[\\s@&.?$+-_]+"); |
| 176 | + |
| 177 | + for (int i = 0; i < scratch.length; i++) { |
| 178 | + String word = scratch[i].trim(); |
| 179 | + if (word.length() == 0) { |
| 180 | + continue; |
| 181 | + } else if (!capitalInital && i == 0) { |
| 182 | + result.append(word); |
| 183 | + } else { |
| 184 | + result.append(Character.toUpperCase(word.charAt(0)) + word.substring(1)); |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + return result.toString(); |
| 189 | + } |
| 190 | + |
| 191 | + public void writeFile(String contents, String directoryName, String fileName) throws IOException { |
| 192 | + |
| 193 | + File directory = new File(directoryName); |
| 194 | + if (!directory.exists()) { |
| 195 | + if (!directory.mkdirs()) { |
| 196 | + System.err.println("Failed to create one or more directories"); |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + File file = new File(directoryName + "/" + fileName); |
| 201 | + FileUtils.writeStringToFile(file, contents, "UTF-8"); |
| 202 | + } |
| 203 | + |
| 204 | +} |
0 commit comments