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 org.opendaylight.mdsal.binding.model.util.BindingGeneratorUtil.encodeAngleBrackets
12 import com.google.common.base.CharMatcher
13 import com.google.common.base.MoreObjects
14 import com.google.common.base.Splitter
15 import com.google.common.collect.Iterables
16 import java.util.Collection
18 import java.util.StringTokenizer
19 import java.util.regex.Pattern
20 import org.opendaylight.mdsal.binding.model.api.Constant
21 import org.opendaylight.mdsal.binding.model.api.GeneratedProperty
22 import org.opendaylight.mdsal.binding.model.api.GeneratedType
23 import org.opendaylight.mdsal.binding.model.api.MethodSignature
24 import org.opendaylight.mdsal.binding.model.api.TypeMember
25 import org.opendaylight.mdsal.binding.model.api.YangSourceDefinition.Single
26 import org.opendaylight.mdsal.binding.model.api.YangSourceDefinition.Multiple
27 import org.opendaylight.mdsal.binding.model.util.Types
28 import org.opendaylight.yangtools.yang.binding.CodeHelpers
29 import org.opendaylight.yangtools.yang.common.QName
30 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode
31 import org.opendaylight.yangtools.yang.model.api.DocumentedNode
32 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode
33 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition
34 import org.opendaylight.yangtools.yang.model.api.RpcDefinition
35 import org.opendaylight.yangtools.yang.model.api.SchemaNode
37 abstract class BaseTemplate extends JavaFileTemplate {
38 private static final char NEW_LINE = '\n'
39 private static val AMP_MATCHER = CharMatcher.is('&')
40 private static val NL_MATCHER = CharMatcher.is(NEW_LINE)
41 private static val TAB_MATCHER = CharMatcher.is('\t')
42 private static val SPACES_PATTERN = Pattern.compile(" +")
43 private static val NL_SPLITTER = Splitter.on(NL_MATCHER)
44 private static val TAIL_COMMENT_PATTERN = Pattern.compile("*/", Pattern.LITERAL);
46 new(GeneratedType type) {
50 final public def generate() {
53 package «type.packageName»;
60 protected abstract def CharSequence body();
63 final protected def fieldName(GeneratedProperty property) '''_«property.name»'''
65 final protected def propertyNameFromGetter(MethodSignature getter) {
67 if (getter.name.startsWith("is")) {
69 } else if (getter.name.startsWith("get")) {
72 throw new IllegalArgumentException("Not a getter")
74 return getter.name.substring(prefix).toFirstLower;
77 final protected def isAccessor(MethodSignature maybeGetter) {
78 return maybeGetter.name.startsWith("is") || maybeGetter.name.startsWith("get");
82 * Template method which generates the getter method for <code>field</code>
85 * generated property with data about field which is generated as the getter method
86 * @return string with the getter method source code in JAVA format
88 protected def getterMethod(GeneratedProperty field) {
90 public «field.returnType.importedName» «field.getterMethodName»() {
91 «IF field.returnType.importedName.contains("[]")»
92 return «field.fieldName» == null ? null : «field.fieldName».clone();
94 return «field.fieldName»;
100 final protected def getterMethodName(GeneratedProperty field) {
101 val prefix = if(field.returnType.equals(Types.BOOLEAN)) "is" else "get"
102 return '''«prefix»«field.name.toFirstUpper»'''
106 * Template method which generates the setter method for <code>field</code>
109 * generated property with data about field which is generated as the setter method
110 * @return string with the setter method source code in JAVA format
112 final protected def setterMethod(GeneratedProperty field) '''
113 «val returnType = field.returnType.importedName»
114 public «type.name» set«field.name.toFirstUpper»(«returnType» value) {
115 this.«field.fieldName» = value;
121 * Template method which generates method parameters with their types from <code>parameters</code>.
124 * group of generated property instances which are transformed to the method parameters
125 * @return string with the list of the method parameters with their types in JAVA format
127 def final protected asArgumentsDeclaration(Iterable<GeneratedProperty> parameters) '''«IF !parameters.empty»«FOR parameter : parameters SEPARATOR ", "»«parameter.
128 returnType.importedName» «parameter.fieldName»«ENDFOR»«ENDIF»'''
131 * Template method which generates sequence of the names of the class attributes from <code>parameters</code>.
134 * group of generated property instances which are transformed to the sequence of parameter names
135 * @return string with the list of the parameter names of the <code>parameters</code>
137 def final protected asArguments(Iterable<GeneratedProperty> parameters) '''«IF !parameters.empty»«FOR parameter : parameters SEPARATOR ", "»«parameter.
138 fieldName»«ENDFOR»«ENDIF»'''
141 * Template method which generates JAVA comments.
143 * @param comment string with the comment for whole JAVA class
144 * @return string with comment in JAVA format
146 def protected CharSequence asJavadoc(String comment) {
147 if(comment === null) return ''
151 txt = formatToParagraph(txt)
154 «wrapToDocumentation(txt)»
158 def String wrapToDocumentation(String text) {
162 val StringBuilder sb = new StringBuilder().append("/**\n")
163 for (String t : NL_SPLITTER.split(text)) {
176 def protected String formatDataForJavaDoc(GeneratedType type) {
177 val sb = new StringBuilder()
178 val comment = type.comment
179 if (comment !== null) {
180 sb.append(comment.javadoc)
183 appendSnippet(sb, type)
192 def static encodeJavadocSymbols(String description) {
193 if (description.nullOrEmpty) {
197 return TAIL_COMMENT_PATTERN.matcher(AMP_MATCHER.replaceFrom(description, "&")).replaceAll("*/")
200 def protected String formatDataForJavaDoc(GeneratedType type, String additionalComment) {
201 val comment = type.comment
202 if (comment === null) {
208 val sb = new StringBuilder().append(comment.javadoc)
209 appendSnippet(sb, type)
214 .append(additionalComment)
221 def private static void appendSnippet(StringBuilder sb, GeneratedType type) {
222 val optDef = type.yangSourceDefinition
223 if (optDef.present) {
227 if (def instanceof Single) {
230 .append("This class represents the following YANG schema fragment defined in module <b>")
231 .append(def.module.name).append("</b>\n")
233 .append(encodeAngleBrackets(encodeJavadocSymbols(YangTemplate.generateYangSnippet(node))))
236 if (node instanceof SchemaNode) {
237 sb.append("The schema path to identify an instance is\n")
239 .append(formatSchemaPath(def.module.name, node.path.pathFromRoot))
242 if (hasBuilderClass(node)) {
243 val builderName = type.name + "Builder";
245 sb.append("\n<p>To create instances of this class use {@link ").append(builderName)
247 .append("@see ").append(builderName).append('\n')
248 if (node instanceof ListSchemaNode) {
249 val keyDef = node.keyDefinition
250 if (keyDef !== null && !keyDef.empty) {
251 sb.append("@see ").append(type.name).append("Key")
257 } else if (def instanceof Multiple) {
259 for (DocumentedNode schemaNode : def.nodes) {
260 sb.append(encodeAngleBrackets(encodeJavadocSymbols(YangTemplate.generateYangSnippet(schemaNode))))
262 sb.append("</pre>\n")
267 def private static boolean hasBuilderClass(SchemaNode schemaNode) {
268 return schemaNode instanceof ContainerSchemaNode || schemaNode instanceof ListSchemaNode
269 || schemaNode instanceof RpcDefinition || schemaNode instanceof NotificationDefinition;
272 def private static String formatSchemaPath(String moduleName, Iterable<QName> schemaPath) {
273 val sb = new StringBuilder().append(moduleName);
275 var currentElement = Iterables.getFirst(schemaPath, null);
276 for (QName pathElement : schemaPath) {
278 if (!currentElement.namespace.equals(pathElement.namespace)) {
279 currentElement = pathElement
280 sb.append(pathElement)
282 sb.append(pathElement.getLocalName())
285 return sb.toString();
288 def protected String formatDataForJavaDoc(TypeMember type, String additionalComment) {
289 val StringBuilder typeDescriptionBuilder = new StringBuilder();
290 if (!type.comment.nullOrEmpty) {
291 typeDescriptionBuilder.append(formatToParagraph(type.comment))
292 typeDescriptionBuilder.append(NEW_LINE)
293 typeDescriptionBuilder.append(NEW_LINE)
294 typeDescriptionBuilder.append(NEW_LINE)
296 typeDescriptionBuilder.append(additionalComment)
297 var typeDescription = wrapToDocumentation(typeDescriptionBuilder.toString)
303 def asCode(String text) {
304 return "<code>" + text + "</code>"
307 def asLink(String text) {
308 val StringBuilder sb = new StringBuilder()
310 var char lastChar = ' '
311 var boolean badEnding = false
313 if (text.endsWith('.') || text.endsWith(':') || text.endsWith(',')) {
314 tempText = text.substring(0, text.length - 1)
315 lastChar = text.charAt(text.length - 1)
318 sb.append("<a href = \"")
330 protected def formatToParagraph(String text) {
331 if(text === null || text.isEmpty)
334 var formattedText = text
335 val StringBuilder sb = new StringBuilder();
336 var StringBuilder lineBuilder = new StringBuilder();
337 var boolean isFirstElementOnNewLineEmptyChar = false;
339 formattedText = encodeJavadocSymbols(formattedText)
340 formattedText = NL_MATCHER.removeFrom(formattedText)
341 formattedText = TAB_MATCHER.removeFrom(formattedText)
342 formattedText = SPACES_PATTERN.matcher(formattedText).replaceAll(" ")
344 val StringTokenizer tokenizer = new StringTokenizer(formattedText, " ", true);
346 while (tokenizer.hasMoreElements) {
347 val nextElement = tokenizer.nextElement.toString
349 if (lineBuilder.length != 0 && lineBuilder.length + nextElement.length > 80) {
350 if (lineBuilder.charAt(lineBuilder.length - 1) == ' ') {
351 lineBuilder.setLength(0)
352 lineBuilder.append(lineBuilder.substring(0, lineBuilder.length - 1))
354 if (lineBuilder.charAt(0) == ' ') {
355 lineBuilder.setLength(0)
356 lineBuilder.append(lineBuilder.substring(1))
359 sb.append(lineBuilder);
360 lineBuilder.setLength(0)
363 if(nextElement.toString == ' ') {
364 isFirstElementOnNewLineEmptyChar = !isFirstElementOnNewLineEmptyChar;
368 if (isFirstElementOnNewLineEmptyChar) {
369 isFirstElementOnNewLineEmptyChar = !isFirstElementOnNewLineEmptyChar
373 lineBuilder.append(nextElement)
376 sb.append(lineBuilder)
382 def protected generateToString(Collection<GeneratedProperty> properties) '''
383 «IF !properties.empty»
385 public «String.importedName» toString() {
386 final «MoreObjects.importedName».ToStringHelper helper = «MoreObjects.importedName».toStringHelper(«type.importedName».class);
387 «FOR property : properties»
388 «CodeHelpers.importedName».appendValue(helper, "«property.fieldName»", «property.fieldName»);
390 return helper.toString();
396 * Template method which generates method parameters with their types from <code>parameters</code>.
399 * list of parameter instances which are transformed to the method parameters
400 * @return string with the list of the method parameters with their types in JAVA format
402 def protected generateParameters(List<MethodSignature.Parameter> parameters) '''«
403 IF !parameters.empty»«
404 FOR parameter : parameters SEPARATOR ", "»«
405 parameter.type.importedName» «parameter.name»«
410 def protected emitConstant(Constant c) '''
411 «IF c.value instanceof QName»
412 «val qname = c.value as QName»
413 «val rev = qname.revision»
414 public static final «c.type.importedName» «c.name» = «QName.name».create("«qname.namespace.toString»",
415 «IF rev.isPresent»"«rev.get»", «ENDIF»"«qname.localName»").intern();
417 public static final «c.type.importedName» «c.name» = «c.value»;