*/
package org.opendaylight.restconf.nb.rfc8040.utils.parser;
+import static com.google.common.base.Preconditions.checkArgument;
import static java.util.Objects.requireNonNull;
import com.google.common.base.CharMatcher;
-import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
private static void prepareNodeWithPredicates(final QName qname, final List<PathArgument> path,
final MainVarsWrapper variables, final ListSchemaNode listSchemaNode) {
- checkValid(listSchemaNode != null, "Data schema node is null", variables.getData(), variables.getOffset());
+ variables.checkValid(listSchemaNode != null, "Data schema node is null");
final Iterator<QName> keys = listSchemaNode.getKeyDefinition().iterator();
final ImmutableMap.Builder<QName, Object> values = ImmutableMap.builder();
// skip already expected equal sign
- skipCurrentChar(variables);
+ variables.skipCurrentChar();
// read key value separated by comma
while (keys.hasNext() && !variables.allCharsConsumed() && variables.currentChar() != RestconfConstants.SLASH) {
// empty key value
if (variables.currentChar() == ParserBuilderConstants.Deserializer.COMMA) {
values.put(keys.next(), ParserBuilderConstants.Deserializer.EMPTY_STRING);
- skipCurrentChar(variables);
+ variables.skipCurrentChar();
continue;
}
// skip comma
if (keys.hasNext() && !variables.allCharsConsumed()
&& variables.currentChar() == ParserBuilderConstants.Deserializer.COMMA) {
- skipCurrentChar(variables);
+ variables.skipCurrentChar();
}
}
}
private static QName prepareQName(final MainVarsWrapper variables) {
- checkValid(
- ParserBuilderConstants.Deserializer.IDENTIFIER_FIRST_CHAR.matches(variables.currentChar()),
- "Identifier must start with character from set 'a-zA-Z_'", variables.getData(), variables.getOffset());
+ variables.checkValid(ParserBuilderConstants.Deserializer.IDENTIFIER_FIRST_CHAR.matches(variables.currentChar()),
+ "Identifier must start with character from set 'a-zA-Z_'");
final String preparedPrefix = nextIdentifierFromNextSequence(
ParserBuilderConstants.Deserializer.IDENTIFIER, variables);
final String prefix;
return getQNameOfDataSchemaNode(prefix, variables);
case ParserBuilderConstants.Deserializer.COLON:
prefix = preparedPrefix;
- skipCurrentChar(variables);
- checkValid(
+ variables.skipCurrentChar();
+ variables.checkValid(
ParserBuilderConstants.Deserializer.IDENTIFIER_FIRST_CHAR.matches(variables.currentChar()),
- "Identifier must start with character from set 'a-zA-Z_'", variables.getData(),
- variables.getOffset());
+ "Identifier must start with character from set 'a-zA-Z_'");
localName = nextIdentifierFromNextSequence(ParserBuilderConstants.Deserializer.IDENTIFIER, variables);
if (!variables.allCharsConsumed()
return getQNameOfDataSchemaNode(localName, variables);
} else {
final Module module = moduleForPrefix(prefix, variables.getSchemaContext());
- Preconditions.checkArgument(module != null, "Failed to lookup prefix %s", prefix);
+ checkArgument(module != null, "Failed to lookup prefix %s", prefix);
return QName.create(module.getQNameModule(), localName);
}
default:
private static void nextSequenceEnd(final CharMatcher matcher, final MainVarsWrapper variables) {
while (!variables.allCharsConsumed() && matcher.matches(variables.currentChar())) {
- variables.setOffset(variables.getOffset() + 1);
+ variables.skipCurrentChar();
}
}
private static void prepareNodeWithValue(final QName qname, final List<PathArgument> path,
final MainVarsWrapper variables) {
- skipCurrentChar(variables);
+ variables.skipCurrentChar();
final String value = nextIdentifierFromNextSequence(
ParserBuilderConstants.Deserializer.IDENTIFIER_PREDICATE, variables);
if (currentNode == null) {
return;
}
- checkValid(!currentNode.isKeyedEntry(), "Entry " + qname + " requires key or value predicate to be present",
- variables.getData(), variables.getOffset());
+ variables.checkValid(!currentNode.isKeyedEntry(),
+ "Entry " + qname + " requires key or value predicate to be present");
}
@SuppressFBWarnings("NP_NULL_ON_SOME_PATH") // code does check for null 'current' but FB doesn't recognize it
return null;
}
}
- checkValid(current != null, qname + " is not correct schema node identifier.", variables.getData(),
- variables.getOffset());
+ variables.checkValid(current != null, qname + " is not correct schema node identifier.");
while (current.isMixin()) {
path.add(current.getIdentifier());
current = current.getChild(qname);
private static void validArg(final MainVarsWrapper variables) {
// every identifier except of the first MUST start with slash
if (variables.getOffset() != MainVarsWrapper.STARTING_OFFSET) {
- checkValid(RestconfConstants.SLASH == variables.currentChar(),
- "Identifier must start with '/'.", variables.getData(), variables.getOffset());
+ variables.checkValid(RestconfConstants.SLASH == variables.currentChar(), "Identifier must start with '/'.");
// skip consecutive slashes, users often assume restconf URLs behave just as HTTP does by squashing
// multiple slashes into a single one
while (!variables.allCharsConsumed() && RestconfConstants.SLASH == variables.currentChar()) {
- skipCurrentChar(variables);
+ variables.skipCurrentChar();
}
// check if slash is not also the last char in identifier
- checkValid(!variables.allCharsConsumed(), "Identifier cannot end with '/'.",
- variables.getData(), variables.getOffset());
+ variables.checkValid(!variables.allCharsConsumed(), "Identifier cannot end with '/'.");
}
}
- private static void skipCurrentChar(final MainVarsWrapper variables) {
- variables.setOffset(variables.getOffset() + 1);
- }
-
- private static void checkValid(final boolean condition, final String errorMsg, final String data,
- final int offset) {
- Preconditions.checkArgument(condition, "Could not parse Instance Identifier '%s'. Offset: %s : Reason: %s",
- data, offset, errorMsg);
- }
-
private static Optional<ActionDefinition> findActionDefinition(final SchemaNode dataSchemaNode,
final String nodeName) {
requireNonNull(dataSchemaNode, "DataSchema Node must not be null.");
return offset == data.length();
}
+ void checkValid(final boolean condition, final String errorMsg) {
+ checkArgument(condition, "Could not parse Instance Identifier '%s'. Offset: %s : Reason: %s", data, offset,
+ errorMsg);
+ }
+
char currentChar() {
return data.charAt(offset);
}
+ void skipCurrentChar() {
+ offset++;
+ }
+
public String getData() {
return data;
}
return offset;
}
- public void setOffset(final int offset) {
- this.offset = offset;
- }
-
public SchemaContext getSchemaContext() {
return schemaContext;
}