summaryrefslogtreecommitdiffstats
blob: 647ab9e5ebde8d91b21b2296f0c695459ebf9bdf (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.tree;

import java.util.LinkedList;

import com.isode.stroke.elements.Payload;
import com.isode.stroke.parser.PayloadParser;
import com.isode.stroke.parser.PayloadParserFactoryCollection;

/**
 * Class for parsing elements of a parser element
 *
 */
public class TreeReparser {
    /**
     * Class representing an element and its handling state
     *
     */
    private static class ElementState {
        ParserElement pe;
        boolean state;
        public ElementState(ParserElement pe, boolean state) {
            super();
            this.pe = pe;
            this.state = state;
        }        
    }

    /**
     * Parse the elements under the given root node
     * @param root root node, not null
     * @param collection payload parser factories, not null
     * @return payload created from the elements of the given node, not null
     */
    public static Payload parseTree(ParserElement root, PayloadParserFactoryCollection collection) {
        PayloadParser parser = collection.getPayloadParserFactory(root.getName(), root.getNamespace(), 
                root.getAttributes()).createPayloadParser();
        LinkedList<ElementState> stack = new LinkedList<ElementState>();
        stack.addLast(new ElementState(root, true));
        while (!stack.isEmpty()) {
            ElementState current = stack.getLast();
            stack.removeLast();
            if (current.state) {
                stack.addLast(new ElementState(current.pe, false));
                parser.handleStartElement(current.pe.getName(), current.pe.getNamespace(), current.pe.getAttributes());
                for(ParserElement child : current.pe.getAllChildren()) {
                    stack.addLast(new ElementState(child, true));
                }
            } else {
                parser.handleCharacterData(current.pe.getText());
                parser.handleEndElement(current.pe.getName(), current.pe.getNamespace());
            }
        }

        Payload payload = parser.getPayload();
        return payload;
    }
}