예제 #1
0
  /**
   * Constructs an attribute designator element from an existing XML block.
   *
   * @param element representing a DOM tree element.
   * @exception SAMLException if that there is an error in the sender or in the element definition.
   */
  public AttributeDesignator(Element element) throws SAMLException {
    // make sure that the input xml block is not null
    if (element == null) {
      SAMLUtilsCommon.debug.message("AttributeDesignator: Input is null.");
      throw new SAMLRequesterException(SAMLUtilsCommon.bundle.getString("nullInput"));
    }
    // Make sure this is an AttributeDesignator.
    String tag = null;
    tag = element.getLocalName();
    if ((tag == null) || (!tag.equals("AttributeDesignator"))) {
      SAMLUtilsCommon.debug.message("AttributeDesignator: wrong input");
      throw new SAMLRequesterException(SAMLUtilsCommon.bundle.getString("wrongInput"));
    }

    // handle attributes
    int i = 0;
    NamedNodeMap atts = ((Node) element).getAttributes();
    int attrCount = atts.getLength();
    for (i = 0; i < attrCount; i++) {
      Node att = atts.item(i);
      if (att.getNodeType() == Node.ATTRIBUTE_NODE) {
        String attName = att.getLocalName();
        if (attName == null || attName.length() == 0) {
          if (SAMLUtilsCommon.debug.messageEnabled()) {
            SAMLUtilsCommon.debug.message(
                "AttributeDesignator:" + "Attribute Name is either null or empty.");
          }
          throw new SAMLRequesterException(SAMLUtilsCommon.bundle.getString("nullInput"));
        }
        if (attName.equals("AttributeName")) {
          _attributeName = ((Attr) att).getValue().trim();
        } else if (attName.equals("AttributeNamespace")) {
          _attributeNameSpace = ((Attr) att).getValue().trim();
        }
      }
    }
    // AttributeName is required
    if (_attributeName == null || _attributeName.length() == 0) {
      if (SAMLUtilsCommon.debug.messageEnabled()) {
        SAMLUtilsCommon.debug.message(
            "AttributeDesignator: " + "AttributeName is required attribute");
      }
      throw new SAMLRequesterException(SAMLUtilsCommon.bundle.getString("missingAttribute"));
    }

    // AttributeNamespace is required
    if (_attributeNameSpace == null || _attributeNameSpace.length() == 0) {
      if (SAMLUtilsCommon.debug.messageEnabled()) {
        SAMLUtilsCommon.debug.message(
            "AttributeDesignator: " + "AttributeNamespace is required attribute");
      }
      throw new SAMLRequesterException(SAMLUtilsCommon.bundle.getString("missingAttribute"));
    }

    // handle the children of AttributeDesignator element
    // Since AttributeDesignator does not have any child element_node,
    // we will throw exception if we found any such child.
    NodeList nodes = element.getChildNodes();
    int nodeCount = nodes.getLength();
    if (nodeCount > 0) {
      for (i = 0; i < nodeCount; i++) {
        Node currentNode = nodes.item(i);
        if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
          if (SAMLUtilsCommon.debug.messageEnabled()) {
            SAMLUtilsCommon.debug.message("AttributeDesignator: illegal input!");
          }
          throw new SAMLRequesterException(SAMLUtilsCommon.bundle.getString("wrongInput"));
        }
      }
    }
  }