public void testSetNamedItemNS3() throws Throwable {

    Document doc;
    Document docAlt;
    NamedNodeMap attributes;
    NamedNodeMap attributesAlt;
    NodeList elementList;
    NodeList elementListAlt;
    Element element;
    Element elementAlt;
    Attr attr;

    String nullNS = null;

    doc = (Document) load("staffNS", builder);
    elementList = doc.getElementsByTagNameNS("*", "address");
    element = (Element) elementList.item(1);
    attributes = element.getAttributes();
    docAlt = (Document) load("staffNS", builder);
    elementListAlt = docAlt.getElementsByTagNameNS("*", "address");
    elementAlt = (Element) elementListAlt.item(1);
    attributesAlt = elementAlt.getAttributes();
    attr = (Attr) attributesAlt.getNamedItemNS(nullNS, "street");
    attributesAlt.removeNamedItemNS(nullNS, "street");

    {
      boolean success = false;
      try {
        attributes.setNamedItemNS(attr);
      } catch (DOMException ex) {
        success = (ex.code == DOMException.WRONG_DOCUMENT_ERR);
      }
      assertTrue("throw_WRONG_DOCUMENT_ERR", success);
    }
  }
  public void testSetNamedItemNS8() throws Throwable {
    Document doc;
    NamedNodeMap attributes;
    NodeList elementList;
    Element element;
    Attr attr;

    doc = (Document) load("staffNS", builder);
    elementList = doc.getElementsByTagNameNS("*", "address");
    element = (Element) elementList.item(0);
    attributes = element.getAttributes();
    attr = (Attr) attributes.getNamedItemNS("http://www.usa.com", "domestic");
    element = (Element) elementList.item(1);
    attributes = element.getAttributes();

    {
      boolean success = false;
      try {
        attributes.setNamedItemNS(attr);
      } catch (DOMException ex) {
        success = (ex.code == DOMException.INUSE_ATTRIBUTE_ERR);
      }
      assertTrue("namednodemapsetnameditemns08", success);
    }
  }
Example #3
0
 LiteralNode(Node source) {
   this.source = source;
   if (source.getNodeType() == Node.ELEMENT_NODE) {
     NamedNodeMap attrs = source.getAttributes();
     Node attr = attrs.getNamedItemNS(Stylesheet.XSL_NS, "exclude-result-prefixes");
     if (attr != null) {
       elementExcludeResultPrefixes = new HashSet();
       StringTokenizer st = new StringTokenizer(attr.getNodeValue());
       while (st.hasMoreTokens()) elementExcludeResultPrefixes.add(st.nextToken());
     } else elementExcludeResultPrefixes = Collections.EMPTY_SET;
   } else elementExcludeResultPrefixes = null;
 }
Example #4
0
 private boolean compareAttributes(Node a, Node b) {
   NamedNodeMap nnma = a.getAttributes(), nnmb = b.getAttributes();
   if (getAttrCount(nnma) != getAttrCount(nnmb)) return false;
   for (int i = 0; i < nnma.getLength(); i++) {
     Node ta = nnma.item(i);
     if (Namespaces.XMLNS_NS.equals(ta.getNamespaceURI())) continue;
     Node tb =
         ta.getLocalName() == null
             ? nnmb.getNamedItem(ta.getNodeName())
             : nnmb.getNamedItemNS(ta.getNamespaceURI(), ta.getLocalName());
     if (tb == null || !safeEquals(ta.getNodeValue(), tb.getNodeValue())) return false;
   }
   return true;
 }
  public void testSetNamedItemNS2() throws Throwable {
    Document doc;
    NamedNodeMap attributes;
    Element element;
    Attr attribute;
    Attr attribute1;

    String attrName;
    doc = (Document) load("staffNS", builder);
    element = doc.createElementNS("http://www.w3.org/DOM/Test", "root");
    attribute1 = doc.createAttributeNS("http://www.w3.org/DOM/L1", "L1:att");
    attributes = element.getAttributes();
    attributes.setNamedItemNS(attribute1);
    attribute = (Attr) attributes.getNamedItemNS("http://www.w3.org/DOM/L1", "att");
    attrName = attribute.getNodeName();
    assertEquals("namednodemapsetnameditemns02", "L1:att", attrName);
  }
  /**
   * Runs the test case.
   *
   * @throws Throwable Any uncaught exception causes test to fail
   */
  public void testSetNamedItemNS1() throws Throwable {
    Document doc;
    NamedNodeMap attributes;
    Node element;
    Attr attribute;

    Attr newAttr1;
    NodeList elementList;
    String attrName;
    doc = (Document) load("staffNS", builder);
    elementList = doc.getElementsByTagNameNS("http://www.nist.gov", "address");
    element = elementList.item(0);
    attributes = element.getAttributes();
    newAttr1 = doc.createAttributeNS("http://www.w3.org/DOM/L1", "streets");
    ((Element) /* Node */ element).setAttributeNodeNS(newAttr1);
    attribute = (Attr) attributes.getNamedItemNS("http://www.w3.org/DOM/L1", "streets");
    attrName = attribute.getNodeName();
    assertEquals("namednodemapsetnameditemns01", "streets", attrName);
  }
Example #7
0
 private static boolean compareElementAttrs(Element e1, Element e2) {
   NamedNodeMap at1 = e1.getAttributes();
   NamedNodeMap at2 = e2.getAttributes();
   if (at1.getLength() != at2.getLength()) {
     System.out.println("Different number of attributes");
   }
   for (int i = 0; i < at1.getLength(); i++) {
     Attr attr1 = (Attr) at1.item(i);
     Attr attr2 = (Attr) at2.getNamedItemNS(attr1.getNamespaceURI(), attr1.getLocalName());
     if (attr2 == null) {
       System.out.println("Attribute " + attr1.getNodeName() + " not found");
       return false;
     }
     if (!compareStrings(attr1.getNodeValue(), attr2.getNodeValue())) {
       System.out.println(
           "Different attributes " + attr1.getNodeName() + " and " + attr2.getNodeName());
       return false;
     }
   }
   return true;
 }
  public static void compareNodes(Node expected, Node actual) throws Exception {
    if (expected.getNodeType() != actual.getNodeType()) {
      throw new Exception("Different types of nodes: " + expected + " " + actual);
    }
    if (expected instanceof Document) {
      Document expectedDoc = (Document) expected;
      Document actualDoc = (Document) actual;
      compareNodes(expectedDoc.getDocumentElement(), actualDoc.getDocumentElement());
    } else if (expected instanceof Element) {
      Element expectedElement = (Element) expected;
      Element actualElement = (Element) actual;

      // compare element names
      if (!expectedElement.getLocalName().equals(actualElement.getLocalName())) {
        throw new Exception(
            "Element names do not match: "
                + expectedElement.getLocalName()
                + " "
                + actualElement.getLocalName());
      }
      // compare element ns
      String expectedNS = expectedElement.getNamespaceURI();
      String actualNS = actualElement.getNamespaceURI();
      if ((expectedNS == null && actualNS != null)
          || (expectedNS != null && !expectedNS.equals(actualNS))) {
        throw new Exception(
            "Element namespaces names do not match: " + expectedNS + " " + actualNS);
      }

      String elementName =
          "{" + expectedElement.getNamespaceURI() + "}" + actualElement.getLocalName();

      // compare attributes
      NamedNodeMap expectedAttrs = expectedElement.getAttributes();
      NamedNodeMap actualAttrs = actualElement.getAttributes();
      if (countNonNamespaceAttribures(expectedAttrs) != countNonNamespaceAttribures(actualAttrs)) {
        throw new Exception(
            elementName
                + ": Number of attributes do not match up: "
                + countNonNamespaceAttribures(expectedAttrs)
                + " "
                + countNonNamespaceAttribures(actualAttrs));
      }
      for (int i = 0; i < expectedAttrs.getLength(); i++) {
        Attr expectedAttr = (Attr) expectedAttrs.item(i);
        if (expectedAttr.getName().startsWith("xmlns")) {
          continue;
        }
        Attr actualAttr = null;
        if (expectedAttr.getNamespaceURI() == null) {
          actualAttr = (Attr) actualAttrs.getNamedItem(expectedAttr.getName());
        } else {
          actualAttr =
              (Attr)
                  actualAttrs.getNamedItemNS(
                      expectedAttr.getNamespaceURI(), expectedAttr.getLocalName());
        }
        if (actualAttr == null) {
          throw new Exception(elementName + ": No attribute found:" + expectedAttr);
        }
        if (!expectedAttr.getValue().equals(actualAttr.getValue())) {
          throw new Exception(
              elementName
                  + ": Attribute values do not match: "
                  + expectedAttr.getValue()
                  + " "
                  + actualAttr.getValue());
        }
      }

      // compare children
      NodeList expectedChildren = expectedElement.getChildNodes();
      NodeList actualChildren = actualElement.getChildNodes();
      if (expectedChildren.getLength() != actualChildren.getLength()) {
        throw new Exception(
            elementName
                + ": Number of children do not match up: "
                + expectedChildren.getLength()
                + " "
                + actualChildren.getLength());
      }
      for (int i = 0; i < expectedChildren.getLength(); i++) {
        Node expectedChild = expectedChildren.item(i);
        Node actualChild = actualChildren.item(i);
        compareNodes(expectedChild, actualChild);
      }
    } else if (expected instanceof Text) {
      String expectedData = ((Text) expected).getData().trim();
      String actualData = ((Text) actual).getData().trim();

      if (!expectedData.equals(actualData)) {
        throw new Exception("Text does not match: " + expectedData + " " + actualData);
      }
    }
  }
  /**
   * Convert the SOAP XML we extract from the DIME message into our local object. Here Axis2 is not
   * parsing the SOAP for us. I tried to use the Amazon PutObject parser but it keep throwing
   * exceptions.
   *
   * @param putObjectInline
   * @return
   * @throws Exception
   */
  public static S3PutObjectRequest toEnginePutObjectRequest(InputStream is) throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);

    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(is);
    Node parent = null;
    Node contents = null;
    NodeList children = null;
    String temp = null;
    String element = null;
    int count = 0;

    S3PutObjectRequest request = new S3PutObjectRequest();

    // [A] Pull out the simple nodes first
    NodeList part = getElement(doc, "http://s3.amazonaws.com/doc/2006-03-01/", "Bucket");
    if (null != part) {
      if (null != (contents = part.item(0)))
        request.setBucketName(contents.getFirstChild().getNodeValue());
    }
    part = getElement(doc, "http://s3.amazonaws.com/doc/2006-03-01/", "Key");
    if (null != part) {
      if (null != (contents = part.item(0)))
        request.setKey(contents.getFirstChild().getNodeValue());
    }
    part = getElement(doc, "http://s3.amazonaws.com/doc/2006-03-01/", "ContentLength");
    if (null != part) {
      if (null != (contents = part.item(0))) {
        String length = contents.getFirstChild().getNodeValue();
        if (null != length) request.setContentLength(Long.decode(length));
      }
    }
    part = getElement(doc, "http://s3.amazonaws.com/doc/2006-03-01/", "AWSAccessKeyId");
    if (null != part) {
      if (null != (contents = part.item(0)))
        request.setAccessKey(contents.getFirstChild().getNodeValue());
    }
    part = getElement(doc, "http://s3.amazonaws.com/doc/2006-03-01/", "Signature");
    if (null != part) {
      if (null != (contents = part.item(0)))
        request.setSignature(contents.getFirstChild().getNodeValue());
    }
    part = getElement(doc, "http://s3.amazonaws.com/doc/2006-03-01/", "Timestamp");
    if (null != part) {
      if (null != (contents = part.item(0)))
        request.setRawTimestamp(contents.getFirstChild().getNodeValue());
    }
    part = getElement(doc, "http://s3.amazonaws.com/doc/2006-03-01/", "StorageClass");
    if (null != part) {
      if (null != (contents = part.item(0)))
        request.setStorageClass(contents.getFirstChild().getNodeValue());
    }
    part = getElement(doc, "http://s3.amazonaws.com/doc/2006-03-01/", "Credential");
    if (null != part) {
      if (null != (contents = part.item(0)))
        request.setCredential(contents.getFirstChild().getNodeValue());
    }

    // [B] Get a list of all 'Metadata' elements
    part = getElement(doc, "http://s3.amazonaws.com/doc/2006-03-01/", "Metadata");
    if (null != part) {
      count = part.getLength();
      S3MetaDataEntry[] metaEntry = new S3MetaDataEntry[count];

      for (int i = 0; i < count; i++) {
        parent = part.item(i);
        metaEntry[i] = new S3MetaDataEntry();

        // -> get a list of all the children elements of the 'Metadata' parent element
        if (null != (children = parent.getChildNodes())) {
          int numChildren = children.getLength();
          for (int j = 0; j < numChildren; j++) {
            contents = children.item(j);
            element = contents.getNodeName().trim();
            if (element.endsWith("Name")) {
              temp = contents.getFirstChild().getNodeValue();
              if (null != temp) metaEntry[i].setName(temp);
            } else if (element.endsWith("Value")) {
              temp = contents.getFirstChild().getNodeValue();
              if (null != temp) metaEntry[i].setValue(temp);
            }
          }
        }
      }
      request.setMetaEntries(metaEntry);
    }

    // [C] Get a list of all Grant elements in an AccessControlList
    part = getElement(doc, "http://s3.amazonaws.com/doc/2006-03-01/", "Grant");
    if (null != part) {
      S3AccessControlList engineAcl = new S3AccessControlList();

      count = part.getLength();
      for (int i = 0; i < count; i++) {
        parent = part.item(i);
        S3Grant engineGrant = new S3Grant();

        // -> get a list of all the children elements of the 'Grant' parent element
        if (null != (children = parent.getChildNodes())) {
          int numChildren = children.getLength();
          for (int j = 0; j < numChildren; j++) {
            contents = children.item(j);
            element = contents.getNodeName().trim();
            if (element.endsWith("Grantee")) {
              NamedNodeMap attbs = contents.getAttributes();
              if (null != attbs) {
                Node type =
                    attbs.getNamedItemNS("http://www.w3.org/2001/XMLSchema-instance", "type");
                if (null != type) temp = type.getFirstChild().getNodeValue().trim();
                else temp = null;

                if (null != temp && temp.equalsIgnoreCase("CanonicalUser")) {
                  engineGrant.setGrantee(SAcl.GRANTEE_USER);
                  engineGrant.setCanonicalUserID(getChildNodeValue(contents, "ID"));
                } else
                  throw new UnsupportedOperationException(
                      "Missing http://www.w3.org/2001/XMLSchema-instance:type value");
              }
            } else if (element.endsWith("Permission")) {
              temp = contents.getFirstChild().getNodeValue().trim();
              if (temp.equalsIgnoreCase("READ")) engineGrant.setPermission(SAcl.PERMISSION_READ);
              else if (temp.equalsIgnoreCase("WRITE"))
                engineGrant.setPermission(SAcl.PERMISSION_WRITE);
              else if (temp.equalsIgnoreCase("READ_ACP"))
                engineGrant.setPermission(SAcl.PERMISSION_READ_ACL);
              else if (temp.equalsIgnoreCase("WRITE_ACP"))
                engineGrant.setPermission(SAcl.PERMISSION_WRITE_ACL);
              else if (temp.equalsIgnoreCase("FULL_CONTROL"))
                engineGrant.setPermission(SAcl.PERMISSION_FULL);
              else throw new UnsupportedOperationException("Unsupported permission: " + temp);
            }
          }
          engineAcl.addGrant(engineGrant);
        }
      }
      request.setAcl(engineAcl);
    }
    return request;
  }
 public String getValue(String uri, String localName) {
   return attrs.getNamedItemNS(uri, localName).getNodeValue();
 }
 public String getType(String uri, String localName) {
   Attr attr = (Attr) attrs.getNamedItemNS(uri, localName);
   return attr.isId() ? "ID" : "CDATA";
 }
  /**
   * Handle attribute node during validation.
   *
   * @param receivedElement
   * @param receivedAttribute
   * @param sourceElement
   * @param validationContext
   */
  private void doAttribute(
      Node receivedElement,
      Node receivedAttribute,
      Node sourceElement,
      XmlMessageValidationContext validationContext,
      NamespaceContext namespaceContext,
      TestContext context) {
    if (receivedAttribute.getNodeName().startsWith(XMLConstants.XMLNS_ATTRIBUTE)) {
      return;
    }

    String receivedAttributeName = receivedAttribute.getLocalName();

    if (log.isDebugEnabled()) {
      log.debug(
          "Validating attribute: "
              + receivedAttributeName
              + " ("
              + receivedAttribute.getNamespaceURI()
              + ")");
    }

    NamedNodeMap sourceAttributes = sourceElement.getAttributes();
    Node sourceAttribute =
        sourceAttributes.getNamedItemNS(receivedAttribute.getNamespaceURI(), receivedAttributeName);

    Assert.isTrue(
        sourceAttribute != null,
        "Attribute validation failed for element '"
            + receivedElement.getLocalName()
            + "', unknown attribute "
            + receivedAttributeName
            + " ("
            + receivedAttribute.getNamespaceURI()
            + ")");

    if (XmlValidationUtils.isAttributeIgnored(
        receivedElement,
        receivedAttribute,
        sourceAttribute,
        validationContext.getIgnoreExpressions(),
        namespaceContext)) {
      return;
    }

    String receivedValue = receivedAttribute.getNodeValue();
    String sourceValue = sourceAttribute.getNodeValue();
    if (isValidationMatcherExpression(sourceAttribute)) {
      ValidationMatcherUtils.resolveValidationMatcher(
          sourceAttribute.getNodeName(),
          receivedAttribute.getNodeValue().trim(),
          sourceAttribute.getNodeValue().trim(),
          context);
    } else if (receivedValue.contains(":") && sourceValue.contains(":")) {
      doNamespaceQualifiedAttributeValidation(
          receivedElement, receivedAttribute, sourceElement, sourceAttribute);
    } else {
      Assert.isTrue(
          receivedValue.equals(sourceValue),
          ValidationUtils.buildValueMismatchErrorMessage(
              "Values not equal for attribute '" + receivedAttributeName + "'",
              sourceValue,
              receivedValue));
    }

    if (log.isDebugEnabled()) {
      log.debug("Attribute '" + receivedAttributeName + "'='" + receivedValue + "': OK");
    }
  }