Bug 1411 #5 BindingGeneratorImpl decomposition - Notifications
[mdsal.git] / binding2 / mdsal-binding2-generator-impl / src / main / java / org / opendaylight / mdsal / binding / javav2 / generator / impl / AuxiliaryGenUtils.java
1 /*
2  * Copyright (c) 2017 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.mdsal.binding.javav2.generator.impl;
10
11 import static org.opendaylight.mdsal.binding.javav2.generator.util.BindingGeneratorUtil.encodeAngleBrackets;
12 import static org.opendaylight.mdsal.binding.javav2.generator.util.Types.BOOLEAN;
13
14 import com.google.common.annotations.Beta;
15 import com.google.common.annotations.VisibleForTesting;
16 import com.google.common.base.Splitter;
17 import com.google.common.base.Strings;
18 import com.google.common.collect.Iterables;
19 import com.google.common.collect.Sets;
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.Set;
25 import java.util.regex.Pattern;
26 import org.opendaylight.mdsal.binding.javav2.generator.impl.txt.yangTemplateForModule;
27 import org.opendaylight.mdsal.binding.javav2.generator.impl.txt.yangTemplateForNode;
28 import org.opendaylight.mdsal.binding.javav2.generator.impl.txt.yangTemplateForNodes;
29 import org.opendaylight.mdsal.binding.javav2.generator.impl.util.YangTextTemplate;
30 import org.opendaylight.mdsal.binding.javav2.generator.spi.TypeProvider;
31 import org.opendaylight.mdsal.binding.javav2.generator.util.JavaIdentifier;
32 import org.opendaylight.mdsal.binding.javav2.generator.util.NonJavaCharsConverter;
33 import org.opendaylight.mdsal.binding.javav2.generator.util.Types;
34 import org.opendaylight.mdsal.binding.javav2.generator.util.generated.type.builder.GeneratedTOBuilderImpl;
35 import org.opendaylight.mdsal.binding.javav2.generator.yang.types.TypeProviderImpl;
36 import org.opendaylight.mdsal.binding.javav2.model.api.AccessModifier;
37 import org.opendaylight.mdsal.binding.javav2.model.api.Constant;
38 import org.opendaylight.mdsal.binding.javav2.model.api.Type;
39 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.EnumBuilder;
40 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.GeneratedPropertyBuilder;
41 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.GeneratedTOBuilder;
42 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.GeneratedTypeBuilder;
43 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.GeneratedTypeBuilderBase;
44 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.MethodSignatureBuilder;
45 import org.opendaylight.mdsal.binding.javav2.spec.runtime.BindingNamespaceType;
46 import org.opendaylight.yangtools.yang.common.QName;
47 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
48 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
49 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
50 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
51 import org.opendaylight.yangtools.yang.model.api.Module;
52 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
53 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
54 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
55 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
56 import org.opendaylight.yangtools.yang.model.api.Status;
57 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
58 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
59 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
60 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
61 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
62 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
63
64 /**
65  * Auxiliary util class for {@link GenHelperUtil} class
66  */
67 @Beta
68 final class AuxiliaryGenUtils {
69
70     private static final Splitter BSDOT_SPLITTER = Splitter.on("\\.");
71     private static final char NEW_LINE = '\n';
72     private static final Pattern UNICODE_CHAR_PATTERN = Pattern.compile("\\\\+u");
73
74     /**
75      * Constant with the concrete name of identifier.
76      */
77     private static final String AUGMENT_IDENTIFIER_NAME = "augment-identifier";
78
79     /**
80      * Constant with the concrete name of namespace.
81      */
82     private static final String YANG_EXT_NAMESPACE = "urn:opendaylight:yang:extension:yang-ext";
83
84     private AuxiliaryGenUtils() {
85         throw new UnsupportedOperationException("Util class");
86     }
87
88     static void annotateDeprecatedIfNecessary(final Status status, final GeneratedTypeBuilder builder) {
89         if (status == Status.DEPRECATED) {
90             builder.addAnnotation("", "Deprecated");
91         }
92     }
93
94     private static boolean hasBuilderClass(final SchemaNode schemaNode) {
95         if (schemaNode instanceof ContainerSchemaNode || schemaNode instanceof ListSchemaNode ||
96                 schemaNode instanceof RpcDefinition || schemaNode instanceof NotificationDefinition) {
97             return true;
98         }
99         return false;
100     }
101
102     static Constant qNameConstant(final GeneratedTypeBuilderBase<?> toBuilder, final String constantName,
103                                   final QName name) {
104         return toBuilder.addConstant(Types.typeForClass(QName.class), constantName, name);
105     }
106
107     /**
108      * Created a method signature builder as part of
109      * <code>interfaceBuilder</code>.
110      *
111      * The method signature builder is created for the getter method of
112      * <code>schemaNodeName</code>. Also <code>comment</code> and
113      * <code>returnType</code> information are added to the builder.
114      *
115      * @param interfaceBuilder
116      *            generated type builder for which the getter method should be
117      *            created
118      * @param schemaNodeName
119      *            string with schema node name. The name will be the part of the
120      *            getter method name.
121      * @param comment
122      *            string with comment for the getter method
123      * @param returnType
124      *            type which represents the return type of the getter method
125      * @param status
126      *            status from yang file, for deprecated annotation
127      * @return method signature builder which represents the getter method of
128      *         <code>interfaceBuilder</code>
129      */
130     static MethodSignatureBuilder constructGetter(final GeneratedTypeBuilder interfaceBuilder,
131                                                   final String schemaNodeName, final String comment, final Type returnType, final Status status) {
132
133         final MethodSignatureBuilder getMethod = interfaceBuilder
134                 .addMethod(getterMethodName(schemaNodeName, returnType));
135         if (status == Status.DEPRECATED) {
136             getMethod.addAnnotation("", "Deprecated");
137         }
138         getMethod.setComment(encodeAngleBrackets(comment));
139         getMethod.setReturnType(returnType);
140         return getMethod;
141     }
142
143     /**
144      * Creates the name of the getter method name from <code>localName</code>.
145      *
146      * @param localName
147      *            string with the name of the getter method
148      * @param returnType
149      *            return type
150      * @return string with the name of the getter method for
151      *         <code>methodName</code> in JAVA method format
152      */
153     private static String getterMethodName(final String localName, final Type returnType) {
154         final StringBuilder method = new StringBuilder();
155         if (BOOLEAN.equals(returnType)) {
156             method.append("is");
157         } else {
158             method.append("get");
159         }
160         // underscore used as separator for distinction of method parts in convertIdentifier()
161         method.append('_').append(localName);
162         return NonJavaCharsConverter.convertIdentifier(method.toString(), JavaIdentifier.METHOD);
163     }
164
165     static String createDescription(final SchemaNode schemaNode, final String fullyQualifiedName,
166                                     final SchemaContext schemaContext, final boolean verboseClassComments) {
167         final StringBuilder sb = new StringBuilder();
168         final String nodeDescription = encodeAngleBrackets(schemaNode.getDescription());
169         final String formattedDescription = YangTextTemplate.formatToParagraph(nodeDescription, 0);
170
171         if (!Strings.isNullOrEmpty(formattedDescription)) {
172             sb.append(formattedDescription);
173             sb.append(NEW_LINE);
174         }
175
176         if (verboseClassComments) {
177             final Module module = SchemaContextUtil.findParentModule(schemaContext, schemaNode);
178             final StringBuilder linkToBuilderClass = new StringBuilder();
179             final String[] namespace = Iterables.toArray(BSDOT_SPLITTER.split(fullyQualifiedName), String.class);
180             final String className = namespace[namespace.length - 1];
181
182             if (hasBuilderClass(schemaNode)) {
183                 linkToBuilderClass.append(className);
184                 linkToBuilderClass.append("Builder");
185             }
186
187             sb.append("<p>");
188             sb.append("This class represents the following YANG schema fragment defined in module <b>");
189             sb.append(module.getName());
190             sb.append("</b>");
191             sb.append(NEW_LINE);
192             sb.append("<pre>");
193             sb.append(NEW_LINE);
194             sb.append(encodeAngleBrackets(yangTemplateForNode.render(schemaNode).body()));
195             sb.append("</pre>");
196             sb.append(NEW_LINE);
197             sb.append("The schema path to identify an instance is");
198             sb.append(NEW_LINE);
199             sb.append("<i>");
200             sb.append(YangTextTemplate.formatSchemaPath(module.getName(), schemaNode.getPath().getPathFromRoot()));
201             sb.append("</i>");
202             sb.append(NEW_LINE);
203
204             if (hasBuilderClass(schemaNode)) {
205                 sb.append(NEW_LINE);
206                 sb.append("<p>To create instances of this class use " + "{@link " + linkToBuilderClass + "}.");
207                 sb.append(NEW_LINE);
208                 sb.append("@see ");
209                 sb.append(linkToBuilderClass);
210                 sb.append(NEW_LINE);
211                 if (schemaNode instanceof ListSchemaNode) {
212                     final List<QName> keyDef = ((ListSchemaNode)schemaNode).getKeyDefinition();
213                     if (keyDef != null && !keyDef.isEmpty()) {
214                         sb.append("@see ");
215                         sb.append(className);
216                         sb.append("Key");
217                     }
218                     sb.append(NEW_LINE);
219                 }
220             }
221         }
222
223         return replaceAllIllegalChars(sb);
224     }
225
226     static String createDescription(final Module module, final boolean verboseClassComments) {
227         final StringBuilder sb = new StringBuilder();
228         final String moduleDescription = encodeAngleBrackets(module.getDescription());
229         final String formattedDescription = YangTextTemplate.formatToParagraph(moduleDescription, 0);
230
231         if (!Strings.isNullOrEmpty(formattedDescription)) {
232             sb.append(formattedDescription);
233             sb.append(NEW_LINE);
234         }
235
236         if (verboseClassComments) {
237             sb.append("<p>");
238             sb.append("This class represents the following YANG schema fragment defined in module <b>");
239             sb.append(module.getName());
240             sb.append("</b>");
241             sb.append(NEW_LINE);
242             sb.append("<pre>");
243             sb.append(NEW_LINE);
244             sb.append(encodeAngleBrackets(yangTemplateForModule.render(module).body()));
245             sb.append("</pre>");
246         }
247
248         return replaceAllIllegalChars(sb);
249     }
250
251     static String createDescription(final Set<? extends SchemaNode> schemaNodes, final String moduleName, final
252             boolean verboseClassComments) {
253         final StringBuilder sb = new StringBuilder();
254
255         if (!isNullOrEmpty(schemaNodes)) {
256             final SchemaNode node = schemaNodes.iterator().next();
257
258             if (node instanceof RpcDefinition) {
259                 sb.append("Interface for implementing the following YANG RPCs defined in module <b>" + moduleName + "</b>");
260             } else if (node instanceof NotificationDefinition) {
261                 sb.append("Interface for receiving the following YANG notifications defined in module <b>" + moduleName + "</b>");
262             }
263         }
264         sb.append(NEW_LINE);
265
266         if (verboseClassComments) {
267             sb.append("<pre>");
268             sb.append(NEW_LINE);
269             sb.append(encodeAngleBrackets(yangTemplateForNodes.render(schemaNodes).body()));
270             sb.append("</pre>");
271             sb.append(NEW_LINE);
272         }
273
274         return replaceAllIllegalChars(sb);
275     }
276
277     private static boolean isNullOrEmpty(final Collection<?> list) {
278         return list == null || list.isEmpty();
279     }
280
281     /**
282      * Returns first unique name for the augment generated type builder. The
283      * generated type builder name for augment consists from name of augmented
284      * node and serial number of its augmentation.
285      *
286      * @param builders
287      *            map of builders which were created in the package to which the
288      *            augmentation belongs
289      * @param genTypeName
290      *            string with name of augmented node
291      * @return string with unique name for augmentation builder
292      */
293     static String augGenTypeName(final Map<String, GeneratedTypeBuilder> builders, final String genTypeName) {
294         int index = 1;
295         if (builders != null) {
296             while (builders.containsKey(genTypeName + index)) {
297                 index = index + 1;
298             }
299         }
300         return genTypeName + index;
301     }
302
303     /**
304      * @param unknownSchemaNodes unknows schema nodes
305      * @return nodeParameter of UnknownSchemaNode
306      */
307     static String getAugmentIdentifier(final List<UnknownSchemaNode> unknownSchemaNodes) {
308         for (final UnknownSchemaNode unknownSchemaNode : unknownSchemaNodes) {
309             final QName nodeType = unknownSchemaNode.getNodeType();
310             if (AUGMENT_IDENTIFIER_NAME.equals(nodeType.getLocalName())
311                     && YANG_EXT_NAMESPACE.equals(nodeType.getNamespace().toString())) {
312                 return unknownSchemaNode.getNodeParameter();
313             }
314         }
315         return null;
316     }
317
318     /**
319      * Adds enumeration builder created from <code>enumTypeDef</code> to
320      * <code>typeBuilder</code>.
321      *
322      * Each <code>enumTypeDef</code> item is added to builder with its name and
323      * value.
324      *
325      * @param enumTypeDef
326      *            EnumTypeDefinition contains enum data
327      * @param enumName
328      *            string contains name which will be assigned to enumeration
329      *            builder
330      * @param typeBuilder
331      *            GeneratedTypeBuilder to which will be enum builder assigned
332      * @param module
333      *            Module in which type should be generated
334      * @return enumeration builder which contains data from
335      *         <code>enumTypeDef</code>
336      */
337     static EnumBuilder resolveInnerEnumFromTypeDefinition(final EnumTypeDefinition enumTypeDef, final QName enumName,
338         final Map<Module, ModuleContext> genCtx, final GeneratedTypeBuilder typeBuilder, final Module module) {
339         if (enumTypeDef != null && typeBuilder != null && enumTypeDef.getQName().getLocalName() != null) {
340             final EnumBuilder enumBuilder = typeBuilder.addEnumeration(enumName.getLocalName());
341             final String enumTypedefDescription = encodeAngleBrackets(enumTypeDef.getDescription());
342             enumBuilder.setDescription(enumTypedefDescription);
343             enumBuilder.updateEnumPairsFromEnumTypeDef(enumTypeDef);
344             final ModuleContext ctx = genCtx.get(module);
345             ctx.addInnerTypedefType(enumTypeDef.getPath(), enumBuilder);
346             return enumBuilder;
347         }
348         return null;
349     }
350
351
352     /**
353      * Builds generated TO builders for <code>typeDef</code> of type
354      * {@link UnionTypeDefinition} or {@link BitsTypeDefinition} which are
355      * also added to <code>typeBuilder</code> as enclosing transfer object.
356      *
357      * If more then one generated TO builder is created for enclosing then all
358      * of the generated TO builders are added to <code>typeBuilder</code> as
359      * enclosing transfer objects.
360      *
361      * @param typeDef
362      *            type definition which can be of type <code>UnionType</code> or
363      *            <code>BitsTypeDefinition</code>
364      * @param typeBuilder
365      *            generated type builder to which is added generated TO created
366      *            from <code>typeDef</code>
367      * @param leaf
368      *            string with name for generated TO builder
369      * @param parentModule
370      *            parent module
371      * @return generated TO builder for <code>typeDef</code>
372      */
373     static GeneratedTOBuilder addTOToTypeBuilder(final TypeDefinition<?> typeDef, final GeneratedTypeBuilder
374             typeBuilder, final DataSchemaNode leaf, final Module parentModule, final TypeProvider typeProvider,
375             final SchemaContext schemaContext) {
376         final String classNameFromLeaf = leaf.getQName().getLocalName();
377         final List<GeneratedTOBuilder> genTOBuilders = new ArrayList<>();
378         final String packageName = typeBuilder.getFullyQualifiedName();
379         if (typeDef instanceof UnionTypeDefinition) {
380             final List<GeneratedTOBuilder> types = ((TypeProviderImpl) typeProvider)
381                     .provideGeneratedTOBuildersForUnionTypeDef(packageName, ((UnionTypeDefinition) typeDef),
382                             classNameFromLeaf, leaf, schemaContext, ((TypeProviderImpl) typeProvider).getGenTypeDefsContextMap());
383             genTOBuilders.addAll(types);
384
385             GeneratedTOBuilder resultTOBuilder;
386             if (types.isEmpty()) {
387                 throw new IllegalStateException("No GeneratedTOBuilder objects generated from union " + typeDef);
388             }
389             resultTOBuilder = types.remove(0);
390             for (final GeneratedTOBuilder genTOBuilder : types) {
391                 resultTOBuilder.addEnclosingTransferObject(genTOBuilder);
392             }
393
394             final GeneratedPropertyBuilder genPropBuilder = resultTOBuilder.addProperty("value");
395             genPropBuilder.setReturnType(Types.CHAR_ARRAY);
396             resultTOBuilder.addEqualsIdentity(genPropBuilder);
397             resultTOBuilder.addHashIdentity(genPropBuilder);
398             resultTOBuilder.addToStringProperty(genPropBuilder);
399
400         } else if (typeDef instanceof BitsTypeDefinition) {
401             genTOBuilders.add((((TypeProviderImpl) typeProvider)).provideGeneratedTOBuilderForBitsTypeDefinition(
402                     packageName, typeDef, classNameFromLeaf, parentModule.getName()));
403         }
404         if (!genTOBuilders.isEmpty()) {
405             for (final GeneratedTOBuilder genTOBuilder : genTOBuilders) {
406                 typeBuilder.addEnclosingTransferObject(genTOBuilder);
407             }
408             return genTOBuilders.get(0);
409         }
410         return null;
411
412     }
413
414     @SuppressWarnings({ "unchecked", "rawtypes" })
415     static Type createReturnTypeForUnion(final GeneratedTOBuilder genTOBuilder, final TypeDefinition<?> typeDef,
416             final GeneratedTypeBuilder typeBuilder, final Module parentModule, final TypeProvider typeProvider) {
417         final GeneratedTOBuilderImpl returnType = new GeneratedTOBuilderImpl(genTOBuilder.getPackageName(),
418                 genTOBuilder.getName());
419         final String typedefDescription = encodeAngleBrackets(typeDef.getDescription());
420
421         returnType.setDescription(typedefDescription);
422         returnType.setReference(typeDef.getReference());
423         returnType.setSchemaPath((List) typeDef.getPath().getPathFromRoot());
424         returnType.setModuleName(parentModule.getName());
425
426         genTOBuilder.setTypedef(true);
427         genTOBuilder.setIsUnion(true);
428         TypeProviderImpl.addUnitsToGenTO(genTOBuilder, typeDef.getUnits());
429
430
431
432         final GeneratedTOBuilder unionBuilder = createUnionBuilder(genTOBuilder,typeBuilder);
433
434
435         final MethodSignatureBuilder method = unionBuilder.addMethod("getDefaultInstance");
436         method.setReturnType(returnType);
437         method.addParameter(Types.STRING, "defaultValue");
438         method.setAccessModifier(AccessModifier.PUBLIC);
439         method.setStatic(true);
440
441         final Set<Type> types = ((TypeProviderImpl) typeProvider).getAdditionalTypes().get(parentModule);
442         if (types == null) {
443             ((TypeProviderImpl) typeProvider).getAdditionalTypes().put(parentModule,
444                     Sets.newHashSet(unionBuilder.toInstance()));
445         } else {
446             types.add(unionBuilder.toInstance());
447         }
448         return returnType.toInstance();
449     }
450
451     private static GeneratedTOBuilder createUnionBuilder(final GeneratedTOBuilder genTOBuilder, final GeneratedTypeBuilder typeBuilder) {
452         final String outerCls = Types.getOuterClassName(genTOBuilder);
453         final StringBuilder name;
454         if (outerCls != null) {
455             name = new StringBuilder(outerCls);
456         } else {
457             name = new StringBuilder();
458         }
459         name.append(genTOBuilder.getName());
460         name.append("Builder");
461         final GeneratedTOBuilderImpl unionBuilder = new GeneratedTOBuilderImpl(typeBuilder.getPackageName(),name.toString());
462         unionBuilder.setIsUnionBuilder(true);
463         return unionBuilder;
464     }
465
466     static boolean isInnerType(final LeafSchemaNode leaf, final TypeDefinition<?> type) {
467         if (leaf.getPath().equals(type.getPath())) {
468             return true;
469         }
470         if (leaf.getPath().equals(type.getPath().getParent())) {
471             return true;
472         }
473
474         return false;
475     }
476
477     /**
478      * Generates for the <code>list</code> which contains any list keys special
479      * generated TO builder.
480      *
481      * @param packageName
482      *            string with package name to which the list belongs
483      * @param list
484      *            schema node of list
485      * @return generated TO builder which represents the keys of the
486      *         <code>list</code> or empty TO builder if <code>list</code> is null or list of
487      *         key definitions is null or empty.
488      */
489     static GeneratedTOBuilder resolveListKeyTOBuilder(final String packageName, final ListSchemaNode list) {
490         GeneratedTOBuilder genTOBuilder = null;
491         if ((list.getKeyDefinition() != null) && (!list.getKeyDefinition().isEmpty())) {
492             // underscore used as separator for distinction of class name parts
493             final String genTOName =
494                     new StringBuilder(list.getQName().getLocalName()).append('_').append(BindingNamespaceType.Key)
495                             .toString();
496             genTOBuilder =
497                     new GeneratedTOBuilderImpl(new StringBuilder(packageName).append(".wrapper").toString(), genTOName);
498         }
499         return genTOBuilder;
500     }
501
502     /**
503      * Converts <code>leaf</code> schema node to property of generated TO
504      * builder.
505      *
506      * @param toBuilder
507      *            generated TO builder to which is <code>leaf</code> added as
508      *            property
509      * @param leaf
510      *            leaf schema node which is added to <code>toBuilder</code> as
511      *            property
512      * @param returnType
513      *            property type
514      * @param isReadOnly
515      *            boolean value which says if leaf property is|isn't read only
516      * @return boolean value
517      *         <ul>
518      *         <li>false - if <code>leaf</code>, <code>toBuilder</code> or leaf
519      *         name equals null or if leaf is added by <i>uses</i>.</li>
520      *         <li>true - other cases</li>
521      *         </ul>
522      */
523     static boolean resolveLeafSchemaNodeAsProperty(final GeneratedTOBuilder toBuilder, final LeafSchemaNode leaf,
524         final Type returnType, final boolean isReadOnly) {
525
526         if (returnType == null) {
527             return false;
528         }
529         final String leafName = leaf.getQName().getLocalName();
530         final String leafDesc = encodeAngleBrackets(leaf.getDescription());
531         final GeneratedPropertyBuilder propBuilder =
532                 toBuilder.addProperty(NonJavaCharsConverter.convertIdentifier(leafName, JavaIdentifier.METHOD));
533         propBuilder.setReadOnly(isReadOnly);
534         propBuilder.setReturnType(returnType);
535         propBuilder.setComment(leafDesc);
536         toBuilder.addEqualsIdentity(propBuilder);
537         toBuilder.addHashIdentity(propBuilder);
538         toBuilder.addToStringProperty(propBuilder);
539         return true;
540     }
541
542     @VisibleForTesting
543     public static String replaceAllIllegalChars(final StringBuilder stringBuilder){
544         final String ret = UNICODE_CHAR_PATTERN.matcher(stringBuilder).replaceAll("\\\\\\\\u");
545         return ret.isEmpty() ? "" : ret;
546     }
547 }