checkArgument(nonCondXPath != null, "Non Conditional Revision Aware XPath cannot be NULL"); final String strXPath = nonCondXPath.toString(); if (strXPath != null) { checkArgument(strXPath.indexOf('[') == -1, "Revision Aware XPath may not contain a condition"); if (nonCondXPath.isAbsolute()) { final List<QName> path = xpathToQNamePath(context, module, strXPath);
@Override public String toString() { return xpath.toString(); } }
private static NormalizedNode<?, ?> getNodeReferencedByLeafref(final RevisionAwareXPath xpath, final NormalizedNodeContext currentNodeContext, final SchemaContext schemaContext, final TypedDataSchemaNode correspondingSchemaNode, final Object nodeValue) { final NormalizedNode<?, ?> referencedNode = xpath.isAbsolute() ? getNodeReferencedByAbsoluteLeafref(xpath, currentNodeContext, schemaContext, correspondingSchemaNode) : getNodeReferencedByRelativeLeafref(xpath, currentNodeContext, schemaContext, correspondingSchemaNode); if (referencedNode instanceof LeafSetNode) { return getReferencedLeafSetEntryNode((LeafSetNode<?>) referencedNode, nodeValue); } if (referencedNode instanceof LeafNode && referencedNode.getValue().equals(nodeValue)) { return referencedNode; } return null; }
/** * Removes conditions from xPath pointed to target node. * * @param pathStatement * xPath to target node * @return string representation of xPath without conditions */ @VisibleForTesting static String stripConditionsFromXPathString(final RevisionAwareXPath pathStatement) { return STRIP_PATTERN.matcher(pathStatement.toString()).replaceAll(""); }
private static NormalizedNode<?, ?> getNodeReferencedByLeafref(final RevisionAwareXPath xpath, final NormalizedNodeContext currentNodeContext, final SchemaContext schemaContext, final TypedDataSchemaNode correspondingSchemaNode, final Object nodeValue) { final NormalizedNode<?, ?> referencedNode = xpath.isAbsolute() ? getNodeReferencedByAbsoluteLeafref(xpath, currentNodeContext, schemaContext, correspondingSchemaNode) : getNodeReferencedByRelativeLeafref(xpath, currentNodeContext, schemaContext, correspondingSchemaNode); if (referencedNode instanceof LeafSetNode) { return getReferencedLeafSetEntryNode((LeafSetNode<?>) referencedNode, nodeValue); } if (referencedNode instanceof LeafNode && referencedNode.getValue().equals(nodeValue)) { return referencedNode; } return null; }
checkArgument(nonCondXPath != null, "Non Conditional Revision Aware XPath cannot be NULL"); final String strXPath = nonCondXPath.toString(); if (strXPath != null) { checkArgument(strXPath.indexOf('[') == -1, "Revision Aware XPath may not contain a condition"); if (nonCondXPath.isAbsolute()) { final List<QName> path = xpathToQNamePath(context, module, strXPath);
@VisibleForTesting static Matcher getWhenConditionMatcher(final String prefix, final RevisionAwareXPath whenConstraint) { String xpathRegex = MODULE_CONDITION_XPATH_TEMPLATE.replace(MAGIC_STRING, prefix); Pattern pattern = Pattern.compile(xpathRegex); return pattern.matcher(whenConstraint.toString()); }
checkArgument(actualSchemaNode != null, "Actual Schema Node reference cannot be NULL"); checkArgument(relativeXPath != null, "Non Conditional Revision Aware XPath cannot be NULL"); checkState(!relativeXPath.isAbsolute(), "Revision Aware XPath MUST be relative i.e. MUST contains ../, " + "for non relative Revision Aware XPath use findDataSchemaNode method");
checkArgument(module != null, "Module reference cannot be NULL"); checkArgument(relativeXPath != null, "Non Conditional Revision Aware XPath cannot be NULL"); checkState(!relativeXPath.isAbsolute(), "Revision Aware XPath MUST be relative i.e. MUST contains ../, " + "for non relative Revision Aware XPath use findDataSchemaNode method"); checkState(actualSchemaNode.getPath() != null, "Schema Path reference for Leafref cannot be NULL"); final Iterable<String> xpaths = SLASH_SPLITTER.split(relativeXPath.toString());
/** * Removes conditions from xPath pointed to target node. * * @param pathStatement * xPath to target node * @return string representation of xPath without conditions */ @VisibleForTesting static String stripConditionsFromXPathString(final RevisionAwareXPath pathStatement) { return STRIP_PATTERN.matcher(pathStatement.toString()).replaceAll(""); }
checkArgument(actualSchemaNode != null, "Actual Schema Node reference cannot be NULL"); checkArgument(relativeXPath != null, "Non Conditional Revision Aware XPath cannot be NULL"); checkState(!relativeXPath.isAbsolute(), "Revision Aware XPath MUST be relative i.e. MUST contains ../, " + "for non relative Revision Aware XPath use findDataSchemaNode method");
private String leafrefToDef(final LeafSchemaNode parentNode, final LeafrefTypeDefinition leafrefType, final String defaultValue) { 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.indexOf('[') == -1) { 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); } final String result = getTypeDefaultConstruction((LeafSchemaNode) dataNode, defaultValue); return result; } } else { return "new java.lang.Object()"; } } return null; }
@Override public void writeArgument(final RevisionAwareXPath xpath) { checkArgumentApplicable(); // FIXME: This implementation assumes prefixes are unchanged // and were not changed in schema context. writeArgument0(xpath.toString()); }
/** * Returns base type for {@code typeDefinition} which belongs to module specified via {@code qname}. This handle * the case when leafref type isn't specified as type substatement of leaf or leaf-list but is defined in other * module as typedef which is then imported to referenced module. * * <p> * Because {@code typeDefinition} is definied via typedef statement, only absolute path is meaningful. */ public static TypeDefinition<?> getBaseTypeForLeafRef(final LeafrefTypeDefinition typeDefinition, final SchemaContext schemaContext, final QName qname) { final RevisionAwareXPath pathStatement = typeDefinition.getPathStatement(); final RevisionAwareXPath strippedPathStatement = new RevisionAwareXPathImpl( stripConditionsFromXPathString(pathStatement), pathStatement.isAbsolute()); if (!strippedPathStatement.isAbsolute()) { return null; } final Optional<Module> parentModule = schemaContext.findModule(qname.getModule()); checkArgument(parentModule.isPresent(), "Failed to find parent module for %s", qname); final DataSchemaNode dataSchemaNode = (DataSchemaNode) SchemaContextUtil.findDataSchemaNode(schemaContext, parentModule.get(), strippedPathStatement); final TypeDefinition<?> targetTypeDefinition = typeDefinition(dataSchemaNode); if (targetTypeDefinition instanceof LeafrefTypeDefinition) { return getBaseTypeForLeafRef((LeafrefTypeDefinition) targetTypeDefinition, schemaContext, dataSchemaNode); } return targetTypeDefinition; }
checkArgument(module != null, "Module reference cannot be NULL"); checkArgument(relativeXPath != null, "Non Conditional Revision Aware XPath cannot be NULL"); checkState(!relativeXPath.isAbsolute(), "Revision Aware XPath MUST be relative i.e. MUST contains ../, " + "for non relative Revision Aware XPath use findDataSchemaNode method"); checkState(actualSchemaNode.getPath() != null, "Schema Path reference for Leafref cannot be NULL"); final Iterable<String> xpaths = SLASH_SPLITTER.split(relativeXPath.toString());
private static NormalizedNode<?, ?> getNodeReferencedByRelativeLeafref(final RevisionAwareXPath xpath, final NormalizedNodeContext currentNodeContext, final SchemaContext schemaContext, final TypedDataSchemaNode correspondingSchemaNode) { NormalizedNodeContext relativeNodeContext = currentNodeContext; final StringBuilder xPathStringBuilder = new StringBuilder(xpath.toString()); // strip the relative path of all ../ at the beginning while (xPathStringBuilder.indexOf("../") == 0) { xPathStringBuilder.delete(0, 3); relativeNodeContext = relativeNodeContext.getParent(); } // add / to the beginning of the path so that it can be processed the same way as an absolute path xPathStringBuilder.insert(0, '/'); final LeafrefXPathStringParsingPathArgumentBuilder builder = new LeafrefXPathStringParsingPathArgumentBuilder( xPathStringBuilder.toString(), schemaContext, correspondingSchemaNode, currentNodeContext); final List<PathArgument> pathArguments = builder.build(); final NormalizedNode<?, ?> relativeNode = relativeNodeContext.getNode(); final Optional<NormalizedNode<?, ?>> possibleNode = NormalizedNodes.findNode(relativeNode, pathArguments); if (possibleNode.isPresent()) { return possibleNode.get(); } return null; }
/** * Returns base type for {@code typeDefinition} which belongs to module specified via {@code qname}. This handle * the case when leafref type isn't specified as type substatement of leaf or leaf-list but is defined in other * module as typedef which is then imported to referenced module. * * <p> * Because {@code typeDefinition} is definied via typedef statement, only absolute path is meaningful. */ public static TypeDefinition<?> getBaseTypeForLeafRef(final LeafrefTypeDefinition typeDefinition, final SchemaContext schemaContext, final QName qname) { final RevisionAwareXPath pathStatement = typeDefinition.getPathStatement(); final RevisionAwareXPath strippedPathStatement = new RevisionAwareXPathImpl( stripConditionsFromXPathString(pathStatement), pathStatement.isAbsolute()); if (!strippedPathStatement.isAbsolute()) { return null; } final Optional<Module> parentModule = schemaContext.findModule(qname.getModule()); checkArgument(parentModule.isPresent(), "Failed to find parent module for %s", qname); final DataSchemaNode dataSchemaNode = (DataSchemaNode) SchemaContextUtil.findDataSchemaNode(schemaContext, parentModule.get(), strippedPathStatement); final TypeDefinition<?> targetTypeDefinition = typeDefinition(dataSchemaNode); if (targetTypeDefinition instanceof LeafrefTypeDefinition) { return getBaseTypeForLeafRef((LeafrefTypeDefinition) targetTypeDefinition, schemaContext, dataSchemaNode); } return targetTypeDefinition; }
private String leafrefToDef(final LeafSchemaNode parentNode, final LeafrefTypeDefinition leafrefType, final String defaultValue) { 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.indexOf('[') == -1) { 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, defaultValue); return result; } } else { return "new java.lang.Object()"; } } return null; }
private static NormalizedNode<?, ?> getNodeReferencedByRelativeLeafref(final RevisionAwareXPath xpath, final NormalizedNodeContext currentNodeContext, final SchemaContext schemaContext, final TypedDataSchemaNode correspondingSchemaNode) { NormalizedNodeContext relativeNodeContext = currentNodeContext; final StringBuilder xPathStringBuilder = new StringBuilder(xpath.toString()); // strip the relative path of all ../ at the beginning while (xPathStringBuilder.indexOf("../") == 0) { xPathStringBuilder.delete(0, 3); relativeNodeContext = relativeNodeContext.getParent(); } // add / to the beginning of the path so that it can be processed the same way as an absolute path xPathStringBuilder.insert(0, '/'); final LeafrefXPathStringParsingPathArgumentBuilder builder = new LeafrefXPathStringParsingPathArgumentBuilder( xPathStringBuilder.toString(), schemaContext, correspondingSchemaNode, currentNodeContext); final List<PathArgument> pathArguments = builder.build(); final NormalizedNode<?, ?> relativeNode = relativeNodeContext.getNode(); final Optional<NormalizedNode<?, ?>> possibleNode = NormalizedNodes.findNode(relativeNode, pathArguments); if (possibleNode.isPresent()) { return possibleNode.get(); } return null; }
if (xpath.isAbsolute()) { dataNode = SchemaContextUtil.findDataSchemaNode(schemaContext, module, xpath); } else {