Esempio n. 1
0
/*================================================================
*   ixmlElement_setAttributeNS
*       Adds a new attribute. If an attribute with the same local name
*       and namespace URI is already present on the element, its prefix
*       is changed to be the prefix part of the qualifiedName, and its
*       value is changed to be the value parameter.  This value is a
*       simple string.
*       External function.
*
*   Parameter:
*       namespaceURI: the namespace of the attribute to create or alter.
*       qualifiedName: the qualified name of the attribute to create or alter.
*       value: the value to set in string form.
*
*   Return Value:
*       IXML_SUCCESS or failure 
*
*=================================================================*/
int
ixmlElement_setAttributeNS( IN IXML_Element * element,
                            IN DOMString namespaceURI,
                            IN DOMString qualifiedName,
                            IN DOMString value )
{
    IXML_Node *attrNode = NULL;
    IXML_Node newAttrNode;
    IXML_Attr *newAttr;
    int rc;

    if( ( element == NULL ) || ( namespaceURI == NULL ) ||
        ( qualifiedName == NULL ) || ( value == NULL ) ) {
        return IXML_INVALID_PARAMETER;
    }

    if( Parser_isValidXmlName( qualifiedName ) == FALSE ) {
        return IXML_INVALID_CHARACTER_ERR;
    }

    ixmlNode_init( &newAttrNode );

#ifdef _UPNP_MM_
    newAttrNode.nodeName = upnp_strdup( qualifiedName );
#else
    newAttrNode.nodeName = strdup( qualifiedName );
#endif
    if( newAttrNode.nodeName == NULL ) {
        return IXML_INSUFFICIENT_MEMORY;
    }

    rc = Parser_setNodePrefixAndLocalName( &newAttrNode );
    if( rc != IXML_SUCCESS ) {
        Parser_freeNodeContent( &newAttrNode );
        return rc;
    }
    // see DOM 2 spec page 59
    if( ( newAttrNode.prefix != NULL && namespaceURI == NULL ) ||
        ( strcmp( newAttrNode.prefix, "xml" ) == 0 &&
          strcmp( namespaceURI,
                  "http://www.w3.org/XML/1998/namespace" ) != 0 )
        || ( strcmp( qualifiedName, "xmlns" ) == 0
             && strcmp( namespaceURI,
                        "http://www.w3.org/2000/xmlns/" ) != 0 ) ) {
        Parser_freeNodeContent( &newAttrNode );
        return IXML_NAMESPACE_ERR;
    }

    attrNode = element->n.firstAttr;
    while( attrNode != NULL ) {
        if( strcmp( attrNode->localName, newAttrNode.localName ) == 0 &&
            strcmp( attrNode->namespaceURI, namespaceURI ) == 0 ) {
            break;              //found it
        } else {
            attrNode = attrNode->nextSibling;
        }
    }

    if( attrNode != NULL ) {
        if( attrNode->prefix != NULL ) {
#ifdef _UPNP_MM_
            upnp_free( attrNode->prefix );   // remove the old prefix
#else
            free( attrNode->prefix );   // remove the old prefix
#endif
        }
        // replace it with the new prefix
#ifdef _UPNP_MM_
        attrNode->prefix = upnp_strdup( newAttrNode.prefix );
#else
        attrNode->prefix = strdup( newAttrNode.prefix );
#endif
        if( attrNode->prefix == NULL ) {
            Parser_freeNodeContent( &newAttrNode );
            return IXML_INSUFFICIENT_MEMORY;
        }

        if( attrNode->nodeValue != NULL ) {
#ifdef _UPNP_MM_
            upnp_free( attrNode->nodeValue );
#else
            free( attrNode->nodeValue );
#endif
        }

#ifdef _UPNP_MM_
        attrNode->nodeValue = upnp_strdup( value );
#else
        attrNode->nodeValue = strdup( value );
#endif
        if( attrNode->nodeValue == NULL ) {
#ifdef _UPNP_MM_
            upnp_free( attrNode->prefix );
#else
            free( attrNode->prefix );
#endif
            Parser_freeNodeContent( &newAttrNode );
            return IXML_INSUFFICIENT_MEMORY;
        }

    } else {
        // add a new attribute
        rc = ixmlDocument_createAttributeNSEx( ( IXML_Document * )
                                               element->n.ownerDocument,
                                               namespaceURI, qualifiedName,
                                               &newAttr );
        if( rc != IXML_SUCCESS ) {
            return rc;
        }

#ifdef _UPNP_MM_
        newAttr->n.nodeValue = upnp_strdup( value );
#else
        newAttr->n.nodeValue = strdup( value );
#endif
        if( newAttr->n.nodeValue == NULL ) {
            ixmlAttr_free( newAttr );
            return IXML_INSUFFICIENT_MEMORY;
        }

        if( ixmlElement_setAttributeNodeNS( element, newAttr, NULL ) !=
            IXML_SUCCESS ) {
            ixmlAttr_free( newAttr );
            return IXML_FAILED;
        }

    }

    Parser_freeNodeContent( &newAttrNode );
    return IXML_SUCCESS;
}
Esempio n. 2
0
int ixmlElement_setAttributeNS(
	IXML_Element *element,
	const DOMString namespaceURI,
	const DOMString qualifiedName,
	const DOMString value)
{
	IXML_Node *attrNode = NULL;
	IXML_Node newAttrNode;
	IXML_Attr *newAttr;
	int rc;

	if (element == NULL || namespaceURI == NULL || qualifiedName == NULL ||
	    value == NULL) {
		return IXML_INVALID_PARAMETER;
	}

	if (Parser_isValidXmlName(qualifiedName) == FALSE) {
		return IXML_INVALID_CHARACTER_ERR;
	}

	ixmlNode_init(&newAttrNode);
	newAttrNode.nodeName = strdup(qualifiedName);
	if (newAttrNode.nodeName == NULL) {
		return IXML_INSUFFICIENT_MEMORY;
	}

	rc = Parser_setNodePrefixAndLocalName(&newAttrNode);
	if (rc != IXML_SUCCESS) {
		Parser_freeNodeContent(&newAttrNode);
		return rc;
	}

	/* see DOM 2 spec page 59 */
	if ((newAttrNode.prefix != NULL && namespaceURI == NULL) ||
	    (newAttrNode.prefix != NULL && strcmp(newAttrNode.prefix, "xml") == 0 &&
	     strcmp(namespaceURI, "http://www.w3.org/XML/1998/namespace") != 0) ||
	    (strcmp(qualifiedName, "xmlns") == 0 &&
	     strcmp(namespaceURI, "http://www.w3.org/2000/xmlns/") != 0)) {
		Parser_freeNodeContent( &newAttrNode );
		return IXML_NAMESPACE_ERR;
	}

	attrNode = element->n.firstAttr;
	while (attrNode != NULL) {
		if (strcmp(attrNode->localName, newAttrNode.localName) == 0 &&
		    strcmp(attrNode->namespaceURI, namespaceURI) == 0) {
			/* Found it */
			break;
		} else {
			attrNode = attrNode->nextSibling;
		}
	}
	if (attrNode != NULL) {
		if (attrNode->prefix != NULL) {
			/* Remove the old prefix */
			free(attrNode->prefix);
		}
		/* replace it with the new prefix */
		if (newAttrNode.prefix != NULL) {
		attrNode->prefix = strdup( newAttrNode.prefix );
		if (attrNode->prefix == NULL) {
			Parser_freeNodeContent(&newAttrNode);
			return IXML_INSUFFICIENT_MEMORY;
		}
		} else
			attrNode->prefix = newAttrNode.prefix;

		if (attrNode->nodeValue != NULL) {
			free(attrNode->nodeValue);
		}
		attrNode->nodeValue = strdup(value);
		if (attrNode->nodeValue == NULL) {
			free(attrNode->prefix);
			Parser_freeNodeContent(&newAttrNode);
			return IXML_INSUFFICIENT_MEMORY;
		}
	} else {
		/* Add a new attribute */
		rc = ixmlDocument_createAttributeNSEx(
			(IXML_Document *)element->n.ownerDocument,
			namespaceURI,
			qualifiedName,
			&newAttr);
		if (rc != IXML_SUCCESS) {
			Parser_freeNodeContent(&newAttrNode);
			return rc;
		}
		newAttr->n.nodeValue = strdup(value);
		if (newAttr->n.nodeValue == NULL) {
			ixmlAttr_free(newAttr);
			Parser_freeNodeContent(&newAttrNode);
			return IXML_INSUFFICIENT_MEMORY;
		}
		if (ixmlElement_setAttributeNodeNS(element, newAttr, &newAttr) != IXML_SUCCESS) {
			ixmlAttr_free(newAttr);
			Parser_freeNodeContent(&newAttrNode);
			return IXML_FAILED;
		}
	}
	Parser_freeNodeContent(&newAttrNode);

	return IXML_SUCCESS;
}