import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import java.util.LinkedList;
+import java.util.List;
import javax.annotation.Nullable;
import org.opendaylight.yangtools.concepts.Builder;
import org.opendaylight.yangtools.yang.common.QName;
private final AbstractStringInstanceIdentifierCodec codec;
private final String data;
- private final LinkedList<PathArgument> product = new LinkedList<>();
+ private final List<PathArgument> product = new LinkedList<>();
private DataSchemaContextNode<?> current;
private int offset;
- XpathStringParsingPathArgumentBuilder(AbstractStringInstanceIdentifierCodec codec, String data) {
+ XpathStringParsingPathArgumentBuilder(final AbstractStringInstanceIdentifierCodec codec, final String data) {
this.codec = Preconditions.checkNotNull(codec);
this.data = Preconditions.checkNotNull(data);
this.current = codec.getDataContextTree().getRoot();
}
private PathArgument computeNextArgument() {
- checkValid(SLASH == currentChar(),"Identifier must start with '/'.");
+ checkValid(SLASH == currentChar(), "Identifier must start with '/'.");
skipCurrentChar();
-
+ checkValid(!allCharactersConsumed(), "Identifier cannot end with '/'.");
QName name = nextQName();
- if(allCharactersConsumed() || SLASH == currentChar()) {
+ if (allCharactersConsumed() || SLASH == currentChar()) {
return computeIdentifier(name);
} else {
checkValid(PRECONDITION_START == currentChar(), "Last element must be identifier, predicate or '/'");
}
- private DataSchemaContextNode<?> nextContextNode(QName name) {
+ private DataSchemaContextNode<?> nextContextNode(final QName name) {
current = current.getChild(name);
checkValid(current != null, "%s is not correct schema node identifier.",name);
- while(current.isMixin()) {
+ while (current.isMixin()) {
product.add(current.getIdentifier());
current = current.getChild(name);
}
* @param name QName of node, for which predicates are computed.
* @return PathArgument representing node selection with predictes
*/
- private PathArgument computeIdentifierWithPredicate(QName name) {
+ private PathArgument computeIdentifierWithPredicate(final QName name) {
DataSchemaContextNode<?> currentNode = nextContextNode(name);
checkValid(currentNode.isKeyedEntry(), "Entry %s does not allow specifying predicates.", name);
ImmutableMap.Builder<QName,Object> keyValues = ImmutableMap.builder();
- while(!allCharactersConsumed() && PRECONDITION_START == currentChar()) {
+ while (!allCharactersConsumed() && PRECONDITION_START == currentChar()) {
skipCurrentChar();
skipWhitespaces();
final QName key;
- if(DOT == currentChar()) {
+ if (DOT == currentChar()) {
key = null;
skipCurrentChar();
} else {
skipWhitespaces();
checkCurrentAndSkip(EQUALS, "Precondition must contain '='");
skipWhitespaces();
- final Object value = deserializeValue(key,nextQuotedValue());
+ final String keyValue = nextQuotedValue();
skipWhitespaces();
checkCurrentAndSkip(PRECONDITION_END, "Precondition must ends with ']'");
// Break-out from method for leaf-list case
- if(key == null && currentNode.isLeaf()) {
+ if (key == null && currentNode.isLeaf()) {
checkValid(offset == data.length(), "Leaf argument must be last argument of instance identifier.");
- return new YangInstanceIdentifier.NodeWithValue(name, value);
+ return new YangInstanceIdentifier.NodeWithValue<>(name, keyValue);
}
+ final DataSchemaContextNode<?> keyNode = currentNode.getChild(key);
+ checkValid(keyNode != null, "%s is not correct schema node identifier.", key);
+ final Object value = codec.deserializeKeyValue(keyNode.getDataSchemaNode(), keyValue);
keyValues.put(key, value);
}
return new YangInstanceIdentifier.NodeIdentifierWithPredicates(name, keyValues.build());
}
- private PathArgument computeIdentifier(QName name) {
+ private PathArgument computeIdentifier(final QName name) {
DataSchemaContextNode<?> currentNode = nextContextNode(name);
checkValid(!currentNode.isKeyedEntry(), "Entry %s requires key or value predicate to be present", name);
return currentNode.getIdentifier();
// Consume prefix or identifie
final String maybePrefix = nextIdentifier();
final String prefix,localName;
- if(COLON == currentChar()) {
+ if (COLON == currentChar()) {
// previous token is prefix;
prefix = maybePrefix;
skipCurrentChar();
}
- private QName createQName(String prefix, String localName) {
+ private QName createQName(final String prefix, final String localName) {
return codec.createQName(prefix, localName);
}
* @param expected Expected character
* @param errorMsg Error message if {@link #currentChar()} does not match expected.
*/
- private void checkCurrentAndSkip(char expected, String errorMsg) {
+ private void checkCurrentAndSkip(final char expected, final String errorMsg) {
checkValid(expected == currentChar(), errorMsg);
offset++;
}
* @param value Value to be checked and deserialized
* @return Object representing value in yang-data-api format.
*/
- private Object deserializeValue(@Nullable QName key, String value) {
+ private Object deserializeValue(@Nullable final QName key, final String value) {
// FIXME: Use codec to deserialize value to correct Java type
return value;
}
* @param errorMsg Error message which will be provided to user.
* @param attributes
*/
- private void checkValid(boolean condition, String errorMsg, Object... attributes) {
+ private void checkValid(final boolean condition, final String errorMsg, final Object... attributes) {
Preconditions.checkArgument(condition, "Could not parse Instance Identifier '%s'. Offset: %s : Reason: %s",
data,
offset,
return data.substring(start, offset);
}
- private void nextSequenceEnd(CharMatcher matcher) {
- while(!allCharactersConsumed() && matcher.matches(data.charAt(offset))) {
+ private void nextSequenceEnd(final CharMatcher matcher) {
+ while (!allCharactersConsumed() && matcher.matches(data.charAt(offset))) {
offset++;
}
}
- private void checkValidQuotation(char quoteChar) {
+ private void checkValidQuotation(final char quoteChar) {
checkValid(
SQUOTE.matches(quoteChar) || DQUOTE.matches(quoteChar),
"Value must be qoute escaped with ''' or '\"'.");