summaryrefslogtreecommitdiffstats
blob: f7304dfe630bc9a79eab8405c8f2802f7273806b (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
63
64
65
66
67
68
69
70
71
/*  Copyright (c) 2016, Isode Limited, London, England.
 *  All rights reserved.
 *
 *  Acquisition and use of this software and related materials for any
 *  purpose requires a written license agreement from Isode Limited,
 *  or a written license from an organisation licensed by Isode Limited
 *  to grant such a license.
 *
 */
package com.isode.stroke.parser.payloadparsers;

import com.isode.stroke.elements.AbstractBlockPayload;
import com.isode.stroke.elements.BlockListPayload;
import com.isode.stroke.elements.BlockPayload;
import com.isode.stroke.elements.Payload;
import com.isode.stroke.elements.UnblockPayload;
import com.isode.stroke.jid.JID;
import com.isode.stroke.parser.AttributeMap;
import com.isode.stroke.parser.GenericPayloadParser;


/**
 * Base class for parser that pass Block pay loads such as 
 * {@link BlockPayload}, {@link BlockListPayload} and {@link UnblockPayload}
 * @param <T> Type of {@link Payload} that will be parsed.
 */
public abstract class BlockParser<T extends AbstractBlockPayload> extends GenericPayloadParser<T> {
    /*
     * Note this is slightly different to Swiften code as templates in C++ work
     * different to Java Generics.  In Swiften there exits a BlockParser template
     * for which instances can be created of type <BlockPayload>, <BlockListPayload> and
     * <UnblockPayload>.  In Java as we need to pass an instance of the class to the constructor
     * (which we can't do with generics) we need to create specific sub types for each of 
     * the different Block payloads.
     */
    
    private int level = 0;
    
    /**
     * Constructor
     * @param payload New instance of T to create the parsed
     * version from.
     */
    protected BlockParser(T payload) {
        super(payload);
    }

    @Override
    public void handleStartElement(String element, String ns,
            AttributeMap attributes) {
        if (level == 1 && "item".equals(element)) {
            JID jid = new JID(attributes.getAttribute("jid"));
            if (jid.isValid()) {
                AbstractBlockPayload payload = getPayloadInternal();
                payload.addItem(jid);
            }
        }
        ++level;
    }

    @Override
    public void handleEndElement(String element, String ns) {
        --level;
    }

    @Override
    public void handleCharacterData(String data) {
        // Empty method
    }

}