summaryrefslogtreecommitdiffstats
blob: 403ab4c8a3b8bd2715b7f0d4f5540ff111d87892 (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
/*
 * Copyright (c) 2011 Tobias Markmann
 * Licensed under the simplified BSD license.
 * See Documentation/Licenses/BSD-simplified.txt for more information.
 */
/*
 * Copyright (c) 2014 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */
/*
 * Copyright (c) 2015 Tarun Gupta.
 * Licensed under the simplified BSD license.
 * See Documentation/Licenses/BSD-simplified.txt for more information.
 */

package com.isode.stroke.parser.payloadparsers;

import com.isode.stroke.parser.GenericPayloadParser;
import com.isode.stroke.parser.AttributeMap;
import com.isode.stroke.elements.StreamInitiationFileInfo;
import com.isode.stroke.base.NotNull;
import com.isode.stroke.base.DateTime;

public class StreamInitiationFileInfoParser extends GenericPayloadParser<StreamInitiationFileInfo> {

	private int level = 0;
	private boolean parseDescription;
	private String desc = "";
	
	public StreamInitiationFileInfoParser() {
		super(new StreamInitiationFileInfo());
	}

	/**
	* @param element, NotNull.
	* @param ns.
	* @param attributes, NotNull.
	*/
	@Override
	public void handleStartElement(String element, String ns, AttributeMap attributes) {
		NotNull.exceptIfNull(element, "element");
		NotNull.exceptIfNull(attributes, "attributes");
		if (level == 0) {
			if(attributes.getAttributeValue("name") != null) {
				getPayloadInternal().setName(attributes.getAttributeValue("name"));
			} else {
				getPayloadInternal().setName("");
			}

			if(attributes.getAttributeValue("hash") != null) {
				getPayloadInternal().setHash(attributes.getAttributeValue("hash"));
			} else {
				getPayloadInternal().setHash("");
			}

			if(attributes.getAttributeValue("algo") != null) {
				getPayloadInternal().setAlgo(attributes.getAttributeValue("algo"));
			} else {
				getPayloadInternal().setAlgo("md5");
			}			

			if(attributes.getAttributeValue("size") != null) {
				try {
					getPayloadInternal().setSize(Long.parseLong(attributes.getAttributeValue("size")));
				} catch (NumberFormatException e) {
					getPayloadInternal().setSize(0L);
				}
			} else {
				getPayloadInternal().setSize(0L);
			}

			if(attributes.getAttributeValue("date") != null) {
				getPayloadInternal().setDate(DateTime.stringToDate(attributes.getAttributeValue("date")));
			} else {
				getPayloadInternal().setDate(DateTime.stringToDate(""));
			}

		} else if (level == 1) {
			if (element.equals("desc")) {
				parseDescription = true;
			} else {
				parseDescription = false;
				if (element.equals("range")) {
					long offset = 0;
					if (attributes.getAttributeValue("offset") != null) {
						try {
							offset = Long.parseLong(attributes.getAttributeValue("offset"));
						} catch (NumberFormatException e) {
							offset = 0;
						}
					} else {
						offset = 0;
					}
					if (offset == 0) {
						getPayloadInternal().setSupportsRangeRequests(true);
					} else {
						getPayloadInternal().setRangeOffset(offset);
					}
				}
			}
		}
		++level;
	}

	/**
	* @param element, NotNull.
	* @param ns.
	*/
	@Override
	public void handleEndElement(String element, String ns) {
		NotNull.exceptIfNull(element, "element");		
		--level;
		if (parseDescription && element.equals("desc")) {
			parseDescription = false;
			getPayloadInternal().setDescription(desc);
		}
	}

	/**
	* @param data, NotNull.
	*/
	@Override
	public void handleCharacterData(String data) {
		NotNull.exceptIfNull(data, "data");
		if (parseDescription) {
			desc += data;
		}
	}
}