1 | package genius.core.xml;
|
---|
2 |
|
---|
3 | import java.io.OutputStream;
|
---|
4 | import java.util.HashMap;
|
---|
5 | import java.util.Map;
|
---|
6 |
|
---|
7 | import javax.xml.stream.XMLOutputFactory;
|
---|
8 | import javax.xml.stream.XMLStreamException;
|
---|
9 | import javax.xml.stream.XMLStreamWriter;
|
---|
10 |
|
---|
11 | /**
|
---|
12 | * a xml stream that can write {@link Map}s
|
---|
13 | *
|
---|
14 | */
|
---|
15 | public class XmlWriteStream {
|
---|
16 |
|
---|
17 | private final XMLStreamWriter stream;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | *
|
---|
21 | * @param out
|
---|
22 | * the {@link OutputStream}
|
---|
23 | * @param topLabel
|
---|
24 | * the top level element name, typically "Tournament" or
|
---|
25 | * "Session"
|
---|
26 | * @throws XMLStreamException
|
---|
27 | */
|
---|
28 | public XmlWriteStream(OutputStream out, String topLabel)
|
---|
29 | throws XMLStreamException {
|
---|
30 | XMLOutputFactory output = XMLOutputFactory.newInstance();
|
---|
31 | stream = output.createXMLStreamWriter(out);
|
---|
32 |
|
---|
33 | stream.writeStartDocument();
|
---|
34 | stream.writeStartElement(topLabel);
|
---|
35 | }
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * write data to xml file
|
---|
39 | *
|
---|
40 | * @param name
|
---|
41 | * the name of the element to write.
|
---|
42 | * @param data
|
---|
43 | * a xml map of key-value pairs. The hashmap is written as a full
|
---|
44 | * element. Then each pair is checked. If the value is a
|
---|
45 | * {@link Map}, we call write recursively. Otherwise, we convert
|
---|
46 | * the key and value to {@link String} and write that element as
|
---|
47 | * an attribute.
|
---|
48 | *
|
---|
49 | * To improve layout, the hashmap's string values are written
|
---|
50 | * first.
|
---|
51 | *
|
---|
52 | * If you want to have multiple keys that look identical, use a
|
---|
53 | * {@link Key} as key.
|
---|
54 | * @throws XMLStreamException
|
---|
55 | */
|
---|
56 | @SuppressWarnings("unchecked")
|
---|
57 | public void write(String name, Map<Object, Object> data)
|
---|
58 | throws XMLStreamException {
|
---|
59 | stream.writeCharacters("\n");
|
---|
60 | stream.writeStartElement(name);
|
---|
61 |
|
---|
62 | for (Object key : data.keySet()) {
|
---|
63 | if (!(data.get(key) instanceof Map<?, ?>)) {
|
---|
64 | Object value = data.get(key);
|
---|
65 | stream.writeAttribute(key.toString(),
|
---|
66 | value == null ? "null" : value.toString());
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | for (Object key : data.keySet()) {
|
---|
71 | if (data.get(key) instanceof Map<?, ?>) {
|
---|
72 | write(key.toString(), (HashMap<Object, Object>) data.get(key));
|
---|
73 | }
|
---|
74 | }
|
---|
75 | stream.writeCharacters("\n");
|
---|
76 | stream.writeEndElement();
|
---|
77 | }
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * Close the stream. After this you should dispose the XmlWriteStream as
|
---|
81 | * calls to write and close will fail.
|
---|
82 | */
|
---|
83 | public void close() {
|
---|
84 | try {
|
---|
85 | stream.writeEndDocument();
|
---|
86 | stream.flush();
|
---|
87 | stream.close();
|
---|
88 | } catch (XMLStreamException e) {
|
---|
89 | e.printStackTrace();
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 | public void flush() throws XMLStreamException {
|
---|
94 | stream.flush();
|
---|
95 | }
|
---|
96 | }
|
---|