XMPPDOMFragment.java 1.97 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
package org.jivesoftware.messenger;

import org.jivesoftware.util.XPPWriter;
import org.jivesoftware.messenger.spi.AbstractFragment;
import java.util.Iterator;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

/**
 * Stores the fragment in a dom4j DOM model. Efficiency of the fragment is
 * relatively low but is the most flexible way to store fragment information.
 *
 * @author Iain Shigeoka
 */
public class XMPPDOMFragment extends AbstractFragment implements XMPPFragment {

    /**
     * The document holding this fragment's data.
     */
    private Element root;

    /**
     * Constructor using a given Document to represent the packet.
     */
    public XMPPDOMFragment(Element root) {
        this.root = root;
        name = root.getName();
        namespace = root.getNamespaceURI();
    }

    /**
     * Constructor creates it's own Document to represent the packet.
     */
    public XMPPDOMFragment() {
        root = DocumentHelper.createElement("jive");
    }

    /**
     * Obtain the root element of the DOM tree representing the data in this fragment.
     *
     * @return the root element of the DOM tree or null if none has been set
     */
    public Element getRootElement() {
        return root;
    }

    public void send(XMLStreamWriter xmlSerializer, int version) throws
            XMLStreamException {
        XPPWriter.write(root, xmlSerializer);
    }

    public XMPPFragment createDeepCopy() {
        XMPPFragment frag = new XMPPDOMFragment((Element)root.clone());

        Iterator frags = getFragments();
        while (frags.hasNext()) {
            frag.addFragment((XMPPFragment)frags.next());
        }
        return frag;
    }
}