|
| 1 | +package io.swagger.codegen.handlebars.helpers; |
| 2 | + |
| 3 | +import com.github.jknack.handlebars.Options; |
| 4 | +import io.swagger.codegen.CodegenConstants; |
| 5 | +import io.swagger.codegen.CodegenModel; |
| 6 | +import io.swagger.codegen.CodegenProperty; |
| 7 | +import io.swagger.codegen.languages.JavaClientCodegen; |
| 8 | +import org.apache.commons.lang3.StringUtils; |
| 9 | + |
| 10 | +import java.io.IOException; |
| 11 | +import java.util.List; |
| 12 | +import java.util.Map; |
| 13 | + |
| 14 | +import static io.swagger.codegen.languages.helpers.ExtensionHelper.getBooleanValue; |
| 15 | + |
| 16 | +public class JavaHelper { |
| 17 | + |
| 18 | + public CharSequence getClassDefinition(CodegenModel codegenModel, Options options) throws IOException { |
| 19 | + final Boolean serializableModel = options.get(CodegenConstants.SERIALIZABLE_MODEL) != null ? options.get(CodegenConstants.SERIALIZABLE_MODEL) : Boolean.FALSE; |
| 20 | + final Boolean parceableModel = options.get(JavaClientCodegen.PARCELABLE_MODEL) != null ? options.get(JavaClientCodegen.PARCELABLE_MODEL) : Boolean.FALSE; |
| 21 | + final StringBuilder builder = new StringBuilder(); |
| 22 | + builder.append(codegenModel.classname); |
| 23 | + if (StringUtils.isNotBlank(codegenModel.parent)) { |
| 24 | + builder.append(StringUtils.SPACE); |
| 25 | + builder.append("extends "); |
| 26 | + builder.append(codegenModel.parent); |
| 27 | + } |
| 28 | + if (parceableModel && serializableModel) { |
| 29 | + builder.append(" implements Parcelable, Serializable"); |
| 30 | + } else { |
| 31 | + if (serializableModel) { |
| 32 | + builder.append(" implements Serializable"); |
| 33 | + } |
| 34 | + } |
| 35 | + return builder.toString(); |
| 36 | + } |
| 37 | + |
| 38 | + public CharSequence getJavaProperty(CodegenProperty codegenProperty, Options options) throws IOException { |
| 39 | + final StringBuilder builder = new StringBuilder(); |
| 40 | + if (getBooleanValue(codegenProperty, CodegenConstants.IS_CONTAINER_EXT_NAME)) { |
| 41 | + builder.append(codegenProperty.getDatatypeWithEnum()); |
| 42 | + builder.append(StringUtils.SPACE); |
| 43 | + builder.append(codegenProperty.getName()); |
| 44 | + builder.append(" = "); |
| 45 | + if (codegenProperty.getRequired()) { |
| 46 | + builder.append(codegenProperty.getDefaultValue()); |
| 47 | + } else { |
| 48 | + builder.append("null"); |
| 49 | + } |
| 50 | + return builder.toString(); |
| 51 | + } |
| 52 | + return String.format("%s %s = %s", codegenProperty.getDatatypeWithEnum(), codegenProperty.getName(), codegenProperty.getDefaultValue()); |
| 53 | + } |
| 54 | + |
| 55 | + public CharSequence getModelImports(Map<String, Object> templateData, Options options) throws IOException { |
| 56 | + if (options == null) { |
| 57 | + return null; |
| 58 | + } |
| 59 | + final List<Map<String, String>> imports = options.get("imports"); |
| 60 | + if (imports == null || imports.isEmpty()) { |
| 61 | + return null; |
| 62 | + } |
| 63 | + final StringBuilder builder = new StringBuilder(); |
| 64 | + |
| 65 | + boolean supportJava6 = Boolean.valueOf(String.valueOf(templateData.get("supportJava6"))); |
| 66 | + if (supportJava6) { |
| 67 | + builder.append("import org.apache.commons.lang3.ObjectUtils;\n"); |
| 68 | + } else { |
| 69 | + builder.append("import java.util.Objects;\n"); |
| 70 | + } |
| 71 | + |
| 72 | + for (Map<String, String> importMap : imports) { |
| 73 | + builder.append("import "); |
| 74 | + builder.append(importMap.get("import")); |
| 75 | + builder.append(";\n"); |
| 76 | + } |
| 77 | + boolean serializableMode = Boolean.valueOf(String.valueOf(templateData.get("serializableModel"))); |
| 78 | + boolean jackson = Boolean.valueOf(String.valueOf(templateData.get("jackson"))); |
| 79 | + boolean withXml = Boolean.valueOf(String.valueOf(templateData.get("withXml"))); |
| 80 | + boolean parcelableModel = Boolean.valueOf(String.valueOf(templateData.get("parcelableModel"))); |
| 81 | + boolean useBeanValidation = Boolean.valueOf(String.valueOf(templateData.get("useBeanValidation"))); |
| 82 | + if (serializableMode) { |
| 83 | + builder.append("import java.io.Serializable;\n"); |
| 84 | + } |
| 85 | + if (jackson && withXml) { |
| 86 | + builder.append("import com.fasterxml.jackson.dataformat.xml.annotation.*;\n"); |
| 87 | + } |
| 88 | + if (withXml) { |
| 89 | + builder.append("import javax.xml.bind.annotation.*;\n"); |
| 90 | + } |
| 91 | + if (parcelableModel) { |
| 92 | + builder.append("import android.os.Parcelable;\n"); |
| 93 | + builder.append("import android.os.Parcel;\n"); |
| 94 | + } |
| 95 | + if (useBeanValidation) { |
| 96 | + builder.append("import javax.validation.constraints.*;\n"); |
| 97 | + builder.append("import javax.validation.Valid;\n"); |
| 98 | + } |
| 99 | + return builder.toString(); |
| 100 | + } |
| 101 | + |
| 102 | + public CharSequence getXmlAttributeName(String xmlName, String baseName) { |
| 103 | + if (StringUtils.isNotBlank(xmlName)) { |
| 104 | + return xmlName; |
| 105 | + } |
| 106 | + return baseName; |
| 107 | + } |
| 108 | + |
| 109 | + public CharSequence getXmlElementName(String xmlNamespace, String xmlName, String baseName) { |
| 110 | + StringBuilder builder = new StringBuilder(); |
| 111 | + if (StringUtils.isNotBlank(xmlNamespace)) { |
| 112 | + builder.append("namespace=\""); |
| 113 | + builder.append(xmlNamespace); |
| 114 | + builder.append("\", "); |
| 115 | + } |
| 116 | + builder.append("name=\""); |
| 117 | + if (StringUtils.isNotBlank(xmlName)) { |
| 118 | + builder.append(xmlName); |
| 119 | + } else { |
| 120 | + builder.append(baseName); |
| 121 | + } |
| 122 | + builder.append("\""); |
| 123 | + return builder.toString(); |
| 124 | + } |
| 125 | + |
| 126 | + public CharSequence getJacksonXmlProperty(CodegenProperty codegenProperty) { |
| 127 | + final boolean isXmlAttribute = getBooleanValue(codegenProperty, CodegenConstants.IS_XML_ATTRIBUTE_EXT_NAME); |
| 128 | + StringBuilder builder = new StringBuilder(); |
| 129 | + if (isXmlAttribute) { |
| 130 | + builder.append("isAttribute = true, "); |
| 131 | + } |
| 132 | + if (StringUtils.isNotBlank(codegenProperty.getXmlNamespace())) { |
| 133 | + builder.append("namespace=\""); |
| 134 | + builder.append(codegenProperty.getXmlNamespace()); |
| 135 | + builder.append("\", "); |
| 136 | + } |
| 137 | + builder.append("localName = \""); |
| 138 | + if (StringUtils.isNotBlank(codegenProperty.getXmlName())) { |
| 139 | + builder.append(codegenProperty.getXmlNamespace()); |
| 140 | + } else { |
| 141 | + builder.append(codegenProperty.getBaseName()); |
| 142 | + } |
| 143 | + builder.append("\""); |
| 144 | + return builder.toString(); |
| 145 | + } |
| 146 | + |
| 147 | + public CharSequence getJacksonXmlElementWrapper(CodegenProperty codegenProperty) { |
| 148 | + final boolean isXmlWrapped = getBooleanValue(codegenProperty, CodegenConstants.IS_XML_WRAPPED_EXT_NAME); |
| 149 | + StringBuilder builder = new StringBuilder(); |
| 150 | + builder.append("useWrapping = "); |
| 151 | + builder.append(isXmlWrapped); |
| 152 | + builder.append(", "); |
| 153 | + if (StringUtils.isNotBlank(codegenProperty.getXmlNamespace())) { |
| 154 | + builder.append("namespace=\""); |
| 155 | + builder.append(codegenProperty.getXmlNamespace()); |
| 156 | + builder.append("\", "); |
| 157 | + } |
| 158 | + builder.append("localName = \""); |
| 159 | + if (StringUtils.isNotBlank(codegenProperty.getXmlName())) { |
| 160 | + builder.append(codegenProperty.getXmlNamespace()); |
| 161 | + } else { |
| 162 | + builder.append(codegenProperty.getBaseName()); |
| 163 | + } |
| 164 | + builder.append("\""); |
| 165 | + return builder.toString(); |
| 166 | + } |
| 167 | +} |
0 commit comments