示例#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;
}
示例#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;
}
示例#3
0
/*================================================================
*   ixmlElement_setAttribute
*       Adds a new attribute.  If an attribute with that name is already
*       present in the element, its value is changed to be that of the value
*       parameter. If not, a new attribute is inserted into the element.
*
*       External function.
*   Parameters:
*       name: the name of the attribute to create or alter.
*       value: value to set in string form
*   Return Values:
*       IXML_SUCCESS or failure code.    
*
*=================================================================*/
int
ixmlElement_setAttribute( IN IXML_Element * element,
                          IN char *name,
                          IN char *value )
{
    IXML_Node *attrNode;
    IXML_Attr *newAttrNode;
    short errCode = IXML_SUCCESS;

    if( ( element == NULL ) || ( name == NULL ) || ( value == NULL ) ) {
        errCode = IXML_INVALID_PARAMETER;
        goto ErrorHandler;
    }

    if( Parser_isValidXmlName( name ) == FALSE ) {
        errCode = IXML_INVALID_CHARACTER_ERR;
        goto ErrorHandler;
    }

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

    if( attrNode == NULL ) {    // add a new attribute
        errCode =
            ixmlDocument_createAttributeEx( ( IXML_Document * ) element->n.
                                            ownerDocument, name,
                                            &newAttrNode );
        if( errCode != IXML_SUCCESS ) {
            goto ErrorHandler;
        }

        attrNode = ( IXML_Node * ) newAttrNode;

#ifdef _UPNP_MM_
        attrNode->nodeValue = upnp_strdup( value );
#else
        attrNode->nodeValue = strdup( value );
#endif
        if( attrNode->nodeValue == NULL ) {
            ixmlAttr_free( newAttrNode );
            errCode = IXML_INSUFFICIENT_MEMORY;
            goto ErrorHandler;
        }

        errCode =
            ixmlElement_setAttributeNode( element, newAttrNode, NULL );
        if( errCode != IXML_SUCCESS ) {
            ixmlAttr_free( newAttrNode );
            goto ErrorHandler;
        }

    } else {
        if( attrNode->nodeValue != NULL ) { // attribute name has a value already
#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 ) {
            errCode = IXML_INSUFFICIENT_MEMORY;
        }
    }

  ErrorHandler:
    return errCode;
}
示例#4
0
int ixmlElement_setAttribute(
	IXML_Element *element,
	const DOMString name,
	const DOMString value)
{
	IXML_Node *attrNode;
	IXML_Attr *newAttrNode;
	int errCode = IXML_SUCCESS;

	if (element == NULL || name == NULL || value == NULL) {
		errCode = IXML_INVALID_PARAMETER;
		goto ErrorHandler;
	}

	if (Parser_isValidXmlName(name) == FALSE) {
		errCode = IXML_INVALID_CHARACTER_ERR;
		goto ErrorHandler;
	}

	attrNode = element->n.firstAttr;
	while (attrNode != NULL) {
		if (strcmp(attrNode->nodeName, name) == 0) {
			/* Found it */
			break;
		} else {
			attrNode = attrNode->nextSibling;
		}
	}

	if (attrNode == NULL) {
		/* Add a new attribute */
		errCode = ixmlDocument_createAttributeEx(
			(IXML_Document *)element->n.ownerDocument, name, &newAttrNode);
		if (errCode != IXML_SUCCESS) {
			goto ErrorHandler;
		}

		attrNode = (IXML_Node *)newAttrNode;
		attrNode->nodeValue = strdup(value);
		if (attrNode->nodeValue == NULL) {
		ixmlAttr_free(newAttrNode);
		errCode = IXML_INSUFFICIENT_MEMORY;
			goto ErrorHandler;
		}

		errCode = ixmlElement_setAttributeNode(element, newAttrNode, NULL);
		if (errCode != IXML_SUCCESS) {
			ixmlAttr_free(newAttrNode);
			goto ErrorHandler;
		}
	} else {
		if (attrNode->nodeValue != NULL) {
			/* Attribute name has a value already */
			free(attrNode->nodeValue);
		}
		attrNode->nodeValue = strdup(value);
		if (attrNode->nodeValue == NULL) {
			errCode = IXML_INSUFFICIENT_MEMORY;
		}
	}

ErrorHandler:
	return errCode;
}