1 17 package org.eclipse.emf.ecore.xmi.impl; 18 19 20 import java.io.IOException ; 21 import java.io.InputStream ; 22 import java.io.OutputStream ; 23 import java.util.Collections ; 24 import java.util.HashMap ; 25 import java.util.Iterator ; 26 import java.util.Map ; 27 import java.util.TreeMap ; 28 import java.util.WeakHashMap ; 29 30 import javax.xml.parsers.DocumentBuilderFactory ; 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 ; 43 import org.w3c.dom.Node ; 44 45 46 51 public class XMLResourceImpl extends ResourceImpl implements XMLResource 52 { 53 58 protected Map idToEObjectMap; 59 60 65 protected Map eObjectToIDMap; 66 67 protected Map eObjectToExtensionMap; 68 69 protected String encoding; 70 protected boolean useZip; 71 protected String publicId; 72 protected String systemId; 73 protected DOMHandler domHandler; 74 75 79 protected static final Map DETACHED_EOBJECT_TO_ID_MAP = Collections.synchronizedMap(new WeakHashMap ()); 80 81 84 public XMLResourceImpl() 85 { 86 super(); 87 init(); 88 } 89 90 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 getDefaultSaveOptions() 121 { 122 if (defaultSaveOptions == null) 123 { 124 defaultSaveOptions = new HashMap (); 125 } 126 return defaultSaveOptions; 127 } 128 129 public Map getDefaultLoadOptions() 130 { 131 if (defaultLoadOptions == null) 132 { 133 defaultLoadOptions = new HashMap (); 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 inputStream, Map options) throws IOException 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 outputStream, Map options) throws IOException 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 save(Document doc, Map options, DOMHandler handler) 203 { 204 XMLSave xmlSave = createXMLSave(); 205 domHandler = handler; 206 if (domHandler == null) 207 { 208 domHandler = new DefaultDOMHandlerImpl(); 209 } 210 Document document = doc; 211 if (document == null) 212 { 213 try 214 { 215 document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); 216 } 217 catch (Exception e) 218 { 219 throw new RuntimeException (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 mergedOptions = new HashMap (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 getPublicId() 254 { 255 return publicId; 256 } 257 public String getSystemId() 258 { 259 return systemId; 260 } 261 public void setDoctypeInfo(String publicId, String systemId) 262 { 263 this.publicId = publicId; 264 this.systemId = systemId; 265 } 266 267 public String getEncoding() 268 { 269 return encoding; 270 } 271 272 public void setEncoding(String encoding) 273 { 274 this.encoding = encoding; 275 } 276 277 public Map getIDToEObjectMap() 278 { 279 if (idToEObjectMap == null) 280 { 281 idToEObjectMap = new HashMap (); 282 } 283 284 return idToEObjectMap; 285 } 286 287 public Map getEObjectToIDMap() 288 { 289 if (eObjectToIDMap == null) 290 { 291 eObjectToIDMap = new HashMap (); 292 } 293 294 return eObjectToIDMap; 295 } 296 297 public Map getEObjectToExtensionMap() 298 { 299 if (eObjectToExtensionMap == null) 300 { 301 eObjectToExtensionMap = new HashMap (); 302 } 303 return eObjectToExtensionMap; 304 } 305 306 309 public String getID(EObject eObject) 310 { 311 if (eObjectToIDMap == null) 312 { 313 return null; 314 } 315 else 316 { 317 return (String )eObjectToIDMap.get(eObject); 318 } 319 } 320 321 328 public void setID(EObject eObject, String id) 329 { 330 Object 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 346 public String getURIFragment(EObject eObject) 347 { 348 String 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 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 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 id = getID(eObject); 391 if (useUUIDs() && id == null) 392 { 393 id = (String )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 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 453 public String toKeyString() 454 { 455 StringBuffer result = new StringBuffer ("Key type: "); 456 result.append(getClass().toString()); 457 if (idToEObjectMap != null) 458 { 459 TreeMap tree = new TreeMap (); 460 for (Iterator i = idToEObjectMap.keySet().iterator(); i.hasNext(); ) 461 { 462 Object key = i.next(); 463 if (key != null) 464 { 465 tree.put(key.toString(), key); 466 } 467 } 468 469 for (Iterator i = tree.values().iterator(); i.hasNext(); ) 471 { 472 Object key = i.next(); 473 Object value = idToEObjectMap.get(key); 474 result.append("\r\n\t[Key=" + key + ", Value=" + value + "]"); 475 } 476 } 477 return result.toString(); 478 } 479 480 483 public void load(Node node, Map options) throws IOException 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 mergedOptions = new HashMap (defaultLoadOptions); 503 mergedOptions.putAll(options); 504 505 xmlLoad.load(this, node, mergedOptions); 506 } 507 508 509 } 510 } 511 | Popular Tags |