1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package net.sf.jdiskcatalog.io;
24
25 import java.awt.image.BufferedImage;
26 import java.io.IOException;
27 import java.io.OutputStream;
28
29 import javax.xml.transform.TransformerConfigurationException;
30 import javax.xml.transform.sax.SAXTransformerFactory;
31 import javax.xml.transform.sax.TransformerHandler;
32 import javax.xml.transform.stream.StreamResult;
33
34
35 import net.sf.jdiskcatalog.api.Document;
36 import net.sf.jdiskcatalog.api.DocumentWriter;
37 import net.sf.jdiskcatalog.api.Node;
38 import net.sf.jdiskcatalog.api.NodeInfo;
39
40 import org.xml.sax.Attributes;
41 import org.xml.sax.ContentHandler;
42 import org.xml.sax.SAXException;
43 import org.xml.sax.helpers.AttributesImpl;
44
45 import com.thoughtworks.xstream.XStream;
46 import com.thoughtworks.xstream.io.xml.SaxWriter;
47
48 public class XmlWriter implements DocumentWriter
49 {
50 public static final String DOCUMENT_TAG = "document";
51 public static final String COLLECTION_TAG = "collection";
52 public static final String DISK_TAG = "disk";
53 public static final String DIRECTORY_TAG = "directory";
54 public static final String FILE_TAG = "file";
55 public static final String PROPERTY_TAG = "property";
56 public static final String NAME_ATTRIBUTE = "name";
57
58 public static final String NSU = "";
59 public static final Attributes EMPTY_ATTS = new AttributesImpl();
60
61 private AttributesImpl attributes = new AttributesImpl();
62
63 private XStream xstream;
64
65 public XmlWriter()
66 {
67 attributes.addAttribute("", "", NAME_ATTRIBUTE, "", "");
68 xstream = new XStream();
69 xstream.registerConverter(new ImageConverter());
70 xstream.alias("image", BufferedImage.class);
71 }
72
73 public void write(Document document, OutputStream stream) throws IOException
74 {
75 StreamResult result = new StreamResult(stream);
76 SAXTransformerFactory factory = (SAXTransformerFactory)SAXTransformerFactory.newInstance();
77 TransformerHandler handler;
78 try
79 {
80 handler = factory.newTransformerHandler();
81 }
82 catch (TransformerConfigurationException e)
83 {
84 throw new IOException(e);
85 }
86 handler.setResult(result);
87
88 try
89 {
90 write(handler, document);
91 }
92 catch (SAXException e)
93 {
94 throw new IOException(e);
95 }
96 }
97
98 private void write(ContentHandler handler, Document document) throws SAXException
99 {
100 handler.startDocument();
101 handler.startElement(NSU, DOCUMENT_TAG, DOCUMENT_TAG, EMPTY_ATTS);
102
103 for (Node node : document.getNodes())
104 write(handler, node);
105
106 handler.endElement(NSU, DOCUMENT_TAG, DOCUMENT_TAG);
107 handler.endDocument();
108 }
109
110 private void write(ContentHandler handler, Node node) throws SAXException
111 {
112 NodeInfo nodeInfo = node.getNodeInfo();
113 String tag = null;
114 switch (nodeInfo.getType())
115 {
116 case COLLECTION:
117 tag = COLLECTION_TAG;
118 break;
119 case DISK:
120 tag = DISK_TAG;
121 break;
122 case DIRECTORY:
123 tag = DIRECTORY_TAG;
124 break;
125 case FILE:
126 tag = FILE_TAG;
127 break;
128 }
129
130 attributes.setValue(0, node.getNodeInfo().getName());
131 handler.startElement(NSU, tag, tag, attributes);
132
133
134 for (String property : nodeInfo.getProperties())
135 {
136 attributes.setValue(0, property);
137 handler.startElement(NSU, PROPERTY_TAG, PROPERTY_TAG, attributes);
138
139
140
141
142 SaxWriter writer = new SaxWriter();
143 writer.setContentHandler(handler);
144 xstream.marshal(nodeInfo.getProperty(property), writer);
145
146 handler.endElement(NSU, PROPERTY_TAG, PROPERTY_TAG);
147 }
148
149
150 for (Node n : node.getChildren())
151 write(handler, n);
152
153 handler.endElement(NSU, tag, tag);
154 }
155 }