1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
|
/***************************************************************************************************
* Copyright (c) 2003, 2004 IBM Corporation and others. All rights reserved. This program and the
* accompanying materials are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors: IBM Corporation - initial API and implementation
**************************************************************************************************/
package org.eclipse.wst.common.internal.emf.resource;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.eclipse.emf.common.util.WrappedException;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.jem.util.logger.proxy.Logger;
import org.eclipse.wst.common.internal.emf.utilities.DOMLoadOptions;
import org.eclipse.wst.common.internal.emf.utilities.DOMUtilities;
import org.eclipse.wst.common.internal.emf.utilities.Revisit;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class EMF2DOMRenderer extends AbstractRendererImpl implements Renderer {
protected Map domAdapterRegistry;
protected boolean needsToCreateDOM = true;
protected Document document;
/**
* Constructor for DOMRendererImpl.
*/
public EMF2DOMRenderer() {
super();
if (managesDOMAdapters())
initDOMAdapterRegistry();
}
/**
* @see com.ibm.etools.emf2xml.Renderer#doLoad(InputStream, Map)
*/
public void doLoad(InputStream in, Map options) throws IOException {
if ((in != null) || !useStreamsForIO()) {
loadDocument(in, options);
EMF2DOMAdapter adapter = createRootDOMAdapter();
adapter.updateMOF();
}
}
protected void loadDocument(InputStream in, Map options) throws IOException {
try {
DOMLoadOptions domOpts = new DOMLoadOptions();
domOpts.setAllowJavaEncodings(true);
domOpts.setExpandEntityRefererences(true);
domOpts.setValidate(isValidating());
document = DOMUtilities.loadDocument(in, domOpts, getResource().getEntityResolver());
needsToCreateDOM = false;
} catch (RuntimeException t_rex) {
throw t_rex;
} catch (IOException iox) {
throw iox;
} catch (Exception ex) {
throw new WrappedException(ex);
}
}
/**
* @see com.ibm.etools.emf2xml.Renderer#doSave(OutputStream, Map)
*/
public void doSave(OutputStream outputStream, Map options) throws IOException {
createDOMTreeIfNecessary();
serializeDocument(outputStream);
}
/**
* Subclasses should override if adapters are not cached within this renderer, e.g., they are
* stored in notifying Nodes
*/
protected boolean managesDOMAdapters() {
return true;
}
protected void initDOMAdapterRegistry() {
if (domAdapterRegistry == null)
domAdapterRegistry = new HashMap();
}
public void registerDOMAdapter(Node node, EMF2DOMAdapter adapter) {
domAdapterRegistry.put(node, adapter);
}
public EMF2DOMAdapter getExistingDOMAdapter(Node node) {
return (EMF2DOMAdapter) domAdapterRegistry.get(node);
}
public void removeDOMAdapter(Node aNode, EMF2DOMAdapter anAdapter) {
domAdapterRegistry.remove(aNode);
}
/**
* @see com.ibm.etools.emf2xml.Renderer#prepareToAddContents()
*/
public void prepareToAddContents() {
// createDOMTreeIfNecessary();
}
protected Node createDOMTree() {
createDocument();
EMF2DOMAdapter adapter = createRootDOMAdapter();
adapter.updateDOM();
needsToCreateDOM = false;
return document;
}
protected EMF2DOMAdapter createRootDOMAdapter() {
EMF2DOMAdapter root = new EMF2DOMAdapterImpl(getResource(), document, this, getResource().getRootTranslator());
registerDOMAdapter(document, root);
return root;
}
protected void createDOMTreeIfNecessary() {
if (needsToCreateDOM)
createDOMTree();
}
/**
* Create a new Document given
*
* @aResource.
*/
protected void createDocument() {
TranslatorResource res = getResource();
res.setDefaults();
try {
document = DOMUtilities.createNewDocument(res.getDoctype(), res.getPublicId(), res.getSystemId());
} catch (ParserConfigurationException e) {
throw new WrappedException(e);
} catch (SAXException e) {
throw new WrappedException(e);
} catch (IOException e) {
throw new WrappedException(e);
}
}
public void serializeDocument(OutputStream out) throws IOException {
/*
* OutputFormat format = createOutputFormat(); Serializer serializer =
* SerializerFactory.getSerializerFactory(Method.XML).makeSerializer(out, format);
* serializer.asDOMSerializer().serialize(document);
*/
try {
TransformerFactory factory = TransformerFactory.newInstance();
/*
* try { factory.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA); } catch
* (IllegalArgumentException x) { }
*/
Transformer transformer = factory.newTransformer();
transformer.setOutputProperty(OutputKeys.ENCODING, getResource().getEncoding());
transformer.setOutputProperty(OutputKeys.VERSION, getResource().getXMLVersion());
transformer.setOutputProperty(OutputKeys.METHOD, "xml"); //$NON-NLS-1$
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); //$NON-NLS-1$
if (getResource().getPublicId() != null)
transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, getResource().getPublicId());
if (getResource().getSystemId() != null)
transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, getResource().getSystemId());
transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); //$NON-NLS-1$ //$NON-NLS-2$
DOMSource source = new DOMSource(document.getDocumentElement());
/* source.setSystemId(getResource().getSystemId()); */
transformer.transform(source, new StreamResult(out));
} catch (TransformerConfigurationException e) {
Logger.getLogger().logError(e);
} catch (TransformerFactoryConfigurationError e) {
Logger.getLogger().logError(e);
} catch (TransformerException e) {
Logger.getLogger().logError(e);
} finally {
}
}
/*
* protected OutputFormat createOutputFormat() { OutputFormat format = new OutputFormat();
* format.setIndenting(true); format.setLineSeparator(DOMUtilities.NEWLINE_STRING);
* //$NON-NLS-1$ format.setEncoding(getResource().getEncoding());
* format.setVersion(getResource().getXMLVersion()); return format; }
*/
public void replaceDocumentType(String docTypeName, String publicId, String systemId) {
Revisit.revisit();
Document newDoc = null;
// Need be able to update the doctype directly on the existing document; right now can't
// because
// of limitations on parser neutral apis
try {
newDoc = DOMUtilities.createNewDocument(docTypeName, publicId, systemId);
} catch (ParserConfigurationException e) {
throw new WrappedException(e);
} catch (SAXException e) {
throw new WrappedException(e);
} catch (IOException e) {
throw new WrappedException(e);
}
replaceNode(document.getDocumentElement(), newDoc, newDoc);
readapt(document, newDoc);
document = newDoc;
}
protected void replaceNode(Node oldChild, Node newParent, Document newDoc) {
Node newChild = newDoc.importNode(oldChild, false);
newParent.appendChild(newChild);
readapt(oldChild, newChild);
NodeList children = oldChild.getChildNodes();
int length = children.getLength();
for (int i = 0; i < length; i++) {
replaceNode(children.item(i), newChild, newDoc);
}
}
public void preUnload() {
EMF2DOMAdapter adapter = (EMF2DOMAdapter) EcoreUtil.getAdapter(resource.eAdapters(), EMF2DOMAdapter.ADAPTER_CLASS);
if (adapter != null) {
adapter.removeAdapters(adapter.getNode());
}
}
protected void readapt(Node oldChild, Node newChild) {
EMF2DOMAdapter adapter = getExistingDOMAdapter(oldChild);
if (adapter != null) {
registerDOMAdapter(newChild, adapter);
// Some nodes are managed by the parent and thus the
// node should not be set on the parent adapter
if (adapter.getNode() == oldChild)
adapter.setNode(newChild);
}
}
public int getVersionID() {
return getResource().getVersionID();
}
}
|