import static com.google.common.base.Preconditions.checkArgument;
import static java.util.Objects.requireNonNull;
-import com.google.common.base.Splitter;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import java.util.Collection;
import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
public final class StmtContextUtils {
- public static final Splitter LIST_KEY_SPLITTER = Splitter.on(' ').omitEmptyStrings().trimResults();
-
private StmtContextUtils() {
- throw new UnsupportedOperationException("Utility class");
+ // Hidden on purpose
}
@SuppressWarnings("unchecked")
public static boolean isInExtensionBody(final StmtContext<?, ?, ?> stmtCtx) {
StmtContext<?, ?, ?> current = stmtCtx;
- while (current.getParentContext().getParentContext() != null) {
+ while (current.coerceParentContext().getParentContext() != null) {
current = current.getParentContext();
if (isUnknownStatement(current)) {
return true;
for (final StmtContext<?, ?, ?> stmt : stmtContext.declaredSubstatements()) {
if (YangStmtMapping.IF_FEATURE.equals(stmt.getPublicDefinition())) {
containsIfFeature = true;
- if (((Predicate<Set<QName>>) stmt.getStatementArgument()).test(supportedFeatures)) {
+ @SuppressWarnings("unchecked")
+ final Predicate<Set<QName>> argument = (Predicate<Set<QName>>) stmt.coerceStatementArgument();
+ if (argument.test(supportedFeatures)) {
isSupported = true;
} else {
isSupported = false;
requireNonNull(ancestorType);
requireNonNull(ancestorChildType);
- StmtContext<?, ?, ?> current = ctx.getParentContext();
+ StmtContext<?, ?, ?> current = ctx.coerceParentContext();
StmtContext<?, ?, ?> parent = current.getParentContext();
while (parent != null) {
if (ancestorType.equals(current.getPublicDefinition())) {
public static boolean hasParentOfType(final StmtContext<?, ?, ?> ctx, final StatementDefinition parentType) {
requireNonNull(parentType);
final StmtContext<?, ?, ?> parentContext = ctx.getParentContext();
- return parentContext != null ? parentType.equals(parentContext.getPublicDefinition()) : false;
+ return parentContext != null && parentType.equals(parentContext.getPublicDefinition());
}
/**
private static boolean isRelevantForIfFeatureAndWhenOnListKeysCheck(final StmtContext<?, ?, ?> ctx) {
return YangVersion.VERSION_1_1.equals(ctx.getRootVersion())
&& StmtContextUtils.hasParentOfType(ctx, YangStmtMapping.LIST)
- && StmtContextUtils.findFirstDeclaredSubstatement(ctx.getParentContext(), KeyStatement.class) != null;
+ && StmtContextUtils.findFirstDeclaredSubstatement(ctx.coerceParentContext(),
+ KeyStatement.class) != null;
}
private static boolean isListKey(final StmtContext<?, ?, ?> leafStmtCtx,
final StmtContext<Collection<SchemaNodeIdentifier>, ?, ?> keyStmtCtx) {
- for (final SchemaNodeIdentifier keyIdentifier : keyStmtCtx.getStatementArgument()) {
+ for (final SchemaNodeIdentifier keyIdentifier : keyStmtCtx.coerceStatementArgument()) {
if (leafStmtCtx.getStatementArgument().equals(keyIdentifier.getLastComponent())) {
return true;
}
ctx = ctx.getOriginalCtx().orElse(null);
qnameModule = StmtContextUtils.getModuleQNameByPrefix(ctx, prefix);
}
- break;
}
- qnameModule = InferenceException.throwIfNull(qnameModule, ctx.getStatementSourceReference(),
- "Cannot resolve QNameModule for '%s'", value);
- return ctx.getFromNamespace(QNameCacheNamespace.class, QName.create(qnameModule, localName));
+ return internedQName(ctx,
+ InferenceException.throwIfNull(qnameModule, ctx.getStatementSourceReference(),
+ "Cannot resolve QNameModule for '%s'", value), localName);
+ }
+
+ /**
+ * Parse a YANG identifier string in context of a statement.
+ *
+ * @param ctx Statement context
+ * @param str String to be parsed
+ * @return An interned QName
+ * @throws NullPointerException if any of the arguments are null
+ * @throws SourceException if the string is not a valid YANG identifier
+ */
+ public static QName parseIdentifier(final StmtContext<?, ?, ?> ctx, final String str) {
+ SourceException.throwIf(str.isEmpty(), ctx.getStatementSourceReference(),
+ "Identifier may not be an empty string");
+ return internedQName(ctx, str);
+ }
+
+ public static QName parseNodeIdentifier(StmtContext<?, ?, ?> ctx, final String prefix,
+ final String localName) {
+ final QNameModule module = StmtContextUtils.getModuleQNameByPrefix(ctx, prefix);
+ if (module != null) {
+ return internedQName(ctx, module, localName);
+ }
+
+ if (ctx.getCopyHistory().getLastOperation() == CopyType.ADDED_BY_AUGMENTATION) {
+ final Optional<StmtContext<?, ?, ?>> optOrigCtx = ctx.getOriginalCtx();
+ if (optOrigCtx.isPresent()) {
+ ctx = optOrigCtx.get();
+ final QNameModule origModule = StmtContextUtils.getModuleQNameByPrefix(ctx, prefix);
+ if (origModule != null) {
+ return internedQName(ctx, origModule, localName);
+ }
+ }
+ }
+
+ throw new InferenceException(ctx.getStatementSourceReference(), "Cannot resolve QNameModule for '%s'", prefix);
+ }
+
+ /**
+ * Parse a YANG node identifier string in context of a statement.
+ *
+ * @param ctx Statement context
+ * @param str String to be parsed
+ * @return An interned QName
+ * @throws NullPointerException if any of the arguments are null
+ * @throws SourceException if the string is not a valid YANG node identifier
+ */
+ public static QName parseNodeIdentifier(final StmtContext<?, ?, ?> ctx, final String str) {
+ SourceException.throwIf(str.isEmpty(), ctx.getStatementSourceReference(),
+ "Node identifier may not be an empty string");
+
+ final int colon = str.indexOf(':');
+ if (colon == -1) {
+ return internedQName(ctx, str);
+ }
+
+ final String prefix = str.substring(0, colon);
+ SourceException.throwIf(prefix.isEmpty(), ctx.getStatementSourceReference(),
+ "String '%s' has an empty prefix", str);
+ final String localName = str.substring(colon + 1);
+ SourceException.throwIf(localName.isEmpty(), ctx.getStatementSourceReference(),
+ "String '%s' has an empty identifier", str);
+
+ return parseNodeIdentifier(ctx, prefix, localName);
+ }
+
+ private static QName internedQName(final StmtContext<?, ?, ?> ctx, final String localName) {
+ return internedQName(ctx, StmtContextUtils.getRootModuleQName(ctx), localName);
+ }
+
+ private static QName internedQName(final StmtContext<?, ?, ?> ctx, final QNameModule module,
+ final String localName) {
+ final QName template;
+ try {
+ template = QName.create(module, localName);
+ } catch (IllegalArgumentException e) {
+ throw new SourceException(ctx.getStatementSourceReference(), e, "Invalid identifier '%s'", localName);
+ }
+
+ return ctx.getFromNamespace(QNameCacheNamespace.class, template);
}
public static QNameModule getRootModuleQName(final StmtContext<?, ?, ?> ctx) {
final QNameModule qNameModule = root.getFromNamespace(ModuleCtxToModuleQName.class, root);
if (qNameModule != null) {
// creates SourceIdentifier for a module
- if (qNameModule.getFormattedRevision() == null) {
- return RevisionSourceIdentifier.create((String) root.getStatementArgument());
- }
-
- return RevisionSourceIdentifier.create((String) root.getStatementArgument(),
- qNameModule.getFormattedRevision());
+ return RevisionSourceIdentifier.create((String) root.getStatementArgument(), qNameModule.getRevision());
}
// creates SourceIdentifier for a submodule
final Optional<Revision> revision = getLatestRevision(root.declaredSubstatements());
- return revision.isPresent()
- ? RevisionSourceIdentifier.create((String) root.getStatementArgument(), revision.get().toString())
- : RevisionSourceIdentifier.create((String) root.getStatementArgument());
+ return RevisionSourceIdentifier.create((String) root.getStatementArgument(), revision);
}
public static Optional<Revision> getLatestRevision(final Iterable<? extends StmtContext<?, ?, ?>> subStmts) {
RevisionStatement.class)) {
if (revision == null && subStmt.getStatementArgument() != null) {
revision = (Revision) subStmt.getStatementArgument();
- } else if (subStmt.getStatementArgument() != null
- && ((Revision) subStmt.getStatementArgument()).compareTo(revision) > 0) {
- revision = (Revision) subStmt.getStatementArgument();
+ } else {
+ final Revision subArg = (Revision) subStmt.getStatementArgument();
+ if (subArg != null && subArg.compareTo(revision) > 0) {
+ revision = subArg;
+ }
}
}
}