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 writeBinaryStorageModel(
      XMLExtendedStreamWriter writer, String storageType, ModelNode storage)
      throws XMLStreamException {
    if (ModelKeys.FILE_BINARY_STORAGE.equals(storageType)) {
      // This is the default, but there is no default value for the ModelAttributes.PATH (which is
      // required),
      // which means we always have to write this out. If it is the default binary storage, then
      // there
      // won't even be a 'binary-storage=BINARIES' model node.
      writer.writeStartElement(Element.FILE_BINARY_STORAGE.getLocalName());
      ModelAttributes.MINIMUM_BINARY_SIZE.marshallAsAttribute(storage, false, writer);
      ModelAttributes.MINIMUM_STRING_SIZE.marshallAsAttribute(storage, false, writer);
      ModelAttributes.PATH.marshallAsAttribute(storage, false, writer);
      ModelAttributes.RELATIVE_TO.marshallAsAttribute(storage, false, writer);
      ModelAttributes.STORE_NAME.marshallAsAttribute(storage, false, writer);
      ModelAttributes.MIME_TYPE_DETECTION.marshallAsAttribute(storage, false, writer);
      writer.writeEndElement();
    } else if (ModelKeys.CACHE_BINARY_STORAGE.equals(storageType)) {
      writer.writeStartElement(Element.CACHE_BINARY_STORAGE.getLocalName());
      ModelAttributes.MINIMUM_BINARY_SIZE.marshallAsAttribute(storage, false, writer);
      ModelAttributes.MINIMUM_STRING_SIZE.marshallAsAttribute(storage, false, writer);
      ModelAttributes.DATA_CACHE_NAME.marshallAsAttribute(storage, false, writer);
      ModelAttributes.METADATA_CACHE_NAME.marshallAsAttribute(storage, false, writer);
      ModelAttributes.CHUNK_SIZE.marshallAsAttribute(storage, false, writer);
      ModelAttributes.CACHE_CONFIG.marshallAsAttribute(storage, false, writer);
      ModelAttributes.STORE_NAME.marshallAsAttribute(storage, false, writer);
      ModelAttributes.MIME_TYPE_DETECTION.marshallAsAttribute(storage, false, writer);
      writer.writeEndElement();
    } else if (ModelKeys.DB_BINARY_STORAGE.equals(storageType)) {
      writer.writeStartElement(Element.DB_BINARY_STORAGE.getLocalName());
      ModelAttributes.MINIMUM_BINARY_SIZE.marshallAsAttribute(storage, false, writer);
      ModelAttributes.MINIMUM_STRING_SIZE.marshallAsAttribute(storage, false, writer);
      ModelAttributes.DATA_SOURCE_JNDI_NAME.marshallAsAttribute(storage, false, writer);
      ModelAttributes.STORE_NAME.marshallAsAttribute(storage, false, writer);
      ModelAttributes.MIME_TYPE_DETECTION.marshallAsAttribute(storage, false, writer);
      writer.writeEndElement();
    } else if (ModelKeys.COMPOSITE_BINARY_STORAGE.equals(storageType)) {
      writer.writeStartElement(Element.COMPOSITE_BINARY_STORAGE.getLocalName());
      ModelAttributes.MINIMUM_BINARY_SIZE.marshallAsAttribute(storage, false, writer);
      ModelAttributes.MINIMUM_STRING_SIZE.marshallAsAttribute(storage, false, writer);
      ModelAttributes.STORE_NAME.marshallAsAttribute(storage, false, writer);
      ModelAttributes.MIME_TYPE_DETECTION.marshallAsAttribute(storage, false, writer);

      writeNestedStoresOfType(
          storage, ModelKeys.NESTED_STORAGE_TYPE_FILE, ModelKeys.FILE_BINARY_STORAGE, writer);
      writeNestedStoresOfType(
          storage, ModelKeys.NESTED_STORAGE_TYPE_CACHE, ModelKeys.CACHE_BINARY_STORAGE, writer);
      writeNestedStoresOfType(
          storage, ModelKeys.NESTED_STORAGE_TYPE_DB, ModelKeys.DB_BINARY_STORAGE, writer);
      writeNestedStoresOfType(
          storage, ModelKeys.NESTED_STORAGE_TYPE_CUSTOM, ModelKeys.CUSTOM_BINARY_STORAGE, writer);

      writer.writeEndElement();
    } else if (ModelKeys.CUSTOM_BINARY_STORAGE.equals(storageType)) {
      writer.writeStartElement(Element.CUSTOM_BINARY_STORAGE.getLocalName());
      ModelAttributes.MINIMUM_BINARY_SIZE.marshallAsAttribute(storage, false, writer);
      ModelAttributes.MINIMUM_STRING_SIZE.marshallAsAttribute(storage, false, writer);
      ModelAttributes.STORE_NAME.marshallAsAttribute(storage, false, writer);
      ModelAttributes.MIME_TYPE_DETECTION.marshallAsAttribute(storage, false, writer);
      for (String key : storage.keys()) {
        if (key.equals(ModelKeys.CLASSNAME)) {
          ModelAttributes.CLASSNAME.marshallAsAttribute(storage, false, writer);
        } else if (key.equals(ModelKeys.MODULE)) {
          ModelAttributes.MODULE.marshallAsAttribute(storage, false, writer);
        } else {
          writer.writeAttribute(key, storage.get(key).asString());
        }
      }
      writer.writeEndElement();
    }
  }