summaryrefslogtreecommitdiffstats
blob: bb7fe275157b924616ac17cbe83ae74c896b71c7 (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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
 * Copyright (c) 2012 Isode Limited, London, England.
 * All rights reserved.
 */
/*
 * Copyright (c) 2010 Remko Tronçon
 * All rights reserved.
 */

package com.isode.stroke.parser.payloadparsers;

import com.isode.stroke.elements.Command;
import com.isode.stroke.elements.Form;
import com.isode.stroke.elements.Command.Action;
import com.isode.stroke.elements.Command.Note;
import com.isode.stroke.elements.Command.Status;
import com.isode.stroke.elements.Command.Note.Type;
import com.isode.stroke.parser.AttributeMap;
import com.isode.stroke.parser.GenericPayloadParser;

/**
 * Parser for {@link Command} element.
 */
public class CommandParser extends GenericPayloadParser<Command> {
    private static final int TopLevel = 0;
    private static final int PayloadLevel = 1;
    private static final int FormOrNoteOrActionsLevel = 2;
    private static final int ActionsActionLevel = 3;

    private int level_;
    private boolean inNote_;
    private boolean inActions_;
    private Type noteType_;
    private FormParserFactory formParserFactory_;
    private FormParser formParser_;
    private String currentText_ = "";

    /**
     * Constructor
     */
    public CommandParser() {
        super(new Command());

        level_ = TopLevel;
        inNote_ = false;
        inActions_ = false;
        noteType_ = Type.INFO;
        formParser_ = null;

        formParserFactory_ = new FormParserFactory();
    }

    public void handleStartElement(String element, String ns,
            final AttributeMap attributes) {
        if (element == null) {
            throw new NullPointerException("'element' must not be null");
        }
        if (ns == null) {
            throw new NullPointerException("'ns' must not be null");
        }
        if (attributes == null) {
            throw new NullPointerException("'attributes' must not be null");
        }

        ++level_;
        Command command = getPayloadInternal();

        if (level_ == PayloadLevel) {
            Action action = parseAction(attributes
                    .getAttribute(Command.COMMAND_ATTRIBUTE_ACTION));
            command.setAction(action);

            String status = attributes
                    .getAttribute(Command.COMMAND_ATTRIBUTE_STATUS);
            command.setStatus(Status.getStatus(status));

            command.setNode(attributes
                    .getAttribute(Command.COMMAND_ATTRIBUTE_NODE));
            command.setSessionID(attributes
                    .getAttribute(Command.COMMAND_ATTRIBUTE_SESSION_ID));
        } else if (level_ == FormOrNoteOrActionsLevel) {
            assert (formParser_ == null);
            if (formParserFactory_.canParse(element, ns, attributes)) {
                formParser_ = (FormParser) (formParserFactory_
                        .createPayloadParser());
                assert (formParser_ != null);
            } else if (element.equals(Command.COMMAND_ELEMENT_NOTE)) {
                inNote_ = true;
                currentText_ = "";
                String noteType = attributes
                        .getAttribute(Command.COMMAND_ATTRIBUTE_TYPE);
                noteType_ = Type.getType(noteType);
            } else if (element.equals(Command.COMMAND_ELEMENT_ACTIONS)) {
                inActions_ = true;
                Action action = parseAction(attributes
                        .getAttribute(Command.COMMAND_ATTRIBUTE_EXECUTE));
                command.setExecuteAction(action);
            }
        } else if (level_ == ActionsActionLevel) {
        }

        if (formParser_ != null) {
            formParser_.handleStartElement(element, ns, attributes);
        }
    }

    public void handleEndElement(String element, String ns) {
        if (element == null) {
            throw new NullPointerException("'element' must not be null");
        }
        if (ns == null) {
            throw new NullPointerException("'ns' must not be null");
        }

        if (formParser_ != null) {
            formParser_.handleEndElement(element, ns);
        }

        Command command = getPayloadInternal();

        if (level_ == FormOrNoteOrActionsLevel) {
            if (formParser_ != null) {
                Form form = (Form) (formParser_.getPayload());
                assert (form != null);
                command.setForm(form);
                formParser_ = null;
            } else if (inNote_) {
                inNote_ = false;
                command.addNote(new Note(currentText_, noteType_));
            } else if (inActions_) {
                inActions_ = false;
            }
        } else if ((level_ == ActionsActionLevel) && inActions_) {
            Action action = parseAction(element);
            command.addAvailableAction(action);
        }

        --level_;
    }

    public void handleCharacterData(String data) {
        if (data == null) {
            throw new NullPointerException("'data' must not be null");
        }

        if (formParser_ != null) {
            formParser_.handleCharacterData(data);
        } else {
            currentText_ += data;
        }
    }

    private static Action parseAction(String action) {
        if (action == null) {
            throw new NullPointerException("'action' must not be null");
        }

        return Action.getAction(action);
    }

    @Override
    public String toString() {
        return CommandParser.class.getSimpleName() + "\nlevel: " + level_
                + "\ncurrent text: " + currentText_ + "\nnote: " + inNote_
                + "\nactions: " + inActions_ + "\nnote type: " + noteType_;
    }
}