/** {@inheritDoc} */
 @Override
 public void startElement(String uri, String localName, String qName, Attributes attrs)
     throws SAXException {
   if ("userprofiles".equals(qName)) {
     String value = attrs.getValue(MetadataManager.PROFILE_FORMAT_VERSION);
     if (value == null) {
       version = 0;
     } else {
       version = Integer.parseInt(value);
     }
     ObjectStoreWriter userprofileOsw = profileManager.getProfileObjectStoreWriter();
     Connection con = null;
     try {
       con = ((ObjectStoreInterMineImpl) userprofileOsw).getConnection();
       if (!DatabaseUtil.tableExists(con, "bagvalues")) {
         DatabaseUtil.createBagValuesTables(con);
       }
     } catch (SQLException sqle) {
       LOG.error("Problem retrieving connection", sqle);
     } finally {
       ((ObjectStoreInterMineImpl) userprofileOsw).releaseConnection(con);
     }
     sharedBagsByUsers = new HashedMap();
   }
   if ("userprofile".equals(qName)) {
     startTime = System.currentTimeMillis();
     profileHandler = new ProfileHandler(profileManager, osw, version, sharedBagsByUsers);
   }
   if (profileHandler != null) {
     profileHandler.startElement(uri, localName, qName, attrs);
   }
   if ("tracks".equals(qName)) {
     trackHandler = new TrackManagerHandler(profileManager.getProfileObjectStoreWriter());
   }
   if (trackHandler != null) {
     trackHandler.startElement(uri, localName, qName, attrs);
   }
 }
 @Override
 public void characters(char[] ch, int start, int length) throws SAXException {
   if (profileHandler != null) {
     profileHandler.characters(ch, start, length);
   }
 }
  /** {@inheritDoc} */
  @Override
  public void endElement(String uri, String localName, String qName) throws SAXException {
    super.endElement(uri, localName, qName);
    if ("userprofile".equals(qName)) {
      Profile profile;
      profile = profileHandler.getProfile();
      profileManager.createProfileWithoutBags(profile);
      try {
        Map<String, Set<BagValue>> bagValues = profileHandler.getBagsValues();
        for (StorableBag bag : profile.getAllBags().values()) {
          bag.saveWithBagValues(profile.getUserId(), bagValues.get(bag.getName()));
        }
      } catch (ObjectStoreException ose) {
        throw new SAXException(ose);
      }
      // Must come after the bags themselves have been stored.
      try {
        SharedBagManager.getInstance(profileManager);
        profileHandler.getInvitationHandler().storeInvites(profile);
      } catch (SQLException e) {
        LOG.error("Cannot store invitations", e);
        if (abortOnError) {
          throw new SAXException(e);
        }
      }
      Set<Tag> tags = profileHandler.getTags();
      TagManager tagManager = new TagManagerFactory(profile.getProfileManager()).getTagManager();
      for (Tag tag : tags) {
        try {
          tagManager.addTag(tag.getTagName(), tag.getObjectIdentifier(), tag.getType(), profile);
        } catch (TagManager.TagException e) {
          LOG.error("Cannot add tag: " + tag.toString(), e);
          if (abortOnError) {
            throw new SAXException(e);
          }
        } catch (RuntimeException e) {
          LOG.error("Error adding tag: " + tag.toString(), e);
          if (abortOnError) {
            throw new SAXException(e);
          }
        }
      }
      profile.getSearchRepository().receiveEvent(new MassTaggingEvent());
      profileHandler = null;
      long totalTime = System.currentTimeMillis() - startTime;
      LOG.info("Finished profile: " + profile.getUsername() + " took " + totalTime + "ms.");
    }
    if ("userprofiles".equals(qName)) {
      if (!sharedBagsByUsers.isEmpty()) {
        SharedBagManager sharedBagManager = SharedBagManager.getInstance(profileManager);
        String bagName, dateCreated;
        for (String user : sharedBagsByUsers.keySet()) {
          List<Map<String, String>> sharedBags = sharedBagsByUsers.get(user);
          if (!sharedBags.isEmpty()) {
            for (Map<String, String> sharedBag : sharedBags) {
              bagName = sharedBag.get("name");
              dateCreated = sharedBag.get("dateCreated");
              sharedBagManager.shareBagWithUser(bagName, dateCreated, user);
            }
          }
        }
      }
    }
    if (profileHandler != null) {
      profileHandler.endElement(uri, localName, qName);
    }

    if (trackHandler != null) {
      trackHandler.endElement(uri, localName, qName);
    }
  }