- public static StatementContextBase<?, ?, ?> getAugmentTargetCtx(
- final Mutable<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> augmentNode) {
-
- final SchemaNodeIdentifier augmentTargetNode = augmentNode.getStatementArgument();
- Preconditions.checkArgument(augmentTargetNode != null,
- "Augment argument null, something bad happened in some of previous parsing phases");
-
- List<StatementContextBase<?, ?, ?>> rootStatementCtxList = new ArrayList<>();
- if (augmentTargetNode.isAbsolute()) {
-
- QNameModule module = augmentTargetNode.getPathFromRoot().iterator().next().getModule();
-
- StatementContextBase<?, ?, ?> rootStatementCtx =
- (StatementContextBase<?, ?, ?>) augmentNode.getFromNamespace(NamespaceToModule.class, module);
- rootStatementCtxList.add(rootStatementCtx);
-
- final Map<?, ?> subModules = rootStatementCtx.getAllFromNamespace(IncludedModuleContext.class);
- if (subModules != null) {
- rootStatementCtxList.addAll((Collection<? extends StatementContextBase<?, ?, ?>>) subModules.values());
- }
-
- } else {
- StatementContextBase<?, ?, ?> parent = (StatementContextBase<?, ?, ?>) augmentNode.getParentContext();
- if (StmtContextUtils.producesDeclared(parent, UsesStatement.class)) {
- rootStatementCtxList.add(parent.getParentContext());
- } else {
- // error
- }
- }
-
- StatementContextBase<?, ?, ?> augmentTargetCtx = null;
- for (final StatementContextBase<?, ?, ?> rootStatementCtx : rootStatementCtxList) {
- augmentTargetCtx = findCtxOfNodeInRoot(rootStatementCtx, augmentTargetNode);
- if (augmentTargetCtx != null) {
- break;
- }
- }
-
- return augmentTargetCtx;
- }
-
- @Nullable
- public static StatementContextBase<?, ?, ?> findCtxOfNodeInSubstatements(final StatementContextBase<?, ?, ?> rootStmtCtx,
- final Iterable<QName> path) {
-
- StatementContextBase<?, ?, ?> parent = rootStmtCtx;
-
- Iterator<QName> pathIter = path.iterator();
- while (pathIter.hasNext()) {
- QName nextPathQName = pathIter.next();
- StatementContextBase<?, ?, ?> foundSubstatement = getSubstatementByQName(parent, nextPathQName);
-
- if (foundSubstatement == null) {
- return null;
- }
- if (!pathIter.hasNext()) {
- return foundSubstatement;
- }
-
- parent = foundSubstatement;
- }
-
- return null;
- }
-
- public static StatementContextBase<?, ?, ?> getSubstatementByQName(final StatementContextBase<?, ?, ?> parent,
- final QName nextPathQName) {
-
- Collection<StatementContextBase<?, ?, ?>> declaredSubstatement = parent.declaredSubstatements();
- Collection<StatementContextBase<?, ?, ?>> effectiveSubstatement = parent.effectiveSubstatements();
-
- for (StatementContextBase<?, ?, ?> substatement : Iterables.concat(declaredSubstatement, effectiveSubstatement)) {
- Object substatementArgument = substatement.getStatementArgument();
- QName substatementQName;
- if (substatementArgument instanceof QName) {
- substatementQName = (QName) substatementArgument;
-
- if (nextPathQName.getLocalName().equals(
- substatementQName.getLocalName())) {
- if (isSupportedAugmentTarget(substatement)) {
- return substatement;
- } else if (Utils.isUnknownNode(substatement)) {
- LOG.warn("Module '{}': augment into unknown node '{}'.",
- substatement.getRoot().getStatementArgument(), substatementArgument);
- return substatement;
- }
- }
- }
- }
-
- return null;
- }
-
- public static boolean isSupportedAugmentTarget(final StatementContextBase<?, ?, ?> substatementCtx) {