summaryrefslogtreecommitdiffstats
blob: 9a72cfa16de0a874ebdf0e15114ed63404ba2e48 (plain)
1
2
3
4
5
6
7
8
9
10
11
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
/*
 * Copyright (c) 2012-2014, Isode Limited, London, England.
 * All rights reserved.
 */
/*
 * Copyright (c) 2010, Kevin Smith
 * All rights reserved.
 */
package com.isode.stroke.serializer.payloadserializers;

import com.isode.stroke.base.DateTime;
import com.isode.stroke.elements.MUCPayload;
import com.isode.stroke.serializer.GenericPayloadSerializer;
import com.isode.stroke.serializer.xml.XMLElement;
import com.isode.stroke.serializer.xml.XMLTextNode;

/**
 * Serializer for {@link MUCPayload} element.
 */
public class MUCPayloadSerializer extends GenericPayloadSerializer<MUCPayload> {

    /**
     * Constructor 
     */
    public MUCPayloadSerializer()  {
        super(MUCPayload.class);
    }

    @Override
    public String serializePayload(MUCPayload muc) {
        XMLElement mucElement = new XMLElement("x", "http://jabber.org/protocol/muc");
        XMLElement historyElement = new XMLElement("history");
        boolean history = false;
        if (muc.getMaxChars() >= 0) {
            historyElement.setAttribute("maxchars", String.valueOf(muc.getMaxChars()));
            history = true;
        }
        if (muc.getMaxStanzas() >= 0) {
            historyElement.setAttribute("maxstanzas", String.valueOf(muc.getMaxStanzas()));
            history = true;
        }
        if (muc.getSeconds() >= 0) {
            historyElement.setAttribute("seconds", String.valueOf(muc.getSeconds()));
            history = true;
        }
        if (muc.getSince() != null) {
            String sinceDateString = DateTime.dateToString(muc.getSince());
            historyElement.setAttribute("since", sinceDateString);
            history = true;
        }
        if (muc.getPassword() != null) {
            String password = muc.getPassword();
            XMLElement passwordElement = new XMLElement("password");
            passwordElement.addNode(new XMLTextNode(password));
            mucElement.addNode(passwordElement);
        }
        if (history) {
            mucElement.addNode(historyElement);
        }
        return mucElement.serialize();
    }
}