blob: e414ac56f1fe2df14ac27a46e6da80bb6ac23766 (
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
|
/*
* Copyright (c) 2012, Isode Limited, London, England.
* All rights reserved.
*/
/*
* Copyright (c) 2011, Kevin Smith
* All rights reserved.
*/
package com.isode.stroke.parser;
import java.util.LinkedList;
import com.isode.stroke.elements.Payload;
import com.isode.stroke.parser.tree.ParserElement;
/**
* Generic parser offering something a bit like a DOM to work with
* @param <T> Payload Type to parse
*/
public abstract class GenericPayloadTreeParser<T extends Payload> extends GenericPayloadParser<T> {
private LinkedList<ParserElement> elementStack_ = new LinkedList<ParserElement>();
private ParserElement root_;
/**
* Create the parser for the given payload type
* @param payload payload type object, not null
*/
public GenericPayloadTreeParser(T payload) {
super(payload);
}
@Override
public void handleCharacterData(String data) {
ParserElement current = elementStack_.getLast();
current.appendCharacterData(data);
}
@Override
public void handleEndElement(String element, String xmlns) {
elementStack_.removeLast();
if (elementStack_.isEmpty()) {
handleTree(root_);
}
}
@Override
public void handleStartElement(String element, String xmlns,
AttributeMap attributes) {
if (root_ == null) {
root_ = new ParserElement(element, xmlns, attributes);
elementStack_.addLast(root_);
}else {
ParserElement current = elementStack_.getLast();
elementStack_.addLast(current.addChild(element, xmlns, attributes));
}
}
/**
* Parse children of the root element. Subclasses should implement
* this method to extract the items from child nodes
* @param root root of the node whose children contains items
*/
public abstract void handleTree(ParserElement root);
}
|