@Override
  public EndElement endElement(final EndElement element) throws XMLStreamException {
    final String theName = element.getLocalName();

    if ("component".equals(theName)) {
      if (this.componentId == null) {
        final Map components = (Map) outputStreams.get("components");
        components.remove(this.componentId);
      }
      this.inComponent = false;
      this.componentId = null;
    }
    if (this.inside) {
      this.insideLevel--;
      if (this.insideLevel > 0 || this.insideLevel == 0 && !"md-record".equals(theName)) {
        writer.writeEndElement();
      }

      if (this.insideLevel == 0) {
        this.inside = false;
        writer.flush();
        writer.close();
      }
    }

    return element;
  }
  @Override
  public EndElement endElement(final EndElement element) throws XMLStreamException {
    final String curPath = parser.getCurPath();

    if (this.inside) {
      writer.writeEndElement();
      this.deepLevel--;

      final String ns = element.getNamespace();
      List nsTrace = nsuris.get(ns);

      if (nsTrace != null
          && (nsTrace.get(2) == null || nsTrace.get(2).equals(element.getPrefix()))
          && nsTrace.get(1).equals(element.getLocalName())
          && (Integer) nsTrace.get(0) == this.deepLevel + 1) {

        nsuris.remove(ns);
      }

      // attribute namespaces
      // TODO iteration is a hack, use
      // javax.xml.namespace.NamespaceContext
      Iterator<String> it = nsuris.keySet().iterator();
      final Collection<String> toRemove = new ArrayList<String>();
      while (it.hasNext()) {
        final String key = it.next();
        nsTrace = nsuris.get(key);
        if ((Integer) nsTrace.get(0) == this.deepLevel + 1) {
          toRemove.add(key);
        }
      }
      it = toRemove.iterator();
      while (it.hasNext()) {
        final String key = it.next();
        nsuris.remove(key);
      }
      if (curPath.endsWith("components/component")) {
        this.inside = false;
        writer.flush();
        writer.close();
      }
    }
    return element;
  }