From: Robert Varga Date: Mon, 4 Apr 2016 16:11:10 +0000 (+0200) Subject: Minimize xtend code in mdsal-binding-generator-impl X-Git-Tag: release/boron~163 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=5b52ab597ab7c89fa044d43da090ba719fd13adf;hp=41e160e109257a99d09a81ee9f33a0bce8802c15;p=mdsal.git Minimize xtend code in mdsal-binding-generator-impl This introduces YangTextTemplate as a Java class and moves the trivial bits of java code from xtend to Java. As a side-effect it also fixes space trimming code, which has never worked due to xtend bug, which lead to characters being compared via Char.equals(String). Change-Id: Ia86db1136fca056cf5c0e21b55e1cc904870664a Signed-off-by: Robert Varga --- diff --git a/binding/mdsal-binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/impl/BindingGeneratorImpl.java b/binding/mdsal-binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/impl/BindingGeneratorImpl.java index 9caae2f318..d4e4ad7c46 100644 --- a/binding/mdsal-binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/impl/BindingGeneratorImpl.java +++ b/binding/mdsal-binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/impl/BindingGeneratorImpl.java @@ -2118,7 +2118,7 @@ public class BindingGeneratorImpl implements BindingGenerator { private String createDescription(final SchemaNode schemaNode, final String fullyQualifiedName) { final StringBuilder sb = new StringBuilder(); final String nodeDescription = encodeAngleBrackets(schemaNode.getDescription()); - final String formattedDescription = YangTemplate.formatToParagraph(nodeDescription, 0); + final String formattedDescription = YangTextTemplate.formatToParagraph(nodeDescription, 0); if (!Strings.isNullOrEmpty(formattedDescription)) { sb.append(formattedDescription); @@ -2149,7 +2149,7 @@ public class BindingGeneratorImpl implements BindingGenerator { sb.append("The schema path to identify an instance is"); sb.append(NEW_LINE); sb.append(""); - sb.append(YangTemplate.formatSchemaPath(module.getName(), schemaNode.getPath().getPathFromRoot())); + sb.append(YangTextTemplate.formatSchemaPath(module.getName(), schemaNode.getPath().getPathFromRoot())); sb.append(""); sb.append(NEW_LINE); @@ -2186,7 +2186,7 @@ public class BindingGeneratorImpl implements BindingGenerator { private String createDescription(final Module module) { final StringBuilder sb = new StringBuilder(); final String moduleDescription = encodeAngleBrackets(module.getDescription()); - final String formattedDescription = YangTemplate.formatToParagraph(moduleDescription, 0); + final String formattedDescription = YangTextTemplate.formatToParagraph(moduleDescription, 0); if (!Strings.isNullOrEmpty(formattedDescription)) { sb.append(formattedDescription); diff --git a/binding/mdsal-binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/impl/YangTemplate.xtend b/binding/mdsal-binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/impl/YangTemplate.xtend index df1cc86b08..9c5ee67624 100644 --- a/binding/mdsal-binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/impl/YangTemplate.xtend +++ b/binding/mdsal-binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/impl/YangTemplate.xtend @@ -12,8 +12,6 @@ import java.util.Date import java.util.List import java.util.Map import java.util.Set -import java.util.StringTokenizer -import org.opendaylight.yangtools.yang.common.QName import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode import org.opendaylight.yangtools.yang.model.api.AugmentationSchema import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode @@ -41,15 +39,12 @@ import org.opendaylight.yangtools.yang.model.api.UsesNode import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition.EnumPair import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil -import com.google.common.base.CharMatcher class YangTemplate { // FIXME: this is not thread-safe and seems to be unused! private static var Module module = null - private static val CharMatcher NEWLINE_OR_TAB = CharMatcher.anyOf("\n\t") - def static String generateYangSnipet(SchemaNode schemaNode) { if (schemaNode == null) return '' @@ -103,7 +98,7 @@ class YangTemplate { ''' } - def static writeEnumPair(EnumPair pair) { + def private static writeEnumPair(EnumPair pair) { var boolean hasEnumPairValue = pair.value != null ''' enum «pair.name»«IF !hasEnumPairValue»;«ELSE»{ @@ -113,7 +108,7 @@ class YangTemplate { ''' } - def static String writeModuleImports(Set moduleImports) { + def private static String writeModuleImports(Set moduleImports) { if (moduleImports.nullOrEmpty) return '' @@ -126,12 +121,12 @@ class YangTemplate { ''' } - def static writeRevision(Date moduleRevision, String moduleDescription) { + def private static writeRevision(Date moduleRevision, String moduleDescription) { val revisionIndent = 12 ''' revision «SimpleDateFormatUtil.getRevisionFormat.format(moduleRevision)» { - description "«formatToParagraph(moduleDescription, revisionIndent)»"; + description "«YangTextTemplate.formatToParagraph(moduleDescription, revisionIndent)»"; } ''' } @@ -198,7 +193,7 @@ class YangTemplate { ''' } - def static writeRPCs(Set rpcDefs) { + def private static writeRPCs(Set rpcDefs) { ''' «FOR rpc : rpcDefs» «IF rpc != null» @@ -208,7 +203,7 @@ class YangTemplate { ''' } - def static writeRPC(RpcDefinition rpc) { + def private static writeRPC(RpcDefinition rpc) { var boolean isStatusDeprecated = rpc.status == Status::DEPRECATED ''' «IF isStatusDeprecated» @@ -238,7 +233,7 @@ class YangTemplate { ''' } - def static writeRpcInput(ContainerSchemaNode input) { + def private static writeRpcInput(ContainerSchemaNode input) { if(input == null) return '' @@ -252,7 +247,7 @@ class YangTemplate { ''' } - def static writeRpcOutput(ContainerSchemaNode output) { + def private static writeRpcOutput(ContainerSchemaNode output) { if(output == null) return '' @@ -265,7 +260,7 @@ class YangTemplate { ''' } - def static writeNotifications(Set notifications) { + def private static writeNotifications(Set notifications) { ''' «FOR notification : notifications» «IF notification != null» @@ -275,7 +270,7 @@ class YangTemplate { ''' } - def static writeNotification(NotificationDefinition notification) { + def private static writeNotification(NotificationDefinition notification) { var boolean isStatusDeprecated = notification.status == Status::DEPRECATED ''' «IF isStatusDeprecated» @@ -309,7 +304,7 @@ class YangTemplate { ''' } - def static writeUnknownSchemaNodes(List unknownSchemaNodes) { + def private static writeUnknownSchemaNodes(List unknownSchemaNodes) { if (unknownSchemaNodes.nullOrEmpty) return '' @@ -320,11 +315,11 @@ class YangTemplate { ''' } - def static writeUnknownSchemaNode(UnknownSchemaNode unknownSchemaNode) { + def private static writeUnknownSchemaNode(UnknownSchemaNode unknownSchemaNode) { return '' } - def static writeUsesNodes(Set usesNodes) { + def private static writeUsesNodes(Set usesNodes) { if (usesNodes == null) { return '' } @@ -338,7 +333,7 @@ class YangTemplate { ''' } - def static writeUsesNode(UsesNode usesNode) { + def private static writeUsesNode(UsesNode usesNode) { val hasRefines = !usesNode.refines.empty ''' @@ -350,7 +345,7 @@ class YangTemplate { ''' } - def static writeRefines(Map refines) { + def private static writeRefines(Map refines) { ''' «FOR path : refines.keySet» «val schemaNode = refines.get(path)» @@ -359,7 +354,7 @@ class YangTemplate { ''' } - def static writeRefine(SchemaPath path, SchemaNode schemaNode) { + def private static writeRefine(SchemaPath path, SchemaNode schemaNode) { ''' refine «path.pathFromRoot.last» { «IF schemaNode instanceof DataSchemaNode» @@ -369,17 +364,7 @@ class YangTemplate { ''' } - def static writeTypeDefinitions(Set> typeDefinitions) { - ''' - «FOR typeDefinition : typeDefinitions» - «IF typeDefinition != null» - «writeTypeDefinition(typeDefinition)» - «ENDIF» - «ENDFOR» - ''' - } - - def static writeTypeDefinition(TypeDefinition typeDefinition) { + def private static writeTypeDefinition(TypeDefinition typeDefinition) { var boolean isStatusDeprecated = typeDefinition.status == Status::DEPRECATED ''' «IF isStatusDeprecated» @@ -392,7 +377,7 @@ class YangTemplate { ''' } - def static writeIdentities(Set identities) { + def private static writeIdentities(Set identities) { if (identities.nullOrEmpty) return '' ''' @@ -402,7 +387,7 @@ class YangTemplate { ''' } - def static writeIdentity(IdentitySchemaNode identity) { + def private static writeIdentity(IdentitySchemaNode identity) { if (identity == null) return '' ''' @@ -425,7 +410,7 @@ class YangTemplate { ''' } - def static writeIdentityNs(IdentitySchemaNode identity) { + def private static writeIdentityNs(IdentitySchemaNode identity) { if(module == null) return '' @@ -436,7 +421,7 @@ class YangTemplate { return '' } - def static writeFeatures(Set features) { + def private static writeFeatures(Set features) { ''' «FOR feature : features» «IF feature != null» @@ -446,7 +431,7 @@ class YangTemplate { ''' } - def static writeFeature(FeatureDefinition featureDef) { + def private static writeFeature(FeatureDefinition featureDef) { ''' feature «featureDef.QName.localName» { «IF !featureDef.description.nullOrEmpty» @@ -464,7 +449,7 @@ class YangTemplate { ''' } - def static writeExtensions(List extensions) { + def private static writeExtensions(List extensions) { ''' «FOR anExtension : extensions» «IF anExtension != null» @@ -474,7 +459,7 @@ class YangTemplate { ''' } - def static writeExtension(ExtensionDefinition extensionDef) { + def private static writeExtension(ExtensionDefinition extensionDef) { var boolean isStatusDeprecated = extensionDef.status == Status::DEPRECATED ''' @@ -500,7 +485,7 @@ class YangTemplate { ''' } - def static writeDeviations(Set deviations) { + def private static writeDeviations(Set deviations) { ''' «FOR deviation : deviations» «IF deviation != null» @@ -510,7 +495,7 @@ class YangTemplate { ''' } - def static writeDeviation(Deviation deviation) { + def private static writeDeviation(Deviation deviation) { ''' deviation «deviation.targetPath» { «IF !deviation.reference.nullOrEmpty» @@ -524,7 +509,7 @@ class YangTemplate { ''' } - def static writeAugments(Set augments) { + def private static writeAugments(Set augments) { ''' «FOR augment : augments» «IF augment != null» @@ -534,7 +519,7 @@ class YangTemplate { ''' } - def static writeDataSchemaNodes(Collection dataSchemaNodes) { + def private static writeDataSchemaNodes(Collection dataSchemaNodes) { ''' «FOR schemaNode : dataSchemaNodes» «writeDataSchemaNode(schemaNode)» @@ -542,7 +527,7 @@ class YangTemplate { ''' } - def static CharSequence writeGroupingDefs(Set groupingDefs) { + def private static CharSequence writeGroupingDefs(Set groupingDefs) { ''' «FOR groupingDef : groupingDefs» «IF groupingDef != null» @@ -552,9 +537,9 @@ class YangTemplate { ''' } - def static writeAugment(AugmentationSchema augment) { + def private static writeAugment(AugmentationSchema augment) { ''' - augment «formatToAugmentPath(augment.targetPath.pathFromRoot)» { + augment «YangTextTemplate.formatToAugmentPath(augment.targetPath.pathFromRoot)» { «IF augment.whenCondition != null && !augment.whenCondition.toString.nullOrEmpty» when "«augment.whenCondition.toString»"; «ENDIF» @@ -579,7 +564,7 @@ class YangTemplate { ''' } - def static writeGroupingDef(GroupingDefinition groupingDef) { + def private static writeGroupingDef(GroupingDefinition groupingDef) { var boolean isStatusDeprecated = groupingDef.status == Status::DEPRECATED ''' «IF isStatusDeprecated» @@ -602,7 +587,7 @@ class YangTemplate { ''' } - def static writeContSchemaNode(ContainerSchemaNode contSchemaNode) { + def private static writeContSchemaNode(ContainerSchemaNode contSchemaNode) { var boolean isStatusDeprecated = contSchemaNode.status == Status::DEPRECATED ''' «IF isStatusDeprecated» @@ -631,7 +616,7 @@ class YangTemplate { ''' } - def static writeAnyXmlSchemaNode(AnyXmlSchemaNode anyXmlSchemaNode) { + def private static writeAnyXmlSchemaNode(AnyXmlSchemaNode anyXmlSchemaNode) { var boolean isStatusDeprecated = anyXmlSchemaNode.status == Status::DEPRECATED ''' «IF isStatusDeprecated» @@ -644,7 +629,7 @@ class YangTemplate { ''' } - def static writeLeafSchemaNode(LeafSchemaNode leafSchemaNode) { + def private static writeLeafSchemaNode(LeafSchemaNode leafSchemaNode) { var boolean isStatusDeprecated = leafSchemaNode.status == Status::DEPRECATED ''' «IF isStatusDeprecated» @@ -659,7 +644,7 @@ class YangTemplate { ''' } - def static writeLeafListSchemaNode(LeafListSchemaNode leafListSchemaNode) { + def private static writeLeafListSchemaNode(LeafListSchemaNode leafListSchemaNode) { var boolean isStatusDeprecated = leafListSchemaNode.status == Status::DEPRECATED ''' «IF isStatusDeprecated» @@ -674,7 +659,7 @@ class YangTemplate { ''' } - def static writeChoiceCaseNode(ChoiceCaseNode choiceCaseNode) { + def private static writeChoiceCaseNode(ChoiceCaseNode choiceCaseNode) { var boolean isStatusDeprecated = choiceCaseNode.status == Status::DEPRECATED ''' «IF isStatusDeprecated» @@ -691,7 +676,7 @@ class YangTemplate { ''' } - def static writeChoiceNode(ChoiceSchemaNode choiceNode) { + def private static writeChoiceNode(ChoiceSchemaNode choiceNode) { var boolean isStatusDeprecated = choiceNode.status == Status::DEPRECATED ''' «IF isStatusDeprecated» @@ -708,7 +693,7 @@ class YangTemplate { ''' } - def static writeListSchemaNode(ListSchemaNode listSchemaNode) { + def private static writeListSchemaNode(ListSchemaNode listSchemaNode) { var boolean isStatusDeprecated = listSchemaNode.status == Status::DEPRECATED ''' @@ -740,7 +725,7 @@ class YangTemplate { ''' } - def static CharSequence writeDataSchemaNode(DataSchemaNode child) { + def private static CharSequence writeDataSchemaNode(DataSchemaNode child) { ''' «IF child instanceof ContainerSchemaNode» «writeContSchemaNode(child)» @@ -765,99 +750,4 @@ class YangTemplate { «ENDIF» ''' } - - static def String formatSchemaPath(String moduleName, Iterable schemaPath) { - var currentElement = schemaPath.head - val StringBuilder sb = new StringBuilder() - sb.append(moduleName) - - for(pathElement : schemaPath) { - if(!currentElement.namespace.equals(pathElement.namespace)) { - currentElement = pathElement - sb.append('/') - sb.append(pathElement) - } - else { - sb.append('/') - sb.append(pathElement.localName) - } - } - return sb.toString - } - - static def String formatToParagraph(String text, int nextLineIndent) { - if (text == null || text.isEmpty()) - return ''; - - var String formattedText = text; - val StringBuilder sb = new StringBuilder(); - val StringBuilder lineBuilder = new StringBuilder(); - var boolean isFirstElementOnNewLineEmptyChar = false; - val lineIndent = computeNextLineIndent(nextLineIndent); - - formattedText = NEWLINE_OR_TAB.removeFrom(formattedText); - formattedText = formattedText.replaceAll(" +", " "); - - val StringTokenizer tokenizer = new StringTokenizer(formattedText, " ", true); - - while (tokenizer.hasMoreElements()) { - val String nextElement = tokenizer.nextElement().toString(); - - if (lineBuilder.length() + nextElement.length() > 80) { - if (lineBuilder.charAt(lineBuilder.length() - 1) == ' ') { - lineBuilder.setLength(0); - lineBuilder.append(lineBuilder.substring(0, lineBuilder.length() - 1)); - } - if (lineBuilder.charAt(0) == ' ') { - lineBuilder.setLength(0); - lineBuilder.append(lineBuilder.substring(1)); - } - - sb.append(lineBuilder); - lineBuilder.setLength(0); - sb.append("\n"); - - if (nextLineIndent > 0) { - sb.append(lineIndent) - } - - if (nextElement.toString().equals(" ")) - isFirstElementOnNewLineEmptyChar = !isFirstElementOnNewLineEmptyChar; - } - if (isFirstElementOnNewLineEmptyChar) { - isFirstElementOnNewLineEmptyChar = !isFirstElementOnNewLineEmptyChar; - } else { - lineBuilder.append(nextElement); - } - } - sb.append(lineBuilder); - sb.append("\n"); - - return sb.toString(); - } - - def private static formatToAugmentPath(Iterable schemaPath) { - val StringBuilder sb = new StringBuilder(); - - for(pathElement : schemaPath) { - val ns = pathElement.namespace - val localName = pathElement.localName - - sb.append("\\(") - sb.append(ns) - sb.append(')') - sb.append(localName) - } - return sb.toString - } - - private static def computeNextLineIndent(int nextLineIndent) { - val StringBuilder sb = new StringBuilder() - var i = 0 - while (i < nextLineIndent) { - sb.append(' ') - i = i + 1 - } - return sb.toString - } } diff --git a/binding/mdsal-binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/impl/YangTextTemplate.java b/binding/mdsal-binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/impl/YangTextTemplate.java new file mode 100644 index 0000000000..0c1db74bf2 --- /dev/null +++ b/binding/mdsal-binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/impl/YangTextTemplate.java @@ -0,0 +1,97 @@ +/* + * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.yangtools.sal.binding.generator.impl; + +import com.google.common.base.CharMatcher; +import com.google.common.base.Strings; +import com.google.common.collect.Iterables; +import java.util.StringTokenizer; +import org.opendaylight.yangtools.yang.common.QName; + +final class YangTextTemplate { + private static final CharMatcher NEWLINE_OR_TAB = CharMatcher.anyOf("\n\t"); + + private YangTextTemplate() { + throw new UnsupportedOperationException(); + } + + static String formatSchemaPath(final String moduleName, final Iterable schemaPath) { + final StringBuilder sb = new StringBuilder(); + sb.append(moduleName); + + QName currentElement = Iterables.getFirst(schemaPath, null); + for (QName pathElement : schemaPath) { + sb.append('/'); + if (!currentElement.getNamespace().equals(pathElement.getNamespace())) { + currentElement = pathElement; + sb.append(pathElement); + } else { + sb.append(pathElement.getLocalName()); + } + } + return sb.toString(); + } + + static String formatToAugmentPath(final Iterable schemaPath) { + final StringBuilder sb = new StringBuilder(); + for (QName pathElement : schemaPath) { + sb.append("\\(").append(pathElement.getNamespace()).append(')').append(pathElement.getLocalName()); + } + return sb.toString(); + } + + static String formatToParagraph(final String text, final int nextLineIndent) { + if (Strings.isNullOrEmpty(text)) { + return ""; + } + + final StringBuilder sb = new StringBuilder(); + final StringBuilder lineBuilder = new StringBuilder(); + boolean isFirstElementOnNewLineEmptyChar = false; + final String lineIndent = Strings.repeat(" ", nextLineIndent); + + String formattedText = NEWLINE_OR_TAB.removeFrom(text); + formattedText = formattedText.replaceAll(" +", " "); + + final StringTokenizer tokenizer = new StringTokenizer(formattedText, " ", true); + + while (tokenizer.hasMoreElements()) { + final String nextElement = tokenizer.nextElement().toString(); + + if (lineBuilder.length() + nextElement.length() > 80) { + // Trim trailing whitespace + for (int i = lineBuilder.length() - 1; i >= 0 && lineBuilder.charAt(i) != ' '; --i) { + lineBuilder.setLength(i); + } + + // Trim leading whitespace + while (lineBuilder.charAt(0) == ' ') { + lineBuilder.deleteCharAt(0); + } + + sb.append(lineBuilder).append('\n'); + lineBuilder.setLength(0); + + if (nextLineIndent > 0) { + sb.append(lineIndent); + } + + if (" ".equals(nextElement)) { + isFirstElementOnNewLineEmptyChar = true; + } + } + if (isFirstElementOnNewLineEmptyChar) { + isFirstElementOnNewLineEmptyChar = false; + } else { + lineBuilder.append(nextElement); + } + } + + return sb.append(lineBuilder).append('\n').toString(); + } +}