Example #1
0
  /**
   * Removes style attributes from this node and it's children recursively.
   *
   * @param node node being filtered.
   * @param attributeMappings attribute map to be used for filtering.
   */
  private void filter(Node node, Map<String, String> attributeMappings) {
    if (node instanceof Element) {
      Element element = (Element) node;
      String allowedAttributes = attributeMappings.get(element.getNodeName().toLowerCase());
      NamedNodeMap currentAttributes = element.getAttributes();
      if (null == allowedAttributes) {
        // Strip off all attributes.
        while (currentAttributes.getLength() > 0) {
          currentAttributes.removeNamedItem(currentAttributes.item(0).getNodeName());
        }
      } else {
        // Collect those attributes that need to be removed.
        List<String> attributesToBeRemoved = new ArrayList<String>();
        for (int i = 0; i < currentAttributes.getLength(); i++) {
          String attributeName = currentAttributes.item(i).getNodeName();
          String pattern = ATTRIBUTE_SEPARATOR + attributeName.toLowerCase() + ATTRIBUTE_SEPARATOR;
          if (allowedAttributes.indexOf(pattern) == -1) {
            attributesToBeRemoved.add(attributeName);
          }
        }

        // Remove those attributes collected above.
        for (String attribute : attributesToBeRemoved) {
          currentAttributes.removeNamedItem(attribute);
        }
      }
      if (node.hasChildNodes()) {
        NodeList children = node.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
          filter(children.item(i), attributeMappings);
        }
      }
    }
  }
Example #2
0
 /**
  * Check SOS response for xsi:schemaLocation, remove attribute and add attribute to SOAP message
  *
  * @param xmlObject
  * @param soapResponseMessage SOAP response message
  * @throws SOAPException If an error occurs
  */
 private void addAndRemoveSchemaLocationForSOAP(
     XmlObject xmlObject, SOAPMessage soapResponseMessage) throws SOAPException {
   String value = null;
   Node nodeToRemove = null;
   NamedNodeMap attributeMap = xmlObject.getDomNode().getFirstChild().getAttributes();
   for (int i = 0; i < attributeMap.getLength(); i++) {
     Node node = attributeMap.item(i);
     if (node.getLocalName().equals(W3CConstants.AN_SCHEMA_LOCATION)) {
       value = node.getNodeValue();
       nodeToRemove = node;
     }
   }
   if (nodeToRemove != null) {
     attributeMap.removeNamedItem(nodeToRemove.getNodeName());
   }
   SOAPEnvelope envelope = soapResponseMessage.getSOAPPart().getEnvelope();
   StringBuilder string = new StringBuilder();
   string.append(envelope.getNamespaceURI());
   string.append(BLANK_CHAR);
   string.append(envelope.getNamespaceURI());
   if (value != null && !value.isEmpty()) {
     string.append(BLANK_CHAR);
     string.append(value);
   }
   envelope.addAttribute(N52XmlHelper.getSchemaLocationQNameWithPrefix(), string.toString());
 }
 /**
  * Runs the test case.
  *
  * @throws Throwable Any uncaught exception causes test to fail
  */
 public void runTest() throws Throwable {
   Document doc;
   NodeList elementList;
   Node testEmployee;
   NamedNodeMap attributes;
   Attr streetAttr;
   String value;
   Node removedNode;
   doc = (Document) load("staff", true);
   elementList = doc.getElementsByTagName("address");
   testEmployee = elementList.item(2);
   attributes = testEmployee.getAttributes();
   assertNotNull("attributesNotNull", attributes);
   removedNode = attributes.removeNamedItem("street");
   streetAttr = (Attr) attributes.getNamedItem("street");
   assertNotNull("streetAttrNotNull", streetAttr);
   value = streetAttr.getValue();
   assertEquals("namednodemapRemoveNamedItemGetValueAssert", "Yes", value);
 }
Example #4
0
  public static Object getPropertyValue(Element element) {
    NamedNodeMap map = element.getAttributes();
    map.removeNamedItem(NAME_ATTR);

    if (map.item(0).getNodeName().equals(STRING_ATTR)) {
      return map.item(0).getNodeValue();
    } else if (map.item(0).getNodeName().equals(INTEGER_ATTR)) {
      return new Integer(map.item(0).getNodeValue());
    } else if (map.item(0).getNodeName().equals(DOUBLE_ATTR)) {
      return new Double(map.item(0).getNodeValue());
    } else if (map.item(0).getNodeName().equals(FLOAT_ATTR)) {
      return new Float(map.item(0).getNodeValue());
    } else if (map.item(0).getNodeName().equals(BOOLEAN_ATTR)) {
      return Boolean.valueOf(map.item(0).getNodeValue());
    } else if (map.item(0).getNodeName().equals(COLOR_ATTR)) {
      String[] rgb = map.item(0).getNodeValue().split(",");
      return new Color(new Integer(rgb[0]), new Integer(rgb[1]), new Integer(rgb[2]));
    } else if (map.item(0).getNodeName().equals(FONT_ATTR)) {
      String[] font = map.item(0).getNodeValue().split(",");
      return new Font(font[0], new Integer(font[1]), new Integer(font[2]));
    } else {
      return null;
    }
  }
Example #5
0
 void doApply(
     Stylesheet stylesheet,
     QName mode,
     Node context,
     int pos,
     int len,
     Node parent,
     Node nextSibling)
     throws TransformerException {
   Node result = null;
   Document doc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument();
   short nodeType = source.getNodeType();
   if (nodeType == Node.ATTRIBUTE_NODE && parent.getFirstChild() != null) {
     // Ignore attributes added after child elements
   } else {
     // Namespace aliasing
     if (nodeType == Node.ELEMENT_NODE) {
       String prefix = source.getPrefix();
       if (prefix == null) prefix = "#default";
       String resultPrefix = (String) stylesheet.namespaceAliases.get(prefix);
       if (resultPrefix != null) {
         if ("#default".equals(resultPrefix)) resultPrefix = null;
         String uri = source.lookupNamespaceURI(resultPrefix);
         String name = source.getNodeName();
         // Create a new element node in the result document
         result = doc.createElementNS(uri, name);
         // copy attributes
         NamedNodeMap srcAttrs = source.getAttributes();
         NamedNodeMap dstAttrs = result.getAttributes();
         int l = srcAttrs.getLength();
         for (int i = 0; i < l; i++) {
           Node attr = srcAttrs.item(i);
           if (!Stylesheet.XSL_NS.equals(attr.getNamespaceURI())) {
             attr = attr.cloneNode(true);
             attr = doc.adoptNode(attr);
             dstAttrs.setNamedItemNS(attr);
           }
         }
       }
     }
     if (result == null) {
       // Create result node
       result = source.cloneNode(false);
       // Remove any XSL attributes
       NamedNodeMap attrs = result.getAttributes();
       if (attrs != null) {
         int l = attrs.getLength();
         for (int i = 0; i < l; i++) {
           Node attr = attrs.item(i);
           if (Stylesheet.XSL_NS.equals(attr.getNamespaceURI())) {
             attrs.removeNamedItem(attr.getNodeName());
             i--;
             l--;
           }
         }
       }
       Node result2 = doc.adoptNode(result);
       if (result2 == null) {
         String msg =
             "Error adopting node to result tree: "
                 + result
                 + " ("
                 + result.getClass().getName()
                 + ")";
         DOMSourceLocator l = new DOMSourceLocator(context);
         throw new TransformerException(msg, l);
       }
       result = result2;
     }
     if (nextSibling != null) parent.insertBefore(result, nextSibling);
     else parent.appendChild(result);
     if (nodeType == Node.ELEMENT_NODE)
       stylesheet.addNamespaceNodes(source, result, doc, elementExcludeResultPrefixes);
     // children
     if (children != null) children.apply(stylesheet, mode, context, pos, len, result, null);
   }
   // next sibling
   if (next != null) next.apply(stylesheet, mode, context, pos, len, parent, nextSibling);
 }
 /**
  * This method removes the specified attribute from the specified node
  *
  * @param iNode The node whose attribute is to be removed
  * @param iAttributeName The name of the attribute to be removed
  */
 public static void removeAttribute(Node iNode, String iAttributeName) {
   NamedNodeMap attrList = iNode.getAttributes();
   attrList.removeNamedItem(iAttributeName);
 }
Example #7
0
 public static void removeAttribute(Node node, String attName) {
   NamedNodeMap attributes = node.getAttributes();
   attributes.removeNamedItem(attName);
 }