PacketFactoryImpl.java 3.17 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
package org.jivesoftware.messenger.spi;

import org.jivesoftware.messenger.container.BasicModule;
import org.jivesoftware.messenger.container.TrackInfo;
import org.jivesoftware.util.XPPReader;
import org.jivesoftware.messenger.*;
import org.jivesoftware.messenger.user.UserManager;
import org.jivesoftware.messenger.user.spi.IQRosterImpl;
import java.io.StringReader;
//import java.util.HashMap;
//import java.util.Iterator;
import java.util.LinkedList;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;

public class PacketFactoryImpl extends BasicModule implements PacketFactory {

    public LinkedList iqHandlers = new LinkedList();
    public UserManager userManager;
    private XMLInputFactory xppFactory;

    public PacketFactoryImpl() {
        super("Packet factory");
        xppFactory = XMLInputFactory.newInstance();
        xppFactory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);
    }

    public Message getMessage() {
        return new MessageImpl();
    }

    public Message getMessage(XMLStreamReader xpp) throws XMLStreamException {
        Message msg = new MessageImpl();
        parse(msg, xpp);
        return msg;
    }

    public Message getMessage(String msgText) throws XMLStreamException {
        Message msg = null;
        XMLStreamReader xpp = null;
        xpp = xppFactory.createXMLStreamReader(new StringReader(msgText));
        xpp.next(); // move parser to start tag
        msg = new MessageImpl();
        parse(msg, xpp);
        return msg;
    }

    public IQ getIQ() {
        return new IQImpl();
    }

    public IQ getIQ(XMLStreamReader xpp) throws XMLStreamException {

        IQ iq = null;

        try {
            Document doc = XPPReader.parseDocument(xpp);
            Element query = doc.getRootElement().element("query");
            if (query != null && "jabber:iq:roster".equals(query.getNamespaceURI())) {
                iq = new IQRosterImpl();
                ((IQImpl)iq).parse(doc);
            }
            else {
                iq = new IQImpl();
                ((IQImpl)iq).parse(doc);
            }
        }
        catch (DocumentException e) {
            throw new XMLStreamException(e.getMessage());
        }
        return iq;
    }

    public Presence getPresence() {
        return new PresenceImpl();
    }

    public Presence getPresence(XMLStreamReader xpp) throws XMLStreamException {
        Presence presence = new PresenceImpl();
        parse(presence, xpp);
        return presence;
    }

    private void parse(XMPPPacket packet, XMLStreamReader xpp) throws
            XMLStreamException {
        packet.parse(xpp);
    }

    protected TrackInfo getTrackInfo() {
        TrackInfo trackInfo = new TrackInfo();
        trackInfo.getTrackerClasses().put(UserManager.class, "userManager");
        return trackInfo;
    }
}