summaryrefslogtreecommitdiffstats
blob: 6b637b1a2477c9257c0e1bda1a326d52d736fa7b (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
168
169
170
171
172
173
174
/*  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;

import java.util.Arrays;

import com.isode.stroke.base.ByteArray;

public class BOSHBodyExtractor {

    private BOSHBody body = null;

    public BOSHBodyExtractor(XMLParserFactory parserFactory,ByteArray data) {
        // Look for the opening body element
        byte[] rawData = data.getData();
        int i = 0;
        while (i < rawData.length && isWhitespace((char) rawData[i])) {
            ++i;
        }
        if ((rawData.length - i) < 6 || rawData[i] != '<' || rawData[i+1] != 'b' 
                || rawData[i+2] != 'o' || rawData[i+3] != 'd' || rawData[i+4] != 'y' 
                || !isEndCharacter((char) rawData[i+5])) {
            return;
        }
        i += 5;


        // Parse until end of element
        boolean inSingleQuote = false;
        boolean inDoubleQuote = false;
        boolean endStartTagSeen = false;
        boolean endElementSeen = false;

        for (; i < rawData.length; ++i) {
            char c = (char) rawData[i];
            if (inSingleQuote) {
                if (c == '\'') {
                    inSingleQuote = false;
                }
            }
            else if (inDoubleQuote) {
                if (c == '"') {
                    inDoubleQuote = false;
                }
            }
            else if (c == '\'') {
                inSingleQuote = true;
            }
            else if (c == '"') {
                inDoubleQuote = true;
            }
            else if (c == '/') {
                if (i + 1 == rawData.length || rawData[i+1] != '>') {
                    return;
                }
                else {
                    endElementSeen = true;
                    endStartTagSeen = true;
                    i += 2;
                    break;
                }
            }
            else if (c == '>') {
                endStartTagSeen = true;
                i += 1;
                break;
            }
        }

        if (!endStartTagSeen) {
            return;
        }

        // Look for the end of the element

        int j = rawData.length - 1;
        if (!endElementSeen) {
            while (isWhitespace((char) rawData[j]) && j > -1) {
                j--;
            }

            if (j == -1 || rawData[j] != '>') {
                return;
            }
            j--;

            while (j > -1 && isWhitespace((char) rawData[j])) {
                j--;
            }

            if (j < 6 || rawData[j-5] != '<' || rawData[j-4] != '/' || rawData[j-3] != 'b' 
                    || rawData[j-2] != 'o' || rawData[j-1] != 'd' || rawData[j] != 'y' ) {
                return;
            }

            j -= 6;
        }

        body = new BOSHBody();

        if (!endElementSeen) {
            byte[] rawBodyContent =
                    Arrays.copyOfRange(rawData, i, j+1);
            body.content = (new ByteArray(rawBodyContent)).toString();
        }

        BOSHBodyParserClient parserClient = new BOSHBodyParserClient(this);
        XMLParser parser = parserFactory.createParser(parserClient);
        String stringToParse = data.toString().substring(0, i);
        if(!parser.parse(stringToParse)) {
            body = null;
        }
    }

    public static boolean isWhitespace(char c) {
        return c == ' ' || c == '\n' || c == '\t' || c == '\r';
    }

    private static boolean isEndCharacter(char c) {
        return isWhitespace(c) || c == '>' || c == '/';
    }

    public BOSHBody getBody() {
        return body;
    }

    public static class BOSHBody {
        private AttributeMap attributes = new AttributeMap();
        private String content  = "";
        
        public AttributeMap getAttributes() {
            return attributes;
        }
        
        public String getContent() {
            return content;
        }
        
    }

    private final static class BOSHBodyParserClient implements XMLParserClient {

        private BOSHBodyParserClient(BOSHBodyExtractor bodyExtractor) {
            bodyExtractor_ = bodyExtractor;
        }

        @Override
        public void handleStartElement(String element, String ns,
                AttributeMap attributes) {
            bodyExtractor_.body.attributes = attributes;
        }

        @Override
        public void handleEndElement(String element, String ns) {
            // Empty Method
        }

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

        private final BOSHBodyExtractor bodyExtractor_;

    }

}