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.Splitter
14 import com.google.common.collect.Iterables
15 import java.util.Arrays
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.common.QName
29 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode
30 import org.opendaylight.yangtools.yang.model.api.DocumentedNode
31 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode
32 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition
33 import org.opendaylight.yangtools.yang.model.api.RpcDefinition
34 import org.opendaylight.yangtools.yang.model.api.SchemaNode
36 abstract class BaseTemplate extends JavaFileTemplate {
37 private static final char NEW_LINE = '\n'
38 private static val AMP_MATCHER = CharMatcher.is('&')
39 private static val NL_MATCHER = CharMatcher.is(NEW_LINE)
40 private static val TAB_MATCHER = CharMatcher.is('\t')
41 private static val SPACES_PATTERN = Pattern.compile(" +")
42 private static val NL_SPLITTER = Splitter.on(NL_MATCHER)
43 private static val TAIL_COMMENT_PATTERN = Pattern.compile("*/", Pattern.LITERAL);
45 new(GeneratedType type) {
49 final public def generate() {
52 package «type.packageName»;
59 protected abstract def CharSequence body();
62 final protected def fieldName(GeneratedProperty property) '''_«property.name»'''
64 final protected def propertyNameFromGetter(MethodSignature getter) {
66 if (getter.name.startsWith("is")) {
68 } else if (getter.name.startsWith("get")) {
71 throw new IllegalArgumentException("Not a getter")
73 return getter.name.substring(prefix).toFirstLower;
76 final protected def isAccessor(MethodSignature maybeGetter) {
77 return maybeGetter.name.startsWith("is") || maybeGetter.name.startsWith("get");
81 * Template method which generates the getter method for <code>field</code>
84 * generated property with data about field which is generated as the getter method
85 * @return string with the getter method source code in JAVA format
87 protected def getterMethod(GeneratedProperty field) {
89 public «field.returnType.importedName» «field.getterMethodName»() {
90 «IF field.returnType.importedName.contains("[]")»
91 return «field.fieldName» == null ? null : «field.fieldName».clone();
93 return «field.fieldName»;
99 final protected def getterMethodName(GeneratedProperty field) {
100 val prefix = if(field.returnType.equals(Types.BOOLEAN)) "is" else "get"
101 return '''«prefix»«field.name.toFirstUpper»'''
105 * Template method which generates the setter method for <code>field</code>
108 * generated property with data about field which is generated as the setter method
109 * @return string with the setter method source code in JAVA format
111 final protected def setterMethod(GeneratedProperty field) '''
112 «val returnType = field.returnType.importedName»
113 public «type.name» set«field.name.toFirstUpper»(«returnType» value) {
114 this.«field.fieldName» = value;
120 * Template method which generates method parameters with their types from <code>parameters</code>.
123 * group of generated property instances which are transformed to the method parameters
124 * @return string with the list of the method parameters with their types in JAVA format
126 def final protected asArgumentsDeclaration(Iterable<GeneratedProperty> parameters) '''«IF !parameters.empty»«FOR parameter : parameters SEPARATOR ", "»«parameter.
127 returnType.importedName» «parameter.fieldName»«ENDFOR»«ENDIF»'''
130 * Template method which generates sequence of the names of the class attributes from <code>parameters</code>.
133 * group of generated property instances which are transformed to the sequence of parameter names
134 * @return string with the list of the parameter names of the <code>parameters</code>
136 def final protected asArguments(Iterable<GeneratedProperty> parameters) '''«IF !parameters.empty»«FOR parameter : parameters SEPARATOR ", "»«parameter.
137 fieldName»«ENDFOR»«ENDIF»'''
140 * Template method which generates JAVA comments.
142 * @param comment string with the comment for whole JAVA class
143 * @return string with comment in JAVA format
145 def protected CharSequence asJavadoc(String comment) {
146 if(comment === null) return ''
150 txt = formatToParagraph(txt)
153 «wrapToDocumentation(txt)»
157 def String wrapToDocumentation(String text) {
161 val StringBuilder sb = new StringBuilder().append("/**\n")
162 for (String t : NL_SPLITTER.split(text)) {
175 def protected String formatDataForJavaDoc(GeneratedType type) {
176 val sb = new StringBuilder()
177 val comment = type.comment
178 if (comment !== null) {
179 sb.append(comment.javadoc)
182 appendSnippet(sb, type)
191 def static encodeJavadocSymbols(String description) {
192 if (description.nullOrEmpty) {
196 return TAIL_COMMENT_PATTERN.matcher(AMP_MATCHER.replaceFrom(description, "&")).replaceAll("*/")
199 def protected String formatDataForJavaDoc(GeneratedType type, String additionalComment) {
200 val comment = type.comment
201 if (comment === null) {
207 val sb = new StringBuilder().append(comment.javadoc)
208 appendSnippet(sb, type)
213 .append(additionalComment)
220 def private static void appendSnippet(StringBuilder sb, GeneratedType type) {
221 val optDef = type.yangSourceDefinition
222 if (optDef.present) {
226 if (def instanceof Single) {
229 .append("This class represents the following YANG schema fragment defined in module <b>")
230 .append(def.module.name).append("</b>\n")
232 .append(encodeAngleBrackets(encodeJavadocSymbols(YangTemplate.generateYangSnippet(node))))
235 if (node instanceof SchemaNode) {
236 sb.append("The schema path to identify an instance is\n")
238 .append(formatSchemaPath(def.module.name, node.path.pathFromRoot))
241 if (hasBuilderClass(node)) {
242 val builderName = type.name + "Builder";
244 sb.append("\n<p>To create instances of this class use {@link ").append(builderName)
246 .append("@see ").append(builderName).append('\n')
247 if (node instanceof ListSchemaNode) {
248 val keyDef = node.keyDefinition
249 if (keyDef !== null && !keyDef.empty) {
250 sb.append("@see ").append(type.name).append("Key")
256 } else if (def instanceof Multiple) {
258 for (DocumentedNode schemaNode : def.nodes) {
259 sb.append(encodeAngleBrackets(encodeJavadocSymbols(YangTemplate.generateYangSnippet(schemaNode))))
261 sb.append("</pre>\n")
266 def private static boolean hasBuilderClass(SchemaNode schemaNode) {
267 return schemaNode instanceof ContainerSchemaNode || schemaNode instanceof ListSchemaNode
268 || schemaNode instanceof RpcDefinition || schemaNode instanceof NotificationDefinition;
271 def private static String formatSchemaPath(String moduleName, Iterable<QName> schemaPath) {
272 val sb = new StringBuilder().append(moduleName);
274 var currentElement = Iterables.getFirst(schemaPath, null);
275 for (QName pathElement : schemaPath) {
277 if (!currentElement.namespace.equals(pathElement.namespace)) {
278 currentElement = pathElement
279 sb.append(pathElement)
281 sb.append(pathElement.getLocalName())
284 return sb.toString();
287 def protected String formatDataForJavaDoc(TypeMember type, String additionalComment) {
288 val StringBuilder typeDescriptionBuilder = new StringBuilder();
289 if (!type.comment.nullOrEmpty) {
290 typeDescriptionBuilder.append(formatToParagraph(type.comment))
291 typeDescriptionBuilder.append(NEW_LINE)
292 typeDescriptionBuilder.append(NEW_LINE)
293 typeDescriptionBuilder.append(NEW_LINE)
295 typeDescriptionBuilder.append(additionalComment)
296 var typeDescription = wrapToDocumentation(typeDescriptionBuilder.toString)
302 def asCode(String text) {
303 return "<code>" + text + "</code>"
306 def asLink(String text) {
307 val StringBuilder sb = new StringBuilder()
309 var char lastChar = ' '
310 var boolean badEnding = false
312 if (text.endsWith('.') || text.endsWith(':') || text.endsWith(',')) {
313 tempText = text.substring(0, text.length - 1)
314 lastChar = text.charAt(text.length - 1)
317 sb.append("<a href = \"")
329 protected def formatToParagraph(String text) {
330 if(text === null || text.isEmpty)
333 var formattedText = text
334 val StringBuilder sb = new StringBuilder();
335 var StringBuilder lineBuilder = new StringBuilder();
336 var boolean isFirstElementOnNewLineEmptyChar = false;
338 formattedText = encodeJavadocSymbols(formattedText)
339 formattedText = NL_MATCHER.removeFrom(formattedText)
340 formattedText = TAB_MATCHER.removeFrom(formattedText)
341 formattedText = SPACES_PATTERN.matcher(formattedText).replaceAll(" ")
343 val StringTokenizer tokenizer = new StringTokenizer(formattedText, " ", true);
345 while (tokenizer.hasMoreElements) {
346 val nextElement = tokenizer.nextElement.toString
348 if (lineBuilder.length != 0 && lineBuilder.length + nextElement.length > 80) {
349 if (lineBuilder.charAt(lineBuilder.length - 1) == ' ') {
350 lineBuilder.setLength(0)
351 lineBuilder.append(lineBuilder.substring(0, lineBuilder.length - 1))
353 if (lineBuilder.charAt(0) == ' ') {
354 lineBuilder.setLength(0)
355 lineBuilder.append(lineBuilder.substring(1))
358 sb.append(lineBuilder);
359 lineBuilder.setLength(0)
362 if(nextElement.toString == ' ') {
363 isFirstElementOnNewLineEmptyChar = !isFirstElementOnNewLineEmptyChar;
367 if (isFirstElementOnNewLineEmptyChar) {
368 isFirstElementOnNewLineEmptyChar = !isFirstElementOnNewLineEmptyChar
372 lineBuilder.append(nextElement)
375 sb.append(lineBuilder)
381 def protected generateToString(Collection<GeneratedProperty> properties) '''
382 «IF !properties.empty»
384 public «String.importedName» toString() {
385 «StringBuilder.importedName» builder = new «StringBuilder.importedName»(«type.importedName».class.getSimpleName()).append(" [");
386 boolean first = true;
388 «FOR property : properties»
389 if («property.fieldName» != null) {
393 builder.append(", ");
395 builder.append("«property.fieldName»=");
396 «IF property.returnType.name.contains("[")»
397 builder.append(«Arrays.importedName».toString(«property.fieldName»));
399 builder.append(«property.fieldName»);
403 return builder.append(']').toString();
409 * Template method which generates method parameters with their types from <code>parameters</code>.
412 * list of parameter instances which are transformed to the method parameters
413 * @return string with the list of the method parameters with their types in JAVA format
415 def protected generateParameters(List<MethodSignature.Parameter> parameters) '''«
416 IF !parameters.empty»«
417 FOR parameter : parameters SEPARATOR ", "»«
418 parameter.type.importedName» «parameter.name»«
423 def protected emitConstant(Constant c) '''
424 «IF c.value instanceof QName»
425 «val qname = c.value as QName»
426 «val rev = qname.revision»
427 public static final «c.type.importedName» «c.name» = «QName.name».create("«qname.namespace.toString»",
428 «IF rev.isPresent»"«rev.get»", «ENDIF»"«qname.localName»").intern();
430 public static final «c.type.importedName» «c.name» = «c.value»;