Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
XmiWriterMDRImpl |
|
| 3.0;3 |
1 | /* $Id: XmiWriterMDRImpl.java 17765 2010-01-11 21:20:14Z 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) 2005-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.model.mdr; | |
40 | ||
41 | import java.io.IOException; | |
42 | import java.io.OutputStream; | |
43 | import java.io.Writer; | |
44 | ||
45 | import javax.jmi.reflect.RefObject; | |
46 | import javax.jmi.reflect.RefPackage; | |
47 | ||
48 | import org.apache.log4j.Logger; | |
49 | import org.argouml.model.UmlException; | |
50 | import org.argouml.model.XmiExtensionWriter; | |
51 | import org.argouml.model.XmiWriter; | |
52 | import org.netbeans.api.xmi.XMIWriter; | |
53 | import org.netbeans.api.xmi.XMIWriterFactory; | |
54 | import org.netbeans.lib.jmi.xmi.OutputConfig; | |
55 | ||
56 | /** | |
57 | * XmiWriter implementation for MDR. | |
58 | * | |
59 | * This implementation is clumsy because the specified Writer interface wants | |
60 | * characters, while the XmiWriter wants an OutputStream dealing in bytes. We | |
61 | * could easily create a Writer from an OutputStream, but the reverse is not | |
62 | * true. | |
63 | * | |
64 | * TODO: The old Writer based interface can be removed when the deprecated | |
65 | * ModelImplementation.getXmiWriter is removed. | |
66 | * | |
67 | * @author lmaitre | |
68 | * | |
69 | */ | |
70 | class XmiWriterMDRImpl implements XmiWriter { | |
71 | ||
72 | 0 | private Logger LOG = Logger.getLogger(XmiWriterMDRImpl.class); |
73 | ||
74 | private MDRModelImplementation modelImpl; | |
75 | ||
76 | private Object model; | |
77 | ||
78 | private OutputConfig config; | |
79 | ||
80 | private Writer writer; | |
81 | ||
82 | private OutputStream oStream; | |
83 | ||
84 | private static final String ENCODING = "UTF-8"; | |
85 | ||
86 | private static final String XMI_VERSION = "1.2"; | |
87 | ||
88 | private XmiExtensionWriter xmiExtensionWriter; | |
89 | ||
90 | 0 | private static final char[] TARGET = "/XMI.content".toCharArray(); |
91 | ||
92 | /* | |
93 | * Private constructor for common work needed by both public | |
94 | * constructors. | |
95 | */ | |
96 | private XmiWriterMDRImpl(MDRModelImplementation theParent, Object theModel, | |
97 | 0 | String version) { |
98 | 0 | if (theModel == null) { |
99 | 0 | throw new IllegalArgumentException("A model must be provided"); |
100 | } | |
101 | 0 | if (theParent == null) { |
102 | 0 | throw new IllegalArgumentException("A parent must be provided"); |
103 | } | |
104 | 0 | this.modelImpl = theParent; |
105 | 0 | this.model = theModel; |
106 | 0 | config = new OutputConfig(); |
107 | 0 | config.setEncoding(ENCODING); |
108 | 0 | config.setReferenceProvider(new XmiReferenceProviderImpl(modelImpl |
109 | .getObjectToId())); | |
110 | 0 | config.setHeaderProvider(new XmiHeaderProviderImpl(version)); |
111 | 0 | } |
112 | ||
113 | ||
114 | /** | |
115 | * Create an XMI writer for the given model. | |
116 | * | |
117 | * @param theParent | |
118 | * The ModelImplementation | |
119 | * @param theModel | |
120 | * The Model to write. If null, write all top-level model | |
121 | * elements. | |
122 | * @param theStream | |
123 | * The OutputStream to write to. | |
124 | * @param version | |
125 | * the ArgoUML version | |
126 | * @throws IllegalArgumentException | |
127 | * if no output stream is provided | |
128 | * @since 0.25.4 | |
129 | */ | |
130 | public XmiWriterMDRImpl(MDRModelImplementation theParent, Object theModel, | |
131 | OutputStream theStream, String version) { | |
132 | 0 | this(theParent, theModel, version); |
133 | 0 | if (theStream == null) { |
134 | 0 | throw new IllegalArgumentException("A writer must be provided"); |
135 | } | |
136 | 0 | oStream = theStream; |
137 | 0 | } |
138 | ||
139 | ||
140 | public void write() throws UmlException { | |
141 | 0 | XMIWriter xmiWriter = XMIWriterFactory.getDefault().createXMIWriter( |
142 | config); | |
143 | try { | |
144 | 0 | modelImpl.getRepository().beginTrans(false); |
145 | try { | |
146 | 0 | RefPackage extent = ((RefObject) model).refOutermostPackage(); |
147 | 0 | xmiWriter.write(oStream, "file:///ThisIsADummyName.xmi", extent, |
148 | XMI_VERSION); | |
149 | } finally { | |
150 | // end our transaction | |
151 | 0 | modelImpl.getRepository().endTrans(); |
152 | 0 | } |
153 | 0 | } catch (IOException e) { |
154 | 0 | throw new UmlException(e); |
155 | 0 | } |
156 | 0 | } |
157 | ||
158 | public void setXmiExtensionWriter(XmiExtensionWriter theWriter) { | |
159 | 0 | xmiExtensionWriter = theWriter; |
160 | 0 | } |
161 | } |