View Javadoc

1   /*
2    * This file is a part of CAST project.
3    * (c) Copyright 2007, AGH University of Science & Technology
4    * https://caribou.iisg.agh.edu.pl/trac/cast
5    *
6    * Licensed under the Eclipse Public License, Version 1.0 (the "License").
7    * You may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    * http://www.eclipse.org/legal/epl-v10.html
10   */
11  /*
12   * File: XMLExporter.java
13   * Created: 2007-00-00
14   * Author: kpietak
15   * $Id: XMLExporter.java 3020 2009-07-17 14:41:19Z kpietak $
16   */
17  
18  package pl.edu.agh.cast.backward.resources.xml;
19  
20  import java.io.BufferedWriter;
21  import java.io.File;
22  import java.io.FileWriter;
23  import java.io.IOException;
24  import java.io.ObjectOutputStream;
25  import java.io.Writer;
26  import java.util.List;
27  
28  import org.apache.log4j.Logger;
29  
30  import pl.edu.agh.cast.Activator;
31  import pl.edu.agh.cast.backward.resources.IExporter;
32  import pl.edu.agh.cast.model.visual.backward.IDiagram;
33  
34  import com.thoughtworks.xstream.XStream;
35  
36  /**
37   * Exports project to XML file.
38   *
39   * @author AGH CAST Team
40   */
41  public class XMLExporter implements IExporter {
42  
43  	private static final String XML_ENCODING_HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" //$NON-NLS-1$
44  	        + System.getProperty("line.separator"); //$NON-NLS-1$
45  
46  	private static final Logger LOG = Activator.getLogger();
47  
48  	/**
49  	 * XML serializer.
50  	 */
51  	private XStream xstream;
52  
53  	/**
54  	 * Default constructor. Same as <code>XMLExporter(null)</code>.
55  	 */
56  	public XMLExporter() {
57  		this(null);
58  	}
59  
60  	/**
61  	 * Constructor.
62  	 *
63  	 * @param classesWithAnnotations
64  	 *            see {@link XStreamInitializer#createXStreamInstance(Class[])}
65  	 */
66  	public XMLExporter(Class<?>[] classesWithAnnotations) {
67  		this.xstream = XStreamInitializer.createXStreamInstance(classesWithAnnotations);
68  		LOG.debug("XMLProjectExporter created."); //$NON-NLS-1$
69  	}
70  
71  	/**
72  	 * {@inheritDoc}
73  	 *
74  	 * @see pl.edu.agh.cast.backward.resources.IExporter#export(java.lang.String, java.util.List, java.io.File)
75  	 */
76  	public void export(String projectName, List<IDiagram> diagrams, File file) throws IOException {
77  		LOG.debug("Exporting project to file: " + file.getAbsolutePath()); //$NON-NLS-1$
78  		Writer fileWriter = new BufferedWriter(new FileWriter(file));
79  		fileWriter.write(XML_ENCODING_HEADER); // XStream doesn't do that
80  		xstream.toXML(new XMLProjectContainer(projectName, diagrams), fileWriter);
81  		fileWriter.close();
82  	}
83  
84  	/**
85  	 * {@inheritDoc}
86  	 *
87  	 * @see pl.edu.agh.cast.backward.resources.IExporter#export(pl.edu.agh.cast.model.visual.backward.IDiagram, java.io.File)
88  	 */
89  	public void export(IDiagram diagram, File file) throws IOException {
90  		LOG.debug("Exporting a diagram to file: " + file.getAbsolutePath()); //$NON-NLS-1$
91  		Writer fileWriter = new BufferedWriter(new FileWriter(file));
92  		fileWriter.write(XML_ENCODING_HEADER); // XStream doesn't do that
93  		ObjectOutputStream objectOutputStream = xstream.createObjectOutputStream(fileWriter);
94  		objectOutputStream.writeObject(diagram.getSettings());
95  		objectOutputStream.writeObject(diagram);
96  		objectOutputStream.close();
97  		fileWriter.close();
98  	}
99  
100 	/**
101 	 * Reads additional XStream annotations from specified class.
102 	 *
103 	 * @param clazz
104 	 *            class to read additional annotations from
105 	 */
106 	public void updateXStreamAnnotataions(Class<?> clazz) {
107 		xstream.processAnnotations(clazz);
108 	}
109 
110 }