private void writeRepositoryConfiguration(
      XMLExtendedStreamWriter writer, ModelNode repository, String repositoryName)
      throws XMLStreamException {

    writer.writeStartElement(Element.REPOSITORY.getLocalName());
    writer.writeAttribute(Attribute.NAME.getLocalName(), repositoryName);

    // Repository attributes ...

    ModelAttributes.CACHE_NAME.marshallAsAttribute(repository, false, writer);
    ModelAttributes.CACHE_CONFIG.marshallAsAttribute(repository, false, writer);
    ModelAttributes.CONFIG_RELATIVE_TO.marshallAsAttribute(repository, false, writer);
    ModelAttributes.JNDI_NAME.marshallAsAttribute(repository, false, writer);
    ModelAttributes.ENABLE_MONITORING.marshallAsAttribute(repository, false, writer);
    ModelAttributes.SECURITY_DOMAIN.marshallAsAttribute(repository, false, writer);
    writeAttributeAsList(writer, repository, ModelAttributes.ANONYMOUS_ROLES);
    ModelAttributes.ANONYMOUS_USERNAME.marshallAsAttribute(repository, false, writer);
    ModelAttributes.USE_ANONYMOUS_IF_AUTH_FAILED.marshallAsAttribute(repository, false, writer);
    ModelAttributes.GARBAGE_COLLECTION_THREAD_POOL.marshallAsAttribute(repository, false, writer);
    ModelAttributes.GARBAGE_COLLECTION_INITIAL_TIME.marshallAsAttribute(repository, false, writer);
    ModelAttributes.GARBAGE_COLLECTION_INTERVAL.marshallAsAttribute(repository, false, writer);
    ModelAttributes.DOCUMENT_OPTIMIZATION_THREAD_POOL.marshallAsAttribute(
        repository, false, writer);
    ModelAttributes.DOCUMENT_OPTIMIZATION_INITIAL_TIME.marshallAsAttribute(
        repository, false, writer);
    ModelAttributes.DOCUMENT_OPTIMIZATION_INTERVAL.marshallAsAttribute(repository, false, writer);
    ModelAttributes.DOCUMENT_OPTIMIZATION_CHILD_COUNT_TARGET.marshallAsAttribute(
        repository, false, writer);
    ModelAttributes.DOCUMENT_OPTIMIZATION_CHILD_COUNT_TOLERANCE.marshallAsAttribute(
        repository, false, writer);
    ModelAttributes.EVENT_BUS_SIZE.marshallAsAttribute(repository, false, writer);

    // Nested elements ...
    writeNodeTypes(writer, repository);
    writeWorkspaces(writer, repository);
    writeJournaling(writer, repository);
    writeAuthenticators(writer, repository);
    writeIndexProviders(writer, repository);
    writeIndexes(writer, repository);
    writeBinaryStorage(writer, repository);
    writeSequencing(writer, repository);
    writeExternalSources(writer, repository);
    writeTextExtraction(writer, repository);
    writer.writeEndElement();
  }
  private void parseRepository(
      final XMLExtendedStreamReader reader,
      final ModelNode address,
      final List<ModelNode> repositories)
      throws XMLStreamException {

    final ModelNode repositoryAddress = address.clone();
    final ModelNode repository =
        Util.getEmptyOperation(ModelDescriptionConstants.ADD, repositoryAddress);

    String repositoryName = null;
    if (reader.getAttributeCount() > 0) {
      for (int i = 0; i < reader.getAttributeCount(); i++) {
        String attrName = reader.getAttributeLocalName(i);
        String attrValue = reader.getAttributeValue(i);
        Attribute attribute = Attribute.forName(attrName);
        switch (attribute) {
          case NAME:
            repositoryName = attrValue;
            repositoryAddress.add("repository", attrValue); // $NON-NLS-1$
            repositoryAddress.protect();
            repository.get(OP).set(ADD);
            repository.get(OP_ADDR).set(repositoryAddress);
            repositories.add(repository);
            break;
          case CACHE_NAME:
            ModelAttributes.CACHE_NAME.parseAndSetParameter(attrValue, repository, reader);
            break;
          case CACHE_CONTAINER:
            ModelAttributes.CACHE_CONTAINER.parseAndSetParameter(attrValue, repository, reader);
            break;
          case JNDI_NAME:
            ModelAttributes.JNDI_NAME.parseAndSetParameter(attrValue, repository, reader);
            break;
          case ENABLE_MONITORING:
            ModelAttributes.ENABLE_MONITORING.parseAndSetParameter(attrValue, repository, reader);
            break;
          case SECURITY_DOMAIN:
            ModelAttributes.SECURITY_DOMAIN.parseAndSetParameter(attrValue, repository, reader);
            break;
          case ANONYMOUS_ROLES:
            for (String role : reader.getListAttributeValue(i)) {
              repository.get(ModelKeys.ANONYMOUS_ROLES).add(role);
            }
            break;
          case ANONYMOUS_USERNAME:
            ModelAttributes.ANONYMOUS_USERNAME.parseAndSetParameter(attrValue, repository, reader);
            break;
          case USE_ANONYMOUS_IF_AUTH_FAILED:
            ModelAttributes.USE_ANONYMOUS_IF_AUTH_FAILED.parseAndSetParameter(
                attrValue, repository, reader);
            break;
          case CLUSTER_STACK:
            ModelAttributes.CLUSTER_STACK.parseAndSetParameter(attrValue, repository, reader);
            break;
          case CLUSTER_NAME:
            ModelAttributes.CLUSTER_NAME.parseAndSetParameter(attrValue, repository, reader);
            break;
          default:
            throw ParseUtils.unexpectedAttribute(reader, i);
        }
      }
    }

    ModelNode indexStorage = null;
    ModelNode binaryStorage = null;
    List<ModelNode> sequencers = new ArrayList<ModelNode>();
    List<ModelNode> textExtractors = new ArrayList<ModelNode>();
    while (reader.hasNext() && (reader.nextTag() != XMLStreamConstants.END_ELEMENT)) {
      Element element = Element.forName(reader.getLocalName());
      switch (element) {
        case WORKSPACES:
          parseWorkspaces(reader, address, repository);
          break;
        case INDEXING:
          parseIndexing(reader, address, repository);
          break;

          // Index storage ...
        case RAM_INDEX_STORAGE:
          addIndexStorageConfiguration(repositories, repositoryName);
          indexStorage = parseRamIndexStorage(reader, repositoryName);
          break;
        case LOCAL_FILE_INDEX_STORAGE:
          addIndexStorageConfiguration(repositories, repositoryName);
          indexStorage =
              parseFileIndexStorage(reader, repositoryName, ModelKeys.LOCAL_FILE_INDEX_STORAGE);
          break;
        case MASTER_FILE_INDEX_STORAGE:
          addIndexStorageConfiguration(repositories, repositoryName);
          indexStorage =
              parseFileIndexStorage(reader, repositoryName, ModelKeys.MASTER_FILE_INDEX_STORAGE);
          break;
        case SLAVE_FILE_INDEX_STORAGE:
          addIndexStorageConfiguration(repositories, repositoryName);
          indexStorage =
              parseFileIndexStorage(reader, repositoryName, ModelKeys.SLAVE_FILE_INDEX_STORAGE);
          break;
        case CACHE_INDEX_STORAGE:
          addIndexStorageConfiguration(repositories, repositoryName);
          indexStorage = parseCacheIndexStorage(reader, repositoryName);
          break;
        case CUSTOM_INDEX_STORAGE:
          addIndexStorageConfiguration(repositories, repositoryName);
          indexStorage = parseCustomIndexStorage(reader, repositoryName);
          break;

          // Binary storage ...
        case FILE_BINARY_STORAGE:
          addBinaryStorageConfiguration(repositories, repositoryName);
          binaryStorage = parseFileBinaryStorage(reader, repositoryName);
          break;
        case DB_BINARY_STORAGE:
          addBinaryStorageConfiguration(repositories, repositoryName);
          binaryStorage = parseDatabaseBinaryStorage(reader, repositoryName);
          break;
        case CACHE_BINARY_STORAGE:
          addBinaryStorageConfiguration(repositories, repositoryName);
          binaryStorage = parseCacheBinaryStorage(reader, repositoryName);
          break;
        case CUSTOM_BINARY_STORAGE:
          addBinaryStorageConfiguration(repositories, repositoryName);
          binaryStorage = parseCustomBinaryStorage(reader, repositoryName);
          break;

          // Sequencing ...
        case AUTHENTICATORS:
          parseAuthenticators(reader, address, repository);
          break;

          // Sequencing ...
        case SEQUENCERS:
          sequencers = parseSequencers(reader, address, repositoryName);
          break;

          // Text extracting ...
        case TEXT_EXTRACTORS:
          textExtractors = parseTextExtracting(reader, repositoryName);
          break;

        default:
          throw ParseUtils.unexpectedElement(reader);
      }
    }

    if (indexStorage != null) repositories.add(indexStorage);
    if (binaryStorage != null) repositories.add(binaryStorage);
    repositories.addAll(sequencers);
    repositories.addAll(textExtractors);
  }