2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.mdsal.binding.java.api.generator
10 import static extension org.opendaylight.mdsal.binding.generator.BindingGeneratorUtil.encodeAngleBrackets
11 import static org.opendaylight.mdsal.binding.model.ri.Types.STRING;
12 import static org.opendaylight.mdsal.binding.model.ri.Types.objectType;
14 import com.google.common.base.CharMatcher
15 import com.google.common.base.Splitter
16 import java.util.Collection
18 import java.util.Locale
19 import java.util.Map.Entry
20 import java.util.StringTokenizer
21 import java.util.regex.Pattern
22 import org.eclipse.jdt.annotation.NonNull;
23 import org.opendaylight.mdsal.binding.model.api.AnnotationType
24 import org.opendaylight.mdsal.binding.model.api.ConcreteType
25 import org.opendaylight.mdsal.binding.model.api.Constant
26 import org.opendaylight.mdsal.binding.model.api.GeneratedProperty
27 import org.opendaylight.mdsal.binding.model.api.GeneratedType
28 import org.opendaylight.mdsal.binding.model.api.JavaTypeName
29 import org.opendaylight.mdsal.binding.model.api.MethodSignature
30 import org.opendaylight.mdsal.binding.model.api.Restrictions
31 import org.opendaylight.mdsal.binding.model.api.Type
32 import org.opendaylight.mdsal.binding.model.api.TypeMemberComment
33 import org.opendaylight.mdsal.binding.model.ri.TypeConstants
34 import org.opendaylight.yangtools.yang.binding.BaseIdentity
35 import org.opendaylight.yangtools.yang.binding.contract.Naming
36 import org.opendaylight.yangtools.yang.common.YangDataName
38 abstract class BaseTemplate extends AbstractBaseTemplate {
39 static final char NEW_LINE = '\n'
40 static final char SPACE = ' '
41 static val WS_MATCHER = CharMatcher.anyOf("\n\t")
42 static val SPACES_PATTERN = Pattern.compile(" +")
43 static val NL_SPLITTER = Splitter.on(NEW_LINE)
45 new(GeneratedType type) {
49 new(AbstractJavaGeneratedType javaType, GeneratedType type) {
55 * Template method which generates the getter method for <code>field</code>
58 * generated property with data about field which is generated as the getter method
59 * @return string with the getter method source code in JAVA format
61 protected def getterMethod(GeneratedProperty field) '''
62 «val methodName = field.getterMethodName»
63 public «field.returnType.importedName» «methodName»() {
64 «val fieldName = field.fieldName»
65 «IF field.returnType.name.endsWith("[]")»
66 return «fieldName» == null ? null : «fieldName».clone();
74 * Template method which generates the setter method for <code>field</code>
77 * generated property with data about field which is generated as the setter method
78 * @return string with the setter method source code in JAVA format
80 final protected def setterMethod(GeneratedProperty field) '''
81 «val returnType = field.returnType.importedName»
82 public «type.name» set«field.name.toFirstUpper»(«returnType» value) {
83 this.«field.fieldName» = value;
89 * Template method which generates method parameters with their types from <code>parameters</code>.
92 * group of generated property instances which are transformed to the method parameters
93 * @return string with the list of the method parameters with their types in JAVA format
95 def final protected asArgumentsDeclaration(Iterable<GeneratedProperty> parameters) '''«IF !parameters.empty»«FOR parameter : parameters SEPARATOR ", "»«parameter.
96 returnType.importedName» «parameter.fieldName»«ENDFOR»«ENDIF»'''
99 * Template method which generates method parameters with their types from <code>parameters</code>, annotating them
100 * with {@link NonNull}.
102 * @param parameters group of generated property instances which are transformed to the method parameters
103 * @return string with the list of the method parameters with their types in JAVA format
105 def final protected asNonNullArgumentsDeclaration(Iterable<GeneratedProperty> parameters) '''«IF !parameters.empty»
106 «FOR parameter : parameters SEPARATOR ", "»«parameter.returnType.importedNonNull» «parameter
107 .fieldName»«ENDFOR»«ENDIF»'''
110 * Template method which generates sequence of the names of the class attributes from <code>parameters</code>.
113 * group of generated property instances which are transformed to the sequence of parameter names
114 * @return string with the list of the parameter names of the <code>parameters</code>
116 def final protected asArguments(Collection<GeneratedProperty> parameters) '''«IF !parameters.empty»«FOR parameter : parameters SEPARATOR ", "»«parameter.
117 fieldName»«ENDFOR»«ENDIF»'''
120 * Template method which generates JAVA comments.
122 * @param comment string with the comment for whole JAVA class
123 * @return string with comment in JAVA format
125 def final protected asJavadoc(TypeMemberComment comment) {
126 if (comment === null) {
129 return wrapToDocumentation('''
130 «comment.contractDescription»
132 «comment.referenceDescription.formatReference»
134 «comment.typeSignature»
138 def static String wrapToDocumentation(String text) {
142 val StringBuilder sb = new StringBuilder().append("/**\n")
143 for (String t : NL_SPLITTER.split(text)) {
146 sb.append(SPACE).append(t)
155 def protected String formatDataForJavaDoc(GeneratedType type) {
156 val sb = new StringBuilder()
157 val comment = type.comment
158 if (comment !== null) {
159 sb.append(comment.javadoc)
162 appendSnippet(sb, type)
171 def protected String formatDataForJavaDoc(GeneratedType type, String additionalComment) {
172 val comment = type.comment
173 if (comment === null) {
179 val sb = new StringBuilder().append(comment.javadoc)
180 appendSnippet(sb, type)
185 .append(additionalComment)
192 def static formatReference(String reference) '''
193 «IF reference !== null»
196 «reference.encodeAngleBrackets.formatToParagraph»
203 def asLink(String text) {
204 val StringBuilder sb = new StringBuilder()
206 var char lastChar = SPACE
207 var boolean badEnding = false
209 if (text.endsWith('.') || text.endsWith(':') || text.endsWith(',')) {
210 tempText = text.substring(0, text.length - 1)
211 lastChar = text.charAt(text.length - 1)
214 sb.append("<a href = \"").append(tempText).append("\">").append(tempText).append("</a>")
222 protected static def formatToParagraph(String inputText) {
223 val StringBuilder sb = new StringBuilder();
224 var StringBuilder lineBuilder = new StringBuilder();
225 var boolean isFirstElementOnNewLineEmptyChar = false;
227 var formattedText = WS_MATCHER.replaceFrom(inputText.encodeJavadocSymbols, SPACE)
228 formattedText = SPACES_PATTERN.matcher(formattedText).replaceAll(" ")
230 val StringTokenizer tokenizer = new StringTokenizer(formattedText, " ", true)
231 while (tokenizer.hasMoreTokens) {
232 val nextElement = tokenizer.nextToken
234 if (lineBuilder.length != 0 && lineBuilder.length + nextElement.length > 80) {
235 if (lineBuilder.charAt(lineBuilder.length - 1) == SPACE) {
236 lineBuilder.setLength(lineBuilder.length - 1)
238 if (lineBuilder.length != 0 && lineBuilder.charAt(0) == SPACE) {
239 lineBuilder.deleteCharAt(0)
242 sb.append(lineBuilder).append(NEW_LINE)
243 lineBuilder.setLength(0)
245 if (" ".equals(nextElement)) {
246 isFirstElementOnNewLineEmptyChar = !isFirstElementOnNewLineEmptyChar;
249 if (isFirstElementOnNewLineEmptyChar) {
250 isFirstElementOnNewLineEmptyChar = !isFirstElementOnNewLineEmptyChar
252 lineBuilder.append(nextElement)
256 return sb.append(lineBuilder).append(NEW_LINE).toString
260 * Template method which generates method parameters with their types from <code>parameters</code>.
263 * list of parameter instances which are transformed to the method parameters
264 * @return string with the list of the method parameters with their types in JAVA format
266 def protected generateParameters(List<MethodSignature.Parameter> parameters) '''«
267 IF !parameters.empty»«
268 FOR parameter : parameters SEPARATOR ", "»«
269 parameter.type.importedName» «parameter.name»«
274 def protected emitConstant(Constant c) '''
275 «IF Naming.QNAME_STATIC_FIELD_NAME.equals(c.name)»
276 «val entry = c.value as Entry<JavaTypeName, String>»
278 * YANG identifier of the statement represented by this class.
280 public static final «c.type.importedNonNull» «c.name» = «entry.key.importedName».«Naming.MODULE_INFO_QNAMEOF_METHOD_NAME»("«entry.value»");
281 «ELSEIF Naming.NAME_STATIC_FIELD_NAME.equals(c.name)»
282 «val entry = c.value as Entry<JavaTypeName, YangDataName>»
284 * Yang Data template name of the statement represented by this class.
286 public static final «c.type.importedNonNull» «c.name» = «entry.key.importedName».«Naming.MODULE_INFO_YANGDATANAMEOF_METHOD_NAME»("«entry.value.name»");
287 «ELSEIF Naming.VALUE_STATIC_FIELD_NAME.equals(c.name) && BaseIdentity.equals(c.value)»
288 «val typeName = c.type.importedName»
289 «val override = OVERRIDE.importedName»
291 * Singleton value representing the {@link «typeName»} identity.
293 public static final «c.type.importedNonNull» «c.name» = new «typeName»() {
295 public «CLASS.importedName»<«typeName»> «Naming.BINDING_CONTRACT_IMPLEMENTED_INTERFACE_NAME»() {
296 return «typeName».class;
300 public int hashCode() {
301 return «typeName».class.hashCode();
305 public boolean equals(final «objectType.importedName» obj) {
306 return obj == this || obj instanceof «typeName» other
307 && «typeName».class.equals(other.«Naming.BINDING_CONTRACT_IMPLEMENTED_INTERFACE_NAME»());
311 public «STRING.importedName» toString() {
312 return «MOREOBJECTS.importedName».toStringHelper("«c.type.name»").add("qname", QNAME).toString();
316 public static final «c.type.importedName» «c.name» = «c.value»;
320 def protected generateCheckers(GeneratedProperty field, Restrictions restrictions, Type actualType) '''
321 «IF restrictions.rangeConstraint.present»
322 «AbstractRangeGenerator.forType(actualType).generateRangeChecker(field.name.toFirstUpper,
323 restrictions.rangeConstraint.orElseThrow, this)»
325 «IF restrictions.lengthConstraint.present»
326 «LengthGenerator.generateLengthChecker(field.fieldName, actualType, restrictions.lengthConstraint.orElseThrow, this)»
330 def protected checkArgument(GeneratedProperty property, Restrictions restrictions, Type actualType, String value) '''
331 «IF restrictions.getRangeConstraint.isPresent»
332 «IF actualType instanceof ConcreteType»
333 «AbstractRangeGenerator.forType(actualType).generateRangeCheckerCall(property.getName.toFirstUpper, value)»
335 «AbstractRangeGenerator.forType(actualType).generateRangeCheckerCall(property.getName.toFirstUpper, value + ".getValue()")»
338 «val fieldName = property.fieldName»
339 «IF restrictions.getLengthConstraint.isPresent»
340 «IF actualType instanceof ConcreteType»
341 «LengthGenerator.generateLengthCheckerCall(fieldName, value)»
343 «LengthGenerator.generateLengthCheckerCall(fieldName, value + ".getValue()")»
347 «val fieldUpperCase = fieldName.toUpperCase(Locale.ROOT)»
348 «FOR currentConstant : type.getConstantDefinitions»
349 «IF currentConstant.getName.startsWith(TypeConstants.PATTERN_CONSTANT_NAME)
350 && fieldUpperCase.equals(currentConstant.getName.substring(TypeConstants.PATTERN_CONSTANT_NAME.length))»
351 «CODEHELPERS.importedName».checkPattern(value, «Constants.MEMBER_PATTERN_LIST»«fieldName», «Constants.MEMBER_REGEX_LIST»«fieldName»);
356 def protected final generateAnnotation(AnnotationType annotation) '''
357 @«annotation.importedName»
358 «IF annotation.parameters !== null && !annotation.parameters.empty»
360 «FOR param : annotation.parameters SEPARATOR ","»
361 «param.name»=«param.value»