+ public void addUnitsToGenTO(GeneratedTOBuilder to, String units) {
+ if (units != null && !units.isEmpty()) {
+ to.addConstant(Types.STRING, "_UNITS", "\"" + units + "\"");
+ GeneratedPropertyBuilder prop = new GeneratedPropertyBuilderImpl("UNITS");
+ prop.setReturnType(Types.STRING);
+ to.addToStringProperty(prop);
+ }
+ }
+
+ @Override
+ public String getTypeDefaultConstruction(LeafSchemaNode node) {
+ return getTypeDefaultConstruction(node, node.getDefault());
+ }
+
+ public String getTypeDefaultConstruction(LeafSchemaNode node, String defaultValue) {
+ TypeDefinition<?> type = node.getType();
+ QName typeQName = type.getQName();
+ TypeDefinition<?> base = baseTypeDefForExtendedType(type);
+ Preconditions.checkNotNull(type, "Cannot provide default construction for null type of " + node);
+ Preconditions.checkNotNull(defaultValue, "Cannot provide default construction for null default statement of "
+ + node);
+
+ StringBuilder sb = new StringBuilder();
+ String result = null;
+ if (base instanceof BinaryTypeDefinition) {
+ result = binaryToDef(defaultValue);
+ } else if (base instanceof BitsTypeDefinition) {
+ String parentName;
+ String className;
+ SchemaPath nodePath = node.getPath();
+ Module parent = getParentModule(node);
+ if (nodePath.getPath().size() == 1) {
+ parentName = BindingMapping.getClassName((parent).getName()) + "Data";
+ String basePackageName = BindingGeneratorUtil.moduleNamespaceToPackageName(parent);
+ className = basePackageName + "." + parentName + "." + BindingMapping.getClassName(node.getQName());
+ } else {
+ String basePackageName = BindingGeneratorUtil.moduleNamespaceToPackageName(parent);
+ String packageName = packageNameForGeneratedType(basePackageName, type.getPath());
+ parentName = BindingMapping.getClassName(((SchemaNode) parent).getQName());
+ className = packageName + "." + parentName + "." + BindingMapping.getClassName(node.getQName());
+ }
+ result = bitsToDef((BitsTypeDefinition) base, className, defaultValue, type instanceof ExtendedType);
+ } else if (base instanceof BooleanTypeDefinition) {
+ result = typeToDef(Boolean.class, defaultValue);
+ } else if (base instanceof DecimalTypeDefinition) {
+ result = typeToDef(BigDecimal.class, defaultValue);
+ } else if (base instanceof EmptyTypeDefinition) {
+ result = typeToDef(Boolean.class, defaultValue);
+ } else if (base instanceof EnumTypeDefinition) {
+ char[] defValArray = defaultValue.toCharArray();
+ char first = Character.toUpperCase(defaultValue.charAt(0));
+ defValArray[0] = first;
+ String newDefVal = new String(defValArray);
+ String className;
+ if (type instanceof ExtendedType) {
+ Module m = getParentModule(type);
+ String basePackageName = BindingGeneratorUtil.moduleNamespaceToPackageName(m);
+ String packageName = packageNameForGeneratedType(basePackageName, type.getPath());
+ className = packageName + "." + BindingMapping.getClassName(typeQName);
+ } else {
+ Module parentModule = getParentModule(node);
+ String basePackageName = BindingGeneratorUtil.moduleNamespaceToPackageName(parentModule);
+ String packageName = packageNameForGeneratedType(basePackageName, node.getPath());
+ className = packageName + "." + BindingMapping.getClassName(node.getQName());
+ }
+ result = className + "." + newDefVal;
+ } else if (base instanceof IdentityrefTypeDefinition) {
+ throw new UnsupportedOperationException("Cannot get default construction for identityref type");
+ } else if (base instanceof InstanceIdentifierTypeDefinition) {
+ throw new UnsupportedOperationException("Cannot get default construction for instance-identifier type");
+ } else if (base instanceof Int8) {
+ result = typeToDef(Byte.class, defaultValue);
+ } else if (base instanceof Int16) {
+ result = typeToDef(Short.class, defaultValue);
+ } else if (base instanceof Int32) {
+ result = typeToDef(Integer.class, defaultValue);
+ } else if (base instanceof Int64) {
+ result = typeToDef(Long.class, defaultValue);
+ } else if (base instanceof LeafrefTypeDefinition) {
+ result = leafrefToDef(node, (LeafrefTypeDefinition) base);
+ } else if (base instanceof StringTypeDefinition) {
+ result = "\"" + defaultValue + "\"";
+ } else if (base instanceof Uint8) {
+ result = typeToDef(Short.class, defaultValue);
+ } else if (base instanceof Uint16) {
+ result = typeToDef(Integer.class, defaultValue);
+ } else if (base instanceof Uint32) {
+ result = typeToDef(Long.class, defaultValue);
+ } else if (base instanceof Uint64) {
+ result = typeToDef(BigInteger.class, defaultValue);
+ } else if (base instanceof UnionTypeDefinition) {
+ result = unionToDef(node);
+ } else {
+ result = "";
+ }
+ sb.append(result);
+
+ if (type instanceof ExtendedType && !(base instanceof LeafrefTypeDefinition)
+ && !(base instanceof EnumerationType) && !(base instanceof UnionTypeDefinition)) {
+ Module m = getParentModule(type);
+ String basePackageName = BindingGeneratorUtil.moduleNamespaceToPackageName(m);
+ String packageName = packageNameForGeneratedType(basePackageName, type.getPath());
+ String className = packageName + "." + BindingMapping.getClassName(typeQName);
+ sb.insert(0, "new " + className + "(");
+ sb.insert(sb.length(), ")");
+ }
+
+ return sb.toString();
+ }
+
+ private String typeToDef(Class<?> clazz, String defaultValue) {
+ return "new " + clazz.getName() + "(\"" + defaultValue + "\")";
+ }
+
+ private String binaryToDef(String defaultValue) {
+ StringBuilder sb = new StringBuilder();
+ BaseEncoding en = BaseEncoding.base64();
+ byte[] encoded = en.decode(defaultValue);
+ sb.append("new byte[] {");
+ for (int i = 0; i < encoded.length; i++) {
+ sb.append(encoded[i]);
+ if (i != encoded.length - 1) {
+ sb.append(", ");
+ }
+ }
+ sb.append("}");
+ return sb.toString();
+ }
+
+ private String bitsToDef(BitsTypeDefinition type, String className, String defaultValue, boolean isExt) {
+ List<Bit> bits = new ArrayList<>(type.getBits());
+ Collections.sort(bits, new Comparator<Bit>() {
+ @Override
+ public int compare(Bit o1, Bit o2) {
+ return o1.getName().compareTo(o2.getName());
+ }
+ });
+ StringBuilder sb = new StringBuilder();
+ sb.append(isExt ? "" : "new " + className + "(");
+ for (int i = 0; i < bits.size(); i++) {
+ if (bits.get(i).getName().equals(defaultValue)) {
+ sb.append(true);
+ } else {
+ sb.append(false);
+ }
+ if (i != bits.size() - 1) {
+ sb.append(", ");
+ }
+ }
+ sb.append(isExt ? "" : ")");
+ return sb.toString();
+ }
+
+ private Module getParentModule(SchemaNode node) {
+ QName qname = node.getPath().getPath().get(0);
+ URI namespace = qname.getNamespace();
+ Date revision = qname.getRevision();
+ return schemaContext.findModuleByNamespaceAndRevision(namespace, revision);
+ }
+
+ private String leafrefToDef(LeafSchemaNode parentNode, LeafrefTypeDefinition leafrefType) {
+ Preconditions.checkArgument(leafrefType != null, "Leafref Type Definition reference cannot be NULL!");
+ Preconditions.checkArgument(leafrefType.getPathStatement() != null,
+ "The Path Statement for Leafref Type Definition cannot be NULL!");
+
+ final RevisionAwareXPath xpath = leafrefType.getPathStatement();
+ final String strXPath = xpath.toString();
+
+ if (strXPath != null) {
+ if (strXPath.contains("[")) {
+ return "new java.lang.Object()";
+ } else {
+ final Module module = findParentModule(schemaContext, parentNode);
+ if (module != null) {
+ final SchemaNode dataNode;
+ if (xpath.isAbsolute()) {
+ dataNode = findDataSchemaNode(schemaContext, module, xpath);
+ } else {
+ dataNode = findDataSchemaNodeForRelativeXPath(schemaContext, module, parentNode, xpath);
+ }
+ String result = getTypeDefaultConstruction((LeafSchemaNode) dataNode, parentNode.getDefault());
+ return result;
+ }
+ }
+ }
+
+ return null;
+ }
+
+ private String unionToDef(LeafSchemaNode node) {
+ String parentName;
+ String className;
+
+ if (node.getType() instanceof ExtendedType) {
+ ExtendedType type = (ExtendedType) node.getType();
+ QName typeQName = type.getQName();
+ Module module = null;
+ Set<Module> modules = schemaContext.findModuleByNamespace(typeQName.getNamespace());
+ if (modules.size() > 1) {
+ for (Module m : modules) {
+ if (m.getRevision().equals(typeQName.getRevision())) {
+ module = m;
+ break;
+ }
+ }
+ if (module == null) {
+ List<Module> modulesList = new ArrayList<>(modules);
+ Collections.sort(modulesList, new Comparator<Module>() {
+ @Override
+ public int compare(Module o1, Module o2) {
+ return o1.getRevision().compareTo(o2.getRevision());
+ }
+ });
+ module = modulesList.get(0);
+ }
+ } else {
+ module = modules.iterator().next();
+ }
+
+ String basePackageName = BindingGeneratorUtil.moduleNamespaceToPackageName(module);
+ className = basePackageName + "." + BindingMapping.getClassName(typeQName);
+ } else {
+ SchemaPath nodePath = node.getPath();
+ if (nodePath.getPath().size() == 1) {
+ QName first = nodePath.getPath().get(0);
+ URI namespace = first.getNamespace();
+ Date revision = first.getRevision();
+ Module parent = schemaContext.findModuleByNamespaceAndRevision(namespace, revision);
+ parentName = BindingMapping.getClassName((parent).getName()) + "Data";
+ String basePackageName = BindingGeneratorUtil.moduleNamespaceToPackageName(parent);
+ className = basePackageName + "." + parentName + "." + BindingMapping.getClassName(node.getQName());
+ } else {
+ QName first = node.getPath().getPath().get(0);
+ URI namespace = first.getNamespace();
+ Date revision = first.getRevision();
+ Module parentModule = schemaContext.findModuleByNamespaceAndRevision(namespace, revision);
+ String basePackageName = BindingGeneratorUtil.moduleNamespaceToPackageName(parentModule);
+ String packageName = packageNameForGeneratedType(basePackageName, node.getType().getPath());
+ className = packageName + "." + BindingMapping.getClassName(node.getQName());
+ }
+ }
+ return union(className, node.getDefault(), node);
+ }
+
+ private String union(String className, String defaultValue, LeafSchemaNode node) {
+ StringBuilder sb = new StringBuilder();
+ sb.append("new " + className + "(");
+ sb.append("\"");
+ sb.append(defaultValue);
+ sb.append("\"");
+ sb.append(".toCharArray()");
+ sb.append(")");
+ return sb.toString();
+ }
+
+ @Override
+ public String getConstructorPropertyName(SchemaNode node) {
+ if (node instanceof TypeDefinition<?>) {
+ return "value";
+ } else {
+ return "";
+ }
+ }
+
+ @Override
+ public String getParamNameFromType(TypeDefinition<?> type) {
+ return BindingGeneratorUtil.parseToValidParamName(type.getQName().getLocalName());
+ }
+