PayloadFragment.java 2.58 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
package org.jivesoftware.messenger;

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

public class PayloadFragment extends AbstractFragment {

    public PayloadFragment(String namespace, String name) {
        this.namespace = namespace;
        this.name = name;
    }

    public void send(XMLStreamWriter xmlSerializer, int version) throws XMLStreamException {
        xmlSerializer.writeStartElement(name);
        xmlSerializer.writeDefaultNamespace(namespace);
        Iterator frags = getFragments();
        while (frags != null && frags.hasNext()) {
            ((XMPPFragment)frags.next()).send(xmlSerializer, version);
        }
        xmlSerializer.writeEndElement();
    }

    public XMPPFragment createDeepCopy() {

        PayloadFragment payload = new PayloadFragment(namespace, name);
        Iterator frags = getFragments();
        while (frags != null && frags.hasNext()) {
            payload.addFragment((XMPPFragment)frags.next());
        }
        return payload;
    }

    /**
     * <p>Converts the given fragment into a payload fragment.</p>
     * <p>All sub-elements of the original fragment are broken out into separate
     * MetaDataFragments. Currently only supports XMPPDOMFragment objects and their descendents.
     * During conversion attributes of the root element is lost.</p>
     *
     * @param frag The fragment to convert (must be XMPPDOMFragment or descendent)
     * @return The converted fragment with all sub-fragments broken up into separate fragments.
     * @throws IllegalArgumentException If the given fragment does not implement XMPPDOMFragment
     */
    public static PayloadFragment convertToPayload(XMPPFragment frag) throws IllegalArgumentException {
        PayloadFragment payload = null;
        if (frag instanceof XMPPDOMFragment) {
            XMPPDOMFragment dom = (XMPPDOMFragment)frag;
            payload = new PayloadFragment(dom.getNamespace(), dom.getName());
            Iterator frags = dom.getRootElement().elementIterator();
            while (frags.hasNext()) {
                payload.addFragment(new MetaDataFragment((Element)frags.next()));
            }
        }
        else {
            throw new IllegalArgumentException();
        }
        return payload;
    }
}