public void write(ContactType contact) throws ProcessingException {
    StaxUtil.writeStartElement(
        writer, METADATA_PREFIX, JBossSAMLConstants.CONTACT_PERSON.get(), METADATA_NSURI.get());

    ExtensionsType extensions = contact.getExtensions();
    if (extensions != null) {
      StaxUtil.writeDOMElement(writer, extensions.getElement());
    }
    ContactTypeType attribs = contact.getContactType();
    StaxUtil.writeAttribute(writer, JBossSAMLConstants.CONTACT_TYPE.get(), attribs.value());

    // Write the name
    String company = contact.getCompany();
    if (company != null) {
      StaxUtil.writeStartElement(
          writer, METADATA_PREFIX, JBossSAMLConstants.COMPANY.get(), METADATA_NSURI.get());
      StaxUtil.writeCharacters(writer, company);
      StaxUtil.writeEndElement(writer);
    }
    String givenName = contact.getGivenName();
    if (givenName != null) {
      StaxUtil.writeStartElement(
          writer, METADATA_PREFIX, JBossSAMLConstants.GIVEN_NAME.get(), METADATA_NSURI.get());
      StaxUtil.writeCharacters(writer, givenName);
      StaxUtil.writeEndElement(writer);
    }

    String surName = contact.getSurName();
    if (surName != null) {
      StaxUtil.writeStartElement(
          writer, METADATA_PREFIX, JBossSAMLConstants.SURNAME.get(), METADATA_NSURI.get());
      StaxUtil.writeCharacters(writer, surName);
      StaxUtil.writeEndElement(writer);
    }

    List<String> emailAddresses = contact.getEmailAddress();
    for (String email : emailAddresses) {
      StaxUtil.writeStartElement(
          writer, METADATA_PREFIX, JBossSAMLConstants.EMAIL_ADDRESS.get(), METADATA_NSURI.get());
      StaxUtil.writeCharacters(writer, email);
      StaxUtil.writeEndElement(writer);
    }

    List<String> tels = contact.getTelephoneNumber();
    for (String telephone : tels) {
      StaxUtil.writeStartElement(
          writer, METADATA_PREFIX, JBossSAMLConstants.TELEPHONE_NUMBER.get(), METADATA_NSURI.get());
      StaxUtil.writeCharacters(writer, telephone);
      StaxUtil.writeEndElement(writer);
    }

    StaxUtil.writeEndElement(writer);
    StaxUtil.flush(writer);
  }
  public void writeOrganization(OrganizationType org) throws ProcessingException {
    if (org == null) throw new ProcessingException(logger.nullArgumentError("Organization"));
    StaxUtil.writeStartElement(
        writer, METADATA_PREFIX, JBossSAMLConstants.ORGANIZATION.get(), METADATA_NSURI.get());

    ExtensionsType extensions = org.getExtensions();
    if (extensions != null) {
      StaxUtil.writeDOMElement(writer, extensions.getElement());
    }

    // Write the name
    List<LocalizedNameType> nameList = org.getOrganizationName();
    for (LocalizedNameType localName : nameList) {
      StaxUtil.writeStartElement(
          writer,
          METADATA_PREFIX,
          JBossSAMLConstants.ORGANIZATION_NAME.get(),
          METADATA_NSURI.get());

      writeLocalizedType(localName);
    }

    // Write the display name
    List<LocalizedNameType> displayNameList = org.getOrganizationDisplayName();
    for (LocalizedNameType localName : displayNameList) {
      StaxUtil.writeStartElement(
          writer,
          METADATA_PREFIX,
          JBossSAMLConstants.ORGANIZATION_DISPLAY_NAME.get(),
          METADATA_NSURI.get());
      writeLocalizedType(localName);
    }

    // Write the url
    List<LocalizedURIType> uriList = org.getOrganizationURL();
    for (LocalizedURIType uri : uriList) {
      StaxUtil.writeStartElement(
          writer, METADATA_PREFIX, JBossSAMLConstants.ORGANIZATION_URL.get(), METADATA_NSURI.get());

      String lang = uri.getLang();
      String val = uri.getValue().toString();
      StaxUtil.writeAttribute(
          writer,
          new QName(JBossSAMLURIConstants.XML.get(), JBossSAMLConstants.LANG.get(), "xml"),
          lang);

      StaxUtil.writeCharacters(writer, val);

      StaxUtil.writeEndElement(writer);
    }

    StaxUtil.writeEndElement(writer);
    StaxUtil.flush(writer);
  }
  public void writeEntitiesDescriptor(EntitiesDescriptorType entities) throws ProcessingException {
    StaxUtil.writeStartElement(
        writer,
        METADATA_PREFIX,
        JBossSAMLConstants.ENTITIES_DESCRIPTOR.get(),
        METADATA_NSURI.get());

    StaxUtil.writeDefaultNameSpace(writer, JBossSAMLURIConstants.METADATA_NSURI.get());
    StaxUtil.writeNameSpace(writer, "md", JBossSAMLURIConstants.METADATA_NSURI.get());
    StaxUtil.writeNameSpace(writer, "saml", JBossSAMLURIConstants.ASSERTION_NSURI.get());
    StaxUtil.writeNameSpace(writer, "ds", JBossSAMLURIConstants.XMLDSIG_NSURI.get());

    if (entities.getValidUntil() != null) {
      StaxUtil.writeAttribute(
          writer, JBossSAMLConstants.VALID_UNTIL.get(), entities.getValidUntil().toString());
    }
    if (entities.getID() != null) {
      StaxUtil.writeAttribute(writer, JBossSAMLConstants.ID.get(), entities.getID());
    }

    if (entities.getName() != null) {
      StaxUtil.writeAttribute(writer, JBossSAMLConstants.NAME.get(), entities.getName());
    }

    Element signature = entities.getSignature();
    if (signature != null) {
      StaxUtil.writeDOMElement(writer, signature);
    }
    ExtensionsType extensions = entities.getExtensions();
    if (extensions != null) {
      StaxUtil.writeDOMElement(writer, extensions.getElement());
    }

    List<Object> entityDescriptors = entities.getEntityDescriptor();
    for (Object ed : entityDescriptors) {
      if (ed instanceof EntityDescriptorType) {
        writeEntityDescriptor((EntityDescriptorType) ed);
      } else writeEntitiesDescriptor((EntitiesDescriptorType) ed);
    }

    StaxUtil.writeEndElement(writer);
    StaxUtil.flush(writer);
  }
  public void writeAttributeAuthorityDescriptor(AttributeAuthorityDescriptorType attributeAuthority)
      throws ProcessingException {
    StaxUtil.writeStartElement(
        writer,
        METADATA_PREFIX,
        JBossSAMLConstants.ATTRIBUTE_AUTHORITY_DESCRIPTOR.get(),
        METADATA_NSURI.get());

    writeProtocolSupportEnumeration(attributeAuthority.getProtocolSupportEnumeration());

    Element signature = attributeAuthority.getSignature();
    if (signature != null) {
      StaxUtil.writeDOMElement(writer, signature);
    }
    ExtensionsType extensions = attributeAuthority.getExtensions();
    if (extensions != null) {
      StaxUtil.writeDOMElement(writer, extensions.getElement());
    }

    List<KeyDescriptorType> keyDescriptorList = attributeAuthority.getKeyDescriptor();
    for (KeyDescriptorType keyDescriptor : keyDescriptorList) {
      writeKeyDescriptor(keyDescriptor);
    }

    List<EndpointType> attributeServices = attributeAuthority.getAttributeService();
    for (EndpointType endpoint : attributeServices) {
      writeAttributeService(endpoint);
    }

    List<String> nameIDFormats = attributeAuthority.getNameIDFormat();
    for (String nameIDFormat : nameIDFormats) {
      writeNameIDFormat(nameIDFormat);
    }

    List<AttributeType> attributes = attributeAuthority.getAttribute();
    for (AttributeType attributeType : attributes) {
      write(attributeType);
    }
    StaxUtil.writeEndElement(writer);
    StaxUtil.flush(writer);
  }
  public void writeEntityDescriptor(EntityDescriptorType entityDescriptor)
      throws ProcessingException {
    StaxUtil.writeStartElement(
        writer, METADATA_PREFIX, JBossSAMLConstants.ENTITY_DESCRIPTOR.get(), METADATA_NSURI.get());
    StaxUtil.writeDefaultNameSpace(writer, JBossSAMLURIConstants.METADATA_NSURI.get());
    StaxUtil.writeNameSpace(writer, "md", JBossSAMLURIConstants.METADATA_NSURI.get());
    StaxUtil.writeNameSpace(writer, "saml", JBossSAMLURIConstants.ASSERTION_NSURI.get());
    StaxUtil.writeNameSpace(writer, "ds", JBossSAMLURIConstants.XMLDSIG_NSURI.get());

    StaxUtil.writeAttribute(
        writer, JBossSAMLConstants.ENTITY_ID.get(), entityDescriptor.getEntityID());
    if (entityDescriptor.getValidUntil() != null) {
      StaxUtil.writeAttribute(
          writer,
          JBossSAMLConstants.VALID_UNTIL.get(),
          entityDescriptor.getValidUntil().toString());
    }
    if (entityDescriptor.getID() != null) {
      StaxUtil.writeAttribute(writer, JBossSAMLConstants.ID.get(), entityDescriptor.getID());
    }

    Element signature = entityDescriptor.getSignature();
    if (signature != null) {
      StaxUtil.writeDOMElement(writer, signature);
    }
    ExtensionsType extensions = entityDescriptor.getExtensions();
    if (extensions != null) {
      StaxUtil.writeDOMElement(writer, extensions.getElement());
    }

    List<EDTChoiceType> choiceTypes = entityDescriptor.getChoiceType();
    for (EDTChoiceType edtChoice : choiceTypes) {
      AffiliationDescriptorType affliationDesc = edtChoice.getAffiliationDescriptor();
      if (affliationDesc != null) throw logger.notImplementedYet("affliation"); // TODO: affiliation

      List<EDTDescriptorChoiceType> edtDescChoices = edtChoice.getDescriptors();
      for (EDTDescriptorChoiceType edtDescChoice : edtDescChoices) {
        RoleDescriptorType roleDesc = edtDescChoice.getRoleDescriptor();

        if (roleDesc != null) throw logger.notImplementedYet("Role Descriptor type");

        IDPSSODescriptorType idpSSO = edtDescChoice.getIdpDescriptor();
        if (idpSSO != null) write(idpSSO);

        SPSSODescriptorType spSSO = edtDescChoice.getSpDescriptor();
        if (spSSO != null) write(spSSO);

        AttributeAuthorityDescriptorType attribAuth = edtDescChoice.getAttribDescriptor();
        if (attribAuth != null) writeAttributeAuthorityDescriptor(attribAuth);

        AuthnAuthorityDescriptorType authNDesc = edtDescChoice.getAuthnDescriptor();
        if (authNDesc != null) throw logger.notImplementedYet("AuthnAuthorityDescriptorType");

        PDPDescriptorType pdpDesc = edtDescChoice.getPdpDescriptor();
        if (pdpDesc != null) throw logger.notImplementedYet("PDPDescriptorType");
      }
    }
    OrganizationType organization = entityDescriptor.getOrganization();
    if (organization != null) {
      writeOrganization(organization);
    }

    List<ContactType> contactPersons = entityDescriptor.getContactPerson();
    for (ContactType contact : contactPersons) {
      write(contact);
    }

    List<AdditionalMetadataLocationType> addl = entityDescriptor.getAdditionalMetadataLocation();
    if (addl.size() > 0) throw logger.notImplementedYet("AdditionalMetadataLocationType");

    StaxUtil.writeEndElement(writer);
    StaxUtil.flush(writer);
  }