+ private void copyStatements(final SubstatementContext<A, D, E> original, final QNameModule newQNameModule,
+ final CopyType typeOfCopy) {
+ final Collection<StatementContextBase<?, ?, ?>> declared = original.declaredSubstatements();
+ final Collection<StatementContextBase<?, ?, ?>> effective = original.effectiveSubstatements();
+ final Collection<StatementContextBase<?, ?, ?>> buffer = new ArrayList<>(declared.size() + effective.size());
+
+ for (final StatementContextBase<?, ?, ?> stmtContext : declared) {
+ if (StmtContextUtils.areFeaturesSupported(stmtContext)) {
+ copySubstatement(stmtContext, newQNameModule, typeOfCopy, buffer);
+ }
+ }
+
+ for (final StatementContextBase<?, ?, ?> stmtContext : effective) {
+ copySubstatement(stmtContext, newQNameModule, typeOfCopy, buffer);
+ }
+
+ addEffectiveSubstatements(buffer);
+ }
+
+ private void copySubstatement(final StatementContextBase<?, ?, ?> stmtContext,
+ final QNameModule newQNameModule, final CopyType typeOfCopy,
+ final Collection<StatementContextBase<?, ?, ?>> buffer) {
+ if (needToCopyByUses(stmtContext)) {
+ final StatementContextBase<?, ?, ?> copy = stmtContext.createCopy(newQNameModule, this, typeOfCopy);
+ LOG.debug("Copying substatement {} for {} as", stmtContext, this, copy);
+ buffer.add(copy);
+ } else if (isReusedByUses(stmtContext)) {
+ LOG.debug("Reusing substatement {} for {}", stmtContext, this);
+ buffer.add(stmtContext);
+ } else {
+ LOG.debug("Skipping statement {}", stmtContext);
+ }
+ }
+
+ // FIXME: revise this, as it seems to be wrong
+ private static final Set<Rfc6020Mapping> NOCOPY_FROM_GROUPING_SET = ImmutableSet.of(
+ Rfc6020Mapping.DESCRIPTION,
+ Rfc6020Mapping.REFERENCE,
+ Rfc6020Mapping.STATUS);
+ private static final Set<Rfc6020Mapping> REUSED_DEF_SET = ImmutableSet.of(
+ Rfc6020Mapping.TYPE,
+ Rfc6020Mapping.TYPEDEF,
+ Rfc6020Mapping.USES);
+
+ private static boolean needToCopyByUses(final StmtContext<?, ?, ?> stmtContext) {
+ final StatementDefinition def = stmtContext.getPublicDefinition();
+ if (REUSED_DEF_SET.contains(def)) {
+ LOG.debug("Will reuse {} statement {}", def, stmtContext);
+ return false;
+ }
+ if (NOCOPY_FROM_GROUPING_SET.contains(def)) {
+ return !Rfc6020Mapping.GROUPING.equals(stmtContext.getParentContext().getPublicDefinition());
+ }
+
+ LOG.debug("Will copy {} statement {}", def, stmtContext);
+ return true;
+ }
+
+ private static boolean isReusedByUses(final StmtContext<?, ?, ?> stmtContext) {
+ return REUSED_DEF_SET.contains(stmtContext.getPublicDefinition());
+ }
+