+ TypeDefinition<?> newType = createCorrectTypeDefinition(
+ parentSchemaPath, nodeBuilderQName, nodeType);
+ node.setType(newType);
+ } else {
+ TypeDefinitionBuilder nodeBuilderTypedef = node.getTypedef();
+
+ fd = nodeBuilderTypedef.getFractionDigits();
+ lengths = nodeBuilderTypedef.getLengths();
+ patterns = nodeBuilderTypedef.getPatterns();
+ ranges = nodeBuilderTypedef.getRanges();
+
+ String tdbTypeName = nodeBuilderTypedef.getQName().getLocalName();
+ String baseTypeName = null;
+ if (nodeBuilderTypedef.getType() == null) {
+ baseTypeName = nodeBuilderTypedef.getTypedef().getQName()
+ .getLocalName();
+ } else {
+ baseTypeName = nodeBuilderTypedef.getType().getQName()
+ .getLocalName();
+ }
+ if (!(tdbTypeName.equals(baseTypeName))) {
+ return;
+ }
+
+ if (!hasConstraints(fd, lengths, patterns, ranges)) {
+ return;
+ }
+
+ SchemaPath newSchemaPath = createNewSchemaPath(
+ nodeBuilderTypedef.getPath(), nodeBuilderQName,
+ nodeBuilderTypedef.getQName());
+ nodeBuilderTypedef.setPath(newSchemaPath);
+ }
+ }
+
+ /**
+ * Check if there are some constraints.
+ *
+ * @param fd
+ * fraction digits
+ * @param lengths
+ * length constraints
+ * @param patterns
+ * pattern constraints
+ * @param ranges
+ * range constraints
+ * @return true, if any of constraints are present, false otherwise
+ */
+ private static boolean hasConstraints(final Integer fd,
+ final List<LengthConstraint> lengths,
+ final List<PatternConstraint> patterns,
+ final List<RangeConstraint> ranges) {
+ if (fd == null && (lengths == null || lengths.isEmpty())
+ && (patterns == null || patterns.isEmpty())
+ && (ranges == null || ranges.isEmpty())) {
+ return false;
+ } else {
+ return true;
+ }
+ }
+
+ private static TypeDefinition<?> createCorrectTypeDefinition(
+ SchemaPath parentSchemaPath, QName nodeQName,
+ TypeDefinition<?> nodeType) {
+ TypeDefinition<?> result = null;
+ SchemaPath newSchemaPath = null;
+ if (nodeType != null) {
+ if (nodeType instanceof BinaryTypeDefinition) {
+ BinaryTypeDefinition binType = (BinaryTypeDefinition) nodeType;
+ newSchemaPath = createNewSchemaPath(parentSchemaPath,
+ nodeQName, binType.getQName());
+ List<Byte> bytes = (List<Byte>) binType.getDefaultValue();
+ result = new BinaryType(newSchemaPath, bytes);
+ } else if (nodeType instanceof BitsTypeDefinition) {
+ BitsTypeDefinition bitsType = (BitsTypeDefinition) nodeType;
+ newSchemaPath = createNewSchemaPath(parentSchemaPath,
+ nodeQName, nodeType.getQName());
+ result = new BitsType(newSchemaPath, bitsType.getBits());
+ } else if (nodeType instanceof BooleanTypeDefinition) {
+ BooleanTypeDefinition booleanType = (BooleanTypeDefinition) nodeType;
+ newSchemaPath = createNewSchemaPath(parentSchemaPath,
+ nodeQName, booleanType.getQName());
+ result = new BooleanType(newSchemaPath);
+ } else if (nodeType instanceof DecimalTypeDefinition) {
+ DecimalTypeDefinition decimalType = (DecimalTypeDefinition) nodeType;
+ newSchemaPath = createNewSchemaPath(parentSchemaPath,
+ nodeQName, decimalType.getQName());
+ result = new Decimal64(newSchemaPath,
+ decimalType.getFractionDigits());
+ } else if (nodeType instanceof EmptyTypeDefinition) {
+ newSchemaPath = createNewSchemaPath(parentSchemaPath,
+ nodeQName, nodeType.getQName());
+ result = new EmptyType(newSchemaPath);
+ } else if (nodeType instanceof EnumTypeDefinition) {
+ EnumTypeDefinition enumType = (EnumTypeDefinition) nodeType;
+ newSchemaPath = createNewSchemaPath(parentSchemaPath,
+ nodeQName, enumType.getQName());
+ result = new EnumerationType(newSchemaPath,
+ (EnumPair) enumType.getDefaultValue(),
+ enumType.getValues());
+ } else if (nodeType instanceof IdentityrefTypeDefinition) {
+ IdentityrefTypeDefinition idrefType = (IdentityrefTypeDefinition) nodeType;
+ newSchemaPath = createNewSchemaPath(parentSchemaPath,
+ nodeQName, idrefType.getQName());
+ result = new IdentityrefType(idrefType.getIdentity(),
+ newSchemaPath);
+ } else if (nodeType instanceof InstanceIdentifierTypeDefinition) {
+ InstanceIdentifierTypeDefinition instIdType = (InstanceIdentifierTypeDefinition) nodeType;
+ newSchemaPath = createNewSchemaPath(parentSchemaPath,
+ nodeQName, instIdType.getQName());
+ return new InstanceIdentifier(newSchemaPath,
+ instIdType.getPathStatement(),
+ instIdType.requireInstance());
+ } else if (nodeType instanceof StringTypeDefinition) {
+ result = createNewStringType(parentSchemaPath, nodeQName,
+ (StringTypeDefinition) nodeType);
+ } else if (nodeType instanceof IntegerTypeDefinition) {
+ result = createNewIntType(parentSchemaPath, nodeQName,
+ (IntegerTypeDefinition) nodeType);
+ } else if (nodeType instanceof UnsignedIntegerTypeDefinition) {
+ result = createNewUintType(parentSchemaPath, nodeQName,
+ (UnsignedIntegerTypeDefinition) nodeType);
+ } else if (nodeType instanceof LeafrefTypeDefinition) {
+ newSchemaPath = createNewSchemaPath(parentSchemaPath,
+ nodeQName, nodeType.getQName());
+ result = new Leafref(newSchemaPath,
+ ((LeafrefTypeDefinition) nodeType).getPathStatement());
+ } else if (nodeType instanceof UnionTypeDefinition) {
+ UnionTypeDefinition unionType = (UnionTypeDefinition) nodeType;
+ newSchemaPath = createNewSchemaPath(parentSchemaPath,
+ nodeQName, unionType.getQName());
+ return new UnionType(newSchemaPath, unionType.getTypes());
+ } else if (nodeType instanceof ExtendedType) {
+ ExtendedType extType = (ExtendedType) nodeType;
+ newSchemaPath = createNewSchemaPath(parentSchemaPath,
+ nodeQName, extType.getQName());
+ result = createNewExtendedType(newSchemaPath, extType);
+ }
+ }
+ return result;
+ }
+
+ private static TypeDefinition<?> createNewExtendedType(
+ SchemaPath newSchemaPath, ExtendedType oldExtendedType) {
+ QName qname = oldExtendedType.getQName();
+ TypeDefinition<?> baseType = oldExtendedType.getBaseType();
+ String desc = oldExtendedType.getDescription();
+ String ref = oldExtendedType.getReference();
+ ExtendedType.Builder builder = new ExtendedType.Builder(qname,
+ baseType, desc, ref, newSchemaPath);
+ builder.status(oldExtendedType.getStatus());
+ builder.lengths(oldExtendedType.getLengths());
+ builder.patterns(oldExtendedType.getPatterns());
+ builder.ranges(oldExtendedType.getRanges());
+ builder.fractionDigits(oldExtendedType.getFractionDigits());
+ builder.unknownSchemaNodes(oldExtendedType.getUnknownSchemaNodes());
+ return builder.build();
+ }
+
+ private static TypeDefinition<?> createNewStringType(SchemaPath schemaPath,
+ QName nodeQName, StringTypeDefinition nodeType) {
+ List<QName> path = schemaPath.getPath();
+ List<QName> newPath = new ArrayList<QName>(path);
+ newPath.add(nodeQName);
+ newPath.add(nodeType.getQName());
+ SchemaPath newSchemaPath = new SchemaPath(newPath,
+ schemaPath.isAbsolute());
+
+ return new StringType(newSchemaPath);
+ }
+
+ private static TypeDefinition<?> createNewIntType(SchemaPath schemaPath,
+ QName nodeQName, IntegerTypeDefinition type) {
+ QName typeQName = type.getQName();
+ SchemaPath newSchemaPath = createNewSchemaPath(schemaPath, nodeQName,
+ typeQName);
+ String localName = typeQName.getLocalName();
+
+ if ("int8".equals(localName)) {
+ return new Int8(newSchemaPath);
+ } else if ("int16".equals(localName)) {
+ return new Int16(newSchemaPath);
+ } else if ("int32".equals(localName)) {
+ return new Int32(newSchemaPath);
+ } else if ("int64".equals(localName)) {
+ return new Int64(newSchemaPath);
+ } else {
+ return null;
+ }
+ }
+
+ private static TypeDefinition<?> createNewUintType(SchemaPath schemaPath,
+ QName nodeQName, UnsignedIntegerTypeDefinition type) {
+ QName typeQName = type.getQName();
+ SchemaPath newSchemaPath = createNewSchemaPath(schemaPath, nodeQName,
+ typeQName);
+ String localName = typeQName.getLocalName();
+
+ if ("uint8".equals(localName)) {
+ return new Int8(newSchemaPath);
+ } else if ("uint16".equals(localName)) {
+ return new Int16(newSchemaPath);
+ } else if ("uint32".equals(localName)) {
+ return new Int32(newSchemaPath);
+ } else if ("uint64".equals(localName)) {
+ return new Int64(newSchemaPath);
+ } else {
+ return null;