Proper loading of the profiles and packages from the SRS
[EMFCompare2.git] / plugins / org.eclipse.emf.compare.ide / src / org / eclipse / emf / compare / ide / internal / utils / NoNotificationParserPool.java
blob14940d45e47779b058d20d8e41951c8eb4225435
1 /*******************************************************************************
2 * Copyright (c) 2012, 2015 Obeo.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * Obeo - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.emf.compare.ide.internal.utils;
13 import java.util.Iterator;
14 import java.util.Map;
16 import org.eclipse.emf.ecore.resource.Resource;
17 import org.eclipse.emf.ecore.xmi.UnresolvedReferenceException;
18 import org.eclipse.emf.ecore.xmi.XMLDefaultHandler;
19 import org.eclipse.emf.ecore.xmi.XMLHelper;
20 import org.eclipse.emf.ecore.xmi.XMLLoad;
21 import org.eclipse.emf.ecore.xmi.XMLResource;
22 import org.eclipse.emf.ecore.xmi.impl.XMLHandler;
24 /**
25 * This implementation of an {@link org.eclipse.emf.ecore.xmi.XMLParserPool} aims at disabling all
26 * notifications when the {@link XMLResource#OPTION_DISABLE_NOTIFY option} is set, including the notifications
27 * at the very end of parsing.
28 * <p>
29 * This is one of the steps that allows EMF Compare to bypass UML's CacheAdapter when loading UML models from
30 * the logical model.
31 * </p>
33 * @author <a href="mailto:laurent.goubet@obeo.fr">Laurent Goubet</a>
35 public class NoNotificationParserPool extends NotifyingParserPool {
36 /**
37 * Default constructor.
39 * @param containmentOnly
40 * only set containment reference values. The model will be mostly empty except for its
41 * containment tree.
43 public NoNotificationParserPool(boolean containmentOnly) {
44 super(containmentOnly);
47 @Override
48 protected XMLDefaultHandler createDefaultHandler(XMLResource resource, XMLLoad xmlLoad, XMLHelper helper,
49 Map<?, ?> options) {
50 final XMLDefaultHandler handler = super.createDefaultHandler(resource, xmlLoad, helper, options);
51 if (handler instanceof XMLHandler) {
52 return new NoNotificationXMLHandler((XMLHandler)handler, resource, helper, options);
54 return handler;
57 /**
58 * The only purpose of this xml handler is to disable the notifications that are sent from the default
59 * implementation of {@link #endDocument()} .
61 * @author <a href="mailto:laurent.goubet@obeo.fr">Laurent Goubet</a>
63 private class NoNotificationXMLHandler extends ForwardingXMLHandler {
64 /**
65 * Creates this forwarding handler given its delegate. All other parameters are only used to call the
66 * mandatory super-constructor... but none should be of any use here.
68 * @param delegate
69 * Our delegate XMLHandler.
70 * @param xmlResource
71 * The resource we'll be loading. Mandatory for the super-constructor, but we'll forward
72 * all calls to {@code delegate} anyway.
73 * @param helper
74 * The xml helper to use. Mandatory for the super-constructor, but we'll forward all calls
75 * to {@code delegate} anyway.
76 * @param options
77 * The load options that were specified. Mandatory for the super-constructor, but we'll
78 * forward all calls to {@code delegate} anyway.
80 public NoNotificationXMLHandler(XMLHandler delegate, XMLResource xmlResource, XMLHelper helper,
81 Map<?, ?> options) {
82 super(delegate, xmlResource, helper, options);
85 /**
86 * {@inheritDoc}
88 * @see org.eclipse.emf.ecore.xmi.impl.XMLHandler#endDocument()
90 @Override
91 public void endDocument() {
92 // prevent the sending of notifications at the end of loading
93 // "disableNotify" at true would still send some from here
94 setField("disableNotify", delegate(), Boolean.FALSE); //$NON-NLS-1$
95 super.endDocument();
96 setField("disableNotify", delegate(), Boolean.TRUE); //$NON-NLS-1$
98 if (containmentOnly) {
99 // Same document references are all left as proxies since we haven't set them.
100 // The xml handler would diagnose that has an error.
101 final Iterator<Resource.Diagnostic> errors = xmlResource.getErrors().iterator();
102 while (errors.hasNext()) {
103 final Resource.Diagnostic diagnostic = errors.next();
104 if (diagnostic instanceof UnresolvedReferenceException) {
105 errors.remove();