View Javadoc

1   /*
2    * JDiskCatalog
3    *
4    * Copyright 2007 Przemek Więch
5    *
6    * This file is part of JDiskCatalog.
7    *
8    * JDiskCatalog is free software; you can redistribute it and/or modify
9    * it under the terms of the GNU General Public License as published by
10   * the Free Software Foundation; either version 2 of the License, or
11   * (at your option) any later version.
12   *
13   * This program is distributed in the hope that it will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   * GNU General Public License for more details.
17  
18   * You should have received a copy of the GNU General Public License
19   * along with this program; if not, write to the Free Software
20   * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
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 		// Node info
134 		for (String property : nodeInfo.getProperties())
135 		{
136 			attributes.setValue(0, property);
137 			handler.startElement(NSU, PROPERTY_TAG, PROPERTY_TAG, attributes);
138 			//String value = nodeInfo.getProperty(property).toString();
139 			//String value = xstream.toXML(nodeInfo.getProperty(property));
140 			//handler.characters(value.toCharArray(), 0, value.length());
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 		// Sub nodes
150 		for (Node n : node.getChildren())
151 			write(handler, n);
152 
153 		handler.endElement(NSU, tag, tag);
154 	}
155 }