package org.opendaylight.yangtools.yang.parser.stmt.rfc6020;
import static org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils.firstAttributeOf;
+
import com.google.common.base.CharMatcher;
import com.google.common.base.Preconditions;
import com.google.common.base.Splitter;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.common.QNameModule;
import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
-import org.opendaylight.yangtools.yang.common.YangConstants;
import org.opendaylight.yangtools.yang.model.api.Deviation;
import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Relative;
import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleStatement;
import org.opendaylight.yangtools.yang.model.util.RevisionAwareXPathImpl;
+import org.opendaylight.yangtools.yang.parser.spi.meta.QNameCacheNamespace;
import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
import org.opendaylight.yangtools.yang.parser.spi.source.BelongsToPrefixToModuleName;
import org.opendaylight.yangtools.yang.parser.spi.source.ModuleNameToModuleQName;
import org.opendaylight.yangtools.yang.parser.spi.source.PrefixToModule;
import org.opendaylight.yangtools.yang.parser.spi.source.QNameToStatementDefinition;
-import org.opendaylight.yangtools.yang.parser.stmt.reactor.RootStatementContext;
import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
}
private static String trimSingleLastSlashFromXPath(final String path) {
- return path.replaceAll("/$", "");
+ return path.endsWith("/") ? path.substring(0, path.length() - 1) : path;
}
static RevisionAwareXPath parseXPath(final StmtContext<?, ?, ?> ctx, final String path) {
return identifier;
}
- public static String getPrefixFromArgument(final String prefixedLocalName) {
- String[] namesParts = prefixedLocalName.split(":");
- if (namesParts.length == 2) {
- return namesParts[0];
- }
- return null;
- }
-
- public static boolean isValidStatementDefinition(final PrefixToModule prefixes, final QNameToStatementDefinition stmtDef,
- final QName identifier) {
+ /**
+ *
+ * Based on identifier read from source and collections of relevant prefixes and statement definitions mappings
+ * provided for actual phase, method resolves and returns valid QName for declared statement to be written.
+ * This applies to any declared statement, including unknown statements.
+ *
+ * @param prefixes - collection of all relevant prefix mappings supplied for actual parsing phase
+ * @param stmtDef - collection of all relevant statement definition mappings provided for actual parsing phase
+ * @param identifier - statement to parse from source
+ * @return valid QName for declared statement to be written
+ *
+ */
+ public static QName getValidStatementDefinition(final PrefixToModule prefixes, final QNameToStatementDefinition
+ stmtDef, final QName identifier) {
if (stmtDef.get(identifier) != null) {
- return true;
+ return stmtDef.get(identifier).getStatementName();
} else {
String prefixedLocalName = identifier.getLocalName();
String[] namesParts = prefixedLocalName.split(":");
String prefix = namesParts[0];
String localName = namesParts[1];
if (prefixes != null && prefixes.get(prefix) != null
- && stmtDef.get(QName.create(YangConstants.RFC6020_YIN_MODULE, localName)) != null) {
- return true;
- } else {
- if (stmtDef.get(QName.create(YangConstants.RFC6020_YIN_MODULE, localName)) != null) {
- return true;
- }
+ && stmtDef.get(QName.create(prefixes.get(prefix), localName)) != null) {
+ return QName.create(prefixes.get(prefix), localName);
}
}
}
- return false;
+ return null;
}
static SchemaNodeIdentifier nodeIdentifierFromPath(final StmtContext<?, ?, ?> ctx, final String path) {
Preconditions.checkArgument(qNameModule != null, "Error in module '%s': can not resolve QNameModule for '%s'.",
ctx.getRoot().rawStatementArgument(), value);
+ final QNameModule resultQNameModule;
+ if (qNameModule.getRevision() == null) {
+ resultQNameModule = QNameModule.cachedReference(
+ QNameModule.create(qNameModule.getNamespace(), SimpleDateFormatUtil.DEFAULT_DATE_REV));
+ } else {
+ resultQNameModule = qNameModule;
+ }
- QNameModule resultQNameModule = qNameModule.getRevision() == null ? QNameModule.create(
- qNameModule.getNamespace(), SimpleDateFormatUtil.DEFAULT_DATE_REV) : qNameModule;
-
- return QName.create(resultQNameModule, localName);
+ return ctx.getFromNamespace(QNameCacheNamespace.class, QName.create(resultQNameModule, localName));
}
public static QNameModule getModuleQNameByPrefix(final StmtContext<?, ?, ?> ctx, final String prefix) {
- QNameModule qNameModule;
- ModuleIdentifier impModIdentifier = ctx.getRoot().getFromNamespace(ImpPrefixToModuleIdentifier.class, prefix);
- qNameModule = ctx.getFromNamespace(ModuleIdentifierToModuleQName.class, impModIdentifier);
+ final ModuleIdentifier modId = ctx.getRoot().getFromNamespace(ImpPrefixToModuleIdentifier.class, prefix);
+ final QNameModule qNameModule = ctx.getFromNamespace(ModuleIdentifierToModuleQName.class, modId);
if (qNameModule == null && StmtContextUtils.producesDeclared(ctx.getRoot(), SubmoduleStatement.class)) {
String moduleName = ctx.getRoot().getFromNamespace(BelongsToPrefixToModuleName.class, prefix);
- qNameModule = ctx.getFromNamespace(ModuleNameToModuleQName.class, moduleName);
+ return ctx.getFromNamespace(ModuleNameToModuleQName.class, moduleName);
}
return qNameModule;
}
public static QNameModule getRootModuleQName(final StmtContext<?, ?, ?> ctx) {
-
if (ctx == null) {
return null;
}
- StmtContext<?, ?, ?> rootCtx = ctx.getRoot();
- QNameModule qNameModule = null;
+ final StmtContext<?, ?, ?> rootCtx = ctx.getRoot();
+ final QNameModule qNameModule;
if (StmtContextUtils.producesDeclared(rootCtx, ModuleStatement.class)) {
qNameModule = rootCtx.getFromNamespace(ModuleCtxToModuleQName.class, rootCtx);
} else if (StmtContextUtils.producesDeclared(rootCtx, SubmoduleStatement.class)) {
- String belongsToModuleName = firstAttributeOf(rootCtx.substatements(),
- BelongsToStatement.class);
+ final String belongsToModuleName = firstAttributeOf(rootCtx.substatements(), BelongsToStatement.class);
qNameModule = rootCtx.getFromNamespace(ModuleNameToModuleQName.class, belongsToModuleName);
+ } else {
+ qNameModule = null;
+ }
+
+ Preconditions.checkArgument(qNameModule != null, "Failed to look up root QNameModule for %s", ctx);
+ if (qNameModule.getRevision() != null) {
+ return qNameModule;
}
- return qNameModule.getRevision() == null ? QNameModule.create(qNameModule.getNamespace(),
- SimpleDateFormatUtil.DEFAULT_DATE_REV) : qNameModule;
+ return QNameModule.cachedReference(
+ QNameModule.create(qNameModule.getNamespace(), SimpleDateFormatUtil.DEFAULT_DATE_REV));
}
@Nullable
return status;
}
- public static Date getLatestRevision(final RootStatementContext<?, ?, ?> root) {
- return getLatestRevision(root.declaredSubstatements());
- }
-
public static Date getLatestRevision(final Iterable<? extends StmtContext<?, ?, ?>> subStmts) {
Date revision = null;
for (StmtContext<?, ?, ?> subStmt : subStmts) {