New Jide License
[indepmod/experimental.git] / IndependentModeler / src / cz / cvut / promod / services / projectService / localIO / ProjectFileSaver.java
blob75da2218569ac646d72c9a8b8a5387c8eddb0841
1 package cz.cvut.promod.services.projectService.localIO;
3 import cz.cvut.promod.services.ModelerSession;
4 import cz.cvut.promod.services.pluginLoaderService.utils.NotationSpecificPlugins;
5 import cz.cvut.promod.services.projectService.treeProjectNode.ProjectRoot;
6 import cz.cvut.promod.services.projectService.utils.ProjectServiceUtils;
7 import cz.cvut.promod.plugin.notationSpecificPlugIn.notation.Notation;
8 import cz.cvut.promod.plugin.notationSpecificPlugIn.module.Module;
9 import cz.cvut.promod.plugin.extension.Extension;
11 import javax.swing.tree.DefaultMutableTreeNode;
12 import javax.swing.tree.TreePath;
13 import javax.xml.stream.*;
14 import javax.xml.transform.*;
15 import javax.xml.transform.stream.StreamResult;
16 import java.io.*;
17 import java.util.Date;
19 import javax.xml.transform.stax.StAXSource;
21 /**
22 * ProMod, master thesis project
23 * User: Petr Zverina, petr.zverina@gmail.com
24 * Date: 17:49:07, 19.11.2009
26 public class ProjectFileSaver {
28 /**
29 * Saves the project to the xml project file. The location is defined by the
30 * location given by the project node and it's user object.
32 * There is saved some basic information about the project like project name,
33 * project location, date and user, but attributes project name and location
34 * are not important at all. This attributes are not use when the project is
35 * being loaded.
37 * There are then saved some information about the ProMod current configuration.
38 * Some basic information about currently loaded notations, modules and
39 * extension is saved. This info can be used when the project is being
40 * loaded to publish some errors or at least warn the user that there are
41 * some changes in ProMod configuration and some functionality can be missing
42 * or changed.
44 * @param projectTreePath is the tree path of the project navigation tree to the project root that is supposed
45 * to be saved
47 * @param useFormater if true, then then the xml output will be formatted,
48 * decreases performance
50 * @throws javax.xml.stream.XMLStreamException when an xml streaming error occurs
52 * @throws IOException an IO error has occurred
54 * @throws TransformerException specifies an exceptional condition that occurred during the transformation process
56 * @throws NullPointerException when the project location is null
58 * @throws IllegalArgumentException when the tree path to the project root is not valid
60 public static void saveProject(final TreePath projectTreePath, final boolean useFormater)
61 throws XMLStreamException, IOException, TransformerException, NullPointerException, IllegalArgumentException {
63 if(!ProjectServiceUtils.isValidTreePath(projectTreePath)){
64 throw new IllegalArgumentException("Invalid tree path");
67 if(!ProjectServiceUtils.isProjectRoot((DefaultMutableTreeNode) projectTreePath.getLastPathComponent())){
68 throw new IllegalArgumentException("Not a project root to save.");
71 final DefaultMutableTreeNode projectRootNode = (DefaultMutableTreeNode) projectTreePath.getLastPathComponent();
73 final ProjectRoot projectRoot = (ProjectRoot) projectRootNode.getUserObject();
75 final File projectFile = projectRoot.getProjectFile();
77 if(projectFile == null){
78 // should never happened
79 throw new IOException("Nullary project location");
82 final File parentFile = projectFile.getParentFile();
84 if(parentFile == null){
85 throw new IOException("Nullary project location");
88 if(!parentFile.exists()){
89 if(!parentFile.mkdirs()){
90 throw new IOException("Unable to create necessary directories.");
94 if(!projectFile.exists()){
95 if(!projectFile.createNewFile()){
96 throw new IOException("Impossible to create the project file");
100 final XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance();
101 final XMLStreamWriter xmlStreamWriter;
102 ByteArrayOutputStream byteArrayOutputStream = null;
104 if(useFormater){
105 byteArrayOutputStream = new ByteArrayOutputStream();
106 xmlStreamWriter = xmlOutputFactory.createXMLStreamWriter(byteArrayOutputStream, XmlConsts.ENCODING);
107 } else {
108 xmlStreamWriter = xmlOutputFactory.createXMLStreamWriter(new FileOutputStream(projectRoot.getProjectFile()), XmlConsts.ENCODING);
111 xmlStreamWriter.writeStartDocument(XmlConsts.ENCODING, XmlConsts.VERSION);
112 xmlStreamWriter.writeComment(XmlConsts.PROMOD_PROJECT_COMMENT);
113 xmlStreamWriter.writeStartElement(XmlConsts.ROOT_ELEMENT);
115 writeBasicProjectInfo(xmlStreamWriter, projectRoot);
117 writeConfigurationInfo(xmlStreamWriter);
119 xmlStreamWriter.writeEndElement();
120 xmlStreamWriter.writeEndDocument();
121 xmlStreamWriter.close();
123 if(useFormater){
124 final TransformerFactory transformerFactory = TransformerFactory.newInstance();
125 final Transformer transformer = transformerFactory.newTransformer();
127 transformer.setOutputProperty(OutputKeys.METHOD, XmlConsts.METHOD);
128 transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, XmlConsts.NO);
129 transformer.setOutputProperty(OutputKeys.ENCODING, XmlConsts.ENCODING);
130 transformer.setOutputProperty(OutputKeys.INDENT, XmlConsts.YES);
131 transformer.setOutputProperty(XmlConsts.INDEND_AMOUNT_PROPERTY, XmlConsts.INDEND_AMOUNT);
133 final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
134 byteArrayOutputStream.toByteArray()
137 XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
138 XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(
139 byteArrayInputStream
142 transformer.transform(
143 new StAXSource(xmlStreamReader), new StreamResult(projectRoot.getProjectFile()));
149 * Appends current ProMod configuration data to the project xml file.
150 * It is that possible to publish error or warning if the project is
151 * loaded bt the ProMod having different configuration.
153 * It means, that there could be some missing notations, modules, extensions,
154 * or the notation could be the same but it can use different file extension
155 * and so on.
157 * @param xmlStreamWriter is a instance of XMLStreamWriter to that are the
158 * elements supposed to be written
160 * @throws javax.xml.stream.XMLStreamException when an xml streaming error occurs
162 private static void writeConfigurationInfo(final XMLStreamWriter xmlStreamWriter) throws XMLStreamException {
163 xmlStreamWriter.writeStartElement(XmlConsts.CONFIGURATION_ELEMENT);
164 xmlStreamWriter.writeStartElement(XmlConsts.PLUGINS_ELEMENT);
165 writeNotationsInfo(xmlStreamWriter);
166 writeExtensionsInfo(xmlStreamWriter);
167 xmlStreamWriter.writeEndElement();
168 xmlStreamWriter.writeEndElement();
172 * Appends information about all notations and their related modules to the
173 * project xml file.
175 * @param xmlStreamWriter is a instance of XMLStreamWriter to that are the
176 * elements supposed to be written
178 * @throws javax.xml.stream.XMLStreamException when an xml streaming error occurs
180 private static void writeNotationsInfo(final XMLStreamWriter xmlStreamWriter) throws XMLStreamException {
181 for(final String notationIdentifier : ModelerSession.getNotationService().getNotationsIdentifiers()){
182 final NotationSpecificPlugins notationSpecificPlugins =
183 ModelerSession.getNotationService().getNotationSpecificPlugins(notationIdentifier);
184 final Notation notation = notationSpecificPlugins.getNotation();
186 xmlStreamWriter.writeStartElement(XmlConsts.NOTATION_ELEMENT);
187 xmlStreamWriter.writeAttribute(XmlConsts.IDENTIFIER_ATTRIBUTE, notation.getIdentifier());
188 xmlStreamWriter.writeAttribute(XmlConsts.FULL_NAME_ATTRIBUTE, notation.getFullName());
189 xmlStreamWriter.writeAttribute(XmlConsts.ABBREVIATION_ATTRIBUTE, notation.getAbbreviation());
190 xmlStreamWriter.writeAttribute(XmlConsts.EXTENSION_ATTRIBUTE, notation.getLocalIOController().getNotationFileExtension());
192 for(final Module module : notationSpecificPlugins.getModules()){
193 xmlStreamWriter.writeStartElement(XmlConsts.MODULE_ELEMENT);
194 xmlStreamWriter.writeAttribute(XmlConsts.IDENTIFIER_ATTRIBUTE, module.getIdentifier());
195 xmlStreamWriter.writeEndElement();
197 xmlStreamWriter.writeEndElement();
202 * Appends basic extensions info to the project xml file.
204 * @param xmlStreamWriter is a instance of XMLStreamWriter to that are the
205 * extensions supposed to be written
207 * @throws javax.xml.stream.XMLStreamException when an xml streaming error occurs
209 private static void writeExtensionsInfo(final XMLStreamWriter xmlStreamWriter) throws XMLStreamException {
210 for(final Extension extension : ModelerSession.getExtensionService().getExtensions()){
211 xmlStreamWriter.writeStartElement(XmlConsts.EXTENSION_ELEMENT);
212 xmlStreamWriter.writeAttribute(XmlConsts.IDENTIFIER_ATTRIBUTE, extension.getIdentifier());
213 xmlStreamWriter.writeAttribute(XmlConsts.FULL_NAME_ATTRIBUTE, extension.getName());
214 xmlStreamWriter.writeAttribute(XmlConsts.DESCRIPTION_ATTRIBUTE, extension.getDescription());
215 xmlStreamWriter.writeEndElement();
220 * Appends basic project specific information to the project xml file.
222 * @param xmlOutputFactory is a instance of XMLStreamWriter to that are the
223 * elements supposed to be written
225 * @param projectRoot is the project root that is supposed to be written to
226 * the xml file
228 * @throws javax.xml.stream.XMLStreamException when an xml streaming error occurs
230 private static void writeBasicProjectInfo(final XMLStreamWriter xmlOutputFactory, final ProjectRoot projectRoot)
231 throws XMLStreamException {
233 xmlOutputFactory.writeStartElement(XmlConsts.BASIC_INFO_ELEMENT);
235 xmlOutputFactory.writeStartElement(XmlConsts.LAST_NAME_ELEMENT);
236 xmlOutputFactory.writeCharacters(projectRoot.getDisplayName());
237 xmlOutputFactory.writeEndElement();
239 xmlOutputFactory.writeStartElement(XmlConsts.LAST_LOCATION_ELEMENT);
240 xmlOutputFactory.writeCharacters(projectRoot.getProjectFile().getAbsolutePath());
241 xmlOutputFactory.writeEndElement();
243 xmlOutputFactory.writeStartElement(XmlConsts.LAST_DATE_ELEMENT);
244 xmlOutputFactory.writeCharacters(new Date().toString());
245 xmlOutputFactory.writeEndElement();
247 xmlOutputFactory.writeStartElement(XmlConsts.LAST_USER_ELEMENT);
248 ModelerSession.getUserService().getUser();
249 xmlOutputFactory.writeEndElement();
251 xmlOutputFactory.writeEndElement();