private Codec<Object, Object> getCodec(final Class<?> valueType, final DataSchemaNode schema) { checkArgument(schema instanceof TypedDataSchemaNode, "Unsupported leaf node type %s", schema); return getCodec(valueType, ((TypedDataSchemaNode)schema).getType()); }
public static String getGetterMethodName(final TypedDataSchemaNode node) { // Bug 8903: If it is a derived type of boolean or empty, not an inner type, then the return type // of method would be the generated type of typedef not build-in types, so here it should be 'get'. final TypeDefinition<?> type = node.getType(); return BindingMapping.getGetterMethodName(node.getQName(), (type instanceof BooleanTypeDefinition || type instanceof EmptyTypeDefinition) && (type.getPath().equals(node.getPath()) || type.getBaseType() == null)); } }
private T createComplexCodecFor(final TypedDataSchemaNode schema, final TypeDefinition<?> type) { if (type instanceof UnionTypeDefinition) { return createComplexUnion(schema, (UnionTypeDefinition) type); } else if (type instanceof LeafrefTypeDefinition) { final TypeDefinition<?> target = SchemaContextUtil.getBaseTypeForLeafRef((LeafrefTypeDefinition) type, schemaContext, schema); verifyNotNull(target, "Unable to find base type for leafref node %s type %s.", schema.getPath(), target); final T ret = getSimpleCodecFor(target); return ret != null ? ret : createComplexCodecFor(schema, target); } else if (type instanceof IdentityrefTypeDefinition) { return identityRefCodec((IdentityrefTypeDefinition) type, schema.getQName().getModule()); } else { throw new IllegalArgumentException("Unsupported type " + type); } }
private QName createQName(final String prefix, final String localName) { final Module module = schemaContext.findModule(schemaNode.getQName().getModule()).get(); if (prefix.isEmpty() || module.getPrefix().equals(prefix)) { return QName.create(module.getQNameModule(), localName); } for (final ModuleImport moduleImport : module.getImports()) { if (prefix.equals(moduleImport.getPrefix())) { final Module importedModule = schemaContext.findModule(moduleImport.getModuleName(), moduleImport.getRevision()).get(); return QName.create(importedModule.getQNameModule(),localName); } } throw new IllegalArgumentException(String.format("Failed to lookup a module for prefix %s", prefix)); }
private T createComplexCodecFor(final TypedDataSchemaNode schema, final TypeDefinition<?> type) { if (type instanceof UnionTypeDefinition) { return createComplexUnion(schema, (UnionTypeDefinition) type); } else if (type instanceof LeafrefTypeDefinition) { final TypeDefinition<?> target = SchemaContextUtil.getBaseTypeForLeafRef((LeafrefTypeDefinition) type, schemaContext, schema); verifyNotNull(target, "Unable to find base type for leafref node %s type %s.", schema.getPath(), target); final T ret = getSimpleCodecFor(target); return ret != null ? ret : createComplexCodecFor(schema, target); } else if (type instanceof IdentityrefTypeDefinition) { return identityRefCodec((IdentityrefTypeDefinition) type, schema.getQName().getModule()); } else { throw new IllegalArgumentException("Unsupported type " + type); } }
private QName createQName(final String prefix, final String localName) { final Module module = schemaContext.findModule(schemaNode.getQName().getModule()).get(); if (prefix.isEmpty() || module.getPrefix().equals(prefix)) { return QName.create(module.getQNameModule(), localName); } for (final ModuleImport moduleImport : module.getImports()) { if (prefix.equals(moduleImport.getPrefix())) { final Module importedModule = schemaContext.findModule(moduleImport.getModuleName(), moduleImport.getRevision()).get(); return QName.create(importedModule.getQNameModule(),localName); } } throw new IllegalArgumentException(String.format("Failed to lookup a module for prefix %s", prefix)); }
private Codec<Object, Object> getCodec(final Class<?> valueType, final DataSchemaNode schema) { Preconditions.checkArgument(schema instanceof TypedDataSchemaNode, "Unsupported leaf node type %s", schema); return getCodec(valueType, ((TypedDataSchemaNode) schema).getType()); }
private static IdentitySchemaNode getIdentitySchemaNodeFromString(final String identity, final SchemaContext schemaContext, final TypedDataSchemaNode correspondingSchemaNode) { final List<String> identityPrefixAndName = COLON_SPLITTER.splitToList(identity); final Module module = schemaContext.findModule(correspondingSchemaNode.getQName().getModule()).get(); if (identityPrefixAndName.size() == 2) { // prefix of local module if (identityPrefixAndName.get(0).equals(module.getPrefix())) { return findIdentitySchemaNodeInModule(module, QName.create(module.getQNameModule(), identityPrefixAndName.get(1))); } // prefix of imported module for (final ModuleImport moduleImport : module.getImports()) { if (identityPrefixAndName.get(0).equals(moduleImport.getPrefix())) { final Module importedModule = schemaContext.findModule(moduleImport.getModuleName(), moduleImport.getRevision()).get(); return findIdentitySchemaNodeInModule(importedModule, QName.create( importedModule.getQNameModule(), identityPrefixAndName.get(1))); } } throw new IllegalArgumentException(String.format("Cannot resolve prefix '%s' from identity '%s'.", identityPrefixAndName.get(0), identity)); } if (identityPrefixAndName.size() == 1) { // without prefix return findIdentitySchemaNodeInModule(module, QName.create(module.getQNameModule(), identityPrefixAndName.get(0))); } throw new IllegalArgumentException(String.format("Malformed identity argument: %s.", identity)); }
@Override public ImmutableMap<String, LeafNodeCodecContext<?>> getLeafNodes(final Class<?> parentClass, final DataNodeContainer childSchema) { final Map<String, DataSchemaNode> getterToLeafSchema = new HashMap<>(); for (final DataSchemaNode leaf : childSchema.getChildNodes()) { if (leaf instanceof TypedDataSchemaNode) { getterToLeafSchema.put(getGetterName(leaf, ((TypedDataSchemaNode) leaf).getType()), leaf); } } return getLeafNodesUsingReflection(parentClass, getterToLeafSchema); }
private static IdentitySchemaNode getIdentitySchemaNodeFromString(final String identity, final SchemaContext schemaContext, final TypedDataSchemaNode correspondingSchemaNode) { final List<String> identityPrefixAndName = COLON_SPLITTER.splitToList(identity); final Module module = schemaContext.findModule(correspondingSchemaNode.getQName().getModule()).get(); if (identityPrefixAndName.size() == 2) { // prefix of local module if (identityPrefixAndName.get(0).equals(module.getPrefix())) { return findIdentitySchemaNodeInModule(module, QName.create(module.getQNameModule(), identityPrefixAndName.get(1))); } // prefix of imported module for (final ModuleImport moduleImport : module.getImports()) { if (identityPrefixAndName.get(0).equals(moduleImport.getPrefix())) { final Module importedModule = schemaContext.findModule(moduleImport.getModuleName(), moduleImport.getRevision()).get(); return findIdentitySchemaNodeInModule(importedModule, QName.create( importedModule.getQNameModule(), identityPrefixAndName.get(1))); } } throw new IllegalArgumentException(String.format("Cannot resolve prefix '%s' from identity '%s'.", identityPrefixAndName.get(0), identity)); } if (identityPrefixAndName.size() == 1) { // without prefix return findIdentitySchemaNodeInModule(module, QName.create(module.getQNameModule(), identityPrefixAndName.get(0))); } throw new IllegalArgumentException(String.format("Malformed identity argument: %s.", identity)); }
private static Module findParentModuleOfReferencingType(final SchemaContext schemaContext, final SchemaNode schemaNode) { checkArgument(schemaContext != null, "Schema Context reference cannot be NULL!"); checkArgument(schemaNode instanceof TypedDataSchemaNode, "Unsupported node %s", schemaNode); TypeDefinition<?> nodeType = ((TypedDataSchemaNode) schemaNode).getType(); if (nodeType.getBaseType() != null) { while (nodeType.getBaseType() != null) { nodeType = nodeType.getBaseType(); } return schemaContext.findModule(nodeType.getQName().getModule()).orElse(null); } return SchemaContextUtil.findParentModule(schemaContext, schemaNode); }
private static boolean bitIsSet(final Context context, final List<?> args) throws FunctionCallException { if (args == null || args.size() != 1) { throw new FunctionCallException("bit-is-set() takes two arguments: node-set nodes, string bit-name"); } if (!(args.get(0) instanceof String)) { throw new FunctionCallException("Argument bit-name of bit-is-set() function should be a String"); } final String bitName = (String) args.get(0); Verify.verify(context instanceof NormalizedNodeContext, "Unhandled context %s", context.getClass()); final NormalizedNodeContext currentNodeContext = (NormalizedNodeContext) context; final TypedDataSchemaNode correspondingSchemaNode = getCorrespondingTypedSchemaNode(currentNodeContext); final TypeDefinition<?> nodeType = correspondingSchemaNode.getType(); if (!(nodeType instanceof BitsTypeDefinition)) { return false; } final Object nodeValue = currentNodeContext.getNode().getValue(); if (!(nodeValue instanceof Set)) { return false; } final BitsTypeDefinition bitsType = (BitsTypeDefinition) nodeType; Preconditions.checkState(containsBit(bitsType, bitName), "Bit %s does not belong to bits %s.", bitName, bitsType); return ((Set<?>)nodeValue).contains(bitName); }
private static Module findParentModuleOfReferencingType(final SchemaContext schemaContext, final SchemaNode schemaNode) { checkArgument(schemaContext != null, "Schema Context reference cannot be NULL!"); checkArgument(schemaNode instanceof TypedDataSchemaNode, "Unsupported node %s", schemaNode); TypeDefinition<?> nodeType = ((TypedDataSchemaNode) schemaNode).getType(); if (nodeType.getBaseType() != null) { while (nodeType.getBaseType() != null) { nodeType = nodeType.getBaseType(); } return schemaContext.findModule(nodeType.getQName().getModule()).orElse(null); } return SchemaContextUtil.findParentModule(schemaContext, schemaNode); }
private static boolean bitIsSet(final Context context, final List<?> args) throws FunctionCallException { if (args == null || args.size() != 1) { throw new FunctionCallException("bit-is-set() takes two arguments: node-set nodes, string bit-name"); } if (!(args.get(0) instanceof String)) { throw new FunctionCallException("Argument bit-name of bit-is-set() function should be a String"); } final String bitName = (String) args.get(0); Verify.verify(context instanceof NormalizedNodeContext, "Unhandled context %s", context.getClass()); final NormalizedNodeContext currentNodeContext = (NormalizedNodeContext) context; final TypedDataSchemaNode correspondingSchemaNode = getCorrespondingTypedSchemaNode(currentNodeContext); final TypeDefinition<?> nodeType = correspondingSchemaNode.getType(); if (!(nodeType instanceof BitsTypeDefinition)) { return false; } final Object nodeValue = currentNodeContext.getNode().getValue(); if (!(nodeValue instanceof Set)) { return false; } final BitsTypeDefinition bitsType = (BitsTypeDefinition) nodeType; Preconditions.checkState(containsBit(bitsType, bitName), "Bit %s does not belong to bits %s.", bitName, bitsType); return ((Set<?>)nodeValue).contains(bitName); }
private static @Nullable Entry<IdentitySchemaNode, IdentitySchemaNode> commonDerivedFrom(final String functionName, final Context context, final List<?> args) throws FunctionCallException { if (args == null || args.size() != 1) { throw new FunctionCallException(functionName + "() takes two arguments: node-set nodes, string identity"); } if (!(args.get(0) instanceof String)) { throw new FunctionCallException("Argument 'identity' of " + functionName + "() function should be a String."); } Verify.verify(context instanceof NormalizedNodeContext, "Unhandled context %s", context.getClass()); final NormalizedNodeContext currentNodeContext = (NormalizedNodeContext) context; final TypedDataSchemaNode correspondingSchemaNode = getCorrespondingTypedSchemaNode(currentNodeContext); final SchemaContext schemaContext = getSchemaContext(currentNodeContext); return correspondingSchemaNode.getType() instanceof IdentityrefTypeDefinition && currentNodeContext.getNode().getValue() instanceof QName ? new SimpleImmutableEntry<>( getIdentitySchemaNodeFromString((String) args.get(0), schemaContext, correspondingSchemaNode), getIdentitySchemaNodeFromQName((QName) currentNodeContext.getNode().getValue(), schemaContext)) : null; }
private static Object enumValueFunction(final Context context, final List<?> args) throws FunctionCallException { if (!args.isEmpty()) { throw new FunctionCallException("enum-value() takes one argument: node-set nodes."); } Verify.verify(context instanceof NormalizedNodeContext, "Unhandled context %s", context.getClass()); final NormalizedNodeContext currentNodeContext = (NormalizedNodeContext) context; final TypedDataSchemaNode correspondingSchemaNode = getCorrespondingTypedSchemaNode(currentNodeContext); final TypeDefinition<?> nodeType = correspondingSchemaNode.getType(); if (!(nodeType instanceof EnumTypeDefinition)) { return DOUBLE_NAN; } final Object nodeValue = currentNodeContext.getNode().getValue(); if (!(nodeValue instanceof String)) { return DOUBLE_NAN; } final EnumTypeDefinition enumerationType = (EnumTypeDefinition) nodeType; final String enumName = (String) nodeValue; return getEnumValue(enumerationType, enumName); }
private static Object enumValueFunction(final Context context, final List<?> args) throws FunctionCallException { if (!args.isEmpty()) { throw new FunctionCallException("enum-value() takes one argument: node-set nodes."); } Verify.verify(context instanceof NormalizedNodeContext, "Unhandled context %s", context.getClass()); final NormalizedNodeContext currentNodeContext = (NormalizedNodeContext) context; final TypedDataSchemaNode correspondingSchemaNode = getCorrespondingTypedSchemaNode(currentNodeContext); final TypeDefinition<?> nodeType = correspondingSchemaNode.getType(); if (!(nodeType instanceof EnumTypeDefinition)) { return DOUBLE_NAN; } final Object nodeValue = currentNodeContext.getNode().getValue(); if (!(nodeValue instanceof String)) { return DOUBLE_NAN; } final EnumTypeDefinition enumerationType = (EnumTypeDefinition) nodeType; final String enumName = (String) nodeValue; return getEnumValue(enumerationType, enumName); }
final TypeDefinition<?> type = schema.getType(); T ret = cache.lookupSimple(type); if (ret != null) {