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.InputStream;
28  
29  import javax.xml.parsers.ParserConfigurationException;
30  import javax.xml.parsers.SAXParser;
31  import javax.xml.parsers.SAXParserFactory;
32  
33  
34  import net.sf.jdiskcatalog.api.Document;
35  import net.sf.jdiskcatalog.api.DocumentReader;
36  import net.sf.jdiskcatalog.api.MutableNodeInfo;
37  import net.sf.jdiskcatalog.api.Node;
38  import net.sf.jdiskcatalog.api.NodeInfo;
39  import net.sf.jdiskcatalog.model.DocumentImpl;
40  import net.sf.jdiskcatalog.model.NodeImpl;
41  import net.sf.jdiskcatalog.model.NodeInfoImpl;
42  
43  import org.xml.sax.Attributes;
44  import org.xml.sax.SAXException;
45  import org.xml.sax.helpers.DefaultHandler;
46  
47  import com.thoughtworks.xstream.XStream;
48  import com.thoughtworks.xstream.io.xml.DomReader;
49  
50  public class XmlReader implements DocumentReader
51  {
52  	private XStream xstream;
53  
54  	public XmlReader()
55  	{
56  		xstream = new XStream();
57  		xstream.registerConverter(new ImageConverter());
58  		xstream.alias("image", BufferedImage.class);
59  	}
60  
61  	public Document read(InputStream stream) throws IOException
62  	{
63  		SAXParserFactory factory = SAXParserFactory.newInstance();
64  		try
65  		{
66  			SAXParser parser = factory.newSAXParser();
67  			Handler handler = new Handler();
68  			parser.parse(stream, handler);
69  			return handler.getDocument();
70  		}
71  		catch (ParserConfigurationException e)
72  		{
73  			throw new IOException(e);
74  		}
75  		catch (SAXException e)
76  		{
77  			throw new IOException(e);
78  		}
79  	}
80  
81  	private class Handler extends DefaultHandler
82  	{
83  		private Document document = new DocumentImpl();
84  		private Node node = null;
85  		private MutableNodeInfo nodeInfo = null;
86  		private Sax2Dom saxdom = new Sax2Dom();
87  		private String propertyName = null;
88  
89  		@Override
90  		public void characters(char[] ch, int start, int length) throws SAXException
91  		{
92  			if (propertyName != null)
93  				saxdom.characters(ch, start, length);
94  		}
95  
96  		@Override
97  		public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException
98  		{
99  			if (propertyName != null)
100 			{
101 				saxdom.startElement(uri, localName, qName, attributes);
102 				return;
103 			}
104 			Node newNode = null;
105 			MutableNodeInfo newNodeInfo = null;
106 			String name = attributes.getValue(XmlWriter.NAME_ATTRIBUTE);
107 			if (qName.equals(XmlWriter.COLLECTION_TAG))
108 				newNodeInfo = new NodeInfoImpl(name, NodeInfo.Type.COLLECTION);
109 			else if (qName.equals(XmlWriter.DISK_TAG))
110 				newNodeInfo = new NodeInfoImpl(name, NodeInfo.Type.DISK);
111 			else if (qName.equals(XmlWriter.DIRECTORY_TAG))
112 				newNodeInfo = new NodeInfoImpl(name, NodeInfo.Type.DIRECTORY);
113 			else if (qName.equals(XmlWriter.FILE_TAG))
114 				newNodeInfo = new NodeInfoImpl(name, NodeInfo.Type.FILE);
115 
116 			if (newNodeInfo != null)
117 			{
118 				newNode = new NodeImpl(newNodeInfo);
119 				if (node == null)
120 					document.add(newNode);
121 				else
122 					node.add(newNode);
123 				node = newNode;
124 				nodeInfo = newNodeInfo;
125 			}
126 
127 			if (qName.equals(XmlWriter.PROPERTY_TAG))
128 			{
129 				saxdom.startDocument();
130 				propertyName = attributes.getValue(XmlWriter.NAME_ATTRIBUTE);
131 			}
132 		}
133 
134 		@Override
135 		public void endElement(String uri, String localName, String qName) throws SAXException
136 		{
137 			if (qName.equals(XmlWriter.PROPERTY_TAG))
138 			{
139 				org.w3c.dom.Document domDocument = saxdom.getDocument();
140 				Object value = xstream.unmarshal(new DomReader(domDocument), null);
141 				nodeInfo.setProperty(propertyName, value);
142 				propertyName = null;
143 
144 			} else if (propertyName != null)
145 				saxdom.endElement(uri, localName, qName);
146 
147 			else if (qName.equals(XmlWriter.COLLECTION_TAG) ||
148 					qName.equals(XmlWriter.DISK_TAG) ||
149 					qName.equals(XmlWriter.DIRECTORY_TAG) ||
150 					qName.equals(XmlWriter.FILE_TAG))
151 			{
152 				node = node.getParentNode();
153 			}
154 
155 		}
156 
157 		public Document getDocument()
158 		{
159 			return document;
160 		}
161 	}
162 }