KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > ecore > xmi > impl > XMLResourceImpl


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2002-2004 IBM Corporation and others.
5  * All rights reserved. This program and the accompanying materials
6  * are made available under the terms of the Eclipse Public License v1.0
7  * which accompanies this distribution, and is available at
8  * http://www.eclipse.org/legal/epl-v10.html
9  *
10  * Contributors:
11  * IBM - Initial API and implementation
12  *
13  * </copyright>
14  *
15  * $Id: XMLResourceImpl.java,v 1.9 2005/06/15 21:16:49 elena Exp $
16  */

17 package org.eclipse.emf.ecore.xmi.impl;
18
19
20 import java.io.IOException JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.io.OutputStream JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.TreeMap JavaDoc;
28 import java.util.WeakHashMap JavaDoc;
29
30 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
31
32 import org.eclipse.emf.common.util.URI;
33 import org.eclipse.emf.ecore.EObject;
34 import org.eclipse.emf.ecore.resource.impl.ResourceImpl;
35 import org.eclipse.emf.ecore.util.EcoreUtil;
36 import org.eclipse.emf.ecore.xmi.DOMHandler;
37 import org.eclipse.emf.ecore.xmi.DOMHelper;
38 import org.eclipse.emf.ecore.xmi.XMLHelper;
39 import org.eclipse.emf.ecore.xmi.XMLLoad;
40 import org.eclipse.emf.ecore.xmi.XMLResource;
41 import org.eclipse.emf.ecore.xmi.XMLSave;
42 import org.w3c.dom.Document JavaDoc;
43 import org.w3c.dom.Node JavaDoc;
44
45
46 /**
47  * This class implements the XMLResource interface. It overloads the
48  * doLoad method to invoke the XML deserializer rather than using the
49  * default XMI loader.
50  */

51 public class XMLResourceImpl extends ResourceImpl implements XMLResource
52 {
53   /**
54    * The map from {@link #getID ID} to {@link EObject}.
55    * It is used to store IDs during a load or if the user
56    * sets the ID of an object.
57    */

58   protected Map JavaDoc idToEObjectMap;
59
60   /**
61    * The map from {@link EObject} to {@link #getID ID}.
62    * It is used to store IDs during a load or if the user
63    * sets the ID of an object.
64    */

65   protected Map JavaDoc eObjectToIDMap;
66
67   protected Map JavaDoc eObjectToExtensionMap;
68
69   protected String JavaDoc encoding;
70   protected boolean useZip;
71   protected String JavaDoc publicId;
72   protected String JavaDoc systemId;
73   protected DOMHandler domHandler;
74
75   /**
76    * The map from {@link EObject} to {@link #getID ID}. It is used to store
77    * IDs for objects that have been detached.
78    */

79   protected static final Map JavaDoc DETACHED_EOBJECT_TO_ID_MAP = Collections.synchronizedMap(new WeakHashMap JavaDoc());
80
81   /**
82    * Constructor for XMLResourceImpl.
83    */

84   public XMLResourceImpl()
85   {
86     super();
87     init();
88   }
89
90   /**
91    * Constructor for XMLResourceImpl.
92    * @param uri
93    */

94   public XMLResourceImpl(URI uri)
95   {
96     super(uri);
97     init();
98   }
99
100   protected void init()
101   {
102     encoding = "ASCII";
103   }
104
105   protected boolean useIDs()
106   {
107     return true;
108   }
109
110   protected boolean useIDAttributes()
111   {
112     return true;
113   }
114
115   protected boolean useUUIDs()
116   {
117     return false;
118   }
119
120   public Map JavaDoc getDefaultSaveOptions()
121   {
122     if (defaultSaveOptions == null)
123     {
124       defaultSaveOptions = new HashMap JavaDoc();
125     }
126     return defaultSaveOptions;
127   }
128
129   public Map JavaDoc getDefaultLoadOptions()
130   {
131     if (defaultLoadOptions == null)
132     {
133       defaultLoadOptions = new HashMap JavaDoc();
134     }
135     return defaultLoadOptions;
136   }
137
138   protected XMLHelper createXMLHelper()
139   {
140     return new XMLHelperImpl(this);
141   }
142
143   protected XMLLoad createXMLLoad()
144   {
145     return new XMLLoadImpl(createXMLHelper());
146   }
147
148   protected XMLSave createXMLSave()
149   {
150     return new XMLSaveImpl(createXMLHelper());
151   }
152
153   public void doLoad(InputStream JavaDoc inputStream, Map JavaDoc options) throws IOException JavaDoc
154   {
155     XMLLoad xmlLoad = createXMLLoad();
156
157     if (options == null)
158     {
159       options = Collections.EMPTY_MAP;
160     }
161
162     ResourceHandler handler = (ResourceHandler)options.get(OPTION_RESOURCE_HANDLER);
163
164     if (handler != null)
165     {
166       handler.preLoad(this, inputStream, options);
167     }
168
169     xmlLoad.load(this, inputStream, options);
170
171     if (handler != null)
172     {
173       handler.postLoad(this, inputStream, options);
174     }
175   }
176
177   public void doSave(OutputStream JavaDoc outputStream, Map JavaDoc options) throws IOException JavaDoc
178   {
179     XMLSave xmlSave = createXMLSave();
180
181     if (options == null)
182     {
183       options = Collections.EMPTY_MAP;
184     }
185
186     ResourceHandler handler = (ResourceHandler)options.get(OPTION_RESOURCE_HANDLER);
187
188     if (handler != null)
189     {
190       handler.preSave(this, outputStream, options);
191     }
192
193     xmlSave.save(this, outputStream, options);
194
195
196     if (handler != null)
197     {
198       handler.postSave(this, outputStream, options);
199     }
200   }
201
202   public Document JavaDoc save(Document JavaDoc doc, Map JavaDoc options, DOMHandler handler)
203   {
204     XMLSave xmlSave = createXMLSave();
205     domHandler = handler;
206     if (domHandler == null)
207     {
208       domHandler = new DefaultDOMHandlerImpl();
209     }
210     Document JavaDoc document = doc;
211     if (document == null)
212     {
213       try
214       {
215         document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
216       }
217       catch (Exception JavaDoc e)
218       {
219         throw new RuntimeException JavaDoc(e.getMessage());
220       }
221     }
222     if (defaultSaveOptions == null || defaultSaveOptions.isEmpty())
223     {
224       return xmlSave.save(this, document, options, domHandler);
225     }
226     else if (options == null)
227     {
228       return xmlSave.save(this, document, defaultSaveOptions, domHandler);
229     }
230     else
231     {
232       Map JavaDoc mergedOptions = new HashMap JavaDoc(defaultSaveOptions);
233       mergedOptions.putAll(options);
234       return xmlSave.save(this, document, mergedOptions, domHandler);
235     }
236   }
237
238   public DOMHelper getDOMHelper()
239   {
240     return domHandler.getDOMHelper();
241   }
242
243   public boolean useZip()
244   {
245     return useZip;
246   }
247
248   public void setUseZip(boolean useZip)
249   {
250     this.useZip = useZip;
251   }
252   
253   public String JavaDoc getPublicId()
254   {
255     return publicId;
256   }
257   public String JavaDoc getSystemId()
258   {
259     return systemId;
260   }
261   public void setDoctypeInfo(String JavaDoc publicId, String JavaDoc systemId)
262   {
263     this.publicId = publicId;
264     this.systemId = systemId;
265   }
266
267   public String JavaDoc getEncoding()
268   {
269     return encoding;
270   }
271
272   public void setEncoding(String JavaDoc encoding)
273   {
274     this.encoding = encoding;
275   }
276
277   public Map JavaDoc getIDToEObjectMap()
278   {
279     if (idToEObjectMap == null)
280     {
281       idToEObjectMap = new HashMap JavaDoc();
282     }
283
284     return idToEObjectMap;
285   }
286
287   public Map JavaDoc getEObjectToIDMap()
288   {
289     if (eObjectToIDMap == null)
290     {
291       eObjectToIDMap = new HashMap JavaDoc();
292     }
293
294     return eObjectToIDMap;
295   }
296
297   public Map JavaDoc getEObjectToExtensionMap()
298   {
299     if (eObjectToExtensionMap == null)
300     {
301       eObjectToExtensionMap = new HashMap JavaDoc();
302     }
303     return eObjectToExtensionMap;
304   }
305
306   /*
307    * Javadoc copied from interface
308    */

309   public String JavaDoc getID(EObject eObject)
310   {
311     if (eObjectToIDMap == null)
312     {
313       return null;
314     }
315     else
316     {
317       return (String JavaDoc)eObjectToIDMap.get(eObject);
318     }
319   }
320
321   /**
322    * Sets the ID of the object.
323    * This default implementation will update the {@link #eObjectToIDMap}.
324    * Clients may override it to set the ID as an actual attribute object the object.
325    * @param eObject the object.
326    * @param id the object's ID.
327    */

328   public void setID(EObject eObject, String JavaDoc id)
329   {
330     Object JavaDoc oldID = id != null ? getEObjectToIDMap().put(eObject, id) : getEObjectToIDMap().remove(eObject);
331     
332     if (oldID != null)
333     {
334       getIDToEObjectMap().remove(oldID);
335     }
336     
337     if (id != null)
338     {
339       getIDToEObjectMap().put(id, eObject);
340     }
341   }
342
343   /*
344    * Javadoc copied from interface.
345    */

346   public String JavaDoc getURIFragment(EObject eObject)
347   {
348     String JavaDoc id = getID(eObject);
349
350     if (id != null)
351     {
352       return id;
353     }
354     else
355     {
356       return super.getURIFragment(eObject);
357     }
358   }
359
360   protected EObject getEObjectByID(String JavaDoc id)
361   {
362     if (idToEObjectMap != null)
363     {
364       EObject eObject = (EObject) idToEObjectMap.get(id);
365       if (eObject != null)
366       {
367         return eObject;
368       }
369     }
370
371    return useIDAttributes() ? super.getEObjectByID(id) : null;
372   }
373
374   protected boolean isPath(String JavaDoc uriFragment)
375   {
376     return uriFragment.startsWith("/");
377   }
378   
379   protected boolean isAttachedDetachedHelperRequired()
380   {
381     return useIDs() || super.isAttachedDetachedHelperRequired();
382   }
383
384   protected void attachedHelper(EObject eObject)
385   {
386     super.attachedHelper(eObject);
387     
388     if (useIDs())
389     {
390       String JavaDoc id = getID(eObject);
391       if (useUUIDs() && id == null)
392       {
393         id = (String JavaDoc)DETACHED_EOBJECT_TO_ID_MAP.remove(eObject);
394         if (id == null)
395         {
396           id = EcoreUtil.generateUUID();
397         }
398         setID(eObject, id);
399       }
400       else if (id != null)
401       {
402         getIDToEObjectMap().put(id, eObject);
403       }
404     }
405   }
406
407   protected void detachedHelper(EObject eObject)
408   {
409     if (useIDs())
410     {
411       if (useUUIDs())
412       {
413         DETACHED_EOBJECT_TO_ID_MAP.put(eObject, getID(eObject));
414       }
415
416       if (idToEObjectMap != null && eObjectToIDMap != null)
417       {
418         setID(eObject, null);
419       }
420     }
421     
422     super.detachedHelper(eObject);
423   }
424
425   /**
426    * Does all the work of unloading the resource. It calls doUnload in
427    * ResourceImpl, then it clears {@link #idToEObjectMap} and {@link #eObjectToIDMap} as necessary.
428    */

429   protected void doUnload()
430   {
431     super.doUnload();
432
433     if (idToEObjectMap != null)
434     {
435       idToEObjectMap.clear();
436     }
437
438     if (eObjectToIDMap != null)
439     {
440       eObjectToIDMap.clear();
441     }
442
443     if (eObjectToExtensionMap != null)
444     {
445       eObjectToExtensionMap.clear();
446     }
447   }
448
449   /**
450    * Returns a string representation of the {@link #idToEObjectMap ID} map.
451    * @return a string representation of the ID map.
452    */

453   public String JavaDoc toKeyString()
454   {
455     StringBuffer JavaDoc result = new StringBuffer JavaDoc("Key type: ");
456     result.append(getClass().toString());
457     if (idToEObjectMap != null)
458     {
459       TreeMap JavaDoc tree = new TreeMap JavaDoc();
460       for (Iterator JavaDoc i = idToEObjectMap.keySet().iterator(); i.hasNext(); )
461       {
462         Object JavaDoc key = i.next();
463         if (key != null)
464         {
465           tree.put(key.toString(), key);
466         }
467       }
468
469       // add the key/value pairs to the output string
470
for (Iterator JavaDoc i = tree.values().iterator(); i.hasNext(); )
471       {
472         Object JavaDoc key = i.next();
473         Object JavaDoc value = idToEObjectMap.get(key);
474         result.append("\r\n\t[Key=" + key + ", Value=" + value + "]");
475       }
476     }
477     return result.toString();
478   }
479
480   /* (non-Javadoc)
481    * @see org.eclipse.emf.ecore.xmi.XMLResource#load(org.w3c.dom.Node, java.util.Map)
482    */

483   public void load(Node JavaDoc node, Map JavaDoc options) throws IOException JavaDoc
484   {
485     XMLLoad xmlLoad = createXMLLoad();
486
487     if (options == null)
488     {
489       options = Collections.EMPTY_MAP;
490     }
491     
492     if (defaultLoadOptions == null || defaultLoadOptions.isEmpty())
493     {
494       xmlLoad.load(this, node, options);
495     }
496     else if (options == null)
497     {
498       xmlLoad.load(this, node, defaultLoadOptions);
499     }
500     else
501     {
502       Map JavaDoc mergedOptions = new HashMap JavaDoc(defaultLoadOptions);
503       mergedOptions.putAll(options);
504
505       xmlLoad.load(this, node, mergedOptions);
506     }
507
508     
509   }
510 }
511
Popular Tags