Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
DiagramMemberFilePersister |
|
| 3.5;3.5 |
1 | /* $Id: DiagramMemberFilePersister.java 17832 2010-01-12 19:02:29Z linus $ | |
2 | ***************************************************************************** | |
3 | * Copyright (c) 2009 Contributors - see below | |
4 | * All rights reserved. This program and the accompanying materials | |
5 | * are made available under the terms of the Eclipse Public License v1.0 | |
6 | * which accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: | |
10 | * tfmorris | |
11 | ***************************************************************************** | |
12 | * | |
13 | * Some portions of this file was previously release using the BSD License: | |
14 | */ | |
15 | ||
16 | // Copyright (c) 1996-2009 The Regents of the University of California. All | |
17 | // Rights Reserved. Permission to use, copy, modify, and distribute this | |
18 | // software and its documentation without fee, and without a written | |
19 | // agreement is hereby granted, provided that the above copyright notice | |
20 | // and this paragraph appear in all copies. This software program and | |
21 | // documentation are copyrighted by The Regents of the University of | |
22 | // California. The software program and documentation are supplied "AS | |
23 | // IS", without any accompanying services from The Regents. The Regents | |
24 | // does not warrant that the operation of the program will be | |
25 | // uninterrupted or error-free. The end-user understands that the program | |
26 | // was developed for research purposes and is advised not to rely | |
27 | // exclusively on the program for any reason. IN NO EVENT SHALL THE | |
28 | // UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, | |
29 | // SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, | |
30 | // ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF | |
31 | // THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF | |
32 | // SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY | |
33 | // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |
34 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE | |
35 | // PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF | |
36 | // CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT, | |
37 | // UPDATES, ENHANCEMENTS, OR MODIFICATIONS. | |
38 | ||
39 | package org.argouml.persistence; | |
40 | ||
41 | import java.io.IOException; | |
42 | import java.io.InputStream; | |
43 | import java.io.OutputStream; | |
44 | import java.io.OutputStreamWriter; | |
45 | import java.io.UnsupportedEncodingException; | |
46 | import java.net.URL; | |
47 | import java.util.HashMap; | |
48 | import java.util.Map; | |
49 | ||
50 | import org.apache.log4j.Logger; | |
51 | import org.argouml.application.api.Argo; | |
52 | import org.argouml.kernel.Project; | |
53 | import org.argouml.kernel.ProjectMember; | |
54 | import org.argouml.uml.diagram.ArgoDiagram; | |
55 | import org.argouml.uml.diagram.DiagramSettings; | |
56 | import org.argouml.uml.diagram.ProjectMemberDiagram; | |
57 | import org.tigris.gef.ocl.ExpansionException; | |
58 | import org.tigris.gef.ocl.OCLExpander; | |
59 | import org.tigris.gef.ocl.TemplateReader; | |
60 | import org.xml.sax.InputSource; | |
61 | ||
62 | /** | |
63 | * The file persister for the diagram members. | |
64 | * @author Bob Tarling | |
65 | */ | |
66 | 900 | class DiagramMemberFilePersister extends MemberFilePersister { |
67 | ||
68 | /** | |
69 | * Logger. | |
70 | */ | |
71 | 900 | private static final Logger LOG = |
72 | Logger.getLogger(DiagramMemberFilePersister.class); | |
73 | ||
74 | /** | |
75 | * The tee file for persistence. | |
76 | */ | |
77 | private static final String PGML_TEE = "/org/argouml/persistence/PGML.tee"; | |
78 | ||
79 | 900 | private static final Map<String, String> CLASS_TRANSLATIONS = |
80 | new HashMap<String, String>(); | |
81 | ||
82 | @Override | |
83 | public void load(Project project, InputStream inputStream) | |
84 | throws OpenException { | |
85 | 0 | load(project, new InputSource(inputStream)); |
86 | try { | |
87 | 0 | inputStream.close(); |
88 | 0 | } catch (IOException e) { |
89 | 0 | throw new OpenException("I/O error on stream close", e); |
90 | 0 | } |
91 | 0 | } |
92 | ||
93 | @Override | |
94 | public void load(Project project, InputSource inputSource) | |
95 | throws OpenException { | |
96 | ||
97 | // If the model repository doesn't manage a DI model | |
98 | // then we must generate our Figs by inspecting PGML | |
99 | try { | |
100 | // Give the parser a map of model elements | |
101 | // keyed by their UUID. This is used to allocate | |
102 | // figs to their owner using the "href" attribute | |
103 | // in PGML. | |
104 | 0 | DiagramSettings defaultSettings = |
105 | project.getProjectSettings().getDefaultDiagramSettings(); | |
106 | // TODO: We need the project specific diagram settings here | |
107 | 0 | PGMLStackParser parser = new PGMLStackParser(project.getUUIDRefs(), |
108 | defaultSettings); | |
109 | 0 | LOG.info("Adding translations registered by modules"); |
110 | for (Map.Entry<String, String> translation | |
111 | 0 | : CLASS_TRANSLATIONS.entrySet()) { |
112 | 0 | parser.addTranslation( |
113 | translation.getKey(), | |
114 | translation.getValue()); | |
115 | } | |
116 | 0 | ArgoDiagram d = parser.readArgoDiagram(inputSource, false); |
117 | 0 | project.addMember(d); |
118 | 0 | } catch (Exception e) { |
119 | 0 | if (e instanceof OpenException) { |
120 | 0 | throw (OpenException) e; |
121 | } | |
122 | 0 | throw new OpenException(e); |
123 | 0 | } |
124 | 0 | } |
125 | ||
126 | @Override | |
127 | public void load(Project project, URL url) throws OpenException { | |
128 | 0 | load(project, new InputSource(url.toExternalForm())); |
129 | 0 | } |
130 | ||
131 | @Override | |
132 | public String getMainTag() { | |
133 | 0 | return "pgml"; |
134 | } | |
135 | ||
136 | ||
137 | @Override | |
138 | public void save(ProjectMember member, OutputStream outStream) | |
139 | throws SaveException { | |
140 | ||
141 | 0 | ProjectMemberDiagram diagramMember = (ProjectMemberDiagram) member; |
142 | OCLExpander expander; | |
143 | try { | |
144 | 0 | expander = |
145 | new OCLExpander( | |
146 | TemplateReader.getInstance().read(PGML_TEE)); | |
147 | 0 | } catch (ExpansionException e) { |
148 | 0 | throw new SaveException(e); |
149 | 0 | } |
150 | OutputStreamWriter outputWriter; | |
151 | try { | |
152 | 0 | outputWriter = |
153 | new OutputStreamWriter(outStream, Argo.getEncoding()); | |
154 | 0 | } catch (UnsupportedEncodingException e1) { |
155 | 0 | throw new SaveException("Bad encoding", e1); |
156 | 0 | } |
157 | ||
158 | try { | |
159 | // WARNING: the OutputStream version of this doesn't work! - tfm | |
160 | 0 | expander.expand(outputWriter, diagramMember.getDiagram()); |
161 | 0 | } catch (ExpansionException e) { |
162 | 0 | throw new SaveException(e); |
163 | } finally { | |
164 | 0 | try { |
165 | 0 | outputWriter.flush(); |
166 | 0 | } catch (IOException e) { |
167 | 0 | throw new SaveException(e); |
168 | 0 | } |
169 | } | |
170 | ||
171 | 0 | } |
172 | ||
173 | /** | |
174 | * Figs are stored by class name and recreated by reflection. If the class | |
175 | * name changes or moves this provides a simple way of translating from | |
176 | * class name at time of save to the current class name without need for | |
177 | * XSL. | |
178 | * @param originalClassName | |
179 | * @param newClassName | |
180 | */ | |
181 | public void addTranslation( | |
182 | final String originalClassName, | |
183 | final String newClassName) { | |
184 | 9900 | CLASS_TRANSLATIONS.put(originalClassName, newClassName); |
185 | 9900 | } |
186 | } |