AbstractFragment.java 3.05 KB
Newer Older
Matt Tucker's avatar
Matt Tucker committed
1 2 3 4
/**
 * $RCSfile$
 * $Revision$
 * $Date$
Matt Tucker's avatar
Matt Tucker committed
5
 *
Matt Tucker's avatar
Matt Tucker committed
6
 * Copyright (C) 2004 Jive Software. All rights reserved.
Matt Tucker's avatar
Matt Tucker committed
7
 *
Matt Tucker's avatar
Matt Tucker committed
8 9
 * This software is published under the terms of the GNU Public License (GPL),
 * a copy of which is included in this distribution.
Matt Tucker's avatar
Matt Tucker committed
10
 */
Matt Tucker's avatar
Matt Tucker committed
11

Matt Tucker's avatar
Matt Tucker committed
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
package org.jivesoftware.messenger.spi;

import org.jivesoftware.messenger.XMPPFragment;

import java.util.*;

abstract public class AbstractFragment implements XMPPFragment {

    protected LinkedList fragments;
    protected String namespace = "";
    protected String name = "";

    /**
     * <p>Returns a namespace associated with this meta-data or null if none has been associated.</p>
     *
     * @return The namespace associated with this meta-data
     */
    public String getNamespace() {
        return namespace;
    }

    /**
     * <p>Sets a namespace associated with this meta-data or null if none has been associated.</p>
     *
     * @param namespace The namespace associated with this meta-data
     */
    public void setNamespace(String namespace) {
        this.namespace = namespace;
    }

    /**
     * <p>Returns a name associated with this meta-data or null if none has been associated.</p>
     *
     * @return The name associated with this meta-data
     */
    public String getName() {
        return name;
    }

    /**
     * <p>Sets a namespace associated with this meta-data or null if none has been associated.</p>
     *
     * @param name The namespace associated with this meta-data
     */
    public void setName(String name) {
        this.name = name;
    }

    public int getSize() {
        // estimate it to be something smaller than a packet
        return 20;
    }

    public void addFragment(XMPPFragment fragment) {
        if (fragments == null) {
            fragments = new LinkedList();
        }
        else {
            // inspect for circular parent-child relationship
            if (fragment.equals(this)) {
                throw new IllegalArgumentException("Circular parent-child relationship");
            }
            Iterator frags = fragment.getFragments();
            while (frags.hasNext()) {
                if (frags.next().equals(this)) {
                    throw new IllegalArgumentException("Circular parent-child relationship");
                }
            }
        }
        fragments.addLast(fragment);
    }

    public Iterator getFragments() {
        if (fragments == null) {
            return Collections.EMPTY_LIST.iterator();
        }
        else {
            return fragments.iterator();
        }
    }

    public XMPPFragment getFragment(String name, String namespace) {
        if (fragments == null) {
            return null;
        }
        XMPPFragment frag;
        for (Iterator frags = fragments.iterator(); frags.hasNext();) {
            frag = (XMPPFragment)frags.next();
            if (name.equals(frag.getName()) && namespace.equals(frag.getNamespace())) {
                return frag;
            }
        }
        return null;
    }

    public void clearFragments() {
        if (fragments != null) {
            fragments.clear();
        }
    }
}