/**
   * Convert the contents of a ProfileManager to XML and write the XML to the given writer.
   *
   * @param profileManager the ProfileManager
   * @param writer the XMLStreamWriter to write to
   */
  public static void marshal(ProfileManager profileManager, XMLStreamWriter writer) {
    try {
      writer.writeStartElement("userprofiles");
      String profileVersion = getProfileVersion(profileManager.getProfileObjectStoreWriter());
      writer.writeAttribute(MetadataManager.PROFILE_FORMAT_VERSION, profileVersion);
      List<?> usernames = profileManager.getProfileUserNames();

      for (Object userName : usernames) {
        Profile profile = profileManager.getProfile((String) userName);
        LOG.info("Writing profile: " + profile.getUsername());
        long startTime = System.currentTimeMillis();

        ProfileBinding.marshal(
            profile,
            profileManager.getProductionObjectStore(),
            writer,
            profileManager.getVersion(),
            getClassKeys(profileManager.getProductionObjectStore()));

        long totalTime = System.currentTimeMillis() - startTime;
        LOG.info(
            "Finished writing profile: " + profile.getUsername() + " took " + totalTime + "ms.");
      }

      TrackManagerBinding.marshal(profileManager.getProfileObjectStoreWriter(), writer);
      writer.writeEndElement();
    } catch (XMLStreamException e) {
      throw new RuntimeException(e);
    }
  }