view artifact-database/src/main/java/de/intevation/artifactdatabase/state/DefaultSection.java @ 415:d4a846ee35b8

Implement ArtifactDatabase findUser method artifacts/trunk@5262 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Bjoern Ricks <bjoern.ricks@intevation.de>
date Fri, 24 Aug 2012 14:08:31 +0000
parents ab3900303f25
children 92b51af2d02d
line wrap: on
line source
package de.intevation.artifactdatabase.state;

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

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

/**
 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
 */
public class DefaultSection implements Section {

    protected String id;

    protected List<Section> subsections;

    protected Map<String, Attribute> attributes;


    /**
     * Creates a new DefaultSection instance. <b>Note, that the <i>id</i> is used
     * as Node name of the new Element that is created in toXML().</b>
     */
    public DefaultSection(String id) {
        this.id          = id;
        this.attributes  = new HashMap<String, Attribute>();
        this.subsections = new ArrayList<Section>();
    }


    @Override
    public String getId() {
        return id;
    }


    @Override
    public void addSubsection(Section subsection) {
        if (subsection != null) {
            subsections.add(subsection);
        }
    }


    @Override
    public int getSubsectionCount() {
        return subsections.size();
    }


    @Override
    public Section getSubsection(int pos) {
        if (pos >= 0 && pos < getSubsectionCount()) {
            return subsections.get(pos);
        }

        return null;
    }


    @Override
    public void addAttribute(String key, Attribute attribute) {
        if (key != null && key.length() > 0 && attribute != null) {
            attributes.put(key, attribute);
        }
    }


    @Override
    public Attribute getAttribute(String key) {
        if (key == null || key.length() == 0) {
            return null;
        }

        return attributes.get(key);
    }


    @Override
    public Set<String> getKeys() {
        return attributes.keySet();
    }


    @Override
    public void toXML(Node parent) {
        Document owner     = parent.getOwnerDocument();
        Element  sectionEl = owner.createElement(getId());

        parent.appendChild(sectionEl);

        for (String key: getKeys()) {
            Attribute attr = getAttribute(key);
            attr.toXML(sectionEl);
        }

        for (int i = 0, n = getSubsectionCount(); i < n; i++) {
            Section subsection = getSubsection(i);
            subsection.toXML(sectionEl);
        }
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org