All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.dom4j.util.IndexedElement Maven / Gradle / Ivy

There is a newer version: 3.6.2
Show newest version
/*
 * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
 *
 * This software is open source.
 * See the bottom of this file for the licence.
 */

package org.dom4j.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.dom4j.Attribute;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.QName;
import org.dom4j.tree.BackedList; 
import org.dom4j.tree.DefaultElement;

/**
 * IndexedElement is an implementation of {@link Element}which
 * maintains an index of the attributes and elements it contains to optimise
 * lookups via name.
 *
 * @author James Strachan 
 * @version $Revision: 1.10 $
 */ 
@SuppressWarnings("unchecked")
public class IndexedElement extends DefaultElement { 
	private static final long serialVersionUID = 1L;

	/**
     * Lazily constructed index for elements
     *
     * Keys are of type QName and String
     *
     * Values are of type Element and List<Element>
     */

    private Map elementIndex;

    /**
     * Lazily constructed index for attributes

     * Keys are of type QName and String
     *
     * Values are of type <A>
     */
    private Map attributeIndex;

    public IndexedElement(String name) {
        super(name);
    }

    public IndexedElement(QName qname) {
        super(qname);
    }

    public IndexedElement(QName qname, int attributeCount) {
        super(qname, attributeCount);
    }

    public Attribute attribute(String name) {
        return attributeIndex().get(name);
    }

    public Attribute attribute(QName qName) {
        return attributeIndex().get(qName);
    }

    public Element element(String name) {
        return asElement(elementIndex().get(name));
    }

    public Element element(QName qName) {
        return asElement(elementIndex().get(qName));
    }

    public List elements(String name) {
        return asElementList(elementIndex().get(name));
    }

    public List elements(QName qName) {
        return asElementList(elementIndex().get(qName));
    }

    // Implementation methods
    // -------------------------------------------------------------------------
    protected Element asElement(Object object) {
        if (object instanceof Element) {
            return (Element) object;
        } else if (object != null) {
            List list = (List) object;

            if (list.size() >= 1) {
                return list.get(0);
            }
        }

        return null;
    }

    protected List asElementList(Object object) {
        if (object instanceof Element) {
            return createSingleResultList((Element) object);
        } else if (object != null) {
            List list = (List) object;
            BackedList answer = createResultList();

            for (Element aList : list) {
                answer.addLocal(aList);
            }

            return answer;
        }

        return createEmptyList();
    }

    /**
     * DOCUMENT ME!
     * 
     * @param object
     *            DOCUMENT ME!
     * 
     * @return DOCUMENT ME!
     * 
     * @deprecated WILL BE REMOVED IN dom4j-1.6 !!
     */
    protected Iterator asElementIterator(Object object) {
        return asElementList(object).iterator();
    }

    // #### could we override the add(Element) remove(Element methods?
    protected void addNode(Node node) {
        super.addNode(node);

        if ((elementIndex != null) && node instanceof Element) {
            addToElementIndex((Element) node);
        } else if ((attributeIndex != null) && node instanceof Attribute) {
            addToAttributeIndex((Attribute) node);
        }
    }

    protected boolean removeNode(Node node) {
        if (super.removeNode(node)) {
            if ((elementIndex != null) && node instanceof Element) {
                removeFromElementIndex((Element) node);
            } else if ((attributeIndex != null) && node instanceof Attribute) {
                removeFromAttributeIndex((Attribute) node);
            }

            return true;
        }

        return false;
    }

    protected Map attributeIndex() {
        if (attributeIndex == null) {
            attributeIndex = createAttributeIndex();

            for (Iterator iter = attributeIterator(); iter.hasNext();) {
                addToAttributeIndex(iter.next());
            }
        }

        return attributeIndex;
    }

    protected Map elementIndex() {
        if (elementIndex == null) {
            elementIndex = createElementIndex();

            for (Iterator iter = elementIterator(); iter.hasNext();) {
                addToElementIndex(iter.next());
            }
        }

        return elementIndex;
    }

    /**
     * A Factory Method to create the index for attributes
     * 
     * @return DOCUMENT ME!
     */
    protected Map createAttributeIndex() {

        return createIndex();
    }

    /**
     * A Factory Method to create the index for elements
     * 
     * @return DOCUMENT ME!
     */
    protected Map createElementIndex() {

        return createIndex();
    }

    protected void addToElementIndex(Element element) {
        QName qName = element.getQName();
        String name = qName.getName();
        addToElementIndex(qName, element);
        addToElementIndex(name, element);
    }

    protected void addToElementIndex(Object key, Element value) {
        Object oldValue = elementIndex.get(key);

        if (oldValue == null) {
            elementIndex.put(key, value);
        } else {
            if (oldValue instanceof List) {
                List list = (List) oldValue;
                list.add(value);
            } else {
                List list = createList();
                list.add((Element) oldValue);
                list.add(value);
                elementIndex.put(key, list);
            }
        }
    }

    protected void removeFromElementIndex(Element element) {
        QName qName = element.getQName();
        String name = qName.getName();
        removeFromElementIndex(qName, element);
        removeFromElementIndex(name, element);
    }

    protected void removeFromElementIndex(Object key, Element value) {
        Object oldValue = elementIndex.get(key);

        if (oldValue instanceof List) {
            List list = (List) oldValue;
            list.remove(value);
        } else {
            elementIndex.remove(key);
        }
    }

    protected void addToAttributeIndex(Attribute attribute) {
        QName qName = attribute.getQName();
        String name = qName.getName();
        addToAttributeIndex(qName, attribute);
        addToAttributeIndex(name, attribute);
    }

    protected void addToAttributeIndex(Object key, Attribute value) {
        Object oldValue = attributeIndex.get(key);

        if (oldValue != null) {
            attributeIndex.put(key, value);
        }
    }

    protected void removeFromAttributeIndex(Attribute attribute) {
        QName qName = attribute.getQName();
        String name = qName.getName();
        removeFromAttributeIndex(qName, attribute);
        removeFromAttributeIndex(name, attribute);
    }

    protected void removeFromAttributeIndex(Object key, Attribute value) {
        Object oldValue = attributeIndex.get(key);

        if ((oldValue != null) && oldValue.equals(value)) {
            attributeIndex.remove(key);
        }
    }

    /**
     * Factory method to return a new map implementation for indices
     *
     * @param  DOCUMENT ME!
     * @return DOCUMENT ME!
     */
    protected  Map createIndex() {
        return new HashMap();
    }

    /**
     * Factory method to return a list implementation for indices
     *
     * @param  DOCUMENT ME!
     * @return DOCUMENT ME!
     */
    protected  List createList() {
        return new ArrayList();
    }
}

/*
 * Redistribution and use of this software and associated documentation
 * ("Software"), with or without modification, are permitted provided that the
 * following conditions are met:
 * 
 * 1. Redistributions of source code must retain copyright statements and
 * notices. Redistributions must also contain a copy of this document.
 * 
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 * 
 * 3. The name "DOM4J" must not be used to endorse or promote products derived
 * from this Software without prior written permission of MetaStuff, Ltd. For
 * written permission, please contact [email protected].
 * 
 * 4. Products derived from this Software may not be called "DOM4J" nor may
 * "DOM4J" appear in their names without prior written permission of MetaStuff,
 * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
 * 
 * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
 * 
 * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 * 
 * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
 */




© 2015 - 2024 Weber Informatics LLC | Privacy Policy