File:  [Public] / DOM / Element.c
Revision 1.2: download - view: text, annotated - select for diffs
Mon Jun 15 05:26:39 1998 UTC (26 years ago) by daniel
Branches: MAIN
CVS tags: HEAD
Nearly completed Document interfaces, general cleanup, Daniel.

/*
 * Element.c : implementation of the Element interface as defined by
 *       Document Object Model (Core) Level 1
 *       http://www.w3.org/TR/WD-DOM/level-one-core.html
 * 
 * Daniel.Veillard@w3.org
 *
 * $Id: Element.c,v 1.2 1998/06/15 05:26:39 daniel Exp $
 */

#include "config.h"
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include "Element.h"
#include "Attribute.h"
#include "Document.h"

/*
 * Returns the element tag.
 */
char *getTagName(domElementPtr elem) {
    if (elem == NULL) return(NULL);
    if (elem->type != DOM_NODE_ELEMENT) return(NULL);
    return(elem->name);
}

/*
 * Create a NodeIterator for the Attribute list.
 */
domNodeIteratorPtr getAttributes(domElementPtr elem) {
    if (elem == NULL) return(NULL);
    if (elem->type != DOM_NODE_ELEMENT) return(NULL);
    return(domNodeIteratorCreate(elem->attributes));
}

/*
 * Lookup the value for an attribute.
 */
char *getElemAttribute(domElementPtr elem, char *name) {
    domAttributePtr cur;

    if (elem == NULL) return(NULL);
    if (elem->type != DOM_NODE_ELEMENT) return(NULL);
    cur = elem->attributes;
    while (cur != NULL) {
        if (!strcmp(name, cur->name)) {
	    return(cur->value);
	}
	cur = cur->next;
    }
    return(NULL);
}

/*
 * Set the value for an attribute, if needed create a new attribute.
 */
void setElemAttribute(domElementPtr elem, char *name, char *value) {
    domAttributePtr cur;

    if (elem == NULL) return;
    if (name == NULL) return;
    if (elem->type != DOM_NODE_ELEMENT) return;
    cur = elem->attributes;
    while (cur != NULL) {
        if (!strcmp(name, cur->name)) {
	    if (cur->value != NULL) free(cur->value);
	    if (value != NULL)
	        cur->value = strdup(cur->value);
	    else
	        cur->value = NULL;
	    return;
	}
	cur = cur->next;
    }
    /*
     * Link a new attribute at the end.
     */
    cur = createAttribute(elem->doc, name, value);
    if (cur == NULL) return;
    cur->parent = elem;
    cur->next = NULL;
    cur->prev = elem->lastattribute;
    if (cur->prev == NULL)
        elem->attributes = cur;
    else
        cur->prev->next = cur;
}

/*
 * Remove an attribute from the list associated to Elem.
 */
void removeElemAttribute(domElementPtr elem, char *name) {
    domAttributePtr cur;

    if (elem == NULL) return;
    if (elem->type != DOM_NODE_ELEMENT) return;
    cur = elem->attributes;
    while (cur != NULL) {
        if (!strcmp(name, cur->name)) {
	    if (cur->next == NULL) {
	        if (cur->prev == NULL) {
		    elem->attributes = elem->lastattribute = NULL;
		} else {
		    cur->prev->next = NULL;
		    elem->lastattribute = cur->prev;
		}
	    } else {
	        if (cur->prev == NULL) {
		    cur->next->prev = NULL;
		    elem->attributes = cur->next;
		} else {
		    cur->prev->next = cur->next;
		    cur->next->prev = cur->prev;
		}
	    }
	    domAttributeDestroy(cur);
	    return;
	}
	cur = cur->next;
    }
}

/*
 * Return an Attribute node.
 */
domAttributePtr getAttributeNode(domElementPtr elem, char *name) {
    domAttributePtr cur;

    if (elem == NULL) return(NULL);
    if (elem->type != DOM_NODE_ELEMENT) return(NULL);
    cur = elem->attributes;
    while (cur != NULL) {
        if (!strcmp(name, cur->name)) {
	    return(cur);
	}
	cur = cur->next;
    }
    return(NULL);
}

/*
 * 
 */
void setAttributeNode(domElementPtr elem, domAttributePtr newAttr) {
    domAttributePtr cur;

    if (elem == NULL) return;
    if (newAttr == NULL) return;
    if (elem->type != DOM_NODE_ELEMENT) return;
    cur = elem->attributes;
    while (cur != NULL) {
        if (cur == newAttr) {
	    if (cur->prev == NULL) {
	        if (cur->next == NULL) {
		    newAttr->prev = newAttr->next = NULL;
		    elem->attributes = elem->lastattribute = newAttr;
		} else {
		    cur->next->prev = newAttr;
		    newAttr->next = cur->next;
		    newAttr->prev = NULL;
		    elem->attributes = newAttr;
		}
	    } else {
	        if (cur->next == NULL) {
		    cur->prev->next = newAttr;
		    newAttr->prev = cur->prev;
		    newAttr->next = NULL;
		    elem->lastattribute = newAttr;
		} else {
		    cur->next->prev = newAttr;
		    cur->prev->next = newAttr;
		    newAttr->next = cur->next;
		    newAttr->prev = cur->prev;
		}
	    }
	    newAttr->parent = elem;
	    domAttributeDestroy(cur);
	    return;
	}
	cur = cur->next;
    }
    /*
     * Link as a new attribute at the end.
     */
    newAttr->parent = elem;
    newAttr->next = NULL;
    newAttr->prev = elem->lastattribute;
    if (newAttr->prev == NULL)
        elem->attributes = newAttr;
    else
        newAttr->prev->next = newAttr;
}

/*
 * 
 */
void removeAttributeNode(domElementPtr elem, domAttributePtr oldAttr) {
    domAttributePtr cur;

    if (elem == NULL) return;
    if (elem->type != DOM_NODE_ELEMENT) return;
    cur = elem->attributes;
    while (cur != NULL) {
        if (cur == oldAttr) {
	    if (cur->next == NULL) {
	        if (cur->prev == NULL) {
		    elem->attributes = elem->lastattribute = NULL;
		} else {
		    cur->prev->next = NULL;
		    elem->lastattribute = cur->prev;
		}
	    } else {
	        if (cur->prev == NULL) {
		    cur->next->prev = NULL;
		    elem->attributes = cur->next;
		} else {
		    cur->prev->next = cur->next;
		    cur->next->prev = cur->prev;
		}
	    }
	    domAttributeDestroy(cur);
	    return;
	}
	cur = cur->next;
    }
}

/*
 * getElemElementsByTagName ??? The IDL seems seriously corrupted !
 */
void getElemElementsByTagName(domElementPtr elem, char *tagname) {
    /* TODO !!! Once I have understood how to do it cleanly ... */
    fprintf(stderr, "getElemElementsByTagName : not yet implemented !\n");
}

/*
 * 
 */
void normalize(domElementPtr elem) {
    /* TODO !!! It's a pain ( Ok a small one ) */
    fprintf(stderr, "normalize : not yet implemented !\n");
}


Webmaster