@Override
  public void visit(VdbElement vdbElement) {
    if (!checkContext(VDB)) return;

    CommentSets commentSets = vdbElement.getComments();
    List<String> comments = commentSets.getElementCommentSet();
    insertComments(this.context, comments);

    Node vdbContext = this.context;
    this.context = vdbContext.getFirstChild();
    while (this.context != null) {
      if (checkContext(DESCRIPTION) || checkContext(CONNECTION_TYPE)) {
        comments = commentSets.getCommentSet(DESCRIPTION);
        insertComments(this.context, comments);

        this.context = this.context.getNextSibling();
        continue;
      }

      if (checkContext(PROPERTY)) {
        for (PropertyElement pe : vdbElement.getProperties()) pe.accept(this);

        this.context = this.context.getNextSibling();
        continue;
      }

      if (checkContext(IMPORT_VDB)) {
        for (ImportVdbElement ive : vdbElement.getImportVdbEntries()) ive.accept(this);

        this.context = this.context.getNextSibling();
        continue;
      }

      if (checkContext(MODEL)) {
        for (ModelElement me : vdbElement.getModels()) me.accept(this);

        this.context = this.context.getNextSibling();
        continue;
      }

      if (checkContext(TRANSLATOR)) {
        for (TranslatorElement te : vdbElement.getTranslators()) te.accept(this);

        this.context = this.context.getNextSibling();
        continue;
      }

      if (checkContext(DATA_ROLE)) {
        for (DataRoleElement dre : vdbElement.getDataPolicies()) dre.accept(this);
      }

      this.context = this.context.getNextSibling();
    }

    this.context = vdbContext;
  }
  @Override
  public void visit(DataRoleElement drElement) {
    if (!checkContext(DATA_ROLE)) return;

    if (!nameAttribute(drElement.getName())) return;

    CommentSets commentSets = drElement.getComments();
    List<String> comments = commentSets.getElementCommentSet();
    insertComments(this.context, comments);

    Node drContext = this.context;
    this.context = drContext.getFirstChild();
    while (this.context != null) {

      if (checkContext(DESCRIPTION)) {
        comments = commentSets.getCommentSet(DESCRIPTION);
        insertComments(this.context, comments);

        this.context = this.context.getNextSibling();
        continue;
      }

      if (checkContext(MAPPED_ROLE_NAME)) {
        comments =
            commentSets.getCommentSet(MAPPED_ROLE_NAME + HYPHEN + this.context.getTextContent());
        insertComments(this.context, comments);

        this.context = this.context.getNextSibling();
        continue;
      }

      if (checkContext(PERMISSION)) {
        for (PermissionElement pe : drElement.getPermissions()) pe.accept(this);
      }

      this.context = this.context.getNextSibling();
    }

    this.context = drContext;
  }
  @Override
  public void visit(ModelElement modelElement) {
    if (!checkContext(MODEL)) return;

    if (!nameAttribute(modelElement.getName())) return;

    CommentSets commentSets = modelElement.getComments();
    List<String> comments = commentSets.getElementCommentSet();
    insertComments(this.context, comments);

    Node modelContext = this.context;
    this.context = modelContext.getFirstChild();
    while (this.context != null) {

      if (checkContext(DESCRIPTION)) {
        comments = commentSets.getCommentSet(DESCRIPTION);
        insertComments(this.context, comments);

        this.context = this.context.getNextSibling();
        continue;
      }

      if (checkContext(PROPERTY)) {
        for (PropertyElement pe : modelElement.getProperties()) pe.accept(this);

        this.context = this.context.getNextSibling();
        continue;
      }

      if (checkContext(SOURCE)) {
        for (SourceElement se : modelElement.getSources()) se.accept(this);

        this.context = this.context.getNextSibling();
        continue;
      }

      if (checkContext(METADATA)) {
        for (MetadataElement me : modelElement.getMetadata()) me.accept(this);
      }

      this.context = this.context.getNextSibling();
    }

    this.context = modelContext;
  }
 private void checkComment(Comments expected, CommentSets comments, String commentKey, int index) {
   List<String> commentsList = comments.getCommentSet(commentKey);
   assertTrue(commentsList.size() > 0);
   assertTrue(commentsList.size() > index);
   assertEquals(expected.toString(), commentsList.get(index));
 }
  @Override
  public void visit(PermissionElement permElement) {
    if (!checkContext(PERMISSION)) return;

    //
    // Check we have the right permission by checking its
    // resource name sub-element
    //
    String resourceName = null;
    Node permContext = this.context;
    this.context = permContext.getFirstChild();
    while (this.context != null) {
      if (checkContext(RESOURCE_NAME)) {
        resourceName = this.context.getTextContent();
        break;
      }

      this.context = this.context.getNextSibling();
    }
    this.context = permContext;

    if (!permElement.getResourceName().equals(resourceName)) return;

    CommentSets commentSets = permElement.getComments();
    List<String> comments = commentSets.getElementCommentSet();
    insertComments(this.context, comments);

    this.context = permContext.getFirstChild();
    while (this.context != null) {

      if (checkContext(RESOURCE_NAME)) {
        comments = commentSets.getCommentSet(RESOURCE_NAME);
        insertComments(this.context, comments);

        this.context = this.context.getNextSibling();
        continue;
      }

      if (checkContext(MASK) && permElement.getMask() != null) {
        permElement.getMask().accept(this);
        this.context = this.context.getNextSibling();
        continue;
      }

      if (checkContext(CONDITION) && permElement.getCondition() != null) {
        permElement.getCondition().accept(this);
        this.context = this.context.getNextSibling();
        continue;
      }

      if (checkContext(ALLOW_CREATE)
          || checkContext(ALLOW_READ)
          || checkContext(ALLOW_UPDATE)
          || checkContext(ALLOW_DELETE)
          || checkContext(ALLOW_EXECUTE)
          || checkContext(ALLOW_ALTER)
          || checkContext(ALLOW_LANGUAGE)) {
        comments = commentSets.getCommentSet(this.context.getNodeName());
        insertComments(this.context, comments);
      }

      this.context = this.context.getNextSibling();
    }
    this.context = permContext;
  }