forked from aws/aws-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDefaultNamingStrategy.java
More file actions
147 lines (129 loc) · 5.06 KB
/
DefaultNamingStrategy.java
File metadata and controls
147 lines (129 loc) · 5.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
* Copyright (c) 2016. Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package com.amazonaws.codegen.naming;
import com.amazonaws.codegen.model.config.BasicCodeGenConfig;
import com.amazonaws.codegen.model.config.customization.CustomizationConfig;
import com.amazonaws.codegen.model.service.Output;
import com.amazonaws.codegen.model.service.ServiceModel;
import com.amazonaws.util.StringUtils;
import java.util.ArrayList;
import java.util.List;
import static com.amazonaws.codegen.internal.Constants.EXCEPTION_CLASS_SUFFIX;
import static com.amazonaws.codegen.internal.Constants.FAULT_CLASS_SUFFIX;
import static com.amazonaws.codegen.internal.Constants.REQUEST_CLASS_SUFFIX;
import static com.amazonaws.codegen.internal.Constants.RESPONSE_CLASS_SUFFIX;
import static com.amazonaws.codegen.internal.Constants.VARIABLE_NAME_SUFFIX;
import static com.amazonaws.codegen.internal.Utils.capitialize;
import static com.amazonaws.codegen.internal.Utils.unCapitialize;
/**
* Default implementation of naming strategy respecting customizations supplied by {@link
* CustomizationConfig}.
*/
public class DefaultNamingStrategy implements NamingStrategy {
private final static List<String> reservedKeywords = new ArrayList<String>() {{
add("return");
add("public");
add("private");
add("class");
add("static");
add("protected");
add("return");
add("string");
add("boolean");
add("integer");
add("int");
add("char");
add("null");
add("double");
add("object");
add("short");
add("long");
add("float");
add("byte");
add("bigDecimal");
add("bigInteger");
add("protected");
add("inputStream");
add("bytebuffer");
add("date");
add("list");
add("map");
}};
private final ServiceModel serviceModel;
private final BasicCodeGenConfig codeGenConfig;
private final CustomizationConfig customizationConfig;
public DefaultNamingStrategy(ServiceModel serviceModel, BasicCodeGenConfig codeGenConfig,
CustomizationConfig customizationConfig) {
this.serviceModel = serviceModel;
this.customizationConfig = customizationConfig;
this.codeGenConfig = codeGenConfig;
}
@Override
public String getExceptionName(String errorShapeName) {
if (errorShapeName.endsWith(FAULT_CLASS_SUFFIX)) {
return capitialize(errorShapeName.substring(0, errorShapeName.length() -
FAULT_CLASS_SUFFIX.length()) +
EXCEPTION_CLASS_SUFFIX);
} else if (errorShapeName.endsWith(EXCEPTION_CLASS_SUFFIX)) {
return capitialize(errorShapeName);
}
return capitialize(errorShapeName + EXCEPTION_CLASS_SUFFIX);
}
@Override
public String getRequestClassName(String operationName) {
return capitialize(operationName + REQUEST_CLASS_SUFFIX);
}
@Override
public String getResponseClassName(String operationName) {
if (customizationConfig.useModeledOutputShapeNames()) {
final Output operationOutput = serviceModel.getOperation(operationName).getOutput();
if (operationOutput != null) {
return operationOutput.getShape();
}
}
return capitialize(operationName + RESPONSE_CLASS_SUFFIX);
}
@Override
public String getVariableName(String name) {
if (isJavaKeyword(name)) {
return unCapitialize(name + VARIABLE_NAME_SUFFIX);
} else {
return unCapitialize(name);
}
}
@Override
public String getEnumValueName(String enumValue) {
StringBuilder builder = new StringBuilder();
String sanitizedEnumValue = enumValue.replace("::", ":").replace("/", "").replace("(", "")
.replace(")", "");
for (String part : sanitizedEnumValue.split("[ -.:]")) {
if (part.length() > 1) {
builder.append(StringUtils.upperCase(part.substring(0, 1)))
.append(part.substring(1));
} else {
builder.append(StringUtils.upperCase(part));
}
}
return builder.toString();
}
@Override
public String getJavaClassName(String shapeName) {
return capitialize(shapeName);
}
private static boolean isJavaKeyword(String word) {
return reservedKeywords.contains(word) ||
reservedKeywords.contains(StringUtils.lowerCase(word));
}
}